/* <pre>
+--------------------------------------------------------------------------+
| LGJ							|	JavaScript library: cookies.js		   |
+--------------------------------------------------------------------------+
| Author: Erik Fleischer												   |
+--------------------------------------------------------------------------+
</pre> */


function Cookie (_document, _name, _expHours, _path, _domain, _secure)
{
	this.$document = _document;
	this.$name = _name;
	if(_expHours)
	{
		this.$expiration = new Date( (new Date()).getTime() + (_expHours * 3600000) );
	}
	else
	{
		this.$expiration = null;
	}
	if(_path)
	{
		this.$path = _path;
	}
	else
	{
		this.$path = null;
	}
	if(_domain)
	{
		this.$domain = _domain;
	}
	else
	{
		this.$domain = null;
	}
	if(_secure)
	{
		this.$secure = true;
	}
	else
	{
		this.$secure = false;
	}
}


Cookie.prototype.store = function() {
	var _cookieVal = "";

	for(var _prop in this)
	{
		if(_prop.charAt(0) == '$' || (typeof this[_prop]) == "function")
		{
			continue;
		}
		if(_cookieVal != "")
		{
			_cookieVal += "&";
		}
		_cookieVal += _prop + ":" + escape(this[_prop]);
	}

	var _cookie = this.$name + "=" + _cookieVal;
	if(this.$expiration)
	{
		_cookie += "; expires=" + this.$expiration.toGMTString();
	}
	if(this.$path)
	{
		_cookie += "; path=" + this.$path;
	}
	if(this.$domain)
	{
		_cookie += "; domain=" + this.$domain;
	}
	if(this.$secure)
	{
		_cookie += "; secure";
	}

	this.$document.cookie = _cookie;
}


Cookie.prototype.load = function() {
	var _allCookies = this.$document.cookie;

	if(_allCookies == "")
	{
		return false;
	}

	var _start = _allCookies.indexOf(this.$name + "=");
	if(_start == -1)
	{
		return false;
	}
	_start += this.$name.length + 1;

	var _end = _allCookies.indexOf(";", _start);
	if(_end == -1)
	{
		_end = _allCookies.length;
	}

	var _cookieVal = _allCookies.substring(_start, _end);

	var _array = _cookieVal.split("&");
	for(var _i = 0; _i < _array.length; _i++)
	{
		_array[_i] = _array[_i].split(":");
	}
	for(var _i = 0; _i < _array.length; _i++)
	{
		this[_array[_i][0]] = unescape(_array[_i][1]);
	}

	return true;
}


Cookie.prototype.remove = function() {
	var _cookie = this.$name + "=";
	if(this.$path)
	{
		_cookie += "; path=" + this.$path;
	}
	if(this.$domain)
	{
		_cookie += "; domain=" + this.$domain;
	}
	_cookie += "; expires=Fri, 02-Jan-1970 00:00:00 GMT";
	this.$document.cookie = _cookie;
}

/*_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-*/

function getLastVisit ()
{
	var _lastVisit = new Cookie(document, "lastVisit", 8760, "/");
	if(!_lastVisit.load() || !_lastVisit["timestamp"] || !_lastVisit["path"])
	{
		_lastVisit["timestamp"] = 0;
		_lastVisit["path"] = "";
		_lastVisit["title"] = "";
		_lastVisit["count"] = 0;
		var _retVal = null;
	}
	else
	{
		var _retVal = {"timestamp":_lastVisit["timestamp"], "path":_lastVisit["path"], 
					   "title":_lastVisit["title"], "count":_lastVisit["count"]};
	}
	_lastVisit.store();
	return _retVal;
}


function trackVisit (_lastVisitCount)
{
	var _lastVisit = new Cookie(document, "lastVisit", 8760, "/");
	_lastVisit["timestamp"] = (new Date()).getTime();
	_lastVisit["path"] = window.location.pathname;
	var _title = "";
	if (document.title.indexOf("Lucas Gonzaga Jr - ") != -1)
	{
		_title = document.title.substring(19);
	}
	else
	{
		_title = document.title;
	}
	_lastVisit["title"] = _title;
	_lastVisit["count"] = (_lastVisitCount ? Number(_lastVisitCount) + 1 : 1);
	_lastVisit.store();
	return true;
}