var MaxEnregistre = 5;
var TailleMaxCookies = 1500;
var AncreUrlBack = "";
var preselection = "";
var criteresCookie = "";
var sortcriterionID = "";
var sortdirection = "";
var curPage = "";

function GestionHistory(){
	var sUrl = (document.location.href);
	var UrlBack = ""; 

	if( (document.location.href).indexOf("#ListeNum", 0) > -1){
		UrlBack = sUrl.substring(sUrl.indexOf("#", 0) + 1 , sUrl.length);
	}

	if(AncreUrlBack != UrlBack){
		AncreUrlBack = UrlBack;
		showHistory();
	}
	
	SE.ObjTimerBack = setTimeout(GestionHistory, 1000);
}
			
function showHistory(){

	var sUrlBack = (document.location.href);
	var NumListePrecedent = 0;
	var ValeurCookies = "";
	preselection = "";
	criteresCookie = "";
	sortcriterionID = "";
	sortdirection = "";
	curPage = "";

	SE.ValideCookies = false;

	if( (document.location.href).indexOf("#ListeNum", 0) > -1){
	
		NumListePrecedent = parseInt(sUrlBack.substring(sUrlBack.indexOf("#ListeNum", 0) + 9 , sUrlBack.length));
	
		//var ValeurCookies = getCookieListeProduit(AncreUrlBack + "_" + SE.Results.preselectionIDstartUp + "_" +  SE.Results.search);
		ValeurCookies = getCookieListeProduit(AncreUrlBack);
		if(ValeurCookies != "")
			InitVariablesPage(ValeurCookies);
	}
	
//alert("\nValeurCookies= " + ValeurCookies + "\nAncreUrlBack= " + AncreUrlBack + "\npreselection=" + preselection + "\ncriteresCookie=" + criteresCookie + "\nsortcriterionID=" + sortcriterionID + "\nsortdirection=" + sortdirection + "\ncurPage=" + curPage);

	SE.Results.ListeNum = NumListePrecedent;
	
	if(ValeurCookies != "")
		SE.NbListeProduit = GetNbCookiesListeProduit() - (getDerCookieListeProduit() - NumListePrecedent);
	else
		SE.NbListeProduit = 0;
	
	if(preselection != ""){
		SE.Results.preselectionID = preselection;
	}
	else{
		SE.Results.preselectionID = SE.Results.preselectionIDstartUp;
	}
	
	if(sortcriterionID != ""){
		SE.Results.sortcriterionID = sortcriterionID;
	}
	else{
		SE.Results.sortcriterionID = "TRI_PAGE_LOAD";
	}
		
	if(sortdirection != ""){
		SE.Results.sortdirection = sortdirection;
	}
	else{
		SE.Results.sortdirection = "direct";
	}
	
	SE.Results.preSelectionIDSynchrone(SE.Results.preselectionID);
	
	if(criteresCookie != ""){
		var cri = JSON.parse(criteresCookie);
		
		for(var i=0; i<cri.length; i++){
			SE.Zone.addCriterion(cri[i].id, cri[i].selectedIndex);
		}
		
		SE.Zone.drawSynchrone(false);
	}
	

	if((curPage != "") && (curPage != "0")){
	
		SE.Results.drawPage(parseInt(curPage), true);
	}

	SE.ValideCookies = true;
}


/*Methode permettant de rechercher les donnees d'un cookie memoire*/
function getCookieListeProduit(Nom)
{
	var NomCookie = Nom + "=";
	var Result = null;
	var i = 0;

	if (document.cookie.length > 0)
	{ 
		var table = document.cookie.split(/;/);
		var valeur = "";
		for (i = (table.length - 1); i >= 0 ; i--)
		{ 
			if(table[i].indexOf(NomCookie)!= -1)
			{ 
				var sCookies = table[i].substring(Number(table[i].indexOf(NomCookie) + NomCookie.length), table[i].length);

				if(sCookies.indexOf("}")!= -1)
					sCookies = sCookies.substring(0, sCookies.indexOf("}"));
          
				return unescape(sCookies);
			}
		}
	}
	
	return ""; 
}

//initialisation variables de la page pour affichage 
function InitVariablesPage(Valeur){
	var ExpreReg = new RegExp("(;;)", "g");  //recherche valeur vide Pour IE

	if (Valeur.length > 0)
	{ 
		
		Valeur = Valeur.replace(ExpreReg, "; ;"); //remplacement valeur vide par espace pour bonne prise en compte par IE
		Valeur = Valeur.replace(ExpreReg, "; ;"); //idem pour la prise en compte de 2 valeurs vides cote a cote ";;;"

		var table = Valeur.split(/;/);

		if(table.length > 4){
			preselection = table[0];
			criteresCookie = table[1];
			sortcriterionID = table[2];
			sortdirection = table[3];
			curPage = table[4];

			if(preselection == " ")    // Pour IE
				preselection = "";

			if(criteresCookie == " ")
				criteresCookie = "";
			
			if(sortcriterionID == " ")
				sortcriterionID = "";
			
			if(sortdirection == " ")
				sortdirection = "";
			
			if(curPage == " ")
				curPage = "";

		}
	}
}

