
var http = createRequestObject();
var urlHash = "";
var subCat = "";
var lastUrl = "";

$(document).ready(function()
{
	$('body').addClass('js');
	$('body').fadeIn(1000);
	$slideshow.init();
	//alert(document.location.search + " " + document.location.pathname);
	//alert(document.location.hash);
	//lastUrl = document.location.hash.replace('/#','').replace('/','');
	//alert(document.location.href.lastIndexOf('.php'));
	if(document.location.href.lastIndexOf('.php')<0)checkUrl();
	
	$('#header #he_logo a, #header #bonjour, #header #connecting, #header #he_menu a, #header #he_categories a, #header #panier, #header #he_navi a,  #footer a, #he_navi a').click(function(e)
	{
    	$('#loading').fadeIn(250);
		//alert(this.href);
		var hashLink = this.href.split('?');
		var temp = hashLink[0].split('.php');
		urlHash = temp[0].substring(temp[0].lastIndexOf('/')+1, temp[0].length);
		hashLink = hashLink[1].split('&');
		subCat = hashLink[0].replace('arg=','');
		//alert("HashLink : " + hashLink + "\nurlHash : "+urlHash + "\nsubCat : " + subCat);
		
		hashLink[0] = urlHash;
		hashLink[1] = subCat;
		
		//lastUrl = "#/"+urlHash+"/"+subCat;
		
		if(hashLink[1] == undefined)
		{
			subCat = "";
			hashLink[1] = "";
		}
		//alert("hashLink = " + hashLink[1]);
		sndReq(hashLink);
		return false;
		//window.location.hash = "/"+urlHash+"/"+subCat;
	});
	
	$('#deconnexion').click(function(e)
	{
		$('#loading').fadeIn(250);
		$.ajax({
			type: "POST",
			url: "./bin/php/disconnect.php",
			success: function(data) {
				//alert(data);
				if(data == 1)
				{
					window.location.hash = "/accueil/";
					document.location.reload();
				}
				else
				{
					alert("Erreur de deconnexion");
					window.location.hash = "/accueil/";
					document.location.reload();
				}
			}
		});
		return false;
	});
	
	setInterval("checkUrl()",250);
});

function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer")
    {
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else
    {
        ro = new XMLHttpRequest();
    }
    return ro;
}



function sndReq(ar) 
{
	//alert(ar[0] + " " + ar[1]);
	urlHash = ar[0];
	subCat = ar[1];
	var st = '?page='+ar[0];
	if(ar.length>1)
	{
		st+= '&arg='+ ar[1];
	}
	//alert("st = " + st);
    http.open('get',ar[0]+'.php'+st, true);
    http.onreadystatechange = handleResponse;
    http.send(null);
}

