// Cookie.js
// These functions are for getting/setting cookies. Adapted from JavaScript the Definitive Guide 5 Edition
// Parses all cookies from the web browser as the page loads
//
// To read a cookie just look for it as a property of the cookie object (remember that cookies are objects with name/value pairs)
//
//
// First check the name space...

var com;
if (!com || !com.jchristy) { throw new Error("com.jchristy: Required object 'com.jchristy.Support' does not exist."); }


// Name space is ready.

com.jchristy.Cookie = {};
com.jchristy.Cookie.make = function(ID) {
	if (ID == "make" || ID == "enabled") { throw new Error("Error: '" + ID + "' is a protected keyword in the cookie object."); }
	
	if (this[ID]) { return this[ID]; }
	
	this[ID] = { 
		$id: ID, 
		save: this._save,
		remove: this._remove
		};
	
	return this[ID];
}
com.jchristy.Cookie._parse = function() {
	var c = document.cookie;
	
	if (!c) { return; }
	
	c = c.split(";");
	
	for (var i = 0; i < c.length; i++) {
		var p = c[i].indexOf("=");
		
		if (p == -1) { continue; }
		
		var name = c[i].substring(0, p);
		var value = c[i].substring(p + 1);

		if (name == "make" || name == "enabled") { continue; }
		
		this[name] = { 
			$id: name, 
			save: this._save,
			remove: this._remove
		};
		
		var v = value.split("&");
		
		for (var z = 0; z < v.length; z++) {
			var o = v[z].indexOf(":");
			this[name][v[z].substring(0, o)] = decodeURIComponent(v[z].substring(o + 1));
			
		}
	}
	
}
com.jchristy.Cookie._save = function(LIFE, PATH, DOMAIN, SECURE) {
	//Remember this runs as a method of each cookie so 'this' does not refer to com.jchristy.Cookie
	var c = this; 
	var cvalue = "";
	for (var i in c) {
		if (i.charAt(0) != "$" && typeof c[i] != "function") {
			if (cvalue != "") { cvalue += "&"; }
			cvalue += i + ":" + encodeURIComponent(c[i]);
		}
	}
	var cookie = c.$id + "=" + cvalue;
	
	if (LIFE || LIFE == 0) {
		cookie += "; expires=" + (new Date((new Date()).valueOf() + (LIFE * 24 * 60 * 60 * 1000))).toUTCString();	
	}
	
	if (PATH) { cookie += "; path=" + PATH; }
	if (DOMAIN) { cookie += "; domain=" + DOMAIN; }
	if (SECURE) { cookie += "; secure"; }

	document.cookie = cookie;
}
com.jchristy.Cookie._remove = function(PATH, DOMAIN, SECURE) {
	//Remember this runs as a method of each cookie so 'this' does not refer to com.jchristy.Cookie
	var c = this;
	for (var i in c) {
		if (i.charAt(0) != "$" && typeof c[i] != "function") {
			delete c[i];
		}
	}
	c.save(0, PATH, DOMAIN, SECURE);
}
com.jchristy.Cookie.enabled = function() {
	if (com.jchristy.Cookie.enabled.isEnabled != undefined) { return com.jchristy.Cookie.enabled.isEnabled; }
	if (navigator.cookieEnabled) { return com.jchristy.Cookie.enabled.isEnabled = navigator.cookieEnabled; }
	
	document.cookie = "sniffer=true; expires=" + (new Date((new Date()).valueOf() + 2592000000)).toUTCString();
	
	var sniff = document.cookie;
	
	if (sniff.indexOf("sniffer=true") != -1) {
		document.cookie = "sniffer=true; expires=" + (new Date((new Date()).valueOf() - 2592000000)).toUTCString();
		return com.jchristy.Cookie.enabled.isEnabled = true;
	}
}


com.jchristy.Cookie._parse();
