var path_assoluto, co_id=1, tmp;
function init(){
	// assegno alla variabile co_id il valore assegnato nella querysrting
	tmp = location.search.toLowerCase().match(/co_id=(\d+)\D*/);
	if (tmp && tmp.length>0){co_id=tmp[1]}
	menu();	
	PulisciInput("form input:text, .raform2 input:text ");
	my_accordion();
	/* VIRTUAL EXPLORE 
		delay=9000;
		virtual_explore(delay);
	-------------------------*/
	//active_gallery();
	$("form").submit(function (){return get_form_validation(this);}); //"#"+$(this).attr("id")
	ws_json_form.init("form.post_ajax");
	if(swfobject){
		// sostituisco l'elemento con id="animazione_swf" con l'oggetto FLASH indicato nel primo parametro
		swfobject.embedSWF("/forum.swf?co_id="+co_id, "animazione_swf", "940", "420", "9.0.0", "/js/swfobject/expressInstall.swf", {}, {wmode:'transparent'});
	}	
//	replace_flash_objects();

	/*$("#menu_top").css({"opacity": "0.8"});
	$("#menu_top_int").css({"opacity": "0.8"});
	$("#menu_top_prod").css({"opacity": "0.8"});*/
	
	//alert (tmp);
	if (tmp == "co_id=73,73") {
		//alert (73);
		$("#scheda_prod").addClass("colonna_high");
		}
}

/* 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");
					});
		
		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>";
	}
}

/* GALLERY NOTHICK
-------------------------------------------*/
function gallery_nothick(){
	var tg, gscreen, current,top, playing;
	tg = $("a.nothickbox");
	top = $(tg).size()-1;
	current=0;
	playing=0;
	var partenza = false;
	gscreen = $("img.gallery_screen");
	$(tg).click(function(){
						 	$(gscreen).attr("src",this.href); 
							current=$(tg).index(this); 
							$(tg).removeClass("current_img");
							$(this).addClass("current_img");
							return false;
							});
	$("a.gprevious").click(function(){
				var i, ni;
				$(tg).removeClass("current_img");
				if (top<0 ){return false;}
				if(current>0){
					i=current-1;
				}else{
					i=top;
				}
				current = i;
				ni=$(tg).eq(i).attr("href");
				if(ni!=="undefined"){
					$(gscreen).attr("src",ni);
					$(tg).eq(i).addClass("current_img");
				}
				return false;
			});
	$("a.gnext").click(function(){
				var ni,i;
				$(tg).removeClass("current_img");
				if (top<0 ){return false;}
				if(current<top){
					i=current+1;
				}else{
					i=0;
				}
				current = i;
				ni=$(tg).eq(i).attr("href");
				if(ni!=="undefined"){
					$(gscreen).attr("src",ni);
					$(tg).eq(i).addClass("current_img");
				}
				return false;
			});
	$("body").append("<img src='" + $(tg).eq(0).attr("href") + "' alt='' class='hidden_loader' style='display:none' /> ");
//	$("body img.hidden_loader").eq(0).load(function(){$(gscreen).attr("src",$(this).attr("src")); playNext();});
	nothick_playNext = function(){
		if(playing==1){
			if (current<=top){
					//$(gscreen).attr("src",$(tg).eq(current));
					$(tg).removeClass("current_img");
					current++;
					if (current>top){current=0;}
					$("body img.hidden_loader").eq(0).attr("src",$(tg).eq(current).attr("href"));
					$(tg).eq(current).addClass("current_img");
			}

			partenza=window.setTimeout(nothick_playNext,3000);
		}else{
			partenza=false;
		}
	}
	nothick_play=function(){
			playing=1;
			$("body img.hidden_loader").eq(0).load(activePlay);
			$(".nothick_play").removeClass("on");
			$(".nothick_stop").addClass("on");
			nothick_playNext();
			return false;
		}
	nothick_stop=function(){
			playing = 0;
			partenza = false;
			$("body img.hidden_loader").eq(0).unbind("load");
			$(".nothick_play").addClass("on");
			$(".nothick_stop").removeClass("on");
			return false;		
		}
	activePlay = function(){
		var ni=$(this).attr("src");
		$(gscreen).fadeOut("fast",function(){$(gscreen).attr("src",ni)}).fadeIn("fast");

	}
	$(gscreen).click(function(){if (playing==0){nothick_play();}else{nothick_stop()}});
	$(".nothick_play").click(nothick_play);
	$(".nothick_stop").click(nothick_stop);
}


