//tfoNavigateur
//tfoDetection de tfoNavigateur
var tfoDetect = navigator.userAgent.toLowerCase();
var tfoOS,tfoDetect,tfoNavigateur,tfoVerNav,tfoNavStr;

if (checkIt('konqueror'))
{
	tfoNavigateur = "Konqueror";
	tfoOS = "Linux";
}
else if (checkIt('chrome')) tfoNavigateur = "Chrome"
else if (checkIt('firefox')) tfoNavigateur = "Firefox"
else if (checkIt('omniweb')) tfoNavigateur = "OmniWeb"
else if (checkIt('opera')) tfoNavigateur = "Opera"
else if (checkIt('webtv')) tfoNavigateur = "WebTV";
else if (checkIt('icab')) tfoNavigateur = "iCab"
else if (checkIt('msie')) tfoNavigateur = "Internet Explorer"
else if (checkIt('safari')) tfoNavigateur = "Safari"
else if (!checkIt('compatible'))
{
	tfoNavigateur = "Netscape Navigator"
	tfoVerNav = tfoDetect.charAt(8);
}
else tfoNavigateur = "An unknown tfoNavigateur";

if (!tfoVerNav) 
{
	if(tfoNavigateur=="Safari")
	{
		checkIt('version');
		tfoVerNav = tfoDetect.charAt(place + tfoNavStr.length);
	}
	else
	{
		tfoVerNav = tfoDetect.charAt(place + tfoNavStr.length);
	}
}

if (!tfoOS)
{
	if (checkIt('linux')) tfoOS = "Linux";
	else if (checkIt('x11')) tfoOS = "Unix";
	else if (checkIt('mac')) tfoOS = "Mac"
	else if (checkIt('win')) tfoOS = "Windows"
	else tfoOS = "an unknown operating system";
}

function checkIt(string)
{
	place = tfoDetect.indexOf(string) + 1;
	tfoNavStr = string;
	return place;
}

// popups
function tfo_openPopup(urlx,namex,wx,hx){
	var fenetreOpener = window.open(urlx,namex,'height=' + hx+ ',width=' + wx +',toolbar=no,menubar=yes,scrollbars=yes,Status=no,resizable=yes');
	fenetreOpener.focus();
}

function tfo_printAll(){
	//firefox sur mac ou pc s'imprime a partir de window.self.print();
	if (navigator.platform.substring(0,3)=="Win" || navigator.appVersion.indexOf("safari")==-1 ){
		window.self.print();
	}
	else{
		window.alert(tfo_trads['JsImprimMac']);
	}
}
function tfo_allerAncre(nomAncre){
	var url = new String(window.document.location);
	var strippedUrl = url.toString().split("#");
	var nbAncres = strippedUrl.length;
	window.document.location = strippedUrl[0]+'#'+nomAncre;
}

//stats
function tfo_trackExterne(href, classification){
	
}

function tfo_initialiseListeImages(tfo_jsonPoints){
	if( tfo_jsonPoints==null){
		//ne fait rien
	}
	else
	{
		jsonPoints=tfo_jsonPoints;
		var imageCollection = new Array();
		var nb = jsonPoints.length;
		var imagesErreurs = new Array();
		for (var i = 0; i < nb; i++) {
			var tmpImgerreur=tfo_changeImage(i, tfo_jsonPoints);
			if(tmpImgerreur){imagesErreurs.push(i);}
		}
	
		// gestion des erreurs sur les marqueurs
		var nbNull = imagesErreurs.length;
		if(nbNull>0){
			var str = '';		
			for (var i = 0; i < nbNull; i++) {
				str += imagesErreurs[i] + ', ';
			}
			//alert('les images suivantes sont errones : ' + str);
		}
	}
}

function tfo_deduitUrlDetail(id_tfo){
	//alert('tfo_initialiseListeImages');
	
}




function tfo_changeImage(i, tfo_jsonPoints)
{
	var imageErreur=false;
	if(jsonPoints[i]==null){
			imageErreur=true;
		}else{
			var domImg=document.getElementById('tfoImg'+jsonPoints[i].idT);
			if(jsonPoints[i].pIl && domImg){
				//alert('tfoImgOk'+jsonPoints[i].idT);
				domImg.src=jsonPoints[i].pIl;
			}else{
				imageErreur=true;
				if(domImg){
				//alert('tfoImgPasOk'+jsonPoints[i].idT);
					domImg.src=tfo_const['noImgListe']; //constate du js-header.php
					domImg.className+=' invisible'; // RAG 25/08/10 a la demande du CDT
				}

			}
		}
		
		return imageErreur;
}



