/* 
=============================================================================
	AUTHOR: Matteo Galli (www.playcodestudio.com)
=============================================================================
*/ 



// LINK ESTERNI AL SITO IN TARGET _BLANK
	$(document).ready(function() {
		$("a").filter(function() {
			return this.hostname && this.hostname !== location.hostname;
		}).attr('target', '_blank');
	});



// SLIDESHOW 
	var reference = "slideshow-sopra";
	var mypath=$('[id^="'+reference+'-"]:not([id*=prev]):not([id*=next]):not([id*=first]):not([id*=last])');
	mypath.find(".prodotto-slideshow-sopra").css("opacity",0).css("top",40).css("display","none");
	this[reference+"_init"] = this[reference+"_click"] = function(reference, activated, deactivated, prevsteps, nextsteps, multiple, cycle){
		var mypath=$('#'+reference+"-"+deactivated);
		mypath.find(".prodotto-slideshow-sopra").stop().css("zIndex",500).animate({top:-40, opacity:0},{queue:false,duration:800,specialEasing:{top:custom1,opacity:custom1},
			complete:function(){
					$(this).css("display","none");
			}
		});
		//
		var mypath=$('#'+reference+"-"+activated);
		mypath.find(".prodotto-slideshow-sopra").stop().css("zIndex",i).css("display","block").css("top",40).css("opacity",0).delay(11500).animate({top:0, opacity:1},{queue:false,duration:800,specialEasing:{top:custom1,opacity:custom1}});
	}
	mg_init({
		reference:"slideshow-sopra",
		activated:[0],
		click:{
			interactive:false,
			linked:["slideshow-sotto","slideshow-medio"],
			max_activated:1,
			automatic:6000, automaticpause:12000
		},
		interaction:{
			prevsteps:1, nextsteps:1,
			prevtosteps:false, nexttosteps:false,
			cycle:true
		}
	});
	
	var reference = "slideshow-medio";
	var mypath=$('[id^="'+reference+'-"]:not([id*=prev]):not([id*=next]):not([id*=first]):not([id*=last])');
	mypath.find(".sfondo-slideshow-medio").css("top",430);
	mypath.find(".testo-slideshow-medio").css("opacity",0).css("display","none");
	var i=9999;
	this[reference+"_init"] = this[reference+"_click"] = function(reference, activated, deactivated, prevsteps, nextsteps, multiple, cycle){
		var mypath=$('#'+reference+"-"+deactivated);
		mypath.css("zIndex",i+1);
		mypath.find(".sfondo-slideshow-medio").stop().css("zIndex",499).animate({top:430, left:269, opacity:0.5},{queue:false,duration:800,specialEasing:{top:custom1,left:custom1,opacity:custom1}});
		mypath.find(".testo-slideshow-medio").stop().animate({opacity:0},{queue:false,duration:800,specialEasing:{top:custom0},
			complete:function(){
					$(this).css("display","none");
			}
		});
		//
		var mypath=$('#'+reference+"-"+activated);
		mypath.css("zIndex",i);
		mypath.find(".sfondo-slideshow-medio").stop().css("zIndex",498).css("top",0).css("left",193).css("opacity",100);
		mypath.find(".testo-slideshow-medio").stop().css("zIndex",500).css("display","block").css("opacity",0).animate({opacity:1},{queue:false,duration:800,specialEasing:{top:custom0}});
		//
		i--;
		if(i<600){i=9999;}
	}
	mg_init({
		reference:"slideshow-medio",
		activated:[0],
		click:{
			interactive:false,
			linked:[],
			max_activated:1
		},
		interaction:{
			prevsteps:1, nextsteps:1,
			prevtosteps:false, nexttosteps:false,
			cycle:true
		}
	});
	
	var reference = "slideshow-sotto";
	this[reference+"_init"] = this[reference+"_click"] = function(reference, activated, deactivated, prevsteps, nextsteps, multiple, cycle){
		var mypath=$('#'+reference+"-"+deactivated);
		mypath.find(".icona-slideshow-sotto").removeClass("active");
		mypath.find(".freccia-slideshow-sotto").stop().animate({top:0},{queue:false,duration:800,specialEasing: {top:custom0}});
		var mypath=$('#'+reference+"-"+activated);
		mypath.find(".icona-slideshow-sotto").addClass("active");
		mypath.find(".freccia-slideshow-sotto").stop().animate({top:-10},{queue:false,duration:800,specialEasing: {top:custom0}});
	}
	mg_init({
		reference:"slideshow-sotto",
		activated:[0],
		click:{
			interactive:true,
			linked:["slideshow-sopra","slideshow-medio"],
			max_activated:1
		},
		interaction:{
			prevsteps:1, nextsteps:1,
			prevtosteps:false, nexttosteps:false,
			cycle:true
		}
	});
        
        
        
        
