// Get and Set Cookies
var Cookies = Class.create({
	initialize: function(){},
	
	/* Set Cookie
	 * **************************************************
	 * @param string		key		(mandatory)
	 * @param string		valor		(mandatory)
	 * @param integer		ttl		(in hours - optional)
	 * @param string		path		(optional)
	 * @param string		domain	(optional)
	 * @param boolean		secure	(optional)
	 */
	Set: function(key, valor, ttl, path, domain, secure){
		if (key.length < 1) return false;
		
		var cookies = [ key + '=' + escape(valor) ];
		
		if (path.length < 1) cookies.push('path=/');
		else cookies.push('path=' + path);
		
		if (domain.length < 1) cookies.push('domain=.mfti.com.br');
		else cookies.push('domain=' + domain);
		
		if (parseInt(ttl) == 'NaN') cookies.push(Cookies.hoursExpire(24));
		else cookies.push('expires=' + this.hoursExpire(ttl));
		
		if (secure) cookies.push('secure');
		
		document.cookie = cookies.join('; ');
		
		return true;
	},
	
	/* Return TTL in GTM format
	 * ***************************************
	 * @param integer		ttl
	 */
	hoursExpire: function(ttl){
		if (parseInt(ttl) == 'NaN') ttl = 24;
		
		now = new Date();
		now.setTime(now.getTime() + (parseInt(ttl) * 60 * 60 * 1000));
		
		return now.toGMTString();
	},
	
	/* Unset cookie
	 * ***************************************
	 * @param string	key
	 * @param string	path
	 * @param string	domain
	 */
	Zera: function(key, path, domain){
		if (key.length < 1) return false;
		if (path.length < 1) path = '/';
		if (domain.length < 1) domain = '.mfti.com.br';
		
		if (this.Get(key)) this.Set(key, '', 'Thu, 01-Jan-70 00:00:01 GMT', path, domain);

		return true;
	},
	
	/* Get the Cookie Value
	 * ****************************************
	 * @param string	key
	 */
	Get: function(key){
		var response = '';
		
		if (key.length < 1) return false;
		
		if (document.cookie.length > 0){
			var c_start = document.cookie.indexOf(key + "=");
			if (c_start > -1){
				c_start = c_start + (key.length + 1);
				
				var c_end = document.cookie.indexOf(";", c_start);
				
				if (c_end == -1) c_end = document.cookie.length;
				
				response = unescape(document.cookie.substring(c_start, c_end));
			}
		}
		
		return response;
	}
});
