// JavaScript Document

$(document).ready(function() {
//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("-");
		var frm = $(this).prevALL('.form-select:first');
		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,");
		}
		if(frm) {
			frm.submit();
		}

	});
//END TOGGLE	
   
      $('input.onsub').live("keypress", function(e) {   
      /* ENTER PRESSED*/
      if (e.keyCode == 13) {  
		  var form = $(this).parents("form");   
		  form.submit(); 
      }  
      });
//gallery cycle
/*
	$('.cycle').cycle({
		fx: 'fade',
		speed: 1000,
		timeout: 5000,
	    next: "#cycle-next",  
    	prev: "#cycle-prev",  
		easeIn: null,
		easeOut: null
	});
	*/
// END CYCLE	
//GESTIONE POPUP AD AVVIO PAGINA
msg_errore = $("#errore");
if(msg_errore.html()&&msg_errore.html()!="")
	$.fn.colorbox({href:website+"/blocchi.php?dir=ecommerce&file=popup&tit=&err="+msg_errore.html()});
msg_vario = $("#messaggio");
if(msg_vario.html()&&msg_vario.html()!="")
	$.fn.colorbox({href:website+"/blocchi.php?dir=ecommerce&file=popup&tit=&err="+msg_vario.html()});

//$.fn.colorbox({href:website+"/blocchi.php?dir=pagine&file=home&id=9"});
/*
$("#catalogo_completo").click(function(){
  window.open('http://www.laflute.it/upload/media/BRO-VDE-catalogo-L.pdf','new');
return false;
});	
$("#catalogo_regalistica").click(function(){
  window.open('http://www.laflute.it/upload/media/BRO-VDE-regalistica-101025-L.pdf','new');
return false;
});	*/
//END
//GESTIONE GALLERY SCORREVOLE
	var div = $('div.gallery'),
		ul = $('ul.sc_menu'),
		ulPadding = 0;
	if(div)
	{
		//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);
		});
	}
//END GALLERY
$(".cbox-inline").colorbox({inline:true});

$(".tab.attiva li").click(function(){
	$(".tab li").removeClass("selected");
	$(this).addClass("selected");
	idinp = $(this).children("a").attr("rel");
	valinp = $(this).children("a").attr("tabindex");
	$("#"+idinp).val(valinp);
	if(valinp*1==1||valinp*1==3) $(".azienda-only").addClass("hide");
	else $(".azienda-only").removeClass("hide");	
});

});

$(".cbox").livequery(function(){$(this).colorbox({maxWidth:"100%"});});
//GESTIONE ACQUISTO
$('.validate-chart').livequery(function(){
	var divesito;
	var diverrore;
	$(this).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");
					if(diverrore.html()!="")
						$.fn.colorbox({href:website+"/blocchi.php?dir=ecommerce&file=popup&tit=&err="+diverrore.html()});
					if(divesito.html()!="")
						$.fn.colorbox({href:website+"/blocchi.php?dir=ecommerce&file=popup&tit=&msg="+divesito.html()});
				$(".checkbox-on").addClass("checkbox-off");
				$(".checkbox-off").removeClass("checkbox-on");
				$(".inp-qta").val('');
				$(".inp-qta").prev().removeClass("hide");
				}
			} 
			$(form).ajaxSubmit(options);
			return false; 
		   }
		})
	});
});
//END ACQUISTO

//COMPORTAMENTO LABEL SOPRA INPUT
$("label.over").livequery(function(){
	$(this).click(function() { // bind click event to link
		$(this).addClass("hide");
		$(this).next().focus();
	});
	$(this).next().focus(function() { // bind click event to link
		$(this).prev().addClass("hide");
	});
	$(this).next().blur(function() { // bind click event to link
		if(this.value == "") $(this).prev().removeClass("hide");
	});	
	$(this).each(function() {
		if($(this).next().val()!="") $(this).addClass("hide");							 
									 });
});
//END

//RIMPIAZZO TESTI CON FONT TRAJAN	
$(".trajan").livequery(function(){
	Cufon.replace(this, { fontFamily: 'Trajan Pro' });
});
//RIMPIAZZO TESTI CON FONT big_nooddle	
$(".noodle").livequery(function(){
	Cufon.replace(this, { fontFamily: 'BigNoodleTitling' });
});
//END RIMPIAZZO TESTI
/* gestione ceckbox */	
$(".optgroup a.check").livequery(function(){
	$(this).click(function(){   
		var valore = $(this).html();
		var att_rel =$(this).attr("rel");
		var id_frm = "frm-"+att_rel;    
		var id_input = "check-"+att_rel;
		var figli = $("#"+id_frm+" .checkbox-on");
		var parente =$(this).parent();
		var objInput = $(this).parent().parent().find(".inp-qta");		
		figli.addClass("checkbox-off");
		figli.removeClass("checkbox-on");
		parente.addClass("checkbox-on");
		parente.removeClass("checkbox-off");
		$("#"+id_input).val(valore);
		objInput.attr("readonly","readonly");
		switch(valore) {
			case "6-6":
			case "1-1":
			objInput.val("1");
			objInput.attr("readonly","");
			break;	
			case "1-6":
			objInput.val("2");
			break;	
			default:	
			objInput.val("1");
			break;
		}
		if(att_rel=="id_gruppo") {
			var objGruppo = $(".aziende");	
			var objCampi = $(".aziende input");	
			switch(valore) {
				case "1":
				case "Privato":
				case "Privati":
				objGruppo.addClass("hide");	
				objCampi.removeClass("required");				
				break;	
				default:	
				objGruppo.removeClass("hide");
				objCampi.addClass("required");	
				break;
			}				
		}
		objInput.focus();
		objInput.blur();
	});
});
//END

//GESTIONE TABS
$(".tabs").livequery(function(){
		$(this).tabs({
			ajaxOptions: {
				error: function( xhr, status, index, anchor ) {
					$( anchor.hash ).html(
						"Errore" );
				}
			}
		});
});

//FINE GESTIONE TABS
//FUNZIONI VARIE  
function callAjax(obj){
	  linkdest = $(obj).attr("href");
	  result = $(obj).attr("rel");
	  $.ajax({
	  url: linkdest,
	  success: function(data) {
		$('#'+result).html(data);
	  }
	  });
	  return false;
}

$.fn.reverse = function() {
    return this.pushStack(this.get().reverse(), arguments);
};

// create two new functions: prevALL and nextALL. they're very similar, hence this style.
$.each( ['prev', 'next'], function(unusedIndex, name) {
    $.fn[ name + 'ALL' ] = function(matchExpr) {
        // get all the elements in the body, including the body.
        var $all = $('body').find('*').andSelf();

        // slice the $all object according to which way we're looking
        $all = (name == 'prev')
             ? $all.slice(0, $all.index(this)).reverse()
             : $all.slice($all.index(this) + 1)
        ;
        // filter the matches if specified
        if (matchExpr) $all = $all.filter(matchExpr);
        return $all;
    };
});

function svuotaCampi(container) {
	$("#"+container+" input:text").val('');	
	$("#"+container+" select").val('');	
}
