/**
 * Cookies.js, providing easy access to cookies thru the cookiejar object.
 * Enabling so-called "subcookies" thru the subcookiejar object.
 * See this related blogpost for more information on how to use these objects:
 *  <http://www.whatstyle.net/articles/28/subcookies>
 * 
 * @author Harmen Janssen <http://www.whatstyle.net>
 * @version 1.0
 * 
 */

/* based on http://www.quirksmode.org/js/cookies.html, by Peter-Paul Koch */
var cookiejar = {
    /* set a cookie */
    bake: function(cookieName,cookieValue,days,path,host){
        var expires='';
        if (days)
        {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            expires = "; expires="+date.toGMTString();
        }
        var thePath = '; path=/';

        var theHost = null;

        if (path) {
            thePath = '; path='+path;
        }

        if (host !== null && host.match(/^\./)) {
            theHost = '; domain=' + host;
        } else if (host !== null) {
            theHost = '; host=' + host;
        } else {
            theHost = "";
        }

        //document.cookie = cookieName+'='+escape(cookieValue)+expires+thePath+theHost;
        document.cookie = cookieName+'='+cookieValue+expires+thePath+theHost;
    },
    /* get a cookie value */
    fetch: function(cookieName){
        var nameEQ = cookieName + '=';
        var ca = document.cookie.split(';');
        for (var i=0; i<ca.length; i++)
        {
            var c = ca[i];
            while (c.charAt(0)===' ') {
                c = c.substring(1,c.length);
            }
            if (c.indexOf(nameEQ)===0) {
                //return unescape(c.substring(nameEQ.length,c.length));
                return c.substring(nameEQ.length,c.length);
            }
        }
        return null;
    },
    /* delete a cookie */
    crumble: function(cookieName){
        cookiejar.bake(cookieName,'',-1);
    }
};

/* circumventing browser restrictions on the number of cookies one can use */
var subcookiejar = {
    nameValueSeparator: '$$:$$',
    subcookieSeparator: '$$/$$',
    /* set a cookie. subcookieObj is a collection of cookies to be. Every member of subcookieObj is the name of the cookie, its value
     * the cookie value
     */
    bake: function(cookieName,subcookieObj,days,path,host){
        var cookieValue = '';
        for (var i in subcookieObj)
        {
            if (subcookieObj.hasOwnProperty(i))
            {
                cookieValue += i + subcookiejar.nameValueSeparator;
                cookieValue += subcookieObj[i];
                cookieValue += subcookiejar.subcookieSeparator;
            }
        }
        /* remove trailing subcookieSeparator */
        cookieValue = cookieValue.substring(0,cookieValue.length-subcookiejar.subcookieSeparator.length);
        cookiejar.bake(cookieName,cookieValue,days,path,host);
    },
    /* get a subcookie */
    fetch: function(cookieName,subcookieName){
        var cookieValue = cookiejar.fetch(cookieName);
        if (cookieValue) {
            var subcookies = cookieValue.split(subcookiejar.subcookieSeparator);
            for (var i=0; i<subcookies.length; i++)
            {
                var sc = subcookies[i].split(subcookiejar.nameValueSeparator);
                if (sc[0]==subcookieName) {
                    return sc[1];
                }
            }
        }
        return null;
    },
    /* delete a subcookie */
    crumble: function(cookieName,subcookieName,days,path,host)
    {
        var cookieValue = cookiejar.fetch(cookieName);
        var newCookieObj = {};
        var subcookies = cookieValue.split(subcookiejar.subcookieSeparator);
        for (var i=0; i<subcookies.length; i++)
        {
            var sc = subcookies[i].split(subcookiejar.nameValueSeparator);
            if (sc[0]!=subcookieName) {
                newCookieObj[sc[0]] = sc[1];
            }
        }
        subcookiejar.bake(cookieName,newCookieObj,days,path,host);
    }
};

// Extract a GET argument's value from the current URL
function gup(name) {
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if( results == null ) {
        return "";
    } else {
        return results[1];
    }
}
