var path_assoluto;
function init(){
	menu2();	
	my_accordion();
	
	// showreel
	if ($("#showreel_home").size()) {
		$("#showreel_home").showreel();
	}
	
	/* VIRTUAL EXPLORE */
		delay=9000;
		virtual_explore(delay);
	/*-------------------------*/
	//active_gallery();
}

function menu2(){
	var tmp;
	$("#masterdivSX ul li").not(".on").find("ul").hide();
	$("#masterdivSX ul li a").click(function() {
		var the_ul;
		the_ul=$(this).parent().find("ul");
		the_ul.slideToggle("fast");
		/* Se si vogliono lasciare aperti gli altri UL commentare la seguente riga */
//		$("#masterdivSX ul li").find("ul").not(the_ul).slideUp("fast");
	});
	
	curr_url=location.href.split('/')[3];
	
	$("ul li a[href='"+curr_url+"']").addClass("on");
	$("ul li a[href='"+curr_url+"']").attr("title",location.href);

	
}

/* SHOWREEL
- - - - - - - - - - - - - - - - - - - - */
$.fn.showreel = function() {
	return this.each(function() {
		var $this = $(this);
		var li = $this.find("li a");
		var interval;
		var i = 0;
		li.eq(0).addClass("on");
		li.mouseover(function(){
			var indx = li.index($(this));
			clearTimeout(interval);
			li.filter(".on").removeClass("on");
			$(this).addClass("on");
			$this.find(".label").stop().animate({"top": (11+ (indx * 20)) + "px"},800,"easeOutExpo");
			$this.find(".flower").stop().animate({"top": "-" + ((indx * 360)) + "px"},800,"easeOutExpo");
		});
		li.mouseout(function(){
			i = li.index($(this));
			looppa();
		});
		function looppa(){
			if (i > li.size() - 1) {
				i = 0;
			}
			li.eq(i++).mouseover();
			interval = setTimeout(looppa,3000);
		}
		looppa();
	});
}


/* Funzione per Menù 
- - - - - - - - - - - - - - - - - - - - */
function menu(){
	$("#menu_vert_3lev a.active").parent().parent().addClass("on");
	$("#menu_vert_3lev ul li ul").not(".on").hide();
	$("#menu_vert_3lev ul li a").click(function(){
		if($(this).attr("href")=="#"){
			if ($(this).next().attr("class") == "on") {
//				$(this).next().removeClass("on").slideUp("fast");
				// scriverlo due volte per trovare gli elementi successivi con classe .on
				$(this).parent().children().not("a").removeClass("on").slideUp("fast"); 
			} else {
				$(this).parent().parent().find("ul.on").removeClass("on").slideUp("fast");
				$(this).parent().children().not("a").addClass("on").slideDown("normal");
//				$(this).next().addClass("on").slideDown("fast");
//				$(this).next().next().addClass("on").slideDown("fast"); // serve per aprire l'UL fratello
			}
			return false;
		}
	});
	apriMenu();
	$("img[src='']").src='/img/trasp.gif';

	
}
function apriMenu(){
	$("#menu_vert_3lev a.on").each(function(){
		var el=this;
		var conta=0;
		while(el !== null && conta<30){
			el=$(el).parent();
			if(el[0].tagName=="UL"){
//				$(el).show();
//				$(el).addClass("on");
				$(el).parent().children().not("a").show();
				$(el).parent().children().addClass("on");
				
			}else if (el[0].tagName=="LI"){
				conta++;
			}else if (el[0].tagName=="DIV"){
				el=null;
			}else{
				conta++;
			}
		}
	});
}
function active_gallery(){
	if (!$("#TB_Window").length && typeof(tb_init)=='function'){
//		imgLoader = new Image();// preload image
		//imgLoader.src = "/img/trasp.gif";
//		imgLoader.src = tb_pathToImage;
		tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
//		$("#TB_Image").load(function(){ $("#TB_Load").hide();alert("caricata"); });
		
	}
}

function my_accordion(){
	$("h2 a[rel='accordion']").click(function(){
		var thed=$(this).parent().next();
		$(".hidden:visible").not($(thed)).slideUp("fast");
		$(this).parent().next().slideDown("fast");
		return false;
	});
}

/* VIRTUAL EXPLORE */

if(typeof(Array.indexof)!=="function"){
	Array.prototype.indexOf = function(f, s) {
		if (typeof s == 'undefined') s = 0;
		for (var i = s; i < this.length; i++) {
			if (f === this[i]) return i;
		}
		return -1;
	}	
}

var ve_pages = new Array();
function virtual_explore(delay){
//	return;
	if (window.location.href.indexOf("&ve=1")>0){
		document.body.innerHTML+="<a href='"+window.location.href.replace('&ve=1','&ve=0')+"' style='position:fixed;top:0px;left:404px;z-index:100;padding:12px;border:1px solid #ff7777;background-color:#770000;color:#fff;'>stop virtual explore</a>";
		
		$("#menu_vert_3lev a").each(
					function(){if(this.href !=="#" && this.href.indexOf("?")>1 && this.href.indexOf("http://")<1)ve_pages.push(this.href+"&ve=1");}
					)
		/*
		ve_pages[0]="http://robopac.websolute.it/viewdoc.asp?co_id=28&ve=1";
		ve_pages[1]="http://robopac.websolute.it/viewdoc.asp?co_id=29&ve=1";
		ve_pages[2]="http://robopac.websolute.it/viewdoc.asp?co_id=30&ve=1";
		ve_pages[3]="http://robopac.websolute.it/viewdoc.asp?co_id=36&ve=1";
		ve_pages[4]="http://robopac.websolute.it/viewdoc.asp?co_id=8&ve=1";
		ve_pages[5]="http://robopac.websolute.it/viewdoc.asp?co_id=10&ve=1";
		ve_pages[6]="http://robopac.websolute.it/viewdoc.asp?co_id=7&ve=1";
		ve_pages[7]="http://robopac.websolute.it/viewdoc.asp?co_id=31&ve=1";
		ve_pages[8]="http://robopac.websolute.it/viewdoc.asp?co_id=32&ve=1";
		ve_pages[9]="http://robopac.websolute.it/viewdoc.asp?co_id=33&ve=1";
		ve_pages[10]="http://robopac.websolute.it/viewdoc.asp?co_id=34&ve=1";
		*/
		var i=0;
		//alert(window.location.href);
		i=ve_pages.indexOf(window.location.href);
		setTimeout(function(delay){if (i+1<ve_pages.length){window.location.href=ve_pages[i+1];}else{window.location.href=ve_pages[0];}},delay);
	} else if (window.location.href.indexOf("&ve=0")>0){
		document.body.innerHTML+="<a href='"+window.location.href.replace('&ve=0','&ve=1')+"' style='position:fixed;top:0px;left:404px;z-index:100;padding:12px;border:1px solid #ff7777;background-color:#770000;color:#fff;'>start virtual explore</a>";
	}
}

/* LANCIA INIT */
if(typeof($)=="function"){
	path_assoluto = location.href;
	$(document).ready (init);

}

