/* CSS STYLESHEET */
// 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;
}

/* 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();
	}
}



/* MAIN FUNCTION */
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){				
						str_msg_confirm="To see the Flyer communication web site you need the Adobe Flash Player 8.\nClick OK to download Adobe Flash Player.";
						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;
	}
}


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){
		var IE = navigator.appName.indexOf("Microsoft") != -1;
		var filmato = IE ? window.flashobj : window.document.flashobj;
		flash_current_page=filmato.GetVariable("_root.currentURL");
		if(window.location.href.indexOf(flash_current_page)==-1){
			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
			document.getElementById("flash").innerHTML=AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,19,0','width','100%','height','100%','src',swfCurrPage,'quality','high','pluginspage','http://www.macromedia.com/go/getflashplayer','movie',swfCurrPage ,'id','flashobj','wmode','transparent','name','flashobj');
			alertSize();
		}else if(pageStyle=="html"){
			setStyleSheet("advanced");		
			document.getElementById("acc").style.display="block";
			document.getElementById("flash").style.display="none";
			//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";		
		}else if(pageStyle=="nostyle"){		
			setStyleSheet();
			document.getElementById("acc").style.display="block";
			document.getElementById("flash").style.display="none";
		}else{
			alert("ERROR.. pliz rilod de peig :-)");
			document.getElementById("acc").style.display="block";
			document.getElementById("flash").style.display="none";		
		}
		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++;
}

//varibili del timer
var timerID;
var contStep=0;
var maxStep=30;
var detectorComplete=false;
//varibili ajax
var ajaxComplite;
var ajaxGetSessionComplite="vuota";
var xmlRequestObject = false; // XMLHttpRequest Object
if (window.XMLHttpRequest) // try to create XMLHttpRequest
	xmlRequestObject = new XMLHttpRequest();
if (window.ActiveXObject && !(navigator.userAgent.indexOf("MSIE")!=-1 && navigator.userAgent.indexOf("Mac")!=-1 && navigator.appVersion.indexOf("MSIE 5")!=-1) )	// if ActiveXObject use the Microsoft.XMLHTTP
	xmlRequestObject = new ActiveXObject("Microsoft.XMLHTTP");

//varibili relative agli stili
var pageStyle;
var sessionPageStyle;//da ricevere con ajax
var cookiePageStyle;
//paramtri per il flash detect
var requiredMajorVersion = 8;// Major version of Flash required
var requiredMinorVersion = 0;// Minor version of Flash required
var requiredRevision = 0;// Revision of Flash required
var jsVersion = 1.0;// the version of javascript supported
var alternateTextSwfInsert="contenuto alternativo all'inserto flash";
var alternateTextSwf="contenuto alternativo all' swf";
var hasRightFlashVersion=false;
styleDetector();
