/*--------------------------------------------------
Widthdivision
--------------------------------------------------*/
/*function widthDivision()
{
    var i = ($("nav.menu ul.onelevel li").not($("nav.menu ul li").children("ul").children("li")).size()) ;	
	var largh = $("nav.menu").width();
	$("nav.menu ul").each(function (i,e2) {
			var larg_li = 0;
			//$(e).children("li");
			$(e2).children("li").each(function(i,e) {
				larg_li += $(e).width();
			});
			var c = (largh - larg_li);				
			var j = Math.floor( ( c / ($(e2).children("li").size()) ) / 2);
			$(e2).children("li").css({"margin-left": j +"px", "margin-right": j +"px"});
			largh = $("header.header").width();
					//alert(largh);
		});
	
	$("nav.menu ul:gt(0)").each(function (i,e){
		$(e).children("li:eq(0)").css("margin-left", "0");
	});
	$("nav.menu ul:eq(0)").children("li:last").css("margin-right", "0px");
}*/

function widthDivision()
{
    var i = ($("nav.menu ul.onelevel li").not($("nav.menu ul li").children("ul").children("li")).size()) ;	
	var  margin = 7;
	var largh = $("nav.menu").width();
	$("nav.menu ul").each(function (i,e2) {
			var larg_li = 0;
			//$(e).children("li");
			$(e2).children("li").each(function(i,e) {
				larg_li += $(e).width();
			});
			var c = (largh - larg_li);				
			var j = Math.floor( ( c / ($(e2).children("li").size()) / 2));
			if (j < 45) {
				$(e2).children("li").css({"margin-left": j +"px", "margin-right": j +"px"});
				largh = $("header.header").width();
				if (j < 10) {
					$(e2).children("li").css({"margin-left": margin +"px", "margin-right": margin + "px"});
					var h = $(".secondlevel").height();
					//alert(h);
					var dif = (($("header").height())) + (h);
					$("header").css("height", dif);
					$("nav.menu ul li ul li").css("margin-bottom","7px");
				};
				//alert(largh);
			} else {$(e2).children("li").css({"margin-left": "45px", "margin-right": "45px"});}
		});
	
	$("nav.menu ul:gt(0)").each(function (i,e){
		$(e).children("li:eq(0)").css("margin-left", "0");
	});
	$("nav.menu ul:eq(0)").children("li:last").css("margin-right", "0px");
}

/*--------------------------------------------------
Menu
---------------------------------------------------*/
function Menu2() {
    $(".menu ul li a").click(function () {
        if ($(".menu ul li a").is(".on")) {
            $(".menu ul li a").removeClass("on");
        }
    });
    $(".menu ul li a").each(function (i, e) {
        $(e).click(function () {
            $(".menu ul li ul:visible").not($(e).next("ul")).not($(e).parents('ul')).not($(e).removeClass("on")).hide();
            $(e).next("ul").toggle("fast", "linear");
            //$(e).addClass("on");
			if ($(e).is(".on")) {
			//alert("test");
            	$(e).removeClass("on");
			}
			else {
				$(e).addClass("on");
			}
        });
    });
   /* $(".menu ul li a").hover(
			function () {
				var $ul = $(this).children('ul').eq(0).show();
				if($ul.parents('li').offset().left > $(window).width()/2){
					$ul.css({'left':'auto','right':'0'});
				} else {
					$ul.css({'left':'0','right':'auto'});
				}
			},
			function () {
					$(this).children('ul').hide();
			}
		);
    $(".menu ul li ul li ul").hover(
		function () {
		    $(this).show();
		    $(this).prev().addClass('attivo');
		},
		function () {
		    $(this).hide();
		    $(this).prev().removeClass('attivo');
		}
	);
  
	$(".menu ul li ul li").css("padding-left", "0px");*/
	
}

