function flashWriter(trgt,w,h,swfCurrPage) { //v3.0
	if (AC_FL_RunContent == 0 || DetectFlashVer == 0) {
		alert("This page requires AC_RunActiveContent.js.");
	} else {
		var hasRightVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
		if(hasRightVersion) {  // if we've detected an acceptable version
			//swfCurrPage+='&r='+Math.floor(Math.random()*5000000);
			document.getElementById(trgt).style.display = 'block';
			var swfStr=AC_FL_RunContent(
				'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,45,0',
				'width',w,
				'height',h,
				'src',swfCurrPage,
				'quality','high',
				'pluginspage','http://www.macromedia.com/go/getflashplayer',
				'movie',swfCurrPage,
				'id',trgt+"swf",
				'name',trgt+"swf",
				'wmode','transparent',
				//'wmode','window',
				'allowFullScreen','true',
				'align', 'middle',
				'play', 'true',
				'loop', 'true',
				'scale', 'noScale',
				'devicefont', 'false',
				'bgcolor', '#231f20',
				'menu', 'true',
				'allowScriptAccess','always'
			);
			document.getElementById(trgt).innerHTML = swfStr;
		} else {  // flash is too old or we can't detect the plugin
			document.getElementById(trgt).innerHTML = "<div class=\"flashAlt\">This content requires Javascript enabled and the Adobe Flash Player.<br \/><br \/><a href=\"http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash\">Get Adobe Flash Player<\/a><\/div>";
		}
	}
}
function setContentLoader(trgt,cnt){
	if (navigator.appName.indexOf("Microsoft") != -1) {
		var tmp = window[trgt+"swf"];
	} else {
		var tmp = document[trgt+"swf"];
	}
	if (tmp) {
		tmp.avviaJs(sitePath+cnt);
	} else {
		tmp = new Array();
		tmp.push(trgt);
		tmp.push(400);
		tmp.push(338);
		tmp.push('/_fp/flxerPlayer4.swf?cnt=');
		tmp.push(sitePath+cnt);
		flashToLoad.push(tmp);
		flashWriter(trgt,400,338,"/_fp/flxerPlayer4.swf?cnt="+sitePath+cnt)
	}
}
function setContentLoader2(trgt,cnt){
	if (navigator.appName.indexOf("Microsoft") != -1) {
		var tmp = window[trgt+"swf"];
	} else {
		var tmp = document[trgt+"swf"];
	}
	if (tmp) {
		tmp.avviaJs(sitePath+cnt);
	} else {
		tmp = new Array();
		tmp.push(trgt);
		tmp.push(760);
		tmp.push(409);
		tmp.push('/_fp/flxerPlayer4.swf?cnt=');
		tmp.push(sitePath+cnt);
		flashToLoad.push(tmp);
		flashWriter(trgt,760,409,"/_fp/flxerPlayer4.swf?cnt="+sitePath+cnt)
	}
}
function caricaFlashAvvio() {
	for (i=0;i<flashToLoad.length;i++){
		flashWriter(flashToLoad[i][0],flashToLoad[i][1],flashToLoad[i][2],flashToLoad[i][3]+sitePath+flashToLoad[i][4]);
		window[flashToLoad[i][0]+"swf"] = document.getElementById(flashToLoad[i][0]+"swf");
	}
	window.flashswf = document.getElementById("flashswf");
}
function rimuoviFlashAvvio() {
	for (i=0;i<flashToLoad.length;i++){
		document.getElementById(flashToLoad[i][0]).innerHTML = "";
	}
}
// FLASH CSS FUNCTIONS
function styleDetector(){
	var aspetta=false;	
	//valuto se ho lo stile nel cookie o nella sessione
	pageStyle="palm";
	var referPageStyle="";
	cookiePageStyle=GetCookie("pageStyle");
	if(cookiePageStyle!="" && cookiePageStyle!=null && cookiePageStyle!="null" ){
		referPageStyle=cookiePageStyle;
	}else{
		//valuto se ho a disposizioni nelle sessione informazioni relative allo stile		
		if(xmlRequestObject){
			if(ajaxGetSessionComplite=="vuota"){
				getSession('pageStyle');
				aspetta=true;
			}else if(ajaxGetSessionComplite=="complete"){
				referPageStyle=sessionPageStyle;
			}
		}	
	}
	if(aspetta==false){
		if(referPageStyle!=""){	
			hasRightFlashVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
			pageStyle=referPageStyle;
		}else{
			if(screen.availWidth>700 && screen.availHeight>500){
				pageStyle="html";
				//VERIFICHE FLASH
				hasRightFlashVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
				if(hasRightFlashVersion){
					pageStyle="flash";
				}else{
					// non c'è flash immetto un cookie per ricordare che l'azione di controllo l'ho già fatta
					cookieFlashAlert=GetCookie("flashAlert");
					if(!cookieFlashAlert){				
						res=window.confirm(str_msg_confirm);
						if(res){
							window.location.href="http://www.macromedia.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash";
						}
						var expdateflashAlert = new Date();
						expdateflashAlert.setTime(expdateflashAlert.getTime() +  (24 * 60 * 60 * 1000 * 365)); 
						SetCookie("flashAlert", "true", expdateflashAlert, "/", null, false); 	
					}
				}
			}
		}
		detectorComplete=true;
	}
}
/* COOKIE  */
function SetCookie(name, value){		
   var argv = SetCookie.arguments;
   var argc = SetCookie.arguments.length;
   var expires = (2 < argc) ? argv[2] : null;
   var path = (3 < argc) ? argv[3] : null;
   var domain = (4 < argc) ? argv[4] : null;
   var secure = (5 < argc) ? argv[5] : false;
   document.cookie = name + "=" + escape (value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +((path == null) ? "" : ("; path=" + path)) +((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : "");
}
function GetCookie(name){
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen){
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			return getCookieVal(j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) 
			break; 
	}
	return null;
}
function getCookieVal(offset){
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) {
		endstr = document.cookie.length;
	}
	return unescape(document.cookie.substring(offset, endstr));
}
function RemoveCookie(name){
	document.cookie = name+"=; path=/";	
}
/* SESSION */
function setSession(name,value,callback){
	if(xmlRequestObject){
		xmlRequestObject.open("GET", "/_php/setSession.php?"+name+"="+value , true);
		if(callback==""){
			xmlRequestObject.onreadystatechange = setSessionComplite;
		}else{
		 	xmlRequestObject.onreadystatechange = eval(callback);
		}
		xmlRequestObject.send(null);
		ajaxComplite=false;
	}	
}