// ROLLOVER PRODOTTI      
	var reference = "rollover";
	var mypath=$('[id^="'+reference+'-"]:not([id*=prev]):not([id*=next]):not([id*=first]):not([id*=last])');
	mypath.find(".prodotto-rollover").css("opacity",0);
	
	this[reference+"_hover"] = function(reference, activated, deactivated, prevsteps, nextsteps, multiple, cycle){
		$('[id^="'+reference+'-"]').find(".outline-rollover").stop().animate({opacity:0.5},{queue:false,duration:1800,specialEasing:{opacity:custom0}});
		if(!$.browser.msie || $.browser.version>=9){
			$('[id^="'+reference+'-"]').find(".ombra-rollover").stop().animate({opacity:0.5},{queue:false,duration:1800,specialEasing:{opacity:custom0}});
		}
		var mypath=$('#'+reference+"-"+activated);
		mypath.find(".prodotto-rollover").stop().animate({opacity:1},{queue:false,duration:1800,specialEasing:{opacity:custom0}});
		mypath.find(".outline-rollover").stop().animate({opacity:0},{queue:false,duration:1800,specialEasing:{opacity:custom0}});
		mypath.find(".ombra-rollover").stop().animate({opacity:1},{queue:false,duration:1800,specialEasing:{opacity:custom0}});
		/* tooltip */
		$('#testo-tooltip-rollover').html(mypath.find('.testo-rollover').html());
		$('#tooltip-rollover').css("display","block");
		if($.browser.msie && $.browser.version<8){
			$('#testo-tooltip-rollover').css("margin-top",38);
		}else{
			$('#tooltip-rollover').css("opacity",0);
			$('#tooltip-rollover').animate({opacity:1},{queue:false,duration:1800,specialEasing:{opacity:custom0}});
		}
		mypath.mousemove(function(event) {
			
			var offset = $('#tooltip-rollover').parent().offset();
			$('#tooltip-rollover').css("top",event.pageY-130).css("left",event.pageX-52);
			//$('#tooltip-rollover').css("top",event.pageY-130-offset.top).css("left",event.pageX-52-offset.left);
			//alert(event.pageY+"__"+offset.top);
			//$('#tooltip-rollover').css("top",event.pageY-130-offset.top).css("left",event.pageX-52-offset.left);
			//$('#tooltip-rollover').css("top",$('#tooltip-rollover').offsetTop-130).css("left",$('#tooltip-rollover').offsetLeft-52);
		});
		
	}
	this[reference+"_out"] = function(reference, activated, deactivated, prevsteps, nextsteps, multiple, cycle){
		$('[id^="'+reference+'-"]').find(".outline-rollover").stop().animate({opacity:1},{queue:false,duration:1800,specialEasing:{opacity:custom0}});
		$('[id^="'+reference+'-"]').find(".ombra-rollover").stop().animate({opacity:1},{queue:false,duration:1800,specialEasing:{opacity:custom0}});
		var mypath=$('#'+reference+"-"+activated);
		mypath.find(".prodotto-rollover").stop().animate({opacity:0},{queue:false,duration:1800,specialEasing:{opacity:custom0}});
		mypath.find(".outline-rollover").stop().animate({opacity:1},{queue:false,duration:1800,specialEasing:{opacity:custom0}});
		/* tooltip */
		$('#tooltip-rollover').css("display","none");
	}
	
	mg_init({
		reference:"rollover",
		activated:[],
		hover:{
			interactive:true,
			max_activated:1
		},
		out:{
			interactive:true,
			max_activated:1
		}
	});

		
        
// MENU PRINCIPALE
	$(document).ready(function(){
	
		// Mondo Ursini
		$("#nav-main ul li.mondo").hover(
		function() {
			$("#nav-main ul li.mondo").stop().css({"background-position": "center"});
			$("#nav-main ul li.luoghi").stop().css({"background-position": "bottom"});
		},
		function() {
			$("#nav-main ul li.mondo").stop().css({"background-position": "top"});
			$("#nav-main ul li.luoghi").stop().css({"background-position": "top"});
		});
		
		// Luoghi e persone
		$("#nav-main ul li.luoghi").hover(
		function() {
			$("#nav-main ul li.luoghi").stop().css({"background-position": "center"});
			$("#nav-main ul li.prodotti").stop().css({"background-position": "bottom"});
		},
		function() {
			$("#nav-main ul li.luoghi").stop().css({"background-position": "top"});
			$("#nav-main ul li.prodotti").stop().css({"background-position": "top"});
		});
		
		// Prodotti
		$("#nav-main ul li.prodotti").hover(
		function() {
			$("#nav-main ul li.prodotti").stop().css({"background-position": "center"});
			$("#nav-main ul li.news").stop().css({"background-position": "bottom"});
		},
		function() {
			$("#nav-main ul li.prodotti").stop().css({"background-position": "top"});
			$("#nav-main ul li.news").stop().css({"background-position": "top"});
		});
		
		// News
		$("#nav-main ul li.news").hover(
		function() {
			$("#nav-main ul li.news").stop().css({"background-position": "center"});
			$("#nav-main ul li.contatti").stop().css({"background-position": "bottom"});
		},
		function() {
			$("#nav-main ul li.news").stop().css({"background-position": "top"});
			$("#nav-main ul li.contatti").stop().css({"background-position": "top"});
		});
		
		// Contatti
		$("#nav-main ul li.contatti").hover(
		function() {
			$("#nav-main ul li.contatti").stop().css({"background-position": "center"});
		},
		function() {
			$("#nav-main ul li.contatti").stop().css({"background-position": "top"});
		});
	
	});
        



