var input,w,imagenZoom
var cerrar = function(){
	w.content.getElement('img').fade('hide');
}
var proCart = {
	items: {complementos:[],productos:{}},
	sendButton: false,
	cantInput: false,
	cantInfo: false,

	init: function(){
		var addCart = $('addCart');
		if(addCart){
			$$('#grupo input').each(function(el){
				if(el.type=='checkbox'){
					el.addEvent('click',function(){
						this.getParent().getParent().toggleClass('include');
						var cantInput = document.getElementById('c'+this.id.substr(1));
						if(this.checked){
							cantInput.removeProperty('readonly').removeClass('readonly').setProperty('value',1).focus();
						}else{
							cantInput.setProperty('readonly','readonly').addClass('readonly').setProperty('value',0);
						}
					});
				}else{
					el.addEvent('blur',function(){
						if(!this.readOnly){
							cleanCant.bind(this)();
							if(this.value!=0){
								proCart.items.productos['i'+this.id.substr(1)]=this.value;
							}else{
								var checkInput = document.getElementById('b'+this.id.substr(1));
								checkInput.checked=false;
								checkInput.fireEvent('click');
								delete(proCart.items.productos['i'+this.id.substr(1)]);
							}
						}
					});
				}
			});
			
			//
			complementos = $$('#complements input');
			complementos.addEvent('click',function(){
				this.getParent().getParent().toggleClass('include');
				if(this.checked){
					proCart.items.complementos.push(this.name);
				}else{
					proCart.items.complementos.remove(this.name);
				}
			});
			
			//
			this.sendButton = addCart.addEvent('click',this.send.bind(this));
			
			//print
			$('print').injectTop('cont2').addEvent('click',function()
				{
					window.print();
				});
		}
	},
	
	send: function(){
		//if(this.cantInput.disabled==false){
			for(var i=0;i<this.items.complementos.length;i++){
				carrito.addItem(this.items.complementos[i],1,true,false);
			}
			for(var j in this.items.productos){
				carrito.addItem(j.substr(1),this.items.productos[j],false,false);
			}
			carrito.addItem(producto.id,producto.cant);
			carrito.synchronize();
		//}
	}
};

carrito.onUpdate = function(){
	var cantidad = this.response['i'+producto.id] ? this.response['i'+producto.id][0] : 0;
	if($('item-'+producto.id)) $('item-'+producto.id).addClass('active');
	
	proCart.cantInfo.innerHTML = cantidad;
	proCart.cantInput.curval = cantidad;
	
	if(cantidad==proCart.cantInput.max){
		proCart.cantInput.value = 0;
		proCart.cantInput.disabled = true;
	}else{
		proCart.cantInput.value = 1;
		proCart.cantInput.disabled = false;
	}
	
	loading.hide();
	if(this.count()>0){
		this.show();
		this.autoHide(2000);
	}else{
		this.hide();
	}
}

var gallery, galleryControls;

window.addEvent('domready',function(){
	proCart.init();
	var overlay = $('cont1');
	 w = new sWin({size:{top:30},draggable:true,overlay:overlay,onHide:cerrar});
	 
	//SP-510 UZ
	var remember = $('remember');
	var inputDate =$E('#remember input[type=text]');
	
	if(inputDate){
		var message =$E('#remember strong');
		new datePicker($E('#remember input[type=text]'),{
			minDate: minDate,
			startDate: startDate,
			maxDate: maxDate,
			onSelect:function(time)
			{
				input.disabled = time===false;
			}
			
		});
		
		input = $E('#remember input[type=button]').addEvent('click', function(){
			new Request({
				data: 'action=add&fechaEnvio='+inputDate.value+'&id='+remember.get('class'),
				url: base+'usuario_alertas.ajax',
				onSuccess: function(response){
					if(response=='true') {
						var enlace = $E('#remember a');
						//message.set('class','ok').innerHTML=_lng.alertOk; 
						alert('Alerta Grabada');
						enlace.innerHTML = $E('#remember a').innerHTML.toInt()+1;
						enlace.setProperty('title',enlace.getProperty('title') +' | '+ inputDate.value);
					}
					else if (response=='false') alert('No se pudo agregar Alerta');//message.set('class','error').innerHTML=_lng.alertError;
					else alert('error');
				}
			}).send();
		})
	}
	if ($('zoom')){
		$('zoom').addEvent('click',function(){
			imagen = $('imageZoom').getElement('img');
			size = overlay.getCoordinates();
			w.setContent('<img src="'+imagen.src+'" alt="imagen" />');
			imagen = w.content.getElement('img');
			w.options.size.width= imagen.width+45;;
			w.options.size.height= imagen.height+1111140;
			w.options.size.top= 195;
			w.options.size.left = size.left + (size.width/2-((imagen.width+45)/2)) ;	
			w.setTitle('Zoom');
			w.content.getElement('img').fade('hide').fade('in');
			w.show();
			window.scroll(0,0);
		}).setOpacity(0.7);
	}
	
	iniProductList();
});