/*
	Taken from JavaScript: The Definitive Guide
	Chapter 16: Scripting Cookies
	
	Modified to use inner functions instead of prototyping
*/
function Cookie( cname, hours, cpath, cdomain, csecure ) {
	var REMOVED_VALUE = -9999999999999;
	
	var _document = document;
	var _name = cname;
	var _expiration = null;
	var _path = null;
	var _domain = null;
	var _secure = false;
	
	var _properties = {};
	
	this.constructor = function( hours, cpath, cdomain, csecure ) {
		if( hours ) { _expiration = new Date( (new Date() ).getTime() + hours*3600000); }
		if( cpath ) { _path = cpath } 
		if( cdomain ) { _domain = cdomain; }
		if( csecure ) { _secure = true; }
	}
	
	this.constructor( hours, cpath, cdomain, csecure );
	
	
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	Public functions
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.appendValue = function _appendValue_( id, val, string ) {
		if( !val ) { return string; }
		if( string != "" ) { string += "; " }
		
		string += id + "=" + val;
		
		return string;
	}

	this.getProperty = function _getProperty_( id ) {
		return _properties[id];
	}
	
	this.getPropertiesLength = function _getPropertiesLength_() {
		var len = 0;
		for( props in _properties ) {
			len++;
		}
		
		return len;
	}
	
	this.load = function _load_() {
		// first get a list of all cookies that pertain to this document
		var allCookies = _document.cookie;
		if( allCookies == "" ) { return false; }
		
		var start = allCookies.indexOf( _name + "=" );
		
		if( start == -1 ) { return false; } // cookie not defined for this page
		start += _name.length + 1; // skip the name and equals sign
		
		var end = allCookies.indexOf( ";", start );
		if( end == -1 ) { end = allCookies.length; }
		
		var cookieVal = allCookies.substring( start, end );
		
		// now that we've extracted the value of the named cookie, we must break 
		// that value down into individual state variable names and values.  The 
		// name/value pairs are separated from eath other by amperands, and the
		// individual names and values are separated from each other by colons.  We 
		// use the split() method to parse everything.

		var nameValues = cookieVal.split( "&" );	// break into array of name/value pairs
		_properties = {} // reset the properties
		for( var i = 0; i < nameValues.length; i++ ) {
			var arr = nameValues[i].split( ":" );
			_properties[arr[0]] = unescape( arr[1] );
		}
		
		return true;
	}
	
	// removes this cookie ( sets the expiry date to be 1970 )
	this.removeCookie = function _removeCookie_() {
		var cookie = _name + "=";
		cookie = this.appendValue( "path", _path, cookie );
		cookie = this.appendValue( "domain", _domain, cookie );
		cookie = this.appendValue( "expires", "Fri, 02-Jan-1970 00:00:00 GMT", cookie );
		
		_document.cookie = cookie;
	}
	

	
	// removes a property from the properties array
	this.removeProperty = function _removeProperty_( id ) {
		_properties[id]= REMOVED_VALUE;
	}
	
	// sets a property within the properties array
	this.setProperty = function _setProperty_( id, value ) {
		_properties[id] = value;
	}
	
	this.store = function _store_() {
		// Go through all the properties of the object and put together the value of the cookie.  
		// We'll use colons and ampersands for the individual state variables we store within a 
		// single cookie value.  Note that we escape the value of each state variable,
		// in case it contains punctuation or other illegal characters
		var cookieVal = "";
		
		for( prop in _properties ) {
			// Ignore properties where the value = REMOVED_VALUE;
			if( _properties[prop] == REMOVED_VALUE ) { continue; }

			if( cookieVal != "" ) cookieVal += '&';
			
			cookieVal += prop + ":" + escape( _properties[prop] );
		}
		
		// now that we have the value of the cookie, put together the complete cookie string
		// which includes the name and the various attributes specified when the Cookie
		// object was created
		var cookie =  _name + "=" + cookieVal;
		cookie = this.appendValue( "expires", _expiration, cookie );
		cookie = this.appendValue( "path", _path, cookie );
		cookie = this.appendValue( "domain", _domain, cookie );
		cookie = this.appendValue( "secure", _secure, cookie );
		_document.cookie = cookie;
	}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Private functions
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	function appendValue( id, val, string ) {
		if( !val ) { return string; }
		if( string != "" ) { string += "; " }
		
		string += id + "=" + val;
		
		return string;
	}
	
	
}