function Menu() {
    /*$(".menu ul li").click(function () {
        if ($(".menu ul li a").is(".on")) {
            $(".menu ul li a").removeClass("on");
        }
    });*/
    $(".menu ul li a").each(function (i, e) {
        $(e).click(function () {
			$("#menu ul li ul:visible").not($(e).next("ul")).not($(e).parents('ul')).not($(e).removeClass("on")).hide();
            //$(".menu ul:visible").removeClass("on");
            $(e).next("ul").toggle("fast", "linear");
            //$(e).addClass("on");
			if ($(e).is(".on")) {
			//alert("test");
            	$(e).removeClass("on");
			}
			else {
				$(e).addClass("on");
			}
        });
		
    });
    /*$(".menu ul li ul li").hover(
			function () {
				var $ul = $(this).children('ul').eq(0).show();
				if($ul.parents('li').offset().left > $(window).width()/2){
					$ul.css({'left':'auto','right':'0'});
				} else {
					$ul.css({'left':'0','right':'auto'});
				}
			},
			function () {
					$(this).children('ul').hide();
			}
		);*/
    /*$(".menu ul li ul li ul").hover(
		function () {
		    $(this).show();
		    $(this).prev().addClass('attivo');
		},
		function () {
		    $(this).hide();
		    $(this).prev().removeClass('attivo');
		}
	);*/
  
	//$(".menu ul li ul li").css("padding-left", "0px");
	
}

/*--------------------------------------------------
Tab
--------------------------------------------------*/
function Tabs() { 
//$("#accordion").tabs("#accordion div.pane", {tabs: 'h2', effect: 'slide', initialIndex: null});
//$("#accordion h2:eq(0)").trigger('click');
	$("#accordion h2").click(function(){
		var ch2 = this;
		$("#accordion h2.aperto").not(ch2).removeClass('aperto');
		$(ch2).toggleClass('aperto');
		$('.pane', $(this).parent()).slideToggle(function(){
			$("#accordion .pane").not(this).slideUp();
		});
	});

}

/*--------------------------------------------------
Equal Height
--------------------------------------------------*/
function equalHeight(group) {
   tallest = 0;
   group.each(function() {
      thisHeight = $(this).height();
      if(thisHeight > tallest) {
         tallest = thisHeight;
      }
   });
   group.height(tallest);
}

/*--------------------------------------------------
Image Home Resize
--------------------------------------------------*/
var largh1, largh2, height1, height2, c;

function imgResize() {
	largh1 = $(".wrap_welcome").width();
	largh2 = $(".wrap_img_home").width();
	height1 = $(".wrap_welcome").height();
	height2 = $(".wrap_img_home").height();
	c = (720 * largh2) / 1200;
	$(".wrap_welcome").css("height" , + c +"px");
	$(".ombra").children(".ombra_child").css("height" , + c +"px");
}

function imgZoom() {
	imgResize();
	/*
	time = "750,'easeOutQuint'";
	time2 = "0";
	
	$(".wrap_img_home").hover(function () {
		$t = $(this);
		$(".wrap_img_home").not($t).stop().animate({opacity:.15,width:'50%'},time2);
		$t.css({'z-index':5}).stop().animate({opacity:1,width:'70%', height: c + 'px'},time).find("a span img").stop().animate({top:'-25%'},time);
		//$(".ombra", $t).stop().animate({top:"-30%", opacity:1},time);
	},
	function() {
		$t = $(this);
		$(".wrap_img_home").not($t).stop().animate({opacity:1,width:'50%'},time2);	
		$t.css({'z-index':1}).stop().animate({opacity:1,width:'50%', height: c + 'px'},time).find("a span img").stop().animate({top:0},time);	
		//$(".ombra", $t).stop().animate({top:"0%", opacity:0},time);
	});
	*/
}

/*--------------------------------------------------
INIT SCROLLER
--------------------------------------------------*/

function scrollablePerN (n) {
	var qi = $('.scrollprodotto .items').children('a').size();
	if (qi>0){
		var c = 0
		while(qi>0 && c<n){
				$('.scrollprodotto .items').children('a:lt(' + (n) + ')').wrapAll('<div></div>');
				qi = $('.scrollprodotto .items').children('a').size();
				c+=1;
			}
	}
	if($('.scrollprodotto .items').children('div').size()>1){
		$('.scrollprodotto .next').removeClass('disabled');
	}
}

