// JavaScript Document


$(document).ready(function() {                           
//COMPORTAMENTO LABEL SOPRA INPUT
	$("label.over").click(function() { // bind click event to link
		$(this).addClass("hide");
		$(this).next().focus();
	});
	$("label.over").next().focus(function() { // bind click event to link
		$(this).prev().addClass("hide");
	});
	$("label.over").next().blur(function() { // bind click event to link
		if(this.value == "") $(this).prev().removeClass("hide");
	});	
	$("label.over").each(function() {
		if($(this).next().val()!="") $(this).addClass("hide");							 
									 });
	
//END
/*/DATEPICKER E MASCHERE INPUT
	$('.datepicker').datepicker({ dateFormat: 'dd/mm/yy' });
	$('.datepicker').mask("99/99/9999");
	$('.orario').mask("99:99");
	//$('.numero').mask("999",{placeholder:"0"});
*///END DATEPICKETR
//SCROLLING SU ANCORE
	$(".scroll").click(function(event){
		//prevent the default action for the click event
		event.preventDefault();

		//get the full url - like mysitecom/index.htm#home
		var full_url = this.href;

		//split the url by # and get the anchor target name - home in mysitecom/index.htm#home
		var parts = full_url.split("#");
		var trgt = parts[1];

		//get the top offset of the target anchor
		var target_offset = $("#"+trgt).offset();
		var target_top = target_offset.top;

		//goto that anchor by setting the body scroll top to anchor top
		$('html, body').animate({scrollTop:target_top}, 500);
	});
//END SCROLLING
//TOGGLE E SELECT-BOX
	$(".toggle_hide").hide();
	$(".trigger").click(function(){
		$(this).next(".toggle_c").slideToggle("slow,");
	});
	
	$(".select-box a").click(function(){
		var selbox =  $(this).attr('rel').split("-");
		if(selbox!="") {
			$("#sel-"+selbox[0]+" .active").remove();
			$("#sel-"+selbox[0]+" span.first").html($(this).html());
			$("#"+selbox[0]).val(selbox[1]);
			$("#sel-"+selbox[0]+" .toggle_c").slideToggle("fast,");
		}
	});
//END TOGGLE	
//GESTIONE FORM
var divesito;
var diverrore;
$('.validate-chart').each(function() {
	$(this).validate({
	   submitHandler: function(form) {
	   var queryString = $(form).fieldSerialize(); 
		var options = { 
			target:    form.getAttribute('target'), 
		  	data: queryString,
			success: function(result) { 
				divesito = $("#esito");
				diverrore = $("#errore");
				$.fn.colorbox({href:website+"/blocchi.php?dir=pagine&file=popup&tit=tit_popup_carrello&msg="+divesito.html()+"&err="+diverrore.html()});
				reInitChart();	
			}
		} 
		$(form).ajaxSubmit(options);
    	return false; 
	   }
	})
});
					   
	$(".validate").each(function() {
		$(this).validate({
	   submitHandler: function(form) {
		   if(form.getAttribute('target') && form.getAttribute('target')!="")
		   {
		    var queryString = $(form).fieldSerialize();
			var options = { 
				target:    form.getAttribute('target'), 
         		type: 'post',
				data: queryString,
				success: function(result) { 
					diverrore = $("#errore");
				if(diverrore.html()!="") {
					$.fn.colorbox({href:website+"/blocchi.php?dir=pagine&file=popup&tit=&err="+diverrore.html()});
				}
				}
			}
			$(form.getAttribute('target')).html("Invio in corso...");
			$(form).ajaxSubmit(options);
			return false; 
		   } else {form.submit();}
	   }
	});	
	});		
//END FORM

	
$('#galleria').galleria({
    height:400,
    extend: function(options) {
        // listen to when an image is shown
        this.bind(Galleria.IMAGE, function(e) {
            $(e.imageTarget).click(this.proxy(function() {
               this.openLightbox();
            }));
        });
    }
});
	
$(".cbox").colorbox();   
$(".btn-add").colorbox({width:"450", height:"200", iframe:true});
msg_errore = $("#err");
if(msg_errore.html())
	$.fn.colorbox({href:website+"/blocchi.php?dir=pagine&file=popup&tit=errore&err="+msg_errore.html()});
msg_mess = $("#mess");
if(msg_mess.html())
	$.fn.colorbox({href:website+"/blocchi.php?dir=pagine&file=popup&tit=attenzione&err="+msg_errore.html()});

//GALLERY HOME
	// Executed once all the page elements are loaded
	var preventClick=false;
	$(".pic a").bind("click",function(e){

		/* This function stops the drag from firing a click event and showing the lightbox */
		if(preventClick)
		{
			e.stopImmediatePropagation();
			e.preventDefault();
		}
	});

	$(".pic").draggable({

		/* Converting the images into draggable objects */
		containment: 'parent',
		start: function(e,ui){
			/* This will stop clicks from occuring while dragging */
			preventClick=true;
		},
		stop: function(e, ui) {
			/* Wait for 250 milliseconds before re-enabling the clicks */
			setTimeout(function(){ preventClick=false; }, 250);
		}
	});

	$('.pic').mousedown(function(e){
		/* Executed on image click */
		var maxZ = 0;

		/* Find the max z-index property: */
		$('.pic').each(function(){
			var thisZ = parseInt($(this).css('zIndex'))
			if(thisZ>maxZ) maxZ=thisZ;
		});

		/* Clicks can occur in the picture container (with class pic) and in the link inside it */
		if($(e.target).hasClass("pic"))
		{
			/* Show the clicked image on top of all the others: */
			$(e.target).css({zIndex:maxZ+1});
		}
		else $(e.target).closest('.pic').css({zIndex:maxZ+1});
	});

	if(location.hash.indexOf('#pic-')!=-1)
	{
		/* Checks whether a hash is present in the URL */
		/* and shows the respective image */
		$(location.hash+' a.fancybox').click();
	}
});

function reInitChart(){
	$("#chart .validate-chart").validate({
	   submitHandler: function(form) {
	   var queryString = $(form).formSerialize(); 
		var options = { 
			target:    form.getAttribute('target'), 
		  	data: queryString,
			success: function(result) { 
				divesito = $("#esito");
				diverrore = $("#errore");
				$.fn.colorbox({href:website+"/blocchi.php?dir=pagine&file=popup&tit=tit_popup_carrello&msg="+divesito.html()+"&err="+diverrore.html()});
				reInitChart();				
			}
		} 
		$(form).ajaxSubmit(options);
    	return false; 
	   }
	});
}


