// Funzione che apre un div e chiude tutti gli altri //

function THISonTHAToff(code) {

    ff = document.getElementsByTagName("div");
    for (i = 0; i < ff.length; i++) {
        if (ff[i].id.substr(0,5)=="onOff") {
            ff[i].style.display = "none";
        }
    }   
    for (i = 0; i < ff.length; i++) {
        if (ff[i].id == code) {
            if (ff[i].style.display == "none") {
                ff[i].style.display = "";
            }
        }
    }
}

// Funzione che apre e chiude un div cliccando sul link //

function THISonTHISoff(code) {
    ff = document.getElementById(code);
		if (ff.style.display != "none") {
			ff.style.display = "none";
		} else
		if (ff.style.display == "none") {
			ff.style.display = "";
		}
}

// Funzione che apre la popup //

function Popup(url, stile) {
	window.open(url, "", stile);
}

var firstTime = 1;
var abs_Height_Speech_offset = 0;

var size_divrisposta_cont = -1;
var size_divcont_int_sx = 0;
var size_divbox_cerca_argomento_sx = 0;
var size_divdomande_piu_cercate_cont = 0;
var size_divbox_domande_scroll_ce = 0;

var newsize_divrisposta_cont = 0;
var newsize_divcont_int_sx = 0;
var newsize_divbox_cerca_argomento_sx = 0;
var newsize_divdomande_piu_cercate_cont = 0;
var newsize_divbox_domande_scroll_ce = 0;

function toggleSpeech(qualeDiv) 
{
		//alert('Entro: Grandezza speech: ' + abs_Height_Speech_offset);

		if (size_divrisposta_cont == -1)
		{
			divrisposta_cont = document.getElementById("risposta_cont");
			divcont_int_sx = document.getElementById("cont_int_sx");  //Div con il riquadro sx
			divbox_cerca_argomento_sx = document.getElementById("box_cerca_argomento_sx"); //div sx da incrementare se c'e' bisogno
			divdomande_piu_cercate_cont  = document.getElementById("domande_piu_cercate_cont"); //Div con il riquadro centrale
			divbox_domande_scroll_ce = document.getElementById("box_domande_scroll_ce");  //div da incrementare se c'e' bisogno
		
			size_divrisposta_cont = divrisposta_cont.offsetHeight;
			size_divcont_int_sx = divcont_int_sx.offsetHeight;
			size_divbox_cerca_argomento_sx = divbox_cerca_argomento_sx.offsetHeight;
			size_divdomande_piu_cercate_cont = divdomande_piu_cercate_cont.offsetHeight;
			size_divbox_domande_scroll_ce = divbox_domande_scroll_ce.offsetHeight;		
		}
		
		var stoAprendo = true;
		if (abs_Height_Speech_offset != 0)
			stoAprendo = false;
		
		
        THISonTHISoff('lnkCloseSpeech');
        THISonTHISoff('lnkOpenSpeech');
        THISonTHISoff('lblLiSpeechText');


		if (stoAprendo)
		{
			abs_Height_Speech_offset = getDivHeight('divVirtSize');
			if (abs_Height_Speech_offset > 0)
			{
				divrisposta_cont.style.height = (size_divrisposta_cont + abs_Height_Speech_offset)+"px";
				if (firstTime == 1)
				{
					resizeLayout();
					firstTime = 0;
					divrisposta_cont = document.getElementById("risposta_cont");
					divcont_int_sx = document.getElementById("cont_int_sx");  //Div con il riquadro sx
					divbox_cerca_argomento_sx = document.getElementById("box_cerca_argomento_sx"); //div sx da incrementare se c'e' bisogno
					divdomande_piu_cercate_cont  = document.getElementById("domande_piu_cercate_cont"); //Div con il riquadro centrale
					divbox_domande_scroll_ce = document.getElementById("box_domande_scroll_ce");  //div da incrementare se c'e' bisogno
				
					newsize_divrisposta_cont = divrisposta_cont.offsetHeight;
					newsize_divcont_int_sx = divcont_int_sx.offsetHeight;
					newsize_divbox_cerca_argomento_sx = divbox_cerca_argomento_sx.offsetHeight;
					newsize_divdomande_piu_cercate_cont = divdomande_piu_cercate_cont.offsetHeight;
					newsize_divbox_domande_scroll_ce = divbox_domande_scroll_ce.offsetHeight;		
				}
				else
				{
					divrisposta_cont.style.height = (newsize_divrisposta_cont)+"px";
					divcont_int_sx.style.height = (newsize_divcont_int_sx)+"px";
					divbox_cerca_argomento_sx.style.height = (newsize_divbox_cerca_argomento_sx)+"px";
					divdomande_piu_cercate_cont.style.height = (newsize_divdomande_piu_cercate_cont)+"px";
					divbox_domande_scroll_ce.style.height = (newsize_divbox_domande_scroll_ce)+"px";
				}
			}
		}
		else
		{
				divrisposta_cont.style.height = (size_divrisposta_cont)+"px";
				divcont_int_sx.style.height = (size_divcont_int_sx)+"px";
				divbox_cerca_argomento_sx.style.height = (size_divbox_cerca_argomento_sx)+"px";
				divdomande_piu_cercate_cont.style.height = (size_divdomande_piu_cercate_cont)+"px";
				divbox_domande_scroll_ce.style.height = (size_divbox_domande_scroll_ce)+"px";
				
				abs_Height_Speech_offset = 0;
		}	
		
}

