// create com_hp_hope namespace if it does not yet exist:
if (typeof (com_hp_hope) == 'undefined')
{ 
	com_hp_hope = {};
};

// common cookie handler for cookie that can store multiple key/value pairs:
com_hp_hope.CookieHandler = function (name, days, domain)
{
	var cookiename = name + '=';
	var expires = '';
	var cookiedomain = '';
	var path = '; path=/';	
	
	if (typeof (days) != 'undefined')
	{ 
		var date = new Date();
		date.setTime(date.getTime() + days*24*60*60*1000);
		expires = '; expires=' + date.toGMTString();
	}
	
	if (typeof (domain) != 'undefined')
	{ 
		cookiedomain = '; domain=' + domain;
	}
	
	// set cookie:
	this.set = function (value) 
	{
		document.cookie = cookiename + escape(value) + expires + path + cookiedomain;
	};
 
	// get cookie:
	this.get = function () 
	{
		var carray = document.cookie.split(';');
 
		for(var i=0;i < carray.length;i++)
		{
			var c = carray[i];
			while (c.charAt(0)==' ')
			{
				c = c.substring(1,c.length);
			}
			if (c.indexOf(cookiename) == 0)
			{
				return unescape(c.substring(cookiename.length,c.length));
				break;
			}
		}
		return null;
	};
 
	// delete cookie:
	this.del = function () 
	{
		document.cookie = cookiename + '' + '; expires=' + new Date(1000).toGMTString() + path + cookiedomain;
	};
	
	// load entire content from cookie:
	this.load = function ()
	{
		var str = this.get();
		var tmp = [];
		if (str != null)
		{
			tmp = str.split('**');
		}
		var content = {};
		for (var i=0; i<tmp.length; i+=2)
		{
			content[tmp[i]] = tmp[i+1];
		}
		return content;
	};
	
	// save entire content back to cookie
	this.save = function (content)
	{
		var tmp = [];
		for (var i in content)
		{
			tmp.push(i);
			tmp.push(content[i]);
		}
		this.set(tmp.join('**'));
	};
	
	// read value for a specific key:
	this.read = function (key)
	{
		var content = this.load();
		if (content[key] != null)
		{
			return content[key];
		}
		else
		{
			return null;
		}
	};

	// update value for a specific key:
	this.write = function (key, value)
	{
		var content = this.load();
		content[key] = value;
		this.save(content);
	};
	
	// remove specific key/value pair:
	this.remove = function (key)
	{
		var content = this.load();
		delete content[key];
		this.save(content);
	};
};

// hope cookie specific cookie handler:
com_hp_hope.hopeCookieHandler = function () {
	
	// new visit timeframe (= 30 min):
	this.visit = 30*60*1000;
	
	// method to identify new visits. If a user is inactive for more than 30 min then a new visit is identified.
	this.isNewVisit = function ()
	{
		var s = this.read('s');
		if (s != null)
		{
			if ((new Date().getTime() - s) < this.visit)
			{
				return false;
			}
		}
		return true;
	};
	
	// method to update all cookie values at once which need to be updated on each cookie access:
	this.update = function (key, value)
	{
		// read entire content from the cookie:
		var content = this.load();
		
		// set A/B testing group:
		if (content['g'] == null)
		{
			if (typeof (com_hp_hope_g) != 'undefined')
			{
				content['g'] = com_hp_hope_g;
			}
			else
			{
				content['g'] = (Math.floor(Math.random()*2) == 1) ? 'A' : 'B';
			}
		}
		
		// if group is set to X delete all other values and only keep the group:
		if (key == 'g' && value == 'X')
		{
			content = {};
			content[key] = value;
		}
		
		// set cookie version:
		content['v'] = 1;				
		
		// only update data if group is not 'X':
		if (content['g'] != 'X')
		{
			// increment the return visit counter:
			content['r'] = function (r, cookie)
			{
				if (r == null)
				{
					return 1;
				}
				if (cookie.isNewVisit())
				{
					r++;
				}
				return r;
			}(content['r'], this);		
			
			// set cookie access timestamp:
			content['s'] = new Date().getTime();
			
			// set screen size:
			content['x'] = screen.width + 'x' + screen.height;
			
			// update the value for the specified key
			content[key] = value;
		}

		// write whole cotent back to the cookie:
		this.save(content);
	};
	
	// method to get the number of days which have passed since Jan 1st 2010:
	this.dateStamp = function ()
	{
		var reference = new Date(2010,0,1,0,0,0).getTime();
		var timestamp = new Date().getTime();
		return Math.floor((timestamp - reference)/(1000*60*60*24));
	}();
};

// create new hope cookie handler based on the common cookie handler:
com_hp_hope.hopeCookieHandler.prototype = new com_hp_hope.CookieHandler('com_hp_hope', 365, '.hp.com');
//ALEKS
//com_hp_hope.hopeCookieHandler.prototype = new com_hp_hope.CookieHandler('com_hp_hope', 365);

// create new hope cookie object:
com_hp_hope.hopeCookie = new com_hp_hope.hopeCookieHandler();