function setSessionComplite(){
	if (xmlRequestObject.readyState==4) {
		ajaxComplite=true;
	}	
}
function getSession(name){
	if(xmlRequestObject){
		xmlRequestObject.open("GET", "/_php/getSession.php?name="+name, true);
		xmlRequestObject.onreadystatechange = getSessionComplite;
		xmlRequestObject.send(null);
		ajaxGetSessionComplite="loading";
	}	
}

function getSessionComplite(){
	if (xmlRequestObject.readyState==4) {
		ajaxGetSessionComplite="complete";
		if (xmlRequestObject.responseText.indexOf('invalid') == -1){
			var node = xmlRequestObject.responseXML.documentElement;
			var items = node.getElementsByTagName('session');
			if(items[0].firstChild){
				sessionPageStyle=items[0].firstChild.data;
			}else{
				sessionPageStyle="";
			}
		}
		styleDetector();
	}
}
// FLASH RESIZER
function chkW(a) {
	if (a<=1000) {
		var tmp = "1000";
	} else {
		var tmp = "100%";
	}
	return tmp;
}

function chkH(a) {
	if (a<=580) {
		var tmp = "580";
	} else {
		var tmp = "100%";
	}
	return tmp;
}

function alertSize() {
	if( typeof( window.innerWidth ) == 'number' ) {
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth ||document.documentElement.clientHeight ) ) {
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	flashResizer(chkW(myWidth),chkH(myHeight));
}
function flashResizer(w,h){
	if (navigator.appName.indexOf("Microsoft") != -1) {
		var tmp = window["flashswf"];
	} else {
		var tmp = document["flashswf"];
	}
	tmp.height=h;
	tmp.width=w;
	if (w != "100%"){
		w = w+"px";
	}		
	if (h != "100%"){
		h = h+"px";
	}
	document.getElementById("flash").style.width=w;		
	document.getElementById("flash").style.height=h;
}

// La funzione per cambiare i fogli di stile
function setStyleSheet(){	
	if(!document.styleSheets || navigator.userAgent.indexOf("Safari")!=-1 || (navigator.userAgent.indexOf("Konqueror")!=-1)){
		var ss = getAllSheets() //non dom
	}else{
		var ss = document.styleSheets; //Dom
	}
	// disabilita tutti i fogli di stile con un titolo 
	// tranne quello passato per argomento alla funzione
	for( var x = 0; x < ss.length; x++ ) {
		ss[x].disabled=true;
		if(arguments[0]=="default"){
			if( ! ss[x].title ) {
				ss[x].disabled=false;
			}
		}else{
			if( ss[x].title == arguments[0]) {
				ss[x].disabled=false;
			}			
		}
	}
	if( !ss.length ) { 
		alert( 'Il tuo browser non è abilitato a cambiare i fogli di stile CSS' );
	}
}

