SporteoCookiesFactory = Class.create();
SporteoCookiesFactory = {
	_listeCookies: null,
	get: function(domain_name) {
		//return null;
		if (this._listeCookies == null) {
			this._listeCookies = new Array();
		}
		domain_name_key = domain_name;
		if (domain_name == null || domain_name == "localhost") {
			domain_name_key = "no_domain";
		}
		if (this._listeCookies[domain_name_key] == null) {
			this._listeCookies[domain_name_key] = new SporteoCookies(domain_name);
		}
		return this._listeCookies[domain_name_key];
	}
}
SporteoCookies = Class.create();
SporteoCookies.prototype = {
	domain_name: null,
	liste_cookies: null,
	liste_cookie_var: null,
	nb_cookie: 0,
	TAILLE_MAX_COOKIE: cookies_max_length,
	initialize: function(domain_name) {
		this.domain_name = domain_name;
		this.liste_cookies = new Array();
		this.liste_cookie_var = new Array();
		this._init();
	},
	_init: function() {
		//Pour chaque cookie participant au cookie global, on recupere sa valeur
		cookie_name = this._getCookieName().replace(/^\s+|\s+$/g,"");
		liste_valeur = new Array();
		if (document.cookie.length > 0) {
			cookies = document.cookie.split(/;/);
			for(i=0; i<cookies.length; i++) {
				cookie_data = cookies[i].split(/=/);
				//On supprime les espaces vides
				cookie_data[0] = cookie_data[0].replace(/^\s+|\s+$/g,'');
				if (cookie_data[1] != null) {
					cookie_data[1] = cookie_data[1].replace(/^\s+|\s+$/g,'');
				}
				if (cookie_data[0] == cookie_name) {
					liste_valeur[0] = cookie_data[1];
				} else {
					if (cookie_data[0].indexOf(cookie_name) != -1) {
						sc_name = cookie_data[0].split(/_/);
						liste_valeur[parseInt(sc_name[sc_name.length-1])] = cookie_data[1];
					}
				}
			}
		}
		cookie_val = ''; this.nb_cookie = 0;
		for(var i=0; i<liste_valeur.length; i++) {
			cookie_val += liste_valeur[i];
			this.nb_cookie++;
		}
		if (cookie_val != null && cookie_val.length > 0) {
      		lc = cookie_val.split(/__TS__/);
      		for(i=0; i<lc.length; i++) {
				c = lc[i]; tab_temp = c.split(/__EQ__/);
				if (tab_temp.length != 2) break;
				data = tab_temp[1].split(/__ET__/);
				if (data.length != 2) break;
				//On decode la valeur 2 fois car encodé par PHP et lorsque c'est récupéré de javascript
				//il y a aussi l'encodage naturel
				data[0] = unescape(unescape(data[0]));
				//On remet les ; qui sont la valeur du cookie
				while(data[0].indexOf('__pv__') != -1) {
					data[0] = data[0].replace(/__pv__/,';');
					break;
				}
      			this.liste_cookies[tab_temp[0]] = new SporteoCookiesData(tab_temp[0], data[0], data[1]);
      			this.liste_cookie_var.push(tab_temp[0]);
      		}
  		}
	},
	setCookie: function(cookie_name, valeur, time_expired) {
		if (valeur != null) {
			this.liste_cookies[cookie_name] = new SporteoCookiesData(cookie_name, valeur, time_expired);
		} else {
			this.liste_cookies[cookie_name] = null;
		}
		if (this.liste_cookie_var.indexOf(cookie_name) < 0) {
			this.liste_cookie_var.push(cookie_name);
		}
		this._enregistrer();
	},
	_enregistrer: function() {
		cookie_val = this._cookiesToStringValue();
		i_cookie = 0;
		for(var i = 0; i < this.nb_cookie; i++) {
			if (cookie_val != null && cookie_val.length > 0) {
				i_cookie++;
			} else {
				break; //On a atteint la fin
			}
			cookie_val = this._decouperEtDefinirCookie(i, cookie_val);
		}
		while(cookie_val != null) {
			cookie_val = this._decouperEtDefinirCookie(i_cookie, cookie_val);
			i_cookie++;
		}
	},
	_decouperEtDefinirCookie: function(i_cookie, cookie_val) {
		if (i_cookie == 0) {
			c_name = this._getCookieName();
		} else {
			c_name = this._getCookieName() + "_" + i_cookie;
		}
		if (cookie_val != null && cookie_val.length > this.TAILLE_MAX_COOKIE) {
			this._setCookie(c_name, cookie_val.substr(0, this.TAILLE_MAX_COOKIE));
			return cookie_val.substr(this.TAILLE_MAX_COOKIE, cookie_val.length - this.TAILLE_MAX_COOKIE);
		} else {
			if (cookie_val != null && cookie_val.length > 0) {
				this._setCookie(c_name, cookie_val);
				return null;
			} else {
				this._setCookie(c_name, null);
			}
		}
		return null;
	},
	_setCookie: function(cookie_name, cookie_val) {
		date_expire = new Date();
		date_expire.setTime(date_expire.getTime() + (3600 * 24 * 30 * 1000));
		while(cookie_val.indexOf(';') != -1) {
			cookie_val = cookie_val.replace(/;/,'__pv__');
		}
		if (this.domain_name == 'localhost') {
			new_val = cookie_name + '=' + cookie_val + ';path=/;expires='+date_expire.toUTCString();
		} else {
			new_val = cookie_name + '=' + cookie_val + ';path=/;expires='+date_expire.toUTCString()+';domain=.'+this.domain_name;
		}
		document.cookie = new_val;
	},
	getValeurCookie: function(cookie_name) {
		if (this.liste_cookies[cookie_name] == null) {
			return null;
		} else {
			return this.liste_cookies[cookie_name].getValeur();
		}
	},
	_cookiesToStringValue: function() {
		s_tab = new Array();
		for(i=0; i<this.liste_cookie_var.length; i++) {
			if (this.liste_cookies[this.liste_cookie_var[i]] != null) {
				s_tab.push(this.liste_cookies[this.liste_cookie_var[i]].getString());
			}
		}
		return s_tab.join('__TS__');
	},
	_getCookieName: function() {
		if (this.domain_name == null || this.domain_name.length <= 0) {
			return "sporteo_cookies";
		} else {
			domain_transform = this.domain_name.replace(/\-/, '_');
			domain_transform = domain_transform.replace(/\./, '_');
			return 'sp_' + domain_transform;
		}
	}
}
SporteoCookiesData = Class.create();
SporteoCookiesData.prototype = {
	name: null,
	val: null,
	time_expired: null,
	initialize: function(name, val, time_expired) {
		this.name = name;
		this.val = val;
		this.time_expired = time_expired;
		if (this.time_expired == null) {
			date_courante = new Date();
			date_expire = new Date(Math.round(date_courante.getTime()) + (3600 * 24 * 7 * 1000));
			this.time_expired = Math.round(date_expire.getTime()/1000);
		}
	},
	getString: function() {
		return (this.name + '__EQ__' + (escape(this.val)) + '__ET__' + this.time_expired);
	},
	getValeur: function() {
		return this.val;
	}
}
