/* #############################################################################

NS: TESCODIRECT.lib.cookies
	static: yes

NS: TESCODIRECT.lib.cookies.cookie
	static: no

dependancies:
	TESCODIRECT

############################################################################# */

TESCODIRECT.lib.cookies = new function() {
	this.VERSION = "1.0.1";
	this.NAME = "TESCODIRECT.lib.cookies";

	this.cookie = function(name, value, path, domain, days) {
		var _self = this;

		var _value = value;
		var _values;
		var _hasValues;
		
		this.name = name;
		this.path = path ? path : "/";
		this.domain = domain;
		this.days = days;
		
		if(!this.domain) {
			var domainParts = document.location.hostname.split(".");
			this.domain = "";
			for(var i = domainParts.length - 2; i < domainParts.length; i++) {
				this.domain += "." + domainParts[i];
			}
		}

		this.getValue = function() {
			return unescape(_value);
		}
		
		this.setValue = function(value) {
			_hasValues = false;

			_value = value;
		}
	
		this.get = function(name) {
			if(!_values) {
				_values = [];
				var o = _value.split("&"), v;
				for(var i = 0; i < o.length; i++) {
					v = o[i].split("=");
					_values[v[0]] = unescape(v[1]);
				}
			}
			
			if(!name) {
				return _values;
			} else if(_values[name]) {
				return unescape(_values[name]);
			} else {
				return "";
			}
		}

		this.set = function(name, val) {
			_hasValues = true;
			
			if(!_values) {
				this.get();
			}
			
			if(name) {
				_values[name] = escape(val);
				_value = "";
				_values.enumerate(
					function(name, value) {
						_value += (name + "=" + escape(value) + "&");
					}, true
				);
				_value = _value.substr(0, _value.length - 1);
			}
		}
		
		function calculateExpiry() {
			if (_self.days) {
				var date = new Date()
				date.setTime(date.getTime() + (_self.days * 24 * 60 * 60 * 1000));
				return date.toGMTString();
			} else {
				return null;
			}
		}
		
		this.getExpiry = function() {
			var s = calculateExpiry();
			return (s ? "expires=" + s + ";" : "");
		}

		this.getPath = function() {
			return (this.path ? "path=" + this.path + ";" : "");
		}

		this.getDomain = function() {
			return (this.domain ? "domain=" + this.domain + ";" : "");
		}
	}

	this.get = function(name) {
		var cookies = document.cookie.split(';');
		var cookie, cookieName;
		for(var i = 0; i < cookies.length; i++) {
			cookie = cookies[i].split("=");
			cookieName = cookie[0].trim();
			if(cookieName == name) {
				cookie.splice(0, 1);
				return new this.cookie(cookieName, cookie.join("="));
			}
		}
		
		return new this.cookie(name, "");
	}
	
	this.set = function(cookie) {
		document.cookie = cookie.name + "=" + cookie.getValue() + ";" + cookie.getExpiry() + cookie.getPath() + cookie.getDomain();
	}

	this.remove = function(cookie) {
		cookie.days = -1;
		cookie.setValue("");
		this.set(cookie);
	}
}