function handleResponse() 
{
	//$('#footer').animate({'top':'0px'},10);
	window.location.hash = "/"+urlHash+"/"+subCat;
	lastUrl = window.location.hash;
    if(http.readyState == 4)
    {
		//alert(urlHash + " " + subCat);
    	//on affiche le loading.
    	$('#loading').fadeIn(250);
        var response = http.responseText;
        //alert(response);
        var update = new Array();
        if(response.indexOf('|') != -1) 
        {
            update = response.split('|');
            elementId = update[0];
            content = update[1];
            
            //on anime le conteneur.
            $('#in_container').animate({'opacity':'0', 'height':'toggle'}, 500, function()
            {
            	//alert(elementId);
            	//le contenu est remplac�.
                document.getElementById('in_container').innerHTML = content;
                //alert("urlHash = " + urlHash);
                if(urlHash == "metier")
                {
                	//disableSelection(document.getElementById("me_infos"));
                	init_bt_metier();
                	//alert("initiated");
                }
                
                if(urlHash == "panier")
                {
                	subCat = "panier";
                	init_bt_panier();
                }
                
                if(urlHash == "compte")
                {
                	init_bt_compte();
                }
                
                //si on est sur la page d'accueil, on reboot le slideshow.
                
                if(urlHash == "accueil" && subCat == "")
                {
                	init_bt_accueil();
                	//on cache la barre de navigation.
                	$('#he_navi').fadeOut(100);
                	
                	$('div.slides > ul', $slideshow.context).cycle(
    				{
    					fx: $slideshow.fx,
    				    timeout: $slideshow.timeout,
    				    speed: $slideshow.slideSpeed,
    				    fastOnEvent: $slideshow.tabSpeed,
    				    pager: $('ul.slides-nav', $slideshow.context),
    				    pagerAnchorBuilder: $slideshow.prepareTabs,
    				    before: $slideshow.activateTab,
    				    pauseOnPagerHover: true,
    				    height:'278px',
    				    pause: true,
    				    next:$('#ac_diapo #ac_bt_gauche'),
    				    prev:$('#ac_diapo #ac_bt_droite')
    				});
                	//on relance le slideshow.
                	$slideshow.init();
                }
                else if(urlHash == "accueil" && subCat == "contact")
                {
                	init_bt_contact();
                	document.getElementById("he_navi").innerHTML = "<a href='#/accueil/'>Accueil</a>&nbsp;&nbsp;<span>></span>&nbsp;&nbsp;"+f_upper(subCat);
                	
                	$('#he_navi').fadeIn(250);
                	$('#he_navi a').click(function(e)
                	{
						var hashLink = this.href.split('?');
						
						hashLink[0] = "accueil";
						hashLink[1] = "";
						
                		sndReq(hashLink);
                		return false;
                	});
                }
                else if(urlHash == "accueil" && subCat == "plan_du_site")
                {
                	init_bt_plan();
                	document.getElementById("he_navi").innerHTML = "<a href='#/accueil/'>Accueil</a>&nbsp;&nbsp;<span>></span>&nbsp;&nbsp;"+f_upper(subCat);
                	$('#he_navi').fadeIn(250);
                	$('#he_navi a').click(function(e)
                	{
						var hashLink = this.href.split('?');
						
						hashLink[0] = "accueil";
						hashLink[1] = "";
						
                		sndReq(hashLink);
                		return false;
                	});
                }
                else
                {
                	//on modifie la barre de navigation.
                	
                	//nom_du_metier = nom_du_metier.substring(3, nom_du_metier.length - 4);
                	
//                	if(urlHash == "metier")
//                	{
//                		var nom_du_metier = document.getElementById("me_titre").innerHTML;
//                		document.getElementById("he_navi").innerHTML = "<a href='#/accueil'>Accueil</a>&nbsp;&nbsp;<span>></span>&nbsp;&nbsp;"+nom_du_metier;
//                	}
                	document.getElementById("he_navi").innerHTML = "<a href='#/accueil/'>Accueil</a>&nbsp;&nbsp;<span>></span>&nbsp;&nbsp;"+f_upper(subCat);
                	
                	//on affiche la barre de navigation.
                	$('#he_navi').fadeIn(250);
                	$('#he_navi a').click(function(e)
                	{
                		//alert(this.href);
						var hashLink = this.href.split('?');
						/*
						var temp = hashLink[0].split('.php');
						urlHash = temp[0].substring(temp[0].lastIndexOf('/')+1, temp[0].length);
						hashLink = hashLink[1].split('&');
						subCat = hashLink[0].replace('arg=','');
						//alert("HashLink : " + hashLink + "\nurlHash : "+urlHash + "\nsubCat : " + subCat);
*/
						
						hashLink[0] = "accueil";
						hashLink[1] = "";
						
						//lastUrl = "#/"+urlHash+"/"+subCat;
						
						/*
						if(hashLink[1] == undefined)
						{
							subCat = "";
							hashLink[1] = "";
						}
*/
                		sndReq(hashLink);
                		return false;
                	});
                }
                
                //on cache le loading.
                $('#loading').fadeOut('slow');
                //on réaffiche le conteneur.
                $('#footer').animate({'top':'0px'},250);
                $('#in_container').animate({'opacity':'1', 'height':'toggle'},500, function()
                {
                	var footerHeight = document.getElementById('footer').offsetHeight;
                	var contentHeight = document.getElementById('in_container').offsetHeight + document.getElementById('header').offsetHeight;
                	var windowHeight = getWindowHeight();
                	//alert("footer:" + footerHeight+"\ncontent:" + contentHeight+"\nwindow:"+windowHeight);
                	if(footerHeight + contentHeight <= windowHeight && urlHash!='accueil')
                	{
                		//alert(windowHeight + " " + document.getElementById('in_container').offsetHeight);
                		//alert("REDIMENSION");
                        $('#footer').animate({'top':windowHeight-(footerHeight+contentHeight+35)},500);
                	}
                	else
                	{
                		//alert(windowHeight + " " + document.getElementById('in_container').offsetHeight);
                		//alert("ALL GOOD");
                		$('#footer').animate({'top':'0px'},0);
                	}
                });
            });
            
        }
    }
}

function getWindowHeight()
{
    var y = 0;
    if (self.innerHeight)
    {
    	y = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight)
    {
    	y = document.documentElement.clientHeight;
    }
    else if (document.body)
    {
    	y = document.body.clientHeight;
    }
    return y;
}

function checkUrl()
{
if(document.location.href.lastIndexOf('.php')<0)
{
	//alert(document.location.href.lastIndexOf('.php'));
	var hash = window.location.hash;
	
	var requ = window.location.hash.replace('#/','').split('/');
	
	//alert("requ = "+requ);
	
	urlHash = requ[0];
	subCat = requ[1];
	if(subCat == undefined)
	{
		subCat = "";
	}
	if(urlHash == "paiement")
	{
		lastUrl = hash;
	}
    if(urlHash == undefined || urlHash == "")
    {
    	urlHash = "accueil";
    	requ[0] = "accueil";
    	requ[1] = "";
    	hash = "accueil";
    }
	//alert("urlHash = "+urlHash);
	
	if(hash!=lastUrl)
	{
    	$('#loading').fadeIn(250);
		//alert("Actual URL = " + hash + "\nLast URL = " + lastUrl + "\nEqual ? " + (hash==lastUrl));
		lastUrl = hash;
		//window.location.hash = "/"+urlHash+"/"+subCat;
		sndReq(requ);
	}
	}
}

function f_upper(texte) 
{
	//var temp = texte.split('_');
	var t = new Array();
	for(j=0 ; j < texte.length ;j++) 
	{
		//if(t[j] == "&#95;") t[j] = "$nbsp;";
		if(j == 0) t[j] = texte.substr(j,1).toUpperCase();
		else t[j] = texte.substr(j,1).toLowerCase();
		if(t[j]=='_')t[j]='&nbsp;';
	}
	return t.join('');
}

function f_underscore(texte)
{
}

