/*!
 * All-in-one cookie function v1.0
 * http://hexmen.com/blog/2009/03/all-in-one-cookie-function/
 *
 * Copyright (c) 2009 Ash Searle
 * Dual licensed under the MIT and GPL licenses.
 */
function cookie() {
    var a = arguments, n = a.length, r;
    if (n < 2) {
	var cookies = {}, raw = document.cookie.split(/;\s*/);
	for (var i = 0; i < raw.length; i++) {
	    var parsed = raw[i].match(/^([^=]+)=?([^;]*)$/);
	    cookies[unescape(parsed[1])] = unescape(parsed[2]) || '';
	}
	r = n ? cookies[a[0]] : cookies;
    } else if (n == 2) {
	r = cookie(a[0], a[1], (a[1] === null) ? -1 : 0);
    } else {
	var i = 0,
	    name = a[i++],
	    value = a[i++],
	    days = (typeof a[i] == 'number') ? a[i++] : (value === null ? -1 : 0),
	    path = (typeof a[i] == 'string') && /^\//.test(a[i]) && a[i++] || '/',
	    domain = (typeof a[i] == 'string') ? a[i++] : document.domain,
	    secure = !!a[i];
	document.cookie = escape(name)+'='+escape(value)+
	    (days?'; expires='+new Date(+new Date+days*86400000).toGMTString():'')+
	    '; path='+path+
	    '; domain='+domain+
	    (secure?'; secure':'');
    }
    return r;
}
