/**
    AJAX procedures based from
	AnCPMap_ - Ajax Framework
	Version 1.4
	www.anCPMap_.eu
	(c) 2007 Sandrine Takis and Denis Sureau.
	Free under the Mozilla 1.1 Licence.

	By C+
*/

//<![CDATA[
var gb_Caching = false; // les url chargees n'utilisent pas le cache

var CPMap_map; 			// objet MAP Google

bCPMap_Loading = new Array() ;
gs_CPMapToLoad = "" ;

var gao_CPMapMarker = new Array() ;
var gas_CPMapMarkerInfo = new Array() ;
var gas_CPMapTrackKeypage = new Array() ;

var gi_CPMapIdxMarker = 1 ;

// references au id des DIV
var gs_CPMapDetail ;
var gs_CPMapMap ;
var gs_CPMapList ;
var gs_CPMapSearch ;
var gs_langue ;
var gb_CPMapnew  = true;	 	// true a l'initialisation de la map, false ensuite
var gb_CPMarkerNew  = true;		// true si pas encore de marker sur la carte
var gi_CPMappage = 1 ;			// page courante chargee de la liste

// coordonnees sur la carte du valais
var gf_long = 0;
var gf_lat = 0 ;

// Marker target
var go_targetMarker = "" ;

// tabulation du menu
var gs_tab = "" ;

function CPMap_TrackPage(sUrl)
{ 
	//alert(sUrl) 
	pageTracker._trackPageview(sUrl);
}

function CPMap_get_var_url(nomVariable)
{
	var infos = location.href.substring(location.href.indexOf("?")+1, location.href.length)+"&"
	if (infos.indexOf("#")!=-1)
		infos = infos.substring(0,infos.indexOf("#"))+"&"
		
	var variable=0
	{
		nomVariable = nomVariable + "="
		var taille = nomVariable.length
		if (infos.indexOf(nomVariable)!=-1)
			variable = infos.substring(infos.indexOf(nomVariable)+taille,infos.length).substring(0,infos.substring(infos.indexOf(nomVariable)+taille,infos.length).indexOf("&"))
	}
	return variable
}

function CPMap_condition()
{
	if (bCPMap_Loading[gs_CPMapToLoad]==true)
        return false ;

    return true ;
}


function CPMap_findPos(obj)
{
	var curleft = curtop = 0;
	if (obj.offsetParent)
    {
		curleft = obj.offsetLeft ;
		curtop = obj.offsetTop ;

        while (obj = obj.offsetParent)
        {
			curleft += obj.offsetLeft ;
			curtop += obj.offsetTop ;
		}
	}
	return [curleft,curtop];
}


function CPMap_sel_point(event)
{
    var src ;

    // compatiblité avec FireFox
    if(isNaN(event.x))
    {
        gf_long = event.layerX;
        gf_lat = event.layerY;
        src = event.target ;
    }
    else
    {
        gf_long = event.x;
        gf_lat = event.y;
        src = event.srcElement;
    }

    //var offset = CPMap_findPos(src) ;

    var div = document.getElementById('CPMap_point');
    if (div)
    {
        div.style.left = gf_long;
        div.style.top = gf_lat;
        div.style.display = '';
    }
    // -3 : decalage empirique.. hum..
    //gf_long = gf_long - offset[0] - 3 ;
    //gf_lat = gf_lat - offset[1] - 3;

    gf_long = gf_long  ;
    gf_lat = gf_lat ;

}


function CPMap_Create()
{
    var request = false;
        try {
            request = new ActiveXObject('Msxml2.XMLHTTP');
        }
        catch (err2) {
            try {
                request = new ActiveXObject('Microsoft.XMLHTTP');
            }
            catch (err3) {
		try {
			request = new XMLHttpRequest();

		}
		catch (err1)
		{
			request = false;
		}
		}
	}
    return request;
}


function CPMap_GetBody(content)
{
	var x = content.indexOf("<body");
	if(x == -1) return "";

	x = content.indexOf(">", x);
	if(x == -1) return "";

	var y = content.lastIndexOf("</body>");
	if(y == -1) return "";

	return content.slice(x + 1, y);
}

function CPMap_PutHTML(content, target)
{
	target.innerHTML = CPMap_GetBody(content);
}