// ---------------------------------------------------------------------------------

// Funzione per Opera
function getAllSheets(){
	if( document.getElementsByTagName ) {
		var Lt = document.getElementsByTagName('LINK');
		var St = document.getElementsByTagName('STYLE');
	} else {
		// browser minori - restituisce array vuoto
		return []; 
	}
	//per tutti i tag link ...
	for( var x = 0, os = []; Lt[x]; x++ ) {
		//controlla l'attributo rel per vedere se contiene 'style'
		if( Lt[x].rel ) {
			var rel = Lt[x].rel;
		} else if( Lt[x].getAttribute ) {
			var rel = Lt[x].getAttribute('rel');
		} else {
			var rel = '';
		}
		if(typeof(rel)=='string'&&rel.toLowerCase().indexOf('style')+1){
			//riempe la variabile os con i stylesheets linkati
			os[os.length] = Lt[x];
		}
	}
	//include anche tutti i tags style e restituisce l'array
	for( var x = 0; St[x]; x++ ) {
		os[os.length] = St[x];
	}
	return os;
}





/* MAIN FUNCTION */

function setStyle(myStyle){
	var flash_current_page;
	var cambialocation=false;
//elimino l'object del flash se il precedente stile era flash
	if(document.getElementById("flash").innerHTML.indexOf("<object")!=-1 || document.getElementById("flash").innerHTML.indexOf("<embed")!=-1){
		var IE = navigator.appName.indexOf("Microsoft") != -1;
		var filmato = IE ? window.flashswf : window.document.flashswf;
		flash_current_page=filmato.getCurrenURL();
		//alert("bella "+flash_current_page);
		pos=window.location.href.indexOf(flash_current_page);
		if(window.location.href.substr(pos)!=flash_current_page){
			cambialocation=true;
		}		
		//document.getElementById("flash").innerHTML=alternateTextSwf;
	}			
//elimino l'object del flashplayer se il precedente stile era htmlflash
/*
if(document.getElementById("flash").innerHTML.indexOf("<object")!=-1){
		//+---------------------------+
		//|		da definirsi		  |
		//+---------------------------+		
		//document.getElementById("flash").innerHTML=alternateTextSwf;
	}				
*/	
//assegno la var globale dello stile	
	pageStyle=myStyle;
//modifico lo stile della pagina
	if(!cambialocation){
		if(pageStyle=="flash"){
			setStyleSheet("advanced");
			document.getElementById("flash").style.display="block";
			document.getElementById("acc").style.display="none";	
			//scrivo il blocchetto del flash
			flashWriter('flash','100%','100%',swfCurrPage);
			alertSize();
			rimuoviFlashAvvio();
		}else if(pageStyle=="html"){
			setStyleSheet("advanced");		
			document.getElementById("acc").style.display="block";
			document.getElementById("flash").style.display="none";
			caricaFlashAvvio();
			//scrivo il blocchetto dell'inserto flash
			//+---------------------------+
			//|		da definirsi		  |
			//+---------------------------+
		}else if(pageStyle=="palm"){	
			setStyleSheet("default");	
			document.getElementById("acc").style.display="block";
			document.getElementById("flash").style.display="none";		
			caricaFlashAvvio();
		}else if(pageStyle=="nostyle"){		
			setStyleSheet();
			document.getElementById("acc").style.display="block";
			document.getElementById("flash").style.display="none";
			caricaFlashAvvio();
		}else{
			alert("ERROR.. pliz rilod de peig :-)");
			document.getElementById("acc").style.display="block";
			document.getElementById("flash").style.display="none";		
			caricaFlashAvvio();
		}
		makeMenuVersion();	
	}	
//setto il cookie
	var expdate = new Date();
	expdate.setTime(expdate.getTime() +  (24 * 60 * 60 * 1000 * 365)); 
	SetCookie("pageStyle", pageStyle, expdate, "/", null, false);
//setto la sezione	
	setSession("pageStyle",pageStyle,"");	
// 	cambia location
	if(cambialocation){
		window.location.href=flash_current_page;
	}
}

