/**
 * @namespace 
 * This namespace contains common functions for handling cookies
 * 
 * @static
 */
gm.util.Cookie = {
    
    /**
     * Recent locations cookie.
     */
    VISITED : "hasvisited",
    
    /**
     * Gets the value of the cookie with the given name.
     * 
     * @param {Object} name  cookie name
     * @return the cookie value, or null if there is no cookie with the given name
     */
    get : function(name) {
    	var idx = document.cookie.lastIndexOf(name+'=');
    	if(idx == -1) { return null; }
    	var value = document.cookie.substring(idx+name.length+1);
    	var end = value.indexOf(';');
    	if(end == -1) { end = value.length; }
    	value = value.substring(0, end);
    	return value;
    },
    
    /**
     * Sets a cookie with the given name and value.
     * 
     * @param {String} name     cookie name
     * @param {String} value    cookie value
     * @param {Number} time     number of days until cookie expires (optional)
     * @param {String} path     cookie path (optional)
     * @param {boolean} secure  whether or not the cookie is secure (optional)
     */
    set : function(name, value, time, path, secure) {
        var expires;
        if (time) {
            var date = new Date();
            date.setTime((time * 60 * 60 * 24 * 1000) + date.getTime());// # of days    
            expires = date.toGMTString();
        }
        
    	document.cookie = name + "=" + value + ";" +
                          (expires ? " expires=" + expires + ";" : "") +
                          (path ? "path=" + path : "") +
                          (secure ? "; secure" : "");
    },
    
    /**
     * Deletes the cookie with the given name.
     * 
     * @param {Object} name
     */
    remove : function(name, path) {
        this.setCookie(name, "", -1, path);
    }
};