function tfo_initialiseDetailImages(tfo_listeImagesDiapDetail){
	if(tfo_listeImagesDiapDetail!=undefined)
	{
		var imageCollection = tfo_listeImagesDiapDetail;
		var nb = imageCollection.length;
		for (var i = 0; i < nb; i++) {
		var domImg=document.getElementById('tfoDiapDetail'+(i+1));
		domImg.src=imageCollection[i];
		}
	}
}


function tfoGoAncreListe(idT)
{
	
	var myDiv=document.getElementById('tfoBloc'+idT);
	if (tfoNavigateur=="Internet Explorer")
	{
		/* ie + pbv le scroll ancre ne marche pas */
		window.location.hash='tfoBloc'+idT;
		
		
		if(tfoVerNav>6)
		{
			window.scrollTo(0, getDivY(myDiv));
		}
		else
		{
			window.scrollTo(0, getDivY(myDiv));
		}
	}
	else
	{
		//window.location.hash='';
		window.location.hash='tfoBloc'+idT;
		window.scrollTo(0, getDivY(myDiv));
		//window.scrollTo(0, window.pageYOffset-80);
	}
}

function getDivY(element)
{
	var oT = element.offsetTop;
	while(element=element.offsetParent) {
	  oT += element.offsetTop;
	}
	
	return oT;
}

function tfo_goZoomItineraireCarteListeDetail(tfo_jsonPoint)
{
	var lieu=tfo_jsonPoint.com+', '+tfo_jsonPoint.dep;
	tfo_ouvrirGoogleMapDetailItineraire (lieu, tfo_jsonPoint.lat, tfo_jsonPoint.lon);
	//scrollPageElt.scrollBy(100,100);
}

//pop carto html utilisé par les cartes Google 
function tfo_ecritPopCartoItineraireCarteListeZoom(tfo_jsonPoint)
{
	html2 = '';
	html2 = '<div style="">';
	html2 += '<form action="" onsubmit="return tfo_ouvreItineraireGMap();" method="get" name="gmapFormItineraire">';
	html2 += '<span>'+tfo_jsonPoint.tit+'</span>';
	html2 += '<br/><span>'+tfo_trads['JsYAllerDepuis']+'</span><input type="text" name="saddr" id="saddr" />';
	html2 += '<input type="hidden" name="daddr" id="daddr" value="'+tfo_jsonPoint.tit+' @'+tfo_jsonPoint.lat+','+tfo_jsonPoint.lon+'" />';
	html2 += '<br/><input type="submit" value="'+tfo_trads['JsVoirIti']+'" />';
	html2 += '</form>';
	html2 += '</div>';
	return html2;
}

//pop carto html utilisé par les cartes Google 
function tfo_ecritSyntheseHtml(tfo_jsonPoint)
{
	html2 = '';
	html2 = '<div class="tfoGmapSynthHtml" style="height:120px; overflow:hidden; width:260px">';
	
	if(tfo_jsonPoint.pIl){
	html2 += '<p style="float:left; width:78px;">';
	html2 += '<img src="'+tfo_jsonPoint.pIl+'" width="70" style="margin-right:8px; margin-top:2px;"></p>';
	}
	html2 += '<p style="float:right; width:170px; font-size:11px; text-align:left">';
	html2 += '<strong>'+tfo_jsonPoint.tit+ ' -' +tfo_jsonPoint.com+' : </strong>';
	html2 += tfo_jsonPoint.sum;
	html2 += ' <a href="#tfoBloc'+tfo_jsonPoint.idT+'" onclick="tfoGoAncreListe(\''+tfo_jsonPoint.idT+'\'); return false;">'+tfo_trads['JsVoirDessous']+'</a>';
	html2 += '</p>';
	return html2;
}




