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

;(function($){

	$.cookie = function (name, value, opts) {
		
		//var w = opts ? opts.path + ', ' + opts.domain : 'no options';
		//alert(w);

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

		// clone options
		var settings = $.extend({}, 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, optsIn) {
			var opts = optsIn;
			//var w = opts ? opts.path + ', ' + opts.domain : 'no options';
			//alert(w);
			if (typeof value === 'undefined' || value === null) {
				if (typeof opts !== 'object' || opts === null) {
					opts = {};
				}
				value = '';
				opts.hours = -8760;
			}
			document.cookie = name + '=' + encodeURIComponent(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 = decodeURIComponent(namevalue[1]);
				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();
		}

	};

})(jQuery);
