/*****************************************************************************
		Funzioni ausiliarie realizzate per Furla
 *****************************************************************************/

/*****************************************************************************
	 	BrowserDetect
	script di detect del browser preso da QuirksMode
      *  Browser name: 		BrowserDetect.browser
    	* Browser version: 	BrowserDetect.version
    	* OS name: 				BrowserDetect.OS

 *****************************************************************************/
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";
	},
	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.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"
		}
	]

};
BrowserDetect.init();

/*****************************************************************************
	
 *****************************************************************************/
function sizeObj()
{
	this.width 	= 0;
	this.height = 0;
}

/*****************************************************************************
		getClientSize
	restituisce le dimensioni della view del client
 *****************************************************************************/
function getClientSize()
{
	var size = new sizeObj();
	if (BrowserDetect.browser == 'Explorer')
	{
		size.width 	= document.body.clientWidth;
		size.height = document.body.clientHeight;
	}
	else
	{
		size.width 	= window.innerWidth;
		size.height	= window.innerHeight;
	}
	return (size);
}

/*****************************************************************************
		setSitePosition
	imposta la posizione del DIV principale del sito, per evitare che
	stia centrato anche quando le dimensioni del client sono minori di quelle
	necessarie a visualizzare tutti i contenuti
 *****************************************************************************/
function setSitePosition(sid)
{	//alert(document.body.style.overflow);
	var size = new getClientSize();
	var t = $(sid);
	
	if (Math.floor(parseFloat(size.width)) <= 1024) // limite calcolato su FF
		t.style.left = '500px';//'505px';//'510px';
	else
		t.style.left = '';
   if (Math.floor(parseFloat(size.width)) <= 1000 || Math.floor(parseFloat(size.height)) <= 590) // limite calcolato su FF
   {
   	document.documentElement.style.overflow = 'auto';
		document.body.style.overflow = 'auto';
	}
	else
	{
		document.documentElement.style.overflow = 'hidden';
		document.body.style.overflow = 'hidden';
	}
}

/*****************************************************************************
		collapseAll
	Richiamata nell'onLoad, chiude tutti i sotto
	elementi della lista di liste con cui è
	realizzato il menu.
 *****************************************************************************/
function collapseAll()
{
	var frag = $('l_menu');
	var ulli = frag.getElementsByTagName('ul');
	for (i=0; i<ulli.length; i++) // nascondo e associo funzione sul click
		ulli[i].style.display = 'none';
 	var ula = frag.getElementsByTagName('a');
	for (i=0; i<ula.length; i++) // decoloro i link
		ula[i].style.color = '#555555';

	// ---------------------------------------- PATCH FOOTER
	var foot = $('d_foot').getElementsByTagName('a');
	for (w=0;w<foot.length;w++)
	   foot[w].style.color = '#555555'; // setto il colore normale nel footer
	// ---------------------------------------- PATCH FOOTER
	try {
      show_fotoNegozio();
   } catch (e) {}
}

/*****************************************************************************
		change
	Nasconde/mostra un sottomenu
 *****************************************************************************/
function change(t, normal, highlight)
{
	//alert(''+t.href.split('#')[1]);
	try {
		urchinTracker(''+t.href.split('#')[1]);
	}catch(e){ }
	
	if (t.style.color != 'rgb(204, 0, 0)' && t.style.color != '#cc0000')//highlight) // se non sto cliccando quello già "evidenziato"
	{
		var frag = $('l_menu');
		var myul = t.parentNode.getElementsByTagName('ul')[0];
		if (myul+'' != 'undefined' && myul.style.display == 'none') // se ho una sottolista nascosta
		{
			if (t.parentNode.parentNode.parentNode.nodeName == 'DIV') // se sono in un certo livello
			{
	   		var ulli = frag.getElementsByTagName('ul');
				for (i=0; i<ulli.length; i++)
				{
				   if (ulli[i].style.display != 'none')
				   {
				      var ulla = ulli[i].parentNode.getElementsByTagName('a')[0];
						new Effect.toggle(ulli[i], 'blind',{queue:'front', duration:0.5, afterFinish: function(){ulla.style.color = normal;}});
					}
				}
				var ulli = frag.getElementsByTagName('a');
				for (i=0; i<ulli.length; i++)
					ulli[i].style.color = normal; // setto il colore normale
				// ---------------------------------------- PATCH FOOTER
				var foot = $('d_foot').getElementsByTagName('a');
				for (w=0;w<foot.length;w++)
				   foot[w].style.color = '#555555'; // setto il colore normale nel footer
				// ---------------------------------------- PATCH FOOTER
			}
			else // altrimenti
			{
				var brothers = t.parentNode.parentNode.getElementsByTagName('a');
		   	for (i=0; i<brothers.length; i++)
		   	   brothers[i].style.color = normal;
			}
			new Effect.toggle(myul, 'blind',{queue:'end', duration:0.5, beforeStart: function(){t.style.color = highlight;}});
		}
		else if (myul+'' != 'undefined') // ---
		{
			new Effect.toggle(myul, 'blind',{queue:'end', duration:0.5, afterFinish: function(){t.style.color = normal;}});
		}
		else // ---
		{
		   var brothers = t.parentNode.parentNode.getElementsByTagName('a');
		   for (i=0; i<brothers.length; i++)
		   {
		      if (brothers[i] == t)
		      {
		         brothers[i].style.color = highlight;
				}
				else
				{
					var alu = brothers[i].parentNode.getElementsByTagName('ul');
					if (alu.length > 0 && alu[0].style.display != 'none')
					   new Effect.toggle(alu[0], 'blind',{queue:'end', duration:0.5});
					brothers[i].style.color = normal;
				}
			}
		}
	}
}

