var path_assoluto;
function init(){
	menu();	
	my_accordion();
	/* VIRTUAL EXPLORE */
		delay=9000;
		virtual_explore(delay);
	/*-------------------------*/
	//active_gallery();
	$("form").submit(function(){return get_form_validation(this);});
}

/* 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.on").siblings("ul").addClass("on").show();
	$("#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>";
	}
}

/* CONVALIDA FORM
-------------------------------------------*/
function ws_validate(){}
ws_validate.prototype.isEmail = function (what)
{
   var i=new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
   if(!i.test(what)) { return false}   
   return true;
}
ws_validate.prototype.isDate = function (what)
{
   var i=new RegExp("^([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{4})$");
   if(i.test(what))
   {
	datePart=what.split("/");
	giorno=(datePart[0]>=1) && (datePart[0]<=31);
	mese=(datePart[1]>=1) && (datePart[1]<=12);
	anno=(datePart[2]>=1900) && (datePart[2]<=9999);
	if(!giorno || !mese || !anno) { return false; }
   }else{
	return false;
   }
   return true;
}

ws_validate.prototype.isTelFax = function(what)
{
//   var i=new RegExp("([0-9]{2,})\/([0-9]{5,})");
   var i=new RegExp("([0-9]{5,})");
   if(!i.test(what)) { return false;}   
   return true;
}

ws_validate.prototype.notZero = function(what)
{
   if(what=="0") { return false;}   	
   return true;
}
ws_validate.prototype.field = function (what, type){
	try{
		switch (type) {
		  case "isEmail":
			return this.isEmail($(what).val());
			break; 
		  case "isDate":
			return this.isDate($(what).val());
			break; 
		  case "isTelFax":
			return this.isTelFax($(what).val());
			break; 
		  case "isChecked":
		  	return $(what).is(":checked");
			break; 
		  case "notZero":
			return this.notZero($(what).val());
			break; 
		  case "isText":
			return ($.trim($(what).val()).length>0);
			break; 
		  case "isNumeric":
			return (!isNaN($(what).val()) && ($.trim($(what).val()).length>0));
			break; 
		  default: 	
			return ($(what).val().length>0);
		}
		
	}
	
	catch (e){
		alert(e);
	}
}
var validate = new ws_validate;

function get_form_validation(frm_id){
	var oval,valid,validation_string;
	oval=$(frm_id)[0]._validation;
	for (nc in oval){
		if( validate.field("#"+nc,oval[nc])==false ){
			$("#"+nc).parent().addClass("bg_error");
			valid=false;
		}else{
			$("#"+nc).parent().removeClass("bg_error");
		}
	}
	$(".bg_error input, .bg_error select, .bg_error textarea").eq(0).focus();
	return valid;
}


/* LANCIA INIT */
if(typeof($)=="function"){
	path_assoluto = location.href;
	$(document).ready (init);

}


