// Borrowed directly from GWT code
var CookieManager = {
    set: function (name, value) {
		var date = new Date();
		date.setTime(date.getTime()+(30*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
        document.cookie = name+"="+escape(value)+expires+'; path=/';
    },
	
    get: function (name) {
		var dc = document.cookie;
        var prefix = name + "=";
        var begin = dc.indexOf("; " + prefix);
        if (begin == -1) {
            begin = dc.indexOf(prefix);
            if (begin !== 0) {
                return null;
            }
        } else {
            begin += 2;
        }
        var end = document.cookie.indexOf(";", begin);
        if (end == -1) {
            end = dc.length;
        }
        return unescape(dc.substring(begin + prefix.length, end));
    },

    erase: function (name) {
        // Set the expiry date to some old date, and set the value to empty
        var cookieDate = new Date(2000, 1, 1, 19, 30, 30);
        document.cookie = name + "=; expires=" + cookieDate.toGMTString();
    }
};

// The following two functions act as a dictionary
// cookieName is the name of the dictionary
// key, value is the key and value pair
// 
// setCookiePair(cookieName, key, value)
//   is equivalent to cookieName[key] = value
//
// var val = getCookie(cookieName, key)
//   is equivalent to val = cookieName[key]
// 
// Internally the cookie is stored as
// cookie = key1@value1#key2@value2#...
//
// When the hashmap reaches a size of 600, we apply a FIFO stratergy and remove
// key value pairs from the start of the cookie till size reaches below 600
// Thus a cookie like
//   cookie = k1@v1#k2@v2#k3@v3#k4@v4#...
// might become
//   cookie = k4@v4#k5@v5#...

CookieManager.setCookiePair = function(cookieName, key, value) {
    var begin, end;
    var cookie = CookieManager.get(cookieName);
    var entry = [key, "@", value, "#"].join("");

    function trimEntry(s) {
        var a = s.split(",");
        // Remove 3 entries at a time
        return a.slice(3).join(",");
    }

    if(!cookie) {
        CookieManager.set(cookieName, entry);
    } else {
        begin = cookie.search(eval("/(^|#)" + key + "@/"));
        // Entry does not exist
        if(begin === -1) {
            cookie += entry;
        } else {
            // Entry does exist so modify it
            if(cookie.charAt(begin) === "#") {
                begin += 1;
            }
            end = cookie.indexOf("#", begin);
            cookie = cookie.replace(cookie.substring(begin, end+1), entry);
        }

        // Apply FIFO stratergy
        while(cookie.length > 600) {
            cookie = trimEntry(cookie);
            //console.info("after", word, prefs);
        }

        CookieManager.set(cookieName, cookie);
    }
};

CookieManager.getCookiePair = function(cookieName, key) {
    var cookie = CookieManager.get(cookieName);
    if(!cookie) {
        return null;
    } 

    var begin = cookie.search(eval("/(^|#)" + key + "@/"));

    if(begin === -1) {
        return null;
    }

    var end = cookie.indexOf("#", begin+1);
    var s = cookie.substring(begin, end);
    return s.split("@")[1];
};