/*****************************************************************************
		hl
	Cambia il colore in evidenziato se la voce
	non è del colore "aperto" (ovvero voce del
	percorso pagina evidenziato per l'utente)
 *****************************************************************************/
function hl(t)
{
	if (t.style.color != 'rgb(204, 0, 0)' && t.style.color != '#cc0000')
		t.style.color = '#000000';
}
/*****************************************************************************
		ul
	Cambia il colore in normale se la voce
	non è del colore "aperto" (ovvero voce del
	percorso pagina evidenziato per l'utente)
 *****************************************************************************/
function uh(t)
{
	if (t.style.color != 'rgb(204, 0, 0)' && t.style.color != '#cc0000')
		t.style.color = '#555555';
}

/*****************************************************************************
	getVirtualUrl
	Restituisce un array associativo
	output   array
 *****************************************************************************/
function getVirtualUrl()
{
	var wloc = window.location+'';
	wloc = wloc.substr(0, wloc.lastIndexOf('/'));
   wloc = wloc.substr(wloc.lastIndexOf('/')+1, wloc.length);
	return(wloc);
}

/*****************************************************************************
	show_fotoNegozio
 *****************************************************************************/
function show_fotoNegozio()
{
	
	var url		= arguments[0] || '';
	var fotoID  = $('foto_negozio');
	var dvFoto  = $('div_foto_negozio');
	var slideM  = $('slideshow_main');
	if (url != '')
	{
	   //fotoID.style.backgroundColor = '#ffffff';
   	fotoID.innerHTML 		= '<img src="'+url+'"/>';
   	fotoID.style.display = 'block';
   	dvFoto.style.width   = '320px';
   	slideM.style.display = 'none'; // aggiunto per FF per MAC
	}
	else // tolgo foto
	{
		//fotoID.style.backgroundColor = 'transparent';
	   fotoID.innerHTML 		= '';
	   dvFoto.style.width   = '0px';
	   slideM.style.display = 'block'; // aggiunto per FF per MAC

	   var mappa	= $('gmap');
	   mappa.style.top = '-560px';
	}
}

/*****************************************************************************
	show_mappaNegozio
 *****************************************************************************/
function show_mappaNegozio(lat, lng, txt)
{
	var fotoID  = $('foto_negozio');
	var dvFoto  = $('div_foto_negozio');
	var slideM  = $('slideshow_main');
	fotoID.innerHTML 		= '';
	dvFoto.style.width   = '0px';
	slideM.style.display = 'block'; // aggiunto per FF per MAC

	var gmap	= $('gmap');
	gmap.style.top = '60px';
	//frames['gmapiframe'].location.href = "http://www.furla.com/"+"gmap.php?lat="+lat+"&lng="+lng+"&txt="+txt;
}

/*****************************************************************************
	color_place
 *****************************************************************************/
function color_place(t,id)
{
//alert(t)
//alert(id)
	
	if (id == 'content3') {
	    id = 'colonna3';
	}
	else if (id == 'content4') {
	    id = 'colonna4';
	}
	var tag = arguments[2] || 'a'; // il 3zo parametro è facoltativo, di default è 'a'
	var cid = $(id);
	t=$(t);
	if (cid!=null && cid!='undefined') {
	var list = cid.getElementsByTagName(tag);
		for(i=0;i<list.length;i++)
			list[i].style.color='#555555';
		t.style.color='#cc0000';
	}
}
/*****************************************************************************
	initSlider
		inizializza lo slider
 *****************************************************************************/
