// 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

//funzione paginazione lavori
$(".next-page").click(function(){
	if($(".box-pag:visible").next(".box-pag").length ){
		temp = $(".box-pag:visible")
		$(".box-pag:visible").addClass("hide");
		$(temp).next(".box-pag").removeClass("hide");
	}
});

$(".prev-page").click(function(){
	if($(".box-pag:visible").prev(".box-pag").length ){
		temp = $(".box-pag:visible")
		$(".box-pag:visible").addClass("hide");
		$(temp).prev(".box-pag").removeClass("hide");
	}
});
	/*
$('#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});
$(".cbox-login").colorbox({inline:true, href:"#blocco_login"});

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()});
	
msg_popup = $("#popup");
if(msg_popup.html())
	$.fn.colorbox({href:website+"/"+msg_popup.html(),width:"674", height:"421", iframe:true});

$('#gal-img').cycle({ 
    timeout: 5000,
	speed: 1500 
});

$('#gal-prod').cycle({ 
    prev:   '#back', 
    next:   '#next', 
    timeout: 5000,
	speed: 1500 
});

$('#gal-prod-multi').cycle({ 
    prev:   '#back', 
    next:   '#next',
	center : 1, 
    timeout: 5000,
	speed: 1500 
});

$('#gal-foto').cycle({ 
    prev:   '#back', 
    next:   '#next',
	center : 1, 
    timeout: 5000,
	speed: 1500,
    pager:  '#nav',
     
    // callback fn that creates a thumbnail to use as pager anchor 
    pagerAnchorBuilder: function(idx, slide) { 
        return '<li><a href="#"><img src="' + slide.src + '" /></a></li>'; 
    } 
});
if($("a.screenshot").length > 0) screenshotPreview();
});

$(window).load(function(){
    //Get our elements for faster access and set overlay width
    var div = $('div.sc_menu');
	ul = $('ul.sc_menu'); 
	// unordered list's left margin
	ulPadding = 15;

    //Get menu width
    var divWidth = div.width();

    //Remove scrollbars
    div.css({overflow: 'hidden'});

    //Find last image container
    var lastLi = ul.find('li:last-child');
	
    //When user move mouse over menu
    div.mousemove(function(e){

      //As images are loaded ul width increases,
      //so we recalculate it each time
      var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;

      var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
      div.scrollLeft(left);
    });
});

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; 
	   }
	});
}
function updatecaptcha() {
   img = document.getElementById('imgCaptcha'); 
   //Change the image
   img.src = '../../lib/captcha/immagine.php?' + Math.random();
}
/*
 * Url preview script 
 * powered by jQuery (http://www.jquery.com)
 * 
 * written by Alen Grakalic (http://cssglobe.com)
 * 
 * for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery
 *
 */
 
this.screenshotPreview = function(){	
	/* CONFIG */
		
		xOffset = 130;
		yOffset = 150;
		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
		
	/* END CONFIG */
	$("a.screenshot").click(function(){return false;});
	$("a.screenshot").hover(function(e){
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<p id='screenshot'><img src='"+ this.href +"' alt='scheda' />"+ c +"</p>");								 
		$("#screenshot")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");						
    },
	function(){
		this.title = this.t;	
		$("#screenshot").remove();
    });	
	$("a.screenshot").mousemove(function(e){
		$("#screenshot")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};