//fonction création cookie
function setCookieListeProduit(Nom, Valeur)
{
	var date = new Date(1999,0,1);

	if(Valeur == "")
		document.cookie = Nom + "=" + ";expires=" + date.toGMTString() + "; path=/";
	else
		document.cookie = Nom + "=" + escape(Valeur) + "; path=/";
}


function UpdateCookiesListeProduit()
{
	var DebutNomCookie = "ListeNum";
	var bValideUpdate = true;
	var sNomCookies = "";
	var sSaveNomCookies = "";
	var ValeurCookies = "";
	var i = 0;
	
	if (document.cookie.length > 0)
	{
		var table = document.cookie.split(/;/);

		for (i = 0; i < table.length; i++)
		{ 
			if(table[i].indexOf(DebutNomCookie)!= -1)
			{ 
				sNomCookies = table[i].substring(table[i].indexOf(DebutNomCookie), table[i].indexOf("="));
				sSaveNomCookies = sNomCookies;
				
				if(bValideUpdate){ // premier cookie pas de modif infos sera écrasée
					bValideUpdate = false;
				}
				else{ //décalage information cookies
				
					ValeurCookies = getCookieListeProduit(sNomCookies);
					sNomCookies = "ListeNum" + (parseInt(sNomCookies.substring( 8 , sNomCookies.length))  -1);
					setCookieListeProduit(sNomCookies, ValeurCookies);
					
					if(i == (table.length - 1))
						setCookieListeProduit(sSaveNomCookies, "");
					
				}
 			}
		}
	}	
}

function getDerCookieListeProduit()
{
	var DebutNomCookie = "ListeNum";
	var sNomCookies = "";
	var sNumCookies = 0;

	var i = 0;

	if (document.cookie.length > 0)
	{ 
		var table = document.cookie.split(/;/);
		var valeur = "";
		
		for (i = (table.length - 1); i >= 0 ; i--)
		{ 
			if(table[i].indexOf(DebutNomCookie)!= -1)
			{ 
				sNomCookies = table[i].substring(table[i].indexOf(DebutNomCookie), table[i].indexOf("="));
				
				sNumCookies = parseInt(sNomCookies.substring( 8 , sNomCookies.length));
          
				return sNumCookies;
			}
		}
	}
	
	return sNumCookies; 
}

function GetNbCookiesListeProduit()
{
	var DebutNomCookie = "ListeNum";
	var iNbCookies = 0;
	var i = 0;
	
	if (document.cookie.length > 0)
	{
		var table = document.cookie.split(/;/);

		for (i = 0; i < table.length; i++)
		{ 
			if(table[i].indexOf(DebutNomCookie)!= -1)
			{ 
				iNbCookies =  iNbCookies + 1;
 			}
		}
	}	
	
	return iNbCookies;
}

function DeleteCookiesListeProduit()
{
	var DebutNomCookie = "ListeNum";
	var sNomCookies = "";
	var ValeurCookies = "";
	var i = 0;
	
	if (document.cookie.length > 0)
	{
		var table = document.cookie.split(/;/);

		for (i = 0; i < table.length; i++)
		{ 
			if(table[i].indexOf(DebutNomCookie)!= -1)
			{ 
				sNomCookies = table[i].substring(table[i].indexOf(DebutNomCookie), table[i].indexOf("="));
				setCookieListeProduit(sNomCookies, ValeurCookies);
 			}
		}
	}	
}


/** The JSON class is copyright 2005 JSON.org. */
Array.prototype.______array = '______array';