function scrollableAltriPerN (n) {
	var qi = $('.scroller_other .scroll_other_prod .items').children('a').size();
	if (qi>0){
		var c = 0
		while(qi>0 && c<n){
				$('.scroller_other .scroll_other_prod .items').children('a:lt(' + (n) + ')').wrapAll('<div></div>');
				qi = $('.scroller_other .scroll_other_prod .items').children('a').size();
				c+=1;
			}
	}
	if($('.scroller_other .scroll_other_prod .items').children('div').size()>1){
		$('.scroller_other .next').removeClass('disabled');
	}else{
		$('.scroller_other .next').addClass('disabled');
	}
}

function scrollProdotto() {
	scrollablePerN(3);
	scrollableAltriPerN(5);
	$(".scrollprodotto").scrollable({vertical: true, mousewheel: true});
	$(".scrollprodotto .items a").click(function() {
		// see if same thumb is being clicked
		if ($(this).hasClass("active")) {return false;}
		// calclulate large image's URL based on the thumbnail URL (flickr specific)
		var url = $(this).attr("href");
		// get handle to element that wraps the image and make it semi-transparent
		var wrap = $("#image_wrap").fadeTo("medium", 0.5);
		// the large image from www.flickr.com
		var img = new Image();
		// call this function after it's loaded
		img.onload = function() {
			// make wrapper fully visible
			wrap.fadeTo("fast", 1);
			// change the image
			wrap.find("img").attr("src", url);
		};
		// begin loading the image from www.flickr.com
		img.src = url;
		// activate item
		$(".items div a").removeClass("active");
		$(this).addClass("active");
		return false;
	// when page loads simulate a "click" on the first image
	}).filter(":first").click();
}

function scrollOtherProd() {
	$(".scroll_other_prod").scrollable();	
}

function ScrollLista() {
	$(".scrollable_lista").scrollable().navigator();
	
	$(".wrap_navi .navi a").each(function(i,e) {
		$(e).html(i + 1);
	});
	
	Largh1 = $(".wrap_navi_button").width();
	Largh2 =$(".wrap_navi .navi").width();
	Larghtot = ((Largh1 - Largh2) -40) / 2;
	$(".wrap_navi").css({"margin-right" : + Larghtot + "px" , "margin-left" : + Larghtot + "px"});
}

function ScrollCopertina() {
	$(".scrollable_copertina").scrollable({next:".next_cop", prev:".prev_cop", circular : true}).navigator().autoscroll({ autoplay: true, interval: 3000 });
	var largh = $(".wrap_navi_copertina .navi").width();
	$(".wrap_navi_copertina").css ("width", largh + 30);
}

function tooltip() {
	time = "750,'easeOutQuint'";
	var q = $(".colonna .hook").size();
	$('.lista, .ombra_lista').height(Math.max(300, Math.ceil(q/4) * 178));
	
	/*
	$('.colonna .hook').mouseover(function(){$('.lista.prodotti_griglia').data('hover',true);}).mouseout(function(){$('.lista.prodotti_griglia').data('hover',true);});
	*/
	$(".colonna .hook").each(function(i,e){
		$(e).data('idx',i);
		});
	$(".colonna .hook").hover(function(e){
		var L = 0;
		var T = 0;
		var idx = $(this).data('idx');
		var m = idx % 4;
		if (idx >=4){
			T = parseInt((300 - 172) / 2, 10);
		}
		if (m > 0){
			L = parseInt((400 - 229) / 2, 10);
		}
		if (m == 3) {
			L = 400 - 229;	
		}
		var lr = q % 4;
		if( lr == 0 ){lr = 4;}
		if ( ( idx >= 4 ) && ( idx >= ( q - lr ) ) ) {
			T = 300 - 172;	
		}		
		$(this).parent(".colonna").addClass("index");
		$(this).siblings(".popup").fadeIn().css({left: "-" + L + "px", top:"-" + T + "px"});
		//$(".ombra_lista").fadeIn('fast');
		return false;
	}, 
	function(e){
		var hook = $(this);
		$(this).parent(".colonna").removeClass("index");
		$(this).siblings(".popup").fadeOut(time);
	});
	/*
$('.lista.prodotti_griglia').mouseover(function(e){$(this).data('hover',true);}).mouseout(function(e){$(this).data('hover',false);});
	setInterval(function(){
		if( ( $('.lista.prodotti_griglia .colonna.index').size() == 0 ) && ( $('.lista.prodotti_griglia').data('hover')==false ) ){
			$(".ombra_lista").delay(100).fadeOut('slow');
		}
	},500);
*/
}