/**
	Loads a HTML page
	Put the content of the body tag into the current page.
	Arguments:
		url of the other HTML page to load
		id of the tag that has to hold the content
*/


function CPMap_LoadScript(url, script_name)
{
    // cherche ou cree l'element Script demande
    //alert(script_name + " " + gs_CPMapToLoad) ;

  	bCPMap_Loading[script_name] = true;

  	//alert("bCPMap_Loading : "+  script_name + " " + bCPMap_Loading[script_name] + " "+ url + " __" + bCPMap_Loading[script_name] + " __" ) ;

    var oScript = document.getElementById(script_name) ;
    if (oScript)
        oScript.text = "" ;

    var xhr = CPMap_Create();
    
    xhr.onreadystatechange=function()
	{
	   	if(xhr.readyState == 4)
			if(xhr.status == 200)
		    {
		    	//alert(xhr.responseText ) ;
		    	 var headID = document.getElementsByTagName("head")[0];
		    	 
		    	 if (!oScript)
                 {
   					oScript = document.createElement('script');
   					
                    oScript.type = 'text/javascript';
                    oScript.id = script_name ;

                    if(navigator.userAgent.indexOf("KHTML") != -1) 
                    {
						oScript.innerHTML = xhr.responseText ;
			        }
					else
						oScript.text = xhr.responseText ;

                    headID.appendChild(oScript);
                    //alert(xhr.responseText) ;
                 }
                 else
                 {
                 	if(navigator.userAgent.indexOf("MSIE") != -1) 
                    	oScript.text = xhr.responseText ;
                    else
                    {
   						var oScript2 = document.createElement('script');
	                    oScript2.type = 'text/javascript';
	                    oScript2.id = script_name ;
	                    
						if (navigator.userAgent.indexOf("KHTML"))
	                    	oScript2.innerHTML = xhr.responseText ;
	                    else
	                    	oScript2.text = xhr.responseText ;
	                    	
	                    headID.replaceChild(oScript2, oScript);
                    }
                 
                 }

                //alert("bCPMap_Loading : "+  script_name + " " + bCPMap_Loading[script_name]) ;
				bCPMap_Loading[script_name]  = false ;
				
            }
	}

	if(gb_Caching == false)
	{
        // si c'est le premier parametre, mettre "?", sinon "&"
        var start_param ;
        var x = url.indexOf("?");
        if(x == -1)
            start_param = "?" ;
        else
            start_param = "&" ;
	    // rajoute un param pour forcer l'interpretation de l'URL

	}
	xhr.open("GET", url , true);
	xhr.send("");

}



function CPMap_LoadHtml(url, sStorage)
{

	var xhr = CPMap_Create();

	CPMap_setCursor("body", "wait") ;
	CPMap_setCursor("zoneSearchSubmit", "wait") ;
	

	bCPMap_Loading[sStorage]  = true ;

	xhr.onreadystatechange=function()
	{
		//alert( "readyState " + xhr.readyState ) ;
	   	if(xhr.readyState == 4)
		{
			//alert( "status " + xhr.status + " - " + xhr.responseText ) ;
	   		if(xhr.status == 200)
			{
				if (url.indexOf("detail")>=0)
					CPMap_TrackPage(url) ;
					
				oStorage = document.getElementById(sStorage);
			    oStorage.innerHTML = CPMap_GetBody(xhr.responseText);
			    bCPMap_Loading[sStorage] = false ;
				CPMap_setCursor("body", "auto") ;
				CPMap_setCursor("zoneSearchSubmit", "pointer") ;
			    //alert(xhr.responseText) ;
   			}
        }
	}

	if(gb_Caching == false)
	{
        // si c'est le premier parametre, mettre "?", sinon "&"
        var start_param ;
        //alert ("URL " + url) ;
        var x = url.indexOf("?");
        if(x == -1)
            start_param = "?" ;
        else
            start_param = "&" ;
	    // rajoute un param pour forcer l'interpretation de l'URL

	}
	
	
	//alert ("http://www.restaurant-valais.ch/"+url) ;
	//xhr.open("GET", "http://www.restaurant-valais.ch/"+url , true);
	
	xhr.open("GET", url , true);
	//xhr.open("GET", document.location.hostname + "/" + url , true);
	
	xhr.send("");

}

