var changeTracker = false;
/*$(function() {
        $('a.zoom').lightBox();
});*/
$(document).ready(function(){
	
	//popup login
	$("#linklogin").toggle(
      function () { $("#loginbox").fadeIn("fast"); },
      function () { $("#loginbox").fadeOut("fast");   }
    );
	
	//selector 
	$(".box-selector ul li ul").css("display","none");
	$(".box-selector ul li.in-path ul").css("display","block");
		
	/*$(".box-selector UL LI A").toggle(
		function () { $(this).parent().children("UL").slideDown("fast"); },
		function () { $(this).parent().children("UL").slideUp("fast"); }
	);*/

	$(".box-selector UL LI A").toggle(
		function () { 
			$(this).parent().children("UL").slideDown("fast");
			$(this).addClass("opened");
		 },
		function () { 
			/*$(this).parent().children("UL").slideUp("fast");
			$(this).removeClass("opened");*/
			return;
		}
	);
	
	$(".box-selector UL LI UL LI A").unbind();
	$(".box-selector UL LI A").not(".with-children").unbind();
	
	$(".box-info INPUT.campo").focus( function() {
		$(this).val("");			   
	});
	
	
	
$('a.linklogin').toggle(
      function () { $("#loginbox").fadeIn("fast"); },
      function () { $("#loginbox").fadeOut("fast");   }
);
	
	$('#registerform input.required').after('&nbsp;<strong class="required">*</strong>');
	$('a.zoom').lightBox();
	$('form select.auto-submit, form input.auto-submit').change(function(){ this.form.submit(); });
	$('form input.auto-submit:checkbox').click(function(){ this.form.submit(); });

	$('.box-scheda-img a').lightBox();
	$('form.check-changes input, form.check-changes select, form.check-changes textarea').change(function(){ changeOccurred(); });
	$('form.track-changes *').change(function(){ changeOccurred(); });
	$('a, input[type=reset], input[type=submit], input[type=image], button').not('.reload, .ignore, .save, .delete').click(function(){
		if(changeTracker){
			var url = $(this).attr('href');
			//alert(url);
			if(url && url=='#') return true;
			return confirm('Sei sicuro di voler ignorare le modifiche?');
		}
		return true;
	});

	$('a.delete, input.delete, button.delete').click(function(){
		var title = $(this).attr('title');
		if(title || title=='') title = 'Sei sicuro di voler annullare la richiesta?';
		return confirm(title);
	});
	
	$("#bischeda-linknota").click(function () { 
 		$("#bischeda-desc").hide(); 
		$("#bischeda-nota").show(); 
		$("#bischeda-nav").removeClass('bischeda-fondodesc');
		$("#bischeda-nav").addClass('bischeda-fondonota');
		$("#bischeda-linkdesc").css("color","#666"); 
		$("#bischeda-linknota").css("color","#fff"); 
		return false;
	});
		
	$("#bischeda-linkdesc").click(function () { 
 		$("#bischeda-nota").hide(); 
		$("#bischeda-desc").show(); 
		$("#bischeda-nav").removeClass('bischeda-fondonota');
		$("#bischeda-nav").addClass('bischeda-fondodesc');
		$("#bischeda-linkdesc").css("color","#fff"); 
		$("#bischeda-linknota").css("color","#666"); 
		return false;
	});
		
	$("UL.zoomlist LI:last").addClass('iconazoom');
	$("UL.zoomlist LI:first IMG").css("border-right","5px solid #ccc");
		
	$(".box-selector UL LI A").not('.with-children').unbind();
		
	$.validator.addMethod("woselect", function(value, element) { return this.optional(element) || !/^WONoSelectionString/.test(value); }, "Questo campo è obbligatorio.");
	
	$.validator.addClassRules({ woselect: { woselect:true } });
	$.extend($.validator.messages, {
		required: "Questo campo è obbligatorio.",
		remote: "Riempire questo campo per continuare.",
		email: "Inserire un indirizzo email valido.",
		url: "Inserire un indirizzo URL valido.",
		date: "Inserire una data in formato mm-gg-aaaa.",
		dateDE: "Inserire una data in formato gg-mm-aaaa.",
		dateISO: "Inserire una data in formato aaaa-mm-gg.",
		number: "Inserire un numero.",
		digits: "Inserire (solo) un numero.",
		creditcard: "Inserire un numero di carta di credito valido.",
		equalTo: "Inserire lo stesso valore usato sopra.",
		accept: "Usare un'estensione valida.",
		maxlength: $.format("Inserire al massimo {0} caratteri."),
		minlength: $.format("Inserire almeno {0} caratteri."),
		rangelength: $.format("Inserire da {0} a {1} caratteri."),
		range: $.format("Inserire un numero compreso tra {0} e {1}."),
		max: $.format("Inserire un numero minore o uguale a {0}."),
		min: $.format("Inserire un numero maggiore o uguale a {0}.")
	});
	$('form.to-validate').validate();
	
	$('a.disabled').click(function(){return false;});
	
	
	$('a.new-window').click(function(){
		window.open(this.href,'Stampa','');
		return false;
	});
	
	$('a.ajax-buy').click(function(){
		
		//alert('ajax-buy');
		
		
        		var customerQuantity = $(this).parent().find('input.customer-quantity').val();
        		var maxQuantity = $(this).parent().find('input.max-quantity').val();
				var wosid = $(this).parent().find('input.wosid').val();
				
				
				//alert('customerQuantity: ' + customerQuantity);
				//alert('maxQuantity: ' + maxQuantity);
				
        		
        		if(customerQuantity!=null) customerQuantity = parseInt(customerQuantity);
        		if(maxQuantity!=null) maxQuantity = parseInt(maxQuantity);
        		
				
        		if(maxQuantity!=null && customerQuantity!=null){
					var diff = maxQuantity - customerQuantity;
        			if (diff < 0) {
						if(maxQuantity>0) alertMaxQuantity(maxQuantity);
						else{
							alertNoQuantityAvailable();
						}
						return false;
					}
					else if(customerQuantity>0){
						
						$.getJSON(
							this.href,
							{
								quantity : customerQuantity
							}, 
							function(data){
								var myObject = data; //eval('(' + data + ')');
								alert('Prodotto inserito nel carrello:\ntotale: '+myObject.theTotalWithVat+' EUR');
								$('#cart').html(myObject.theTotalQuantity);
								$('#ddx').fadeOut(450);
								//setTimeout('loadSideCart("'+wosid+'")',500);
							});
						
						$(this).parent().find('input.max-quantity').val(diff);
						$(this).parent().find('input.customer-quantity').val(1);
					}
        		}
        		return false;
     });
	
});

function alertMaxQuantity(q){
	if(q!=null){
		$.prompt('Puoi acquistare al massimo: ' + q + ' pezzi');
		//alert('Puoi acquistare al massimo: ' + q + ' pezzi');
	} 
	else{
		$.prompt('La quantità specificata non è disponibile');
		//alert('La quantità specificata non è disponibile');
	} 
}
function alertNoQuantityAvailable(){
	$.prompt('Non ci sono pezzi disponibili');
	//alert('Non ci sono pezzi disponibili');
}

function loadSideCart(wosid){
	$('#ddx').load('/Apps/WebObjects/EmporioDellaCover.woa/wa/ajaxCart', {wosid: wosid});
	$('#ddx').fadeIn();
}
function changeOccurred(){
	if(!changeTracker) changeTracker = true;
}