function makeMenuVersion(){
//	alert(pageStyle.toUpperCase()+" flash version"+hasRightFlashVersion+" sW:"+screen.availWidth+" sH:"+screen.availHeight);
	var mymenuStr="SWAP STYLE | ";
	if(hasRightFlashVersion){ // menu stili (flash, html, palm, nostyle)
		document.getElementById("versionsflashhtml").style.display="inline";		
		document.getElementById("versionshtml").style.display="none";
		document.getElementById("versionspalm").style.display="none";
		if(pageStyle=="flash"){
			mymenuStr+="<span class=\"currentcss\">FLASH CSS</span> | <a onclick=\"setStyle('html');\" href=\"#\" title=\"HTML FLASH STYLE SHEET\">HTML CSS</a> | <a onclick=\"setStyle('palm');\" href=\"#\" title=\"PALM STYLE SHEET\">PALM CSS</a> | <a onclick=\"setStyle('nostyle');\" href=\"#\" title=\"NO STYLE SHEET\">NO CSS</a> |";
		}else if(pageStyle=="html"){
			mymenuStr+="<a onclick=\"setStyle('flash');\" href=\"#\" title=\"FLASH STYLE SHEET\">FLASH CSS</a> | <span class=\"currentcss\">HTML CSS</span> | <a onclick=\"setStyle('palm');\" href=\"#\" title=\"PALM STYLE SHEET\">PALM CSS</a> | <a onclick=\"setStyle('nostyle');\" href=\"#\" title=\"NO STYLE SHEET\">NO CSS</a> |";
		}else if(pageStyle=="palm"){	
			mymenuStr+="<a onclick=\"setStyle('flash');\" href=\"#\" title=\"FLASH STYLE SHEET\">FLASH CSS</a> | <a onclick=\"setStyle('html');\" href=\"#\" title=\"HTML FLASH STYLE SHEET\">HTML CSS</a> | <span class=\"currentcss\">PALM CSS</span> | <a onclick=\"setStyle('nostyle');\" href=\"#\" title=\"NO STYLE SHEET\">NO CSS</a> |";
		}else if(pageStyle=="nostyle"){		
			mymenuStr+="<a onclick=\"setStyle('flash');\" href=\"#\" title=\"FLASH STYLE SHEET\">FLASH CSS</a> | <a onclick=\"setStyle('html');\" href=\"#\" title=\"HTML FLASH STYLE SHEET\">HTML CSS</a> | <a onclick=\"setStyle('palm');\" href=\"#\" title=\"PALM STYLE SHEET\">PALM CSS</a> | <span class=\"currentcss\">NO CSS</span> |";
		}	
		document.getElementById("versionsflashhtml").innerHTML=mymenuStr;
	}else if(screen.availWidth>700 && screen.availHeight>500){  // menu stili (flash not supported, html, palm, nostyle)
		mymenuStr="FLASH CSS (browser not supported) | ";
		document.getElementById("versionsflashhtml").style.display="none";		
		document.getElementById("versionshtml").style.display="inline";
		document.getElementById("versionspalm").style.display="none";	
		if(pageStyle=="html"){
			mymenuStr+="<span class=\"currentcss\">HTML CSS</a> | <a onclick=\"setStyle('palm');\" href=\"#\" title=\"PALM STYLE SHEET\">PALM CSS</span> | <a onclick=\"setStyle('nostyle');\" href=\"#\" title=\"NO STYLE SHEET\">NO CSS</a> |";
		}else if(pageStyle=="palm"){	
			mymenuStr+="<a onclick=\"setStyle('html');\" href=\"#\" title=\"HTML FLASH STYLE SHEET\">HTML CSS</a> | <span class=\"currentcss\">PALM CSS</span> | <a onclick=\"setStyle('nostyle');\" href=\"#\" title=\"NO STYLE SHEET\">NO CSS</a> |";			
		}else if(pageStyle=="nostyle"){		
			mymenuStr+="<a onclick=\"setStyle('html');\" href=\"#\" title=\"HTML FLASH STYLE SHEET\">HTML CSS</a> | <a onclick=\"setStyle('palm');\" href=\"#\" title=\"PALM STYLE SHEET\">PALM CSS</a> | <span class=\"currentcss\">NO CSS</span> |";
		}			
		document.getElementById("versionshtml").innerHTML=mymenuStr;
	}else{
		document.getElementById("versionsflashhtml").style.display="none";		
		document.getElementById("versionshtml").style.display="none";
		document.getElementById("versionspalm").style.display="inline";			
	}
}
//funzione invocata ciclicamente che verifica se il detector ha terminato l'esecuzione
function checkDetectorStatus(){
	if(detectorComplete){
		setStyle(pageStyle);
		clearInterval(timerID);
	}else{
		if(contStep>maxStep){
			clearInterval(timerID);	
			setStyle(pageStyle);
		}
	}
	contStep++;
}