function initSlider(scrollArea, scroller, container, content)//, row, col)
{                                                                                               alert('initSlider');
	var cnt = 0;
 	var tid = window.setInterval(function() {
 	   if (container=='container1' || container=='container2' || container=='container3' || container=='container4')
	   { // per evitare che la barra non appaia alla posizione corretta nella pagina negozi, se prima sono stato altrove
			document.getElementById('d_content').style.position = '';
			document.getElementById('d_content').style.top		= '';
			document.getElementById('d_content').style.left		= '';
		}
		if (document.getElementById(scrollArea))
		{
			//window.clearInterval(tid); // appena lo vedo blocco il check per la barra
			var scr = document.getElementById(scrollArea);
			if (scr.style.display != 'block')
			{
				scrolling(scrollArea,container,content,1); // faccio un test!!! ^_^
				var conLen = 0;
				if (document.getElementById(content).style.marginTop.indexOf('-') >= 0)
				{
				   conLen = 1;
				}
				document.getElementById(content).style.marginTop = '0px';
				document.getElementById(content).style.visibility = 'visible'; // ### usato per colonna.php e dettaglio_comunicato e dettaglio_newsarte
				if (conLen != 0)
				{
				   if (true)//(scr.style.display != 'block')
				   {
				   	scr.style.display = 'block';
				   	document.getElementById(scroller).style.cursor = 'default';//n-resize'; // cambio cursore associato allo scroller
						new Control.Slider(scroller,scrollArea,{
										axis:'vertical',
										onSlide:function(v){
															scrolling(scrollArea,container,content,v);
													},
										onChange:function(v){
															scrolling(scrollArea,container,content,v);
													}
									});
					}
				}
			}
		} // endif ($(scrollArea))
		cnt++;
	 	if (cnt >= 10) // evito loop infiniti
			window.clearInterval(tid);
	},250); // era 250... rallentato per evitare che non faccia apparire lo scroller nei negozi (vedi milano e tokyo)

}

/*****************************************************************************
	Funzioni chiamate sulle onclick di menu e footer
*****************************************************************************/
function jmenu(t,c55,cCC,o5)
{
	try {
 		change(t, c55, cCC, o5); // la change da problemi su safari, per cui è in try
	} catch (e) {}
}
function jmenuNeg(t,c55,cCC,o5, mappe, where,template,imarr,imxid)
{
	try {
		change(t, c55, cCC, o5);
	} catch (e) {}
	new Effect.FadingBackLoad(where, template, imarr, imxid);
	try {
	   locator(mappe);
	} catch (e) {}
}
function jsubmenu(t,c55, cCC, o5, where, template, imarr, imxid, parz)
{
	
	//alert(t)
	//alert(c55)
	//alert(cCC)
	//alert(o5)
	//alert(where)
	//alert(template)
	//alert(imarr)
	//alert(imxid)
	//alert("aaa  "+parz)
	
	try {
      show_fotoNegozio();
   } catch (e) {}
	try {
		change(t, c55, cCC, o5);
	} catch (e) {}
	new Effect.MovingLoad(where, template, imarr, imxid, {pars:parz});
}
function jsubmenuNeg(t,c55,cCC, o5, mappe, parz, label)
{
	try {
		change(t, c55, cCC, o5);
	} catch (e) {}
	//new Effect.FadingLoadCol('colonna1', 'colonna', 1, {pars:parz+'&c=1'}); // se desse problemi sostituire con ajaxload diretto
	show_fotoNegozio();
	JSONload('content', 1, label);
	try {
		locator(mappe);
   } catch (e) {}
}
function jfooter(t,c55,cCC,o5, where, template, imarr, imxid, parz)
{
	try {
      show_fotoNegozio();
   } catch (e) {}
	try {
	   change(t, c55, cCC, o5);
	} catch (e) {}
	new Effect.MovingLoad(where, template, imarr, imxid, {pars:parz});
}