var JSON = {
    org: 'http://www.JSON.org',
    copyright: '(c)2005 JSON.org',
    license: 'http://www.crockford.com/JSON/license.html',

    stringify: function (arg) {
        var c, i, l, s = '', v;

        switch (typeof arg) {
        case 'object':
            if (arg) {
                if (arg.______array == '______array') {
                    for (i = 0; i < arg.length; ++i) {
                        v = this.stringify(arg[i]);
                        if (s) {
                            s += ',';
                        }
                        s += v;
                    }
                    return '[' + s + ']';
                } else if (typeof arg.toString != 'undefined') {
                    for (i in arg) {
                        v = arg[i];
                        if (typeof v != 'undefined' && typeof v != 'function') {
                            v = this.stringify(v);
                            if (s) {
                                s += ',';
                            }
                            s += this.stringify(i) + ':' + v;
                        }
                    }
                    return '{' + s + '}';
                }
            }
            return 'null';
        case 'number':
            return isFinite(arg) ? String(arg) : 'null';
        case 'string':
            l = arg.length;
            s = '"';
            for (i = 0; i < l; i += 1) {
                c = arg.charAt(i);
                if (c >= ' ') {
                    if (c == '\\' || c == '"') {
                        s += '\\';
                    }
                    s += c;
                } else {
                    switch (c) {
                        case '\b':
                            s += '\\b';
                            break;
                        case '\f':
                            s += '\\f';
                            break;
                        case '\n':
                            s += '\\n';
                            break;
                        case '\r':
                            s += '\\r';
                            break;
                        case '\t':
                            s += '\\t';
                            break;
                        default:
                            c = c.charCodeAt();
                            s += '\\u00' + Math.floor(c / 16).toString(16) +
                                (c % 16).toString(16);
                    }
                }
            }
            return s + '"';
        case 'boolean':
            return String(arg);
        default:
            return 'null';
        }
    },
    parse: function (text) {
        var at = 0;
        var ch = ' ';

        function error(m) {
            throw {
                name: 'JSONError',
                message: m,
                at: at - 1,
                text: text
            };
        }

        function next() {
            ch = text.charAt(at);
            at += 1;
            return ch;
        }

        function white() {
            while (ch != '' && ch <= ' ') {
                next();
            }
        }

        function str() {
            var i, s = '', t, u;

            if (ch == '"') {
outer:          while (next()) {
                    if (ch == '"') {
                        next();
                        return s;
                    } else if (ch == '\\') {
                        switch (next()) {
                        case 'b':
                            s += '\b';
                            break;
                        case 'f':
                            s += '\f';
                            break;
                        case 'n':
                            s += '\n';
                            break;
                        case 'r':
                            s += '\r';
                            break;
                        case 't':
                            s += '\t';
                            break;
                        case 'u':
                            u = 0;
                            for (i = 0; i < 4; i += 1) {
                                t = parseInt(next(), 16);
                                if (!isFinite(t)) {
                                    break outer;
                                }
                                u = u * 16 + t;
                            }
                            s += String.fromCharCode(u);
                            break;
                        default:
                            s += ch;
                        }
                    } else {
                        s += ch;
                    }
                }
            }
            error("Bad string");
        }

        function arr() {
            var a = [];

            if (ch == '[') {
                next();
                white();
                if (ch == ']') {
                    next();
                    return a;
                }
                while (ch) {
                    a.push(val());
                    white();
                    if (ch == ']') {
                        next();
                        return a;
                    } else if (ch != ',') {
                        break;
                    }
                    next();
                    white();
                }
            }
            error("Bad array");
        }

        function obj() {
            var k, o = {};

            if (ch == '{') {
                next();
                white();
                if (ch == '}') {
                    next();
                    return o;
                }
                while (ch) {
                    k = str();
                    white();
                    if (ch != ':') {
                        break;
                    }
                    next();
                    o[k] = val();
                    white();
                    if (ch == '}') {
                        next();
                        return o;
                    } else if (ch != ',') {
                        break;
                    }
                    next();
                    white();
                }
            }
            error("Bad object");
        }

        function num() {
            var n = '', v;
            if (ch == '-') {
                n = '-';
                next();
            }
            while (ch >= '0' && ch <= '9') {
                n += ch;
                next();
            }
            if (ch == '.') {
                n += '.';
                while (next() && ch >= '0' && ch <= '9') {
                    n += ch;
                }
            }
            if (ch == 'e' || ch == 'E') {
                n += 'e';
                next();
                if (ch == '-' || ch == '+') {
                    n += ch;
                    next();
                }
                while (ch >= '0' && ch <= '9') {
                    n += ch;
                    next();
                }
            }
            v = +n;
            if (!isFinite(v)) {
                error("Bad number");
            } else {
                return v;
            }
        }

        function word() {
            switch (ch) {
                case 't':
                    if (next() == 'r' && next() == 'u' && next() == 'e') {
                        next();
                        return true;
                    }
                    break;
                case 'f':
                    if (next() == 'a' && next() == 'l' && next() == 's' &&
                            next() == 'e') {
                        next();
                        return false;
                    }
                    break;
                case 'n':
                    if (next() == 'u' && next() == 'l' && next() == 'l') {
                        next();
                        return null;
                    }
                    break;
            }
            error("Syntax error");
        }

        function val() {
            white();
            switch (ch) {
                case '{':
                    return obj();
                case '[':
                    return arr();
                case '"':
                    return str();
                case '-':
                    return num();
                default:
                    return ch >= '0' && ch <= '9' ? num() : word();
            }
        }

        return val();
    }
};