// ACCORDION MENU
	var TINY={};

	function T$(i){return document.getElementById(i)}
	function T$$(e,p){return p.getElementsByTagName(e)}
	
	TINY.accordion=function(){
		function slider(n){this.n=n; this.h=[]; this.c=[]}
		slider.prototype.init=function(t,e,m,o,k){
			var a=T$(t), i=x=0; this.s=k||'', w=[], n=a.childNodes, l=n.length; this.m=m||false;
			for(i;i<l;i++){if(n[i].nodeType!=3){w[x]=n[i]; x++}} this.l=x;
			for(i=0;i<this.l;i++){
				var v=w[i]; this.h[i]=h=T$$(e,v)[0]; this.c[i]=c=T$$('div',v)[0]; h.onclick=new Function(this.n+'.pr(false,this)');
				if(o==i){h.className=this.s; c.style.height='auto'; c.d=1}else{c.style.height=0; c.d=-1}
			}
		};
		slider.prototype.pr=function(f,d){
			for(var i=0;i<this.l;i++){
				var h=this.h[i], c=this.c[i], k=c.style.height; k=k=='auto'?1:parseInt(k); clearInterval(c.t);
				if((k!=1&&c.d==-1)&&(f==1||h==d)){
					c.style.height=''; c.m=c.offsetHeight; c.style.height=k+'px'; c.d=1; h.className=this.s; su(c,1)
				}else if(k>0&&(f==-1||this.m||h==d)){
					c.d=-1; h.className=''; su(c,-1)
				}
			}
		};
		// change speed
		function su(c){c.t=setInterval(function(){sl(c)},25)};
		function sl(c){
			var h=c.offsetHeight, d=c.d==1?c.m-h:h; c.style.height=h+(Math.ceil(d/3)*c.d)+'px'; 
			c.style.opacity=h/c.m; c.style.filter='alpha(opacity='+h*100/c.m+')';
			if((c.d==1&&h>=c.m)||(c.d!=1&&h==1)){if(c.d==1){c.style.height='auto'} clearInterval(c.t)}
		};
		return{slider:slider}
	}();
		
    var parentAccordion=new TINY.accordion.slider("parentAccordion");
    parentAccordion.init("acc","h3",false,0);
    
    var nestedAccordion=new TINY.accordion.slider("nestedAccordion");
    nestedAccordion.init("nested","h3",true,0,"acc-selected"); // 0 apre il primo tab, 1 il secondo ...
	
	

// FRECCIA NEXT DETTAGLIO PRODOTTO
	$(document).ready(function(){
	
		// prodotti
		$("#box_immagine_prodotto").mouseover(
		function() {
			$("#box_immagine_prodotto img.next").stop().css("visibility","visible");
		}).mouseout(
		function(){
			$("#box_immagine_prodotto img.next").stop().css("visibility","hidden");
		});
		
	});



// RELATED PRODUCTS
var reference = "related";
var mypath=$('[id^="'+reference+'-"]:not([id*=prev]):not([id*=next]):not([id*=first]):not([id*=last])');
mypath.css("display","none").css("opacity",0);
this[reference+"_init"] = this[reference+"_click"] = function(reference, activated, deactivated, prevsteps, nextsteps, multiple, cycle){
	var arr=multiple.old_activated;
	for(var i=0;i<arr.length;i++){
		var mypath=$('#'+reference+"-"+arr[i]);
		mypath.stop().delay(40*i).animate({top:20,opacity:0},{queue:true, duration:800, specialEasing: {top:custom0, opacity:custom0},
			complete:function(){
				$(this).css("display","none");
			}
		});
	}
	//
	var arr=multiple.activated;
	for(var i=0;i<arr.length;i++){
		var mypath=$('#'+reference+"-"+arr[i]);
		mypath.css("display","block").css("left",30+i*184).css("top",-120);
		mypath.stop().delay(40*i).animate({top:0,opacity:1},{queue:true, duration:1200, specialEasing:{top:custom0, opacity:custom1}});
	}
}
this[reference+"_prevhide"] = function(reference, prevsteps){$("#related-prev").css("display","none");}
this[reference+"_prevshow"] = function(reference, prevsteps){$("#related-prev").css("display","block");}
this[reference+"_nexthide"] = function(reference, nextsteps){$("#related-next").css("display","none");}
this[reference+"_nextshow"] = function(reference, nextsteps){$("#related-next").css("display","block");}

mg_init({
	reference:"related",
	activated:[0],
	click:{
		interactive:false,
		linked:[],
		less:0, plus:4,
		max_activated:1
	},
	interaction:{
		prevsteps:5, nextsteps:5,
		prevtosteps:false, nexttosteps:false,
		cycle:false
	}
});
	

    

