CookieHandler = {

	//function to set a cookie that last for a year
	setCookie: function(name, value){
		var expdate=new Date();
		expdate.setDate(expdate.getDate() + 365);
		document.cookie = name +"="+ value+"; expires="+expdate.toUTCString()+";"
	},
	
	//remove the cookie with name
	removeCookie: function(name){
		var expdate=new Date();
		expdate.setDate(expdate.getDate() - 365);
		document.cookie = name +"=byebye; expires="+expdate.toUTCString()+";"
	},
	
	//sets the player cookie, if the cookie already exists we add one hour on it.
	setPlayerCookie: function(playerid, playlistid){
		var tmpString = 'amosgtv'+playerid+""+playlistid+'=';
		if(document.cookie.indexOf(tmpString) != -1){
			var amosCookie = document.cookie.split(';');
			var amosPlayTimeValue = 0;

			for(var i = 0; i < amosCookie.length; i++){
				if(document.cookie.indexOf(tmpString) != -1){
					var amosCookie_start = 0;
					amosCookie_start = amosCookie[i].indexOf(tmpString) + tmpString.length;
					amosPlayTimeValue = parseInt(amosCookie[i].substring(amosCookie_start,amosCookie[i].length).split('--')[1]);
				}
			}
			this.setCookie(tmpString, "?playTimeLeft--" + (amosPlayTimeValue + (60*60)));
		} else {
			this.setCookie(tmpString, "?playTimeLeft--" + (60*60));
		}
	},
	
	//retrieve the value of the cookie, player cookie return the playtime left. amosgtv cookie that is used to identify that a purchase has been made returns a token
	getCookieValue: function(name){
	
		var amosCookie = document.cookie.split(';');
		var cookieName = name+'=';
		var cookieReturn = null;
		for(var i = 0; i < amosCookie.length; i++){
			if(amosCookie[i].indexOf(cookieName) != -1){
				var amosCookie_start = 0;
				amosCookie_start = amosCookie[i].indexOf(cookieName) + cookieName.length;
				cookieReturn = parseInt(amosCookie[i].substring(amosCookie_start,amosCookie[i].length).split('--')[1]);
			}
		}
		return cookieReturn;
	},
	getCookie: function(name){
	
		var amosCookie = document.cookie.split(';');
		var cookieName = name+'=';
		var cookieReturn = null;
		for(var i = 0; i < amosCookie.length; i++){
			if(amosCookie[i].indexOf(cookieName) != -1){
				var amosCookie_start = 0;
				amosCookie_start = amosCookie[i].indexOf(cookieName) + cookieName.length;
				cookieReturn = amosCookie[i].substring(amosCookie_start,amosCookie[i].length);
			}
		}
		return cookieReturn;
	}

};
