// JavaScript Document

$(window).bind("load", function() {
	initDropMenu();			
});

$(document).ready(function() {
//RIMPIAZZO TESTI CON FONT TRAJAN						   
	Cufon.replace('#main h1');
	Cufon.replace('.cufon');
//END RIMPIAZZO TESTI
//COMPORTAMENTO LABEL SOPRA INPUT
	$(".row label").click(function() { // bind click event to link
		$(this).addClass("hide");
		$(this).next().focus();
	});
	$(".row input").focus(function() { // bind click event to link
		$(this).prev().addClass("hide");
	});
	$(".row input").blur(function() { // bind click event to link
		if(this.value == "") $(this).prev().removeClass("hide");
	});	
	$(".row textarea").focus(function() { // bind click event to link
		$(this).prev().addClass("hide");
	});
	$(".row textarea").blur(function() { // bind click event to link
		if(this.value == "") $(this).prev().removeClass("hide");
	});	
	$(".row label").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=ecommerce&file=popup&tit=tit_popup_carrello&msg="+divesito.html()+"&err="+diverrore.html()});
				reInitChart();	
			}
		} 
		$(form).ajaxSubmit(options);
    	return false; 
	   }
	})
});
	
//END FORM
//gallery cycle

	$('.cycle').cycle({
		fx: 'fade',
		speed: 1000,
		timeout: 5000,
	    next: "#cycle-next",  
    	prev: "#cycle-prev",  
		easeIn: null,
		easeOut: null
	});
// END CYCLE	
$(".cbox").colorbox();
msg_errore = $("#errore");
if(msg_errore.html())
	$.fn.colorbox({href:website+"/blocchi.php?dir=ecommerce&file=popup&tit=tit_popup_carrello&err="+msg_errore.html()});
});

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=ecommerce&file=popup&tit=tit_popup_carrello&msg="+divesito.html()+"&err="+diverrore.html()});
				reInitChart();				
			}
		} 
		$(form).ajaxSubmit(options);
    	return false; 
	   }
	});
}

//MENU PRINCIPALE
function initDropMenu() {
	var _fadeSpeed = ($.browser.msie ? 0 : 300);
	$('#menu-head > li').each(function(){
		var _opener = $(this);
		/*_opener.css({position:'relative'});*/
		var _drop = _opener.find(' > div');
		var _sub = _opener.find('li');
		var defsrc = _opener.find('img.out').attr('src');
		var _timer;

		_drop.css({display:'none'});
		if(_drop.length) {
			_opener.hover(function(){
				if(_timer) clearTimeout(_timer);
				/*
				var newOn = _opener.find('img.on').attr('src');
			    if (newOn){ _opener.find(' > a > img').attr('src', newOn); }
			    _opener.find(' > a').removeClass('no-hover');
				*/
				_timer = setTimeout(function(){
					_opener.addClass('expanded');
					_drop.fadeIn(_fadeSpeed);
				}, 200);
			}, function(){
				if(_timer) clearTimeout(_timer);
			    /*var newsrc = _opener.find('img.out').attr('src');
			    if (newsrc) {_opener.find('li > div > img').attr('src', newsrc);}*/
				_timer = setTimeout(function(){
					_opener.find(' > a').addClass('no-hover');
					_drop.fadeOut(_fadeSpeed,function(){
						_opener.removeClass('expanded');
						_opener.find('img.out').attr('src', defsrc);
					});
				}, 75);
			});
			_sub.hover(function(){
				var newOn = $(this).find('img.on').attr('src');
			    if (newOn){ _opener.find('img.out').attr('src', newOn); }
				else { _opener.find('img.out').attr('src', defsrc);}				
			},function(){
				_opener.find('img.out').attr('src', defsrc);				
			});
		}
	});

}
//END MENU