function getDivHeight(qualeDiv)
{
	tempDiv = document.getElementById(qualeDiv);
	if (tempDiv != null)
	{
		return tempDiv.offsetHeight
	}
	else		
	{
		return 0;
	}
}

// Riallinea le 3 colonne dinamicamente: //
function resizeLayout(){
    try
    {    
	    divcont_int_sx = document.getElementById("cont_int_sx");  //Div con il riquadro sx
	    divbox_cerca_argomento_sx = document.getElementById("box_cerca_argomento_sx"); //div sx da incrementare se c'e' bisogno
	    
	    divdomande_piu_cercate_cont  = document.getElementById("domande_piu_cercate_cont"); //Div con il riquadro centrale
	    divbox_domande_scroll_ce = document.getElementById("box_domande_scroll_ce");  //div da incrementare se c'e' bisogno
	     
	    divrisposta_cont = document.getElementById("risposta_cont"); //div con il riquadro e da incrementare se necessario
	 
	    //CALCOLO DELL'OFFSET:
    
        offset_sx = 147;  //pixel da sommare al div sx cont_int_sx per immaginare che parta dalla stessa altezza
        //offset_sx = document.getElementById("boxHostessImage").height - 1;
        
        //alert('offset_sx:' + offset_sx);
        
        //Altezze dei 3 div con i riquadri grigi, tenendo conto degll'offset per quello a sx:
        heightSx = divcont_int_sx.offsetHeight + offset_sx;
        heightCx = divdomande_piu_cercate_cont.offsetHeight;
        heightDx = divrisposta_cont.offsetHeight;
               
        //alert('heightSx: ' + heightSx + ' heightCx: ' + heightCx + ' heightDx: ' + heightDx);
		
		
		//divdiv_speech = document.getElementById("divVirtSize");
		//alert('altezza speech_text:' + divdiv_speech.height);
		//alert('altezza speech_text offset:' + divdiv_speech.offsetHeight);
		
        
        //Determino l'altezza di riferimento a cui allineare tutti i div:
        var refHeight = 0;
        if (heightSx >= heightCx && heightSx >= heightDx)
        {
            refHeight = heightSx;
        }
        else if (heightCx >= heightSx && heightCx >= heightDx)
        {
            refHeight = heightCx;
        }
        else
        {
            refHeight = heightDx;
        }
        
        //alert('refHeight:'+refHeight);
        
        //Ora per allineare i box devo tenere conto che ogni riquadro è composto da diversi sotto div che hanno una
        //loro altezza che ora mi devo ricavare per sapere per differenza a quanto impostare quelli che mi interessano:
        
        newHeightSx = divbox_cerca_argomento_sx.offsetHeight + (refHeight - heightSx);
        newHeightCx = divbox_domande_scroll_ce.offsetHeight + (refHeight - heightCx) + 1;
        newHeightDx = divrisposta_cont.offsetHeight + (refHeight - heightDx);
        
        
        //Allineo i box all'altezza di riferimento:
        divbox_cerca_argomento_sx.style.height = (newHeightSx)+"px";
        divbox_domande_scroll_ce.style.height = (newHeightCx)+"px";
        divrisposta_cont.style.height = (newHeightDx)+"px";
        
        //Compatibilità con browser diversi da IE e FireFox:
        
        BrowserDetect.init();
        //alert('Stai usando ' + BrowserDetect.browser);
        
        if (BrowserDetect.browser != 'Firefox' && BrowserDetect.browser != 'Explorer')
        {
            divbox_cerca_argomento_sx.style.height = (divbox_cerca_argomento_sx.offsetHeight  - 5)+"px";
        }             
	}
    catch (e) 
    {
        //for (var i in e) alert(i + ' = ' + e[i]);
        alert(e.name + ' = ' + e.description + '\n\nMessage = ' + e.message + '\n\nLine: ' + e.line);
    }
}



