/**
 * Constructor of the CookieHandler class. Serves methods for writing frequency and unique cookie values
 * in cookie with given name, domain and running-out-date. 
 * 
 * @param expires Expire date of the cookie
 * @param removeExpiresDate Expire Date to remove a cookie. Should be in past.
 * @param domain The domain of the cookie. e.q. 'eventim.de'
 * @param maxKeyValuesCount The max count of the keys, which will be saved into the cookie
 * @param cookieName The name of the cookie
 */
function CookieHandler(expires, removeExpiresDate, domain, maxKeyValuesCount, cookieName)
{
	this.expires = expires;
	this.removeExpiresData = removeExpiresDate;
	this.domain = domain;
	this.maxKeyValuesCount = maxKeyValuesCount;
	this.cookieName = cookieName;
}



/**
 * Stellt sicher, das nur eine bestimmte Anzahl ( this.maxKeyValuesCount ) 
 * an Werten in dem Cookie gespeichert werden. Der neue Wert wird nur in das Cookie gespeichert, wenn er noch nicht vorhanden ist.
 * Speichert das Cookie mit der neuen Kategorie an vordester Stelle
 * Tritt ein Fehler auf wird das Cookie geloescht. Fehler werden gecatched
 * 
 * @param cookie_name Name des Cookies
 * @param currentKeyValue Zu speichendes Json Objekt
 */
CookieHandler.prototype.updateUniqueObjectsCookie = function (currentKeyObj)
{
	try
	{
		var cookieValues = this.getCookie(this.cookieName);
		
		if (!(cookieValues == null || cookieValues == ""))
		{
			var values = cookieValues.split(';');
			if (currentKeyObj != null && currentKeyObj != "")
			{
				if (!this.contains(values,currentKeyObj, true))
				{
					values.unshift(JSON.stringify(currentKeyObj))
				}

				if (values.length > this.maxKeyValuesCount)
				{
					values.pop();
				}
				
				if (values != null && values != "")
				{
					values = values.join(';');
				}	
			}
		} 
		else 
		{ 
			var values = JSON.stringify(currentKeyObj);
		}
		
		//write cookie
		if (values != null && values != "")
		{ 	
			this.setCookie(this.cookieName, values, this.domain);
		}
		
	} catch (e)
	{
		try
		{
		this.removeCookie(this.cookieName, this.domain);
		}
		catch (e)
		{
			//do nothing	
		}	
	}
}

/**
 * Stellt sicher, das nur eine bestimmte Anzahl ( this.maxKeyValuesCount ) 
 * an Werten in dem Cookie gespeichert werden
 * Speichert das Cookie mit der neuen Kategorie an vordester Stelle
 * Tritt ein Fehler auf wird das Cookie geloescht. Fehler werden gecatched
 * 
 * @param cookie_name Name des Cookies
 * @param currentKeyValue Externer Schluessel (momentan mit kuenstlerId auf getgo eingebunden)
 */
CookieHandler.prototype.updateFrequenzyCookie = function (currentKeyValue)
{ 
	 try
	 {
		var cookieValuesString = this.getCookie(this.cookieName);
	
		if (!(cookieValuesString == null || cookieValuesString == ""))
		{
			var values = cookieValuesString.split(',');
			if (currentKeyValue != null && currentKeyValue != "")
			{
				values.unshift(currentKeyValue)
			}
			
			if (values.length > this.maxKeyValuesCount)
			{
				values.pop();
			}
			
			if (values != null && values != "")
			{
				values = values.join();
			}	
		} 
		else 
		{ 
			var values = currentKeyValue;
		}
		
		//write cookie
		if (values != null && values != "")
		{ 	
			this.setCookie(this.cookieName, values, this.domain);
		}
	
	}
	 catch (e)
	{ 
		try
		{
			this.removeCookie(this.cookieName, this.domain);
		}
		catch (e)
		{
			//do nothing	
		}	
	}
		
}
/**
 * 
 * @param {String} name Name des Cookies
 * @param {String} domain Domain fuer die das Cookie gueltig ist
 * @param {Date} date Ablaufdatum des Cookies
 * @param {String} value Wert des Cookies
 */
CookieHandler.prototype.writeCookie = function (name, domain, date, value)
{
	var cookieString = name + "=" + escape(value);
	cookieString += "; expires=" + date.toGMTString();
	if (domain)
	{
		cookieString += "; domain=" + escape(domain);
	}
	cookieString += "; path=/"
	
	document.cookie = cookieString;
}

/**
 * Setzt das Cookie
 * @param {String} name Name des Cookies
 * @param {String} value Wert des Cookies
 * @param {String} domain Domain fuer die das Cookie gueltig ist
 */
CookieHandler.prototype.setCookie = function  ( name, value, domain )
{  
	this.writeCookie(name, domain, this.expires, value);
}

/**
 * Setzt das Cookie
 * @param {String} name Name des Cookies
 * @param {String} value Wert des Cookies
 * @param {String} domain Domain fuer die das Cookie gueltig ist
 */
CookieHandler.prototype.removeCookie = function  ( name, domain )
{
	this.writeCookie(name, domain, this.removeExpiresDate);
}

/**
 * Liest das aktuelle Cookie aus
 * @param {String} cookieName Name des Cookies
 */
CookieHandler.prototype.getCookie = function  ( cookieName )
{
  var results = document.cookie.match ( '(^|;) ?' + cookieName + '=([^;]*)(;|$)' );

  if (results)
  {
  	return (unescape(results[2]));
  }
  else
  {
  	return null;
  }	
}

CookieHandler.prototype.contains = function (values, key, isObj)
{
	if (isObj != null && isObj == true) //we should check the "id" attribute in the objects
	{
		 for (i = 0; i< values.length; i++) {
			 obj = JSON.parse(values[i]);
			 if (obj.id == key.id) return true;
		  }
		   return false;	
	}
	else
	{
		 for (i = 0; i< values.length; i++) {
		       if (values[i] == key) return true;
		 }
		   return false;	
	}
	
}