// cambia i § in '
function quoter(str)
{
	var reg = /§/g;
	var newstr = "'";
	return str.replace(reg,newstr);
}
/*****************************************************************************
			Caricamento dati negozi dall'array di oggetti JSON
*****************************************************************************/
var jsonCol = new Array();
function JSONload(where, col, branch)
{
	for (var t=col+1; t<=4; t++)            // azzero le colonne successive
	{   
	    var cntnt = $('content'+t);	//alert(t+"\n"+cntnt);	//$(where+t);	alert(where+"\n"+t+"\n"+cntnt);
		if (cntnt!='undefined' && cntnt!=null) {
			cntnt.innerHTML = '&nbsp;<br/>';
		}
 	}
 	if ($('colonna3')!='undefined' && $('colonna3')!=null) {
 	    $('colonna3').innerHTML = '';
	}
	if ($('colonna4')!='undefined' && $('colonna4')!=null) {
 	    $('colonna4').innerHTML = '';
	}

	if (col == 1) // nel caso di caricamento in prima colonna lo reinizializzo (il puntatore nella posizione del tree)
		jsonCol = new Array();
	jsonCol[col-1] = branch;
	var colNeg = 2;
	if (jsonCol.length>1)
		if (jsonCol[1].indexOf('ITALIA')>=0 || jsonCol[1].indexOf('ITALY')>=0) // i negozi in italia sono in 4^ colonna
	   	colNeg++;
	
	var jsc0 = jsonCol[0];
	var jsc1 = jsonCol[1];
	if (col>=2) // se sono oltre a paesi (da colonna 3 in poi), devo riferirmi alle versioni _it, in quanto non sono multilingua
	{
		jsc0 = jsonCol[0].split('_');
		if (jsc0[0]=='EUROPE')
			jsc0[0]='EUROPA';
		jsc0 = jsc0[0]+'_it';
		jsc1 = jsonCol[1].split('_');
		jsc1 = jsc1[0]+'_it';
	}
	//alert("col: "+col+"\n colNeg: "+colNeg+"\n jsc0: "+jsc0+"\n jsc1: "+jsc1);
	
	switch (col)//jsonCol.length)
	{
		case 1:  //alert('1');
	 				jsonPtr = JSNegozi[jsc0];
	 				break;
		case 2:  //alert('2');
	 				jsonPtr = JSNegozi[jsc0][jsc1];
	 				break;
   		case 3:  //alert('3');
	 				jsonPtr = JSNegozi[jsc0][jsc1][jsonCol[2]];
	 				break;
	}

	var container;
	if ($(where+col)!='undefined' && $(where+col)!=null) {
		container = $(where+col);   //alert(where+col+"\n"+container);
	}
	else {
	    container = $('colonna'+col);
	}
	container.innerHTML = '';
	
	for(var b in jsonPtr) {
	   var s = b;
	   if (b.indexOf('_')>=0) // se ho il postfisso di lingua, lo rimuovo
		{
			s = b.split('_');
			s = s[0];
		}
		col1 = col +1; // colonna successiva
		if (col<colNeg)
		{
	   	container.innerHTML += '<div style="margin-bottom:4px;"><a style="font-size:11px;" href="#" onmouseover="hl(this)" onmouseout="uh(this)" onclick="color_place(this,\'content'+(col)+'\'); show_fotoNegozio(); JSONload(\'content\', '+col1+', \''+b+'\');">'+quoter(s)+'</a></div>';
		}
		else
		{
		   if (colNeg == 2)
		   {  //alert(jsonCol[0]+'-'+colNeg)
		      container.innerHTML += '<div style="margin-bottom:4px;"><a style="font-size:11px;" href="#" onmouseover="hl(this)" onmouseout="uh(this)" onclick="color_place(this,\'content'+col+'\'); show_fotoNegozio(); new Effect.FadingLoadCol(\'colonna'+col1+'\', \'colonna\', '+col1+', {pars:\'?t=mshop_'+JSNegozi[jsc0][jsc1][b]+'&cn='+b+'&c='+col1+'\'});">'+b+'</a></div>'; //b+'---'+JSNegozi[jsc0][jsc1][b]+"<br/>";
			}
		   else
		   {  //alert(jsonCol[0]+'#'+colNeg)
		   	container.innerHTML += '<div style="margin-bottom:4px;"><a style="font-size:11px;" href="#" onmouseover="hl(this)" onmouseout="uh(this)" onclick="color_place(this,\'content'+col+'\'); show_fotoNegozio(); new Effect.FadingLoadCol(\'colonna'+col1+'\', \'colonna\', '+col1+', {pars:\'?t=ishop_'+JSNegozi[jsc0][jsc1][jsonCol[2]][b]+'&cn='+b+'&c='+col1+'\'});">'+b+'</a></div>'; //b+'---'+JSNegozi[jsc0][jsc1][jsonCol[2]][b]+"<br/>";
			}
			// new Effect.FadingLoadCol(\'colonna'+(col+1).'\', \'colonna\', '+(col+1)+', {pars:\''.$pars.'&c='+(col+1)+'\'});"
		}
	}
}

/*****************************************************************************
		handleError
	Funzione di sviluppo per migliore debugging.
 *****************************************************************************/
function handleError()
{
   /*errorz = "[OD::DEBUG]\n";
	for(i=0; i<arguments.length; i++)
   		errorz += arguments[i]+"\n";
   alert (errorz);*/
	return true;
}
window.onerror = handleError; // load della custom debug alert win