/**
	Send a HEAD request,
	call a function with the value
*/

function CPMap_Head(url, key, fun, element)
{
	var xhr = CPMap_Create();

	xhr.onreadystatechange = function()
	{
		if(xhr.readyState == 4)
		{
			var value;
			if (xhr.status == 200)
			{
				 value = xhr.getResponseHeader(key);
			}
			else
			{
				if(xhr.status==404)
					value = url + " doesn't exist!";
				else
					value = "Error, status is " + xhr.status;
			}
			fun(value, element);
		}
	}
	xhr.open("HEAD", url, true);
	xhr.send(null);
}

function CPMapList(psCPMapList)
{
	    gs_CPMapList = psCPMapList ;
	    gs_CPMapToLoad = psCPMapList ;
	   // CPMap_LoadHtml(CPMapComposeURL("http://www.conchita-plus.ch/maplist.php"), psCPMapList);
       // CPMap_waitcomplete('CPMap_condition()', 'CPMap_mapListHide()', 100);
}

function CPMap_mapListHide()
{

//	if (gf_long==0)
//		CPMap_elementshow(gs_CPMapList, false) ;
}

function CPMap_elementshow(sElement, bDisplay)
{
    var oElement = document.getElementById(sElement);

    if (oElement)
    	if (bDisplay)
        	oElement.style.display = '' ;
        else
        	oElement.style.display = 'none' ;
}

function CPMap_setCursor(sElement, sAttribut)
{
    var oElement = document.getElementById(sElement);

    if (oElement)
       	oElement.style.cursor = sAttribut ;
}


function CPMapDetail(psCPMapDetail)
{
    gs_CPMapDetail = psCPMapDetail ;
    //CPMap_elementshow(gs_CPMapDetail, false)
}

function CPMapSearch(psCPMapSearch)
{
    gs_CPMapSearch = psCPMapSearch ;
    CPMap_LoadHtml(CPMapComposeURL("mapsearch.php"), psCPMapSearch);
    }

function CPMapMap(psCPMapMap)
{
	gs_CPMapMap = psCPMapMap;

    var oCPMapMap = document.getElementById(gs_CPMapMap);

    if (oCPMapMap)
    {
   	    if (GBrowserIsCompatible())
   	        CPMap_map = new GMap2(document.getElementById(psCPMapMap));

   	    oCPMapMap.style.display = 'none' ;
   	    //CPMap_search_fct_submit() ;
    }
}

function CPMap_waitcomplete(fct_condition, fct_after, ms )
{
    setTimeout("CPMap_check_waitcomplete('" + fct_condition + "', '" + fct_after + "', " + ms+ ")", ms);
}

function CPMap_check_waitcomplete(fct_condition, fct_after, ms )
{
	if (!eval(fct_condition))
        setTimeout("CPMap_check_waitcomplete('" + fct_condition + "', '" + fct_after + "', " + ms+ ")", ms);
    else
    {
    	//alert("fct_after " + fct_after) ;
    	//CPMap_SetMarker() ;
    	//alert("Z") ;
    	
    	//alert(fct_after) ;
    	
        eval(fct_after) ;
    }
}

function CPMapComposeURL(url, critere, sCritere, page, iPage)
{
	var param = "" ;
	
 	var sName = document.getElementById("fn_dbsearch").value ; 
 	
   	if (sName != "")
   	{
   		param = "&name=" + encodeURI(sName);
   		//alert(param) ;
   	}
   		
   	 
	if (critere)
	{
		param = param + "&" + critere + "=" + sCritere;
	}

	if (page)
		param = param +  "&" + page + "=" + iPage;

	//alert(url + "?long=" + gf_long + "&lat=" + gf_lat +  param) ;
	
    return ( url + "?long=" + gf_long + "&lat=" + gf_lat +  param + "&" + gs_langue) ;
}

function CPMap_change_page_list(iValue)
{
	gi_CPMappage =  iValue ;
	CPMap_search_fct_submit() ;
}

function CPMap_detail_fct_submit(id)
{
    gs_CPMapToLoad = gs_CPMapDetail ;
    // alert("http://.../mapdetail.php?id=" + id);
	CPMap_LoadHtml(("mapdetail.php?id=" + id + "&" + gs_langue), gs_CPMapDetail);
	CPMap_waitcomplete('CPMap_condition()', 'CPMap_ShowDetail()', 100);
}