function tfo_ouvreItineraireGMap(){
	var frm = document.forms['gmapFormItineraire'];
	
	if(frm.elements['daddr'].value!=''){
		// pour la liste des parametres, voir http://mapki.com/wiki/Google_Map_Parameters
		var url = "http://maps.google.com/maps?";
		url += "f=d"; //f=d Displays the "directions" form (two input boxes: from, to). 
		url +="&saddr=" + frm.elements['saddr'].value; // Source address
		url +="&daddr=" + frm.elements['daddr'].value; // Destination address(es)
		url +="&sll=47.15984,2.988281"; // Latitude,longitude of the point from which the business search should be performed
		url +="&sspn=10.610077,20.566406"; // Stands for "Screen span". Map bounds dimensions in Degrees,
		//url +="&ie=UTF8"; // Can be used to specify the input character encoding set. e.g. ie=UTF8. 
		url +="&z=8"; // Sets the zoom level. (Range from 1-20) 
		//url +="&pw=2"; // Activates print mode and initiates printing. There seems to be a problem at the moment with pw=1, but using settings like pw=2 is OK.
		url +="&om=1"; // The presence of this parameter with a value other than 1 causes the overview map to be closed. If the parameter is omitted, or present with the value 1, then the overview map is open. 
	
		//alert(url);
		window.open (url, 'caravelis-itineraire', config='height=600, width=800, toolbar=no, menubar=yes, scrollbars=auto, resizable=yes, location=no, directories=no, status=no')
	}
	return false;	
}



function tfo_ouvrirGoogleMapDetailItineraire (arrivee,lat,long){
	var lieuTemp;
	if(lat==undefined || long==undefined){lieuTemp=arrivee;}
	else{lieuTemp=arrivee+'@'+lat+','+long;}
	winMentions=window.open('http://maps.google.fr/maps?f=d&source=s_d&saddr=&daddr='+lieuTemp+'&hl=fr&z=8','google','height=692,width=730,toolbar=0,menubar=no,scrollbars=yes,Status=yes,resizable=yes'); //si pb &hl=fr&ie=UTF8
	winMentions.opener = self; 
	winMentions.focus();
	
}


/**
* 
* @param id L'id du div conteneur
*/

var timerDivSuitScroll=[];

function tfo_lanceDivSuitScroll(nomEltDiv, margeBas)
{
	myDiv=document.getElementById(nomEltDiv);
	timerDivSuitScroll[nomEltDiv] = [];
	timerDivSuitScroll[nomEltDiv]['posYHautPage'] = getDivY(myDiv); //pos y relative du div par rapport à son conteneur
	timerDivSuitScroll[nomEltDiv]['positionMargeBasMini'] = margeBas;
	timerDivSuitScroll[nomEltDiv]['hauteurDiv'] = myDiv.offsetHeight;
	timerDivSuitScroll[nomEltDiv]['monEtat'] = 'inconnu'; //fixe ou normalHaut, normalBas, inconnu
	//timerDivSuitScroll[nomEltDiv]['timerEnCours'] = setInterval("tfo_DivSuitScroll('"+nomEltDiv+"')", 30);
}

function tfo_arreteDivSuitScroll(nomEltDiv)
{
		clearInterval(timerDivSuitScroll[nomEltDiv]['timerEnCours']);
		timerDivSuitScroll[nomEltDiv]['timerEnCours'] = 0;
}

/**
* 
* @param id L'id du div conteneur
*/

function tfo_DivSuitScroll(nomEltDiv)
{
	//alert(nomEltDiv);
	myDiv=document.getElementById(nomEltDiv);
	//alert(timerDivSuitScroll[nomEltDiv]['posYHautPage']);
	var posYHautPage = timerDivSuitScroll[nomEltDiv]['posYHautPage'];
	var positionMargeBasMini = timerDivSuitScroll[nomEltDiv]['positionMargeBasMini'];
	var hauteurDiv = timerDivSuitScroll[nomEltDiv]['hauteurDiv'];
	
	
	if (document.location.href.indexOf("admin.")==-1 && timerDivSuitScroll[nomEltDiv]['timerEnCours']!=0)
	{
		
		var monEtatSouhaite='';
		
		if (document.body.scrollTop)
		{ // Pour nestcape
			var positionAscenseur = document.body.scrollTop;
		}
		else
		{ // Pour ie et firefox
			var positionAscenseur = document.documentElement.scrollTop;
		}
		
		window.status='bas';
		//determiner l'etat souhaitable
		if(positionAscenseur>posYHautPage) {monEtatSouhaite='fixe';}
		else {monEtatSouhaite='normalHaut';}
		if(monEtatSouhaite=='fixe' && (positionAscenseur>(document.body.scrollHeight -(positionMargeBasMini+hauteurDiv))))
		{monEtatSouhaite='normalBas';}
		
		//si l'état précédent différent de l'actul, fait les calculs
		if(monEtatSouhaite!=timerDivSuitScroll[nomEltDiv]['monEtat'])
		{
			timerDivSuitScroll[nomEltDiv]['monEtat']=monEtatSouhaite;
			//alert(document.documentElement.scrollTop);
			switch (monEtatSouhaite){
				case 'normalHaut' :
					myDiv.style.position = "relative";
					myDiv.style.marginTop = "0px";
					myDiv.style.clear = "none";
					myDiv.style.top = "auto";
					break; 
				
				case 'fixe' :
					if(versionNavigateur==6 && nav=="Mic")/*ie6 car pas de fixed pour ie6*/
					{
						if(document.documentElement.scrollTop  < (document.body.scrollHeight -positionMargeBasMini - hauteurDiv ))
						{
							myDiv.style.position = "absolute";
							myDiv.style.top = (document.documentElement.scrollTop - hauteurDiv) +"px";		
						}
					}
					else
					{
						myDiv.style.position = "fixed";
						myDiv.style.marginTop = "0px"; 
						myDiv.style.clear = "none";
						myDiv.style.top = "0px";
					}
					break; 
				
				case 'normalBas' :
					myDiv.style.position = "relative";
					myDiv.style.marginTop = (document.body.scrollHeight-positionMargeBasMini - hauteurDiv -posYHautPage) + "px";
					myDiv.style.clear = "none";
					myDiv.style.top = "0px";
				
			}
		}
	
	}
}