/* 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,invioTesto, errmsg;
	errmsg = "<br /><span class='bg_error'>bisogna compilare i campi obbligatori</span>";
	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();
//	invioTesto = $("#isc_messaggio").html();
//	$("#isc_messaggio").html(invioTesto + errmsg);
	$(frm_id)[0].valid = valid;
	return valid;
}
/* REPLACE FLASH OBJECTS*/
function replace_flash_objects()
{
	$(".replace_flash_objects").each(function ()
		{
			var f, re,p;
			f = String($(this).children("noscript").html()).replace("&lt;","<").replace("&gt;",">");

			while(f.indexOf("&lt;")>=0){
				f = f.replace("&lt;","<");
			}
			while(f.indexOf("&gt;")>=0){
				f = f.replace("&gt;",">");
			}
/*
			p="<[^>]{0,1}object[^>]*>|<[^>]{0,1}param[^>]*>";
			re = new RegExp();
			re.pattern=p;
			f.replace(re,"");
			re.pattern="&lt;";
			f.replace(re,"<");
			re.pattern="&gt;";
			f.replace(re,">");
*/
			if(f.length<10){f = $(this).children(".embed").html()}
//			$(this).prepend(f);
			this.innerHTML=f;
		}
	 );
}

/* JSON FORM */
var ws_json_form = {
		theForm: "" ,
		init: function(p){
			try{
			$("body").after("<iframe name=\"ws_ifr_upload_file\" id=\"ws_ifr_upload_file\" style=\"display:none\" rows=\"1\" cols=\"1\" src=\"\"></iframe>");
			$(p).each(function(){this.target="ws_ifr_upload_file";});
			$(p).submit(ws_json_form.ajax_submit);
			}
			catch(e){alert(e);}
		},
		ajax_submit: function(){
			var post_data,ifr_target;
			try{
				ws_json_form.the_form = this;
				if(ws_json_form.the_form.valid==false){return false;}
				if($(this).attr("enctype")=="multipart/form-data"){
				/* upload files BEGIN */
				ifr_target = $("#ws_ifr_upload_file");
				if ($(ifr_target).size()==0){
					$("body").after("<iframe name=\"ws_ifr_upload_file\" id=\"ws_ifr_upload_file\" src=\"\"></iframe>");
				}
				this.target="ws_ifr_upload_file";
				$("#ws_ifr_upload_file").load(function(){
					   var d,s;
					   s="success";
					   d=eval("(" + frames['ws_ifr_upload_file'].document.body.innerHTML +")" );
					   ws_json_form.callback_json(d,s);
					});
				return true;
				/* upload files END*/
				}else{
					post_data = $(this).serializeArray();
					$.post($(this).attr("action"), post_data, ws_json_form.callback_json, "json");
				}
			}
			catch(e){
				self.status = (e);
			}
			return false;
		},
		callback_json:	function(vdata, textStatus){
			var d;
			try{
				d = vdata;
				if ($(ws_json_form.the_form).children("div.response").size()==0 ){$(ws_json_form.the_form).prepend("<div class=\"response\"></div>")}

				if(d.status=="success"){
					$(ws_json_form.the_form).children("div.response").html(d.data);
				}else{
					$(ws_json_form.the_form).children("div.response").html(d.error);
				}
				if(parseInt($(ws_json_form.the_form).height())>parseInt($(ws_json_form.the_form).children("div.response:eq(0)").height())){
					$(ws_json_form.the_form).children("div.response").css({"height": $(ws_json_form.the_form).height()+"px"});
				}
				$(ws_json_form.the_form).children("fieldset").fadeOut("fast",function(){$(ws_json_form.the_form).children("div.response").fadeIn("fast");});
				$(ws_json_form.the_form).children("div.response").hide();
			}
			catch(e){
				self.status = (e);
				alert(e);
			}
		},
		back_and_complete: function (chi){
			$(chi).parent().fadeOut('slow',function(){
				$(chi).parent().parent().children('fieldset').fadeIn('slow');
				return false;
			});
		}
	}

