/*
 * jQuery.cookie - A cookie plugin for jQuery.
 *
 * Copyright (c) 2007 Dave Stewart <dave@yesterdave.com>, http://www.yesterdave.com
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 * 
 * $Version: 2007.05.17 +r4
 *
 * some ideas taken from Klaus Hartl's cookie plugin found here: http://www.stilbuero.de/2006/09/17/cookie-plugin-for-jquery/
 *
 * includes a modified and packed version of Mark Gibson's json.js from http://jollytoad.googlepages.com/json.js
 */


(function($){

var m={'\b':'\\b','\t':'\\t','\n':'\\n','\f':'\\f','\r':'\\r','"':'\\"','\\':'\\\\'},
s={'array':function(x){var a=['['],b,f,i,l=x.length,v;for(i=0;i<l;i+=1){v=x[i];f=s[typeof v];if(f){v=f(v);if(typeof v=='string'){if(b)a.push(',');a.push(v);b=true}}}a.push(']');return a.join('')},'boolean':function(x){return String(x)},'date':function(x){function f(n){return n<10?'0'+n:n}return'"'+x.getFullYear()+'-'+f(x.getMonth()+1)+'-'+f(x.getDate())+'T'+f(x.getHours())+':'+f(x.getMinutes())+':'+f(x.getSeconds())+'"'},'null':function(x){return"null"},'number':function(x){return isFinite(x)?String(x):'null'},'object':function(x){if(x){if(x instanceof Array)return s.array(x);if(x instanceof Date)return s.date(x);var a=['{'],b,f,i,v;for(i in x){v=x[i];f=s[typeof v];if(f){v=f(v);if(typeof v=='string'){if(b)a.push(',');a.push(s.string(i),':',v);b=true}}}a.push('}');return a.join('')}return'null'},'string':function(x){if(/["\\\x00-\x1f]/.test(x)){x=x.replace(/([\x00-\x1f\\"])/g,function(a,b){var c=m[b];if(c)return c;c=b.charCodeAt();return'\\u00'+Math.floor(c/16).toString(16)+(c%16).toString(16)})}return'"'+x+'"'}};
$.toJSON=function(v){var f=isNaN(v)?s[typeof v]:s['number'];if(f)return f(v)};
$.parseJSON=function(v){if(/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/.test(v))return eval('('+v+')');else return v};

var decode = function(data){
	return $.parseJSON(decodeURIComponent(data));
};
var encode = function(data){
	return encodeURIComponent($.toJSON(data));
};
var findCookie = function(name){
	if (!document.cookie || document.cookie == '')
		return null;
	var items = document.cookie.split(';');
	if (name) {
		name += '=';
		var len = name.length;
		for (var i = 0, l = items.length; i < l; i++) {
			var item = $.trim(items[i]);
			if (item.substring(0, len) == name)
				return decode(item.substring(len));
		}
	} else {
		var all = {};
		for (var i = 0, l = items.length; i < l; i++) {
			var item = $.trim(items[i]);
			var idx = item.indexOf('=');
			all[item.substring(0, idx - 1)] = decode(item.substring(idx + 1));
		}
		return all;
	}
};
var isCookieObject = function(o){
	return (o && (typeof o == 'object') && !(o instanceof Array || o instanceof Date) && o.cdata);
};
$.cookie = {
	getAll: function(){
		return findCookie();
	},
	getInfo: function(name){
		return findCookie(name);
	},
	get: function(name){
		var cookie = findCookie(name);
		return isCookieObject(cookie) ? cookie.cdata : cookie;
	},
	exists: function(name){
		return !!findCookie(name);
	},
	set: function(name, value, options){
		var s = options || {};
		if (!value && !s.expires)
			s.expires = -1;
		var expires = '';
		if (s.expires && (typeof s.expires == 'number' || s.expires.toUTCString)) {
			if (typeof s.expires == 'number') {
				var d = new Date();
				d.setTime(d.getTime() + (s.expires * 24 * 60 * 60 * 1000));
				s.expires = d;
			}
			expires = '; expires=' + s.expires.toUTCString();
		}
		var path = s.path ? '; path=' + s.path : '';
		var domain = s.domain ? '; domain=' + s.domain : '';
		var secure = s.secure ? '; secure' : '';
		document.cookie = [name, '=', encode(value), expires, path, domain, secure].join('');
	},
	setInfo: function(name, value, options){
		var now = new Date();
		var existing = findCookie(name);
		if (!isCookieObject(existing))
			existing = null;
		var s = existing || {
			created: now,
			expires: null,
			path: null,
			domain: null,
			secure: null
		};
		$.extend(s, options || {});
		s.modified = now;
		s.cdata = value;
		$.cookie.set(name, s, s);
	},
	remove: function(name){
		$.cookie.set(name, null);
	},
	clear: function(){
		var cookies = getAll();
		for (var c in cookies)
			$.cookie.remove(c);
	}
};
})(jQuery);