var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
		this.Suka = 'pippo';
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.vendor,
			subString: "Google Inc.",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};




//INSERIMENTO STILI LINK:
function init()
{
    //CENTRO (ELENCO FAQ):
	divbox_domande_scroll_ce = document.getElementById("box_domande_scroll_ce");
	links=divbox_domande_scroll_ce.getElementsByTagName("a");
	for (var i = 0; i < links.length; i++) 
	{
		element=links[i];
		var relAtt = element.getAttribute("rev");
	    if((relAtt != null) && (element.getAttribute("rev").indexOf("selected") != -1))
		{   
		    //ho trovato l'elemento selezionato!
		    changeStyleSelectedElement(element);
		}
		else
		{
		    //elementi normali non selezionati
		    element.onmouseover = changeStyle;
		    element.onmouseout = restoreStyle;
		}
		/*
		if (element.addEventListener) {
			element.addEventListener ("mouseover",changeStyle,false);
			element.addEventListener ("mouseout",restoreStyle,false);
		} else if (element.attachEvent) {
			element.attachEvent ("onmouseover",changeStyle);
			element.attachEvent ("onmouseout",restoreStyle);
		} else {
			element.onmouseover = changeStyle;
			element.onmouseout = restoreStyle;
		}
		*/
	}
	
	//SX (ELENCO ARGOMENTI):
	divbox_cerca_argomento_sx = document.getElementById("box_cerca_argomento_sx");
	links=divbox_cerca_argomento_sx.getElementsByTagName("a");
	for (var i = 0; i < links.length; i++) 
	{
		element=links[i];
		var relAtt = element.getAttribute("rev");
	    if((relAtt != null) && (element.getAttribute("rev").indexOf("selected") != -1))
		{   
		    //ho trovato l'elemento selezionato!
		    changeStyleSelectedElement(element);
		}
		else
		{
		    //elementi normali non selezionati
		    element.onmouseover = changeStyle;
		    element.onmouseout = restoreStyle;
		}
		/*
		if (element.addEventListener) {
			element.addEventListener ("mouseover",changeStyle,false);
			element.addEventListener ("mouseout",restoreStyle,false);
		} else if (element.attachEvent) {
			element.attachEvent ("onmouseover",changeStyle);
			element.attachEvent ("onmouseout",restoreStyle);
		} else {
			element.onmouseover = changeStyle;
			element.onmouseout = restoreStyle;
		}
		*/
	}
	

	resizeLayout();
}

function changeStyleSelectedElement(element){
    var parent = element.parentNode || element.parentElement;
	parent.style.backgroundColor="#d7eff7";
	parent.style.color="#003399";
	element.style.textDecoration="underline";
}


function changeStyle(){

    var parent = this.parentNode || this.parentElement;
	parent.style.backgroundColor="#d7eff7";
	parent.style.color="#003399";
	this.style.textDecoration="none";
}

function restoreStyle(){
    var parent = this.parentNode || this.parentElement;
	parent.style.backgroundColor="#fff";
	parent.style.color="#003399";
	this.style.textDecoration="underline";
}


var globalVarTabOpen = 0;