function CPMap_ShowDetail()
{
	CPMap_elementshow(gs_CPMapMap, false) ;
	CPMap_elementshow(gs_CPMapDetail, true) ;
	resto_change_tab("") ;
}


function CPMap_reloadmap()
{
	CPMap_elementshow(gs_CPMapDetail, false) ;
	CPMap_elementshow(gs_CPMapMap, true) ;

}

function CPMap_showVS()
{
	
	gf_long = 0 ; 
	gf_lat= 0 ; 
	
	CPMap_search_fct_submit() ;
	
}


function CPMap_search_fct_submit()
{

    var i ;
    var sCritere = "";
    var iPage ;

    //if (gf_long == 0)
    //	return ;
    
    openCheckCritere(0) ;

    iPage = gi_CPMappage ;
    gi_CPMappage = 1 ;

	for (i=0 ; i<document.getElementById('CPMap_search_form').elements["CPMap_critere_1[]"].length ; i++)
    {
        if(document.getElementById('CPMap_search_form').elements["CPMap_critere_1[]"][i].checked)
            sCritere += document.getElementById('CPMap_search_form').elements["CPMap_critere_1[]"][i].value + "_";
    }

    for (i=0 ; i<document.getElementById('CPMap_search_form').elements["CPMap_critere_2[]"].length ; i++)
    {
        if(document.getElementById('CPMap_search_form').elements["CPMap_critere_2[]"][i].checked)
            sCritere += document.getElementById('CPMap_search_form').elements["CPMap_critere_2[]"][i].value + "_";
    }

    for (i=0 ; i<document.getElementById('CPMap_search_form').elements["CPMap_critere_3[]"].length ; i++)
    {
        if(document.getElementById('CPMap_search_form').elements["CPMap_critere_3[]"][i].checked)
            sCritere += document.getElementById('CPMap_search_form').elements["CPMap_critere_3[]"][i].value + "_";
    }
    
    for (i=0 ; i<document.getElementById('CPMap_search_form').elements["CPMap_critere_4[]"].length ; i++)
    {
        if(document.getElementById('CPMap_search_form').elements["CPMap_critere_4[]"][i].checked)
            sCritere += document.getElementById('CPMap_search_form').elements["CPMap_critere_4[]"][i].value + "_";
    }

    CPMap_LoadHtml(CPMapComposeURL("maplist.php", "CPMap_critere", sCritere, "CPMap_page", iPage), gs_CPMapList);

    gs_CPMapToLoad = "CPMap_jsmapjs" ;
    //alert(CPMapComposeURL("mapjs.php", "CPMap_critere",  sCritere, "CPMap_page", iPage)) ;
	CPMap_LoadScript(CPMapComposeURL("mapjs.php", "CPMap_critere",  sCritere, "CPMap_page", iPage), "CPMap_jsmapjs");
    
	
	CPMap_waitcomplete('CPMap_condition()', 'CPMap_SetMarker()', 100);

	
    return true ;
}

function CPMap_list_submit(pi_x, pi_y)
{
	
	if (pi_x == 0)
    	return ;
    	
    gf_long = pi_y ;
    gf_lat = pi_x ;
    
    var sCritere = "" ;
    
    iPage = 1 ;
    gi_CPMappage = 1 ;
    

	CPMap_LoadHtml(CPMapComposeURL("maplist.php", "CPMap_critere", sCritere, "CPMap_page", iPage), gs_CPMapList);

    gs_CPMapToLoad = "CPMap_jsmapjs" ;
    CPMap_LoadScript(CPMapComposeURL("mapjs.php", "CPMap_critere",  sCritere, "CPMap_page", iPage), "CPMap_jsmapjs");
    
	CPMap_waitcomplete('CPMap_condition()', 'CPMap_SetMarker()', 100);
}

function resto_change_tab(tab)
{
	var tabs = new Array("resto_general", "resto_news", "resto_menu", "resto_salle") ;
	
	if (tab == "")
		tab = "resto_general" ;

	for(i=0 ; i<4 ; i++)
		document.getElementById(tabs[i]).style.display = 'none' ;

	document.getElementById(tab).style.display = '' ;

}

//]]>