var positionGoogle;
var carteZoomFermee=true;
function ouvrirGoogleMap (){
	//clearInterval(tempsConsole);
	$('googleMap').setStyle({width: '660px'});
	$('googleMap').setStyle({height: '350px'});
	positionGoogle = $('googleMap').style.top;
	$('googleMap').setStyle({top: '0px'});
	$('tfoConteneurCartoGMapListeNormale').setStyle({display: 'none'});
	$('ouvrirGoogleZoom').setStyle({display: 'none'});
	window.location.href="#tfo_liste"; 
	$('tfoConteneurCartoGMapListeZoom').setStyle({visibility: 'visible'});
	new Effect.Opacity('tfoConteneurCartoGMapListeZoom', { from: 0.0, to: 1.0,duration: 1.0});
	$('fermerGoogle').setStyle({visibility: 'visible'});
	new Effect.Opacity('fermerGoogle', { from: 0.0, to: 1.0,duration: 1.0});
	document.getElementById('googleMap').style.zIndex=1010;
}

function fermerGoogleMap (){
	$('tfoConteneurCartoGMapListeZoom').setStyle({visibility: 'hidden'});
	$('fermerGoogle').setStyle({visibility: 'hidden'});
	document.getElementById('googleMap').style.zIndex=1000;
	$('googleMap').setStyle({width: '235px'});
	$('googleMap').setStyle({height: '235px'});
	//$('googleMap').setStyle({top: positionGoogle});
	$('tfoConteneurCartoGMapListeNormale').setStyle({display: 'block'});
	$('ouvrirGoogleZoom').setStyle({display: 'block'});
	$('listePrestataires').setStyle({display: 'block'});
}


function ouvrirFermerGoogleMapZoomSeul (){
	var divCartoZoom = document.getElementById('tfoConteneurCartoGMapListeZoom');
	var divCartoNormale = document.getElementById('tfoConteneurCartoGMapListeNormale');
	var divGoogleMap = document.getElementById('googleMap');
	
	if(carteZoomFermee)
	{
		window.location.href="#tfo_liste";
		timerDivSuitScroll['googleMap']['timerEnCours']=0; /* clearTimeout ne fonctionnant pas autre solution*/
		
		divCartoZoom.style.position='relative';
		divCartoZoom.style.left='0px';
		divGoogleMap.style.zIndex=10;
		divCartoNormale.style.display="none";
		carteZoomFermee=false;
		
		
		divGoogleMap.style.position = "absolute";
		divGoogleMap.style.marginTop = "30px";
		divGoogleMap.style.top = "auto";
		document.getElementById('listePrestataires').style.marginTop = "380px";
		
	}
	else
	{
		divCartoZoom.style.position='absolute';
		divCartoZoom.style.left='-2000px';
		divGoogleMap.style.zIndex=9;
		divCartoNormale.style.display="block";
		carteZoomFermee=true;

		
		
		divGoogleMap.style.position = "relative";
		divGoogleMap.style.marginTop = "0px";
		divGoogleMap.style.top = "auto";
		document.getElementById('listePrestataires').style.marginTop = "0px";
		
		timerDivSuitScroll['googleMap']['timerEnCours'] = 1;
	}
}

function determinerCouleurPictoGmap()
{
	tfo_const['gmapMarkerColor']='orange';  
}