//CAMBI DI VISIBILITA' DIV:
function setDivVisibility(whichOn)
{
    try
    {
    
     //div tra il video e i tab:
     js_div_argomento_durata_cont = document.getElementById("div_argomento_durata_cont");
     js_div_chiusura_div_1 = document.getElementById("div_chiusura_div_1");
     js_div_desc_video = document.getElementById("div_desc_video");
     js_div_chiusura_div_2 = document.getElementById("div_chiusura_div_2");
     js_div_tags = document.getElementById("div_tags");
     js_div_chiusura_div_3 = document.getElementById("div_chiusura_div_3");
     js_div_speech = document.getElementById("div_speech");
     js_div_chiusura_div_4 = document.getElementById("div_chiusura_div_4");    
     js_div_linkcorrelati = document.getElementById("div_linkcorrelati");


     
     
     //div dei tab:    
     js_div_contenuto_condividi = document.getElementById("div_contenuto_condividi");
     js_div_contenuto_domanda = document.getElementById("div_contenuto_domanda");
     js_div_contenuto_scarica = document.getElementById("div_contenuto_scarica");

      
     //alert('globalVarTabOpen: ' +globalVarTabOpen + ' whichon: ' + whichOn);
     
    if (((whichOn <= 0)) || ((globalVarTabOpen == whichOn) && (whichOn > 0)))
    {
        js_div_argomento_durata_cont.style.display = "block";
        js_div_chiusura_div_1.style.display = "block";
        js_div_desc_video.style.display = "block";
        js_div_chiusura_div_2.style.display = "block";
        js_div_tags.style.display = "block";
        js_div_chiusura_div_3.style.display = "block";
        if (js_div_speech != null)
            js_div_speech.style.display = "block";
        js_div_chiusura_div_4.style.display = "block";
        js_div_linkcorrelati.style.display = "block";
        if (js_div_contenuto_condividi != null)
            js_div_contenuto_condividi.style.display = "none";
        js_div_contenuto_domanda.style.display = "none";
        js_div_contenuto_scarica.style.display = "none";
        turnOnOffTab("", "Off");
        globalVarTabOpen = 0;
    }
    else
    {
        js_div_argomento_durata_cont.style.display = "none";
        js_div_chiusura_div_1.style.display = "none";
        js_div_desc_video.style.display = "none";
        js_div_chiusura_div_2.style.display = "none";
        js_div_tags.style.display = "none";
        js_div_chiusura_div_3.style.display = "none";
        if (js_div_speech != null)
            js_div_speech.style.display = "none";
        js_div_chiusura_div_4.style.display = "none";
        js_div_linkcorrelati.style.display = "none";
        turnOnOffTab("", "Off");
        
        if (whichOn == 1)
        {
            turnOnOffTab("tab_1", "On");
            if (js_div_contenuto_condividi != null)
                js_div_contenuto_condividi.style.display = "";
            if (js_div_contenuto_domanda != null)
                js_div_contenuto_domanda.style.display = "none";
            if (js_div_contenuto_scarica != null)  
                js_div_contenuto_scarica.style.display = "none";
            //alert('js_div_contenuto_condividi: ' + js_div_contenuto_condividi.innerHtml);
        }
//        else if (whichOn == 2)
//        {
//            turnOnOffTab("tab_2", "On");
//            if (js_div_contenuto_condividi != null)
//                js_div_contenuto_condividi.style.display = "none";
//            if (js_div_contenuto_domanda != null)
//                js_div_contenuto_domanda.style.display = "";
//            if (js_div_contenuto_scarica != null)    
//                js_div_contenuto_scarica.style.display = "none";
//        }
        else if (whichOn == 3)
        {
            turnOnOffTab("tab_3", "On");
            if (js_div_contenuto_condividi != null)
                js_div_contenuto_condividi.style.display = "none";
            if (js_div_contenuto_domanda != null)
                js_div_contenuto_domanda.style.display = "none";
            if (js_div_contenuto_scarica != null)   
                js_div_contenuto_scarica.style.display = "";
        }
        else if (whichOn == 4)
        {
            turnOnOffTab("tab_4", "On");
            if (js_div_contenuto_condividi != null)
                js_div_contenuto_condividi.style.display = "none";
            if (js_div_contenuto_domanda != null)
                js_div_contenuto_domanda.style.display = "none";
            if (js_div_contenuto_scarica != null)   
                js_div_contenuto_scarica.style.display = "";
        }
        globalVarTabOpen = whichOn;
    }
    }
    catch (ex) {
        //alert(ex);
        /*
        var divTab = null;
        divTab = document.getElementById("active_tab_0");
        if (divTab == null)
        {
            divTab = document.getElementById("active_tab_1");
            if (divTab == null)
            {
                divTab = document.getElementById("active_tab_2");
                if (divTab == null)
                {
                    divTab = document.getElementById("active_tab_3");
                    if (divTab == null)
                    {
                        divTab = document.getElementById("active_tab_4"); 
                    }   
                }     
            }   
        }
        if (divTab != null)
        {  
            divTab.style.class = "active_only_question";
            alert(divTab.style.class);
        } 
        */
    } //fine catch
    
}

function turnOnOffTab(id_elemento, onOff)
{
    var divTab = null;
    
    divTab = document.getElementById("active_tab_0");
    if (divTab == null)
        divTab = document.getElementById("active_tab_1");
//    if (divTab == null)
//        divTab = document.getElementById("active_tab_2");
    if (divTab == null)
        divTab = document.getElementById("active_tab_3"); 
    if (divTab == null)
        divTab = document.getElementById("active_tab_4"); 
    if (divTab != null)
    {       
        //alert("ID ATTUALE: " + divTab.id);
        
        if (onOff == "On")
	        divTab.id = "active_" + id_elemento;
	    else
	        divTab.id = "active_tab_0";
     
        //alert("ID NUOVO: " + divTab.id);    
    }
}