/* LANCIA INIT */
if(typeof($)=="function"){
	path_assoluto = location.href;
	$(document).ready (init);
}

function PulisciInput(arr_input) {
	$(arr_input).each(function(){
			if($(this).length){
				$(this)[0].tmp_input_val=$(this).val();
			}
			$(this).focus( function () {
										if (this.value==this.tmp_input_val) {
											this.value='';
										}
									}						  
						  );
			$(this).blur( function () {
								if (this.value=='') {
									this.value=this.tmp_input_val;
								}
							}
						  );
							   
		});
}






///////////////////////////////////////////////////////////////// ----------------------------------------------------------------------
//v1.7
// Flash Player Version Detection
// Detect Client Browser type
// Copyright 2005-2007 Adobe Systems Incorporated.  All rights reserved.
var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;

function ControlVersion()
{
	var version;
	var axo;
	var e;

	// NOTE : new ActiveXObject(strFoo) throws an exception if strFoo isn't in the registry

	try {
		// version will be set for 7.X or greater players
		axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
		version = axo.GetVariable("$version");
	} catch (e) {
	}

	if (!version)
	{
		try {
			// version will be set for 6.X players only
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
			
			// installed player is some revision of 6.0
			// GetVariable("$version") crashes for versions 6.0.22 through 6.0.29,
			// so we have to be careful. 
			
			// default to the first public version
			version = "WIN 6,0,21,0";

			// throws if AllowScripAccess does not exist (introduced in 6.0r47)		
			axo.AllowScriptAccess = "always";

			// safe to call for 6.0r47 or greater
			version = axo.GetVariable("$version");

		} catch (e) {
		}
	}

	if (!version)
	{
		try {
			// version will be set for 4.X or 5.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
			version = axo.GetVariable("$version");
		} catch (e) {
		}
	}

	if (!version)
	{
		try {
			// version will be set for 3.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
			version = "WIN 3,0,18,0";
		} catch (e) {
		}
	}

	if (!version)
	{
		try {
			// version will be set for 2.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
			version = "WIN 2,0,0,11";
		} catch (e) {
			version = -1;
		}
	}
	
	return version;
}

// JavaScript helper required to detect Flash Player PlugIn version information
function GetSwfVer(){
	// NS/Opera version >= 3 check for Flash plugin in plugin array
	var flashVer = -1;
	
	if (navigator.plugins != null && navigator.plugins.length > 0) {
		if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
			var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
			var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
			var descArray = flashDescription.split(" ");
			var tempArrayMajor = descArray[2].split(".");			
			var versionMajor = tempArrayMajor[0];
			var versionMinor = tempArrayMajor[1];
			var versionRevision = descArray[3];
			if (versionRevision == "") {
				versionRevision = descArray[4];
			}
			if (versionRevision[0] == "d") {
				versionRevision = versionRevision.substring(1);
			} else if (versionRevision[0] == "r") {
				versionRevision = versionRevision.substring(1);
				if (versionRevision.indexOf("d") > 0) {
					versionRevision = versionRevision.substring(0, versionRevision.indexOf("d"));
				}
			}
			var flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
		}
	}
	// MSN/WebTV 2.6 supports Flash 4
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
	// WebTV 2.5 supports Flash 3
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
	// older WebTV supports Flash 2
	else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
	else if ( isIE && isWin && !isOpera ) {
		flashVer = ControlVersion();
	}	
	return flashVer;
}

// When called with reqMajorVer, reqMinorVer, reqRevision returns true if that version or greater is available
function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision)
{
	versionStr = GetSwfVer();
	if (versionStr == -1 ) {
		return false;
	} else if (versionStr != 0) {
		if(isIE && isWin && !isOpera) {
			// Given "WIN 2,0,0,11"
			tempArray         = versionStr.split(" "); 	// ["WIN", "2,0,0,11"]
			tempString        = tempArray[1];			// "2,0,0,11"
			versionArray      = tempString.split(",");	// ['2', '0', '0', '11']
		} else {
			versionArray      = versionStr.split(".");
		}
		var versionMajor      = versionArray[0];
		var versionMinor      = versionArray[1];
		var versionRevision   = versionArray[2];

        	// is the major.revision >= requested major.revision AND the minor version >= requested minor
		if (versionMajor > parseFloat(reqMajorVer)) {
			return true;
		} else if (versionMajor == parseFloat(reqMajorVer)) {
			if (versionMinor > parseFloat(reqMinorVer))
				return true;
			else if (versionMinor == parseFloat(reqMinorVer)) {
				if (versionRevision >= parseFloat(reqRevision))
					return true;
			}
		}
		return false;
	}
}

