/*
	Cookies v1.1.1.
	Written by All Web Promotion, Inc. 2008-2009.
*/

;(function($){

	$.cookie = function (name, value, opts) {

		// check cookies enabled
		if (!(name || value || opts)) {
			return enabled();
		}

		// default settings
		var defaults = {	'doEncode'	: true,
							'doDecode'	: true}

		// clone options
		var settings = $.extend({}, defaults, opts);

		// set or get
		if ((typeof name != 'undefined') && (typeof value != 'undefined')) {
			set(name, value, settings);
		} else if (typeof name != 'undefined') {
			return get(name);
		}

		// private functions

		function set (name, value, opts) {
			if (typeof value === 'undefined' || value === null) {
				if (typeof opts !== 'object' || opts === null) {
					opts = {};
				}
				value = '';
				opts.hours = -8760;
			}
			if (settings && settings.doEncode) {
				//value = encodeURIComponent(value);
				value = URLEncode(value);
			}
			document.cookie = name + '=' + value + _options(opts);
		}

		function get (name) {
			var cookies = _get();
			if (typeof name === 'string') {
				return (typeof cookies[name] !== 'undefined') ? cookies[name] : null;
			} else if (typeof name === 'object' && name !== null) {
				var val = [];
				for (var item in name) {
					val[name[item]] = (typeof cookies[name[item]] !== 'undefined') ? cookies[name[item]] : null;
				}
				return val;
			} else {
				return cookies;
			}
		}

		function del (name, opts) {
			if (typeof opts !== 'object' || opts === null) {
				opts = {};
			}
			set(name, null, opts);
		}

		function enabled () {
			var enabled = false, name = 'test', value = 'testdata';
			set(name, value);
			if (get(name) == value) {
				del(name);
				enabled = true;
			}
			return enabled;
		}

		function _get () {
			cookies = [];
			var namevalue, name;
			var parts = document.cookie.split(';');
			for (var i = 0; i < parts.length; i++) {
				namevalue = parts[i].split('=');
				name = namevalue[0].replace(/^\s*/, '').replace(/\s*$/, '');
				value = namevalue[1];
				if (settings && settings.doDecode) {
					//value = decodeURIComponent(value);
					value = URLDecode(value);
				}
				cookies[name] = value;
			}
			return cookies;
		}

		function _options (optsIn) {
			var opts	= typeof optsIn !== 'object' || optsIn === null				? []			: optsIn; 
			var hours	= typeof opts.hours === 'number' && opts.hours > 0			? opts.hours	: null;
			var path 	= typeof opts.path === 'string' && opts.path != ''			? opts.path		: '/';
			var domain	= typeof opts.domain === 'string' && opts.domain != ''		? opts.domain	: null;
			var secure	= typeof opts.secure === 'boolean' && opts.secure			? opts.secure	: false;
			return			(typeof hours == 'number' ? '; expires=' + (_expires(hours)) : '')
						+	'; path=' + path
						+	(typeof domain === 'string' ? '; domain=' + domain : '')
						+	(secure === true ? '; secure' : '');
		}

		function _expires (hours) {
			var dt = new Date();
			dt.setTime(dt.getTime() + (hours*60*60*1000));
			return dt.toGMTString();
		}

		function URLEncode (clearString) {
		  var output = '';
		  var x = 0;
		  clearString = clearString.toString();
		  var regex = /(^[a-zA-Z0-9_.]*)/;
		  while (x < clearString.length) {
			var match = regex.exec(clearString.substr(x));
			if (match != null && match.length > 1 && match[1] != '') {
				output += match[1];
			  x += match[1].length;
			} else {
			  if (clearString[x] == ' ')
				output += '+';
			  else {
				var charCode = clearString.charCodeAt(x);
				var hexVal = charCode.toString(16);
				output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
			  }
			  x++;
			}
		  }
		  return output;
		}
		
		function URLDecode (encodedString) {
		  var output = encodedString;
		  var binVal, thisString;
		  var myregexp = /(%[^%]{2})/;
		  while ((match = myregexp.exec(output)) != null
					 && match.length > 1
					 && match[1] != '') {
			binVal = parseInt(match[1].substr(1),16);
			thisString = String.fromCharCode(binVal);
			output = output.replace(match[1], thisString);
		  }
		  return output;
		}

	};

})(jQuery);