function toggle_copertina_griglia(){
	$('.toggle_index:gt(0)').hide();
	$('a.toggle_div').click(function(){
		$('.toggle_index').slideToggle('normal');
		return false;
	});
}

/*--------------------------------------------------
Language
---------------------------------------------------*/
function Language() {
	if ($("body.language_en").size()>0) {
		$(".language a").html("EN");
	} else {
		$(".language a").html("IT");
	}
}

function Hide ($id) {
	$("#player").height(0).css('overflow','hidden');
}

function ScrollDownload() {
	var c = $(".vertical_scroller .items .item").size();
	if (c > 5) {
		//$(".next.browse_dow.right").addClass("disabled");
		$(".vertical_scroller #allegati").scrollable(/*{next:".next_dow", prev:".prev_dow", circular : true}*/{vertical: true, mousewheel: true});
	} else 
	{
		$(".next.browse_dow.right").addClass("disabled");
		$(".prev.browse_dow.left").addClass("disabled");
	}
}

/*--------------------------------------------------
slide fade
---------------------------------------------------*/
function slideSwitch() {
    var $active = $('#slideshow DIV.active');
    if ( $active.length == 0 ) $active = $('#slideshow DIV:last');
    // use this to pull the divs in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow DIV:first');
    // uncomment below to pull the divs randomly
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );
    $active.addClass('last-active');
    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 800, function() {
            $active.removeClass('active last-active');
        });
}

/*--------------------------------------------------
INIT
--------------------------------------------------*/
$ (document).ready(function() {
	toggle_copertina_griglia();
	tooltip();
	widthDivision();
	scrollProdotto();
	Tabs();
	scrollOtherProd();
	//equalHeight($(".box_col"));
	ScrollCopertina();
	ScrollDownload();
	Language();
	$('.fancybox').fancybox();
//	$('.fancyframe').fancybox({type:'iframe', autoDimensions:false, width:580, height:720});
	$('.fancyframe').fancybox({type:'iframe', autoDimensions:false, width:620, height:740, padding:10});

	$('#fancybox-inner a.fancy_privacy').live('click', function(){
		var ln = this;
		$('.cont_privacy', $(ln).parent()).load(this.href, function(){
			$('.cont_privacy', $(ln).parent()).slideToggle();
		});
		return false;	
	});

	$('.doc_2 a.fancy_privacy').fancybox({type:'ajax', autoDimensions:false, width:620, height:740, padding:10});
	$('form[method=post]').append('<input type="hidden" name="can_submit" value="'+Math.random()+'" />');
	$('a.pop_preventivo').click(function(){
			$.fancybox( {content: $("#fff").clone(), 'transitionIn'    : 'fade', 'transitionOut'  : 'fade', 'titleShow' : false, autoDimensions:false, width:644, height:740, hideOnOverlayClick:false });
			return false;
			/* 'overlayColor'  : '#fff',*/
		}
	);
	$('.doc_2 input#chiedi_preventivo').each(function(){this.checked = false;});
	imgZoom();
	try {

		$("#player").each(function(){
			$("#player").jfe ({ ver:"8,0,0", display:"both", manual:true, noflash:true });
		});
		$("#swf_children").each(function(){		
			$("#swf_children").jfe ({ ver:"8,0,0", display:"both", manual:false, noflash:false });	
		});
	}
	catch(e){
	}
	//$(".scroller_showreel").scrollable({circular : true}).autoscroll({ autoplay: true, interval: 2000 });
	//$(".scroller_showreel.swf").scrollable({circular : true}).autoscroll({ autoplay: true, interval: 1000 });
	$(function() {
    setInterval( "slideSwitch()", 1000 );
});
    //$('#frm_contatti').ht5ifv(); //this is the most simple usage
});

/*--------------------------------------------------
WIN LOAD
--------------------------------------------------*/
$(window).load(function(){
	//equalHeight($(".box_col"));
});
/*--------------------------------------------------
WIN RESIZE
--------------------------------------------------*/
$(window).resize(function(){
	imgResize();
	imgZoom();
});