function AC_AddExtension(src, ext)
{
  if (src.indexOf('?') != -1)
    return src.replace(/\?/, ext+'?'); 
  else
    return src + ext;
}

function AC_Generateobj(objAttrs, params, embedAttrs) 
{ 
  var str = '';
  if (isIE && isWin && !isOpera)
  {
    str += '<object ';
    for (var i in objAttrs)
    {
      str += i + '="' + objAttrs[i] + '" ';
    }
    str += '>';
    for (var i in params)
    {
      str += '<param name="' + i + '" value="' + params[i] + '" /> ';
    }
    str += '</object>';
  }
  else
  {
    str += '<embed ';
    for (var i in embedAttrs)
    {
      str += i + '="' + embedAttrs[i] + '" ';
    }
    str += '> </embed>';
  }

  document.write(str);
}

function AC_FL_RunContent(){
  var ret = 
    AC_GetArgs
    (  arguments, ".swf", "movie", "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
     , "application/x-shockwave-flash"
    );
  AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
}

function AC_SW_RunContent(){
  var ret = 
    AC_GetArgs
    (  arguments, ".dcr", "src", "clsid:166B1BCA-3F9C-11CF-8075-444553540000"
     , null
    );
  AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
}

function AC_GetArgs(args, ext, srcParamName, classid, mimeType){
  var ret = new Object();
  ret.embedAttrs = new Object();
  ret.params = new Object();
  ret.objAttrs = new Object();
  for (var i=0; i < args.length; i=i+2){
    var currArg = args[i].toLowerCase();    

    switch (currArg){	
      case "classid":
        break;
      case "pluginspage":
        ret.embedAttrs[args[i]] = args[i+1];
        break;
      case "src":
      case "movie":	
        args[i+1] = AC_AddExtension(args[i+1], ext);
        ret.embedAttrs["src"] = args[i+1];
        ret.params[srcParamName] = args[i+1];
        break;
      case "onafterupdate":
      case "onbeforeupdate":
      case "onblur":
      case "oncellchange":
      case "onclick":
      case "ondblClick":
      case "ondrag":
      case "ondragend":
      case "ondragenter":
      case "ondragleave":
      case "ondragover":
      case "ondrop":
      case "onfinish":
      case "onfocus":
      case "onhelp":
      case "onmousedown":
      case "onmouseup":
      case "onmouseover":
      case "onmousemove":
      case "onmouseout":
      case "onkeypress":
      case "onkeydown":
      case "onkeyup":
      case "onload":
      case "onlosecapture":
      case "onpropertychange":
      case "onreadystatechange":
      case "onrowsdelete":
      case "onrowenter":
      case "onrowexit":
      case "onrowsinserted":
      case "onstart":
      case "onscroll":
      case "onbeforeeditfocus":
      case "onactivate":
      case "onbeforedeactivate":
      case "ondeactivate":
      case "type":
      case "codebase":
      case "id":
        ret.objAttrs[args[i]] = args[i+1];
        break;
      case "width":
      case "height":
      case "align":
      case "vspace": 
      case "hspace":
      case "class":
      case "title":
      case "accesskey":
      case "name":
      case "tabindex":
        ret.embedAttrs[args[i]] = ret.objAttrs[args[i]] = args[i+1];
        break;
      default:
        ret.embedAttrs[args[i]] = ret.params[args[i]] = args[i+1];
    }
  }
  ret.objAttrs["classid"] = classid;
  if (mimeType) ret.embedAttrs["type"] = mimeType;
  return ret;
}
///////////////////////////////////////////////////////////////// ----------------------------------------------------------------------
