/**
 * @fileoverview Compare Tool Redesign (cookie).
 * @name Zip
 * Requires Prototype version 1.6.0 or greater.
 */

/**
 * Compare Tool Redesign Model class.
 * @author Neil Green
 */
var Cookie = Class.create();

/**
 * Compare Tool Redesign Zip prototype.
 * @scope Cookie.prototype
 */
Cookie.prototype =
{
    /**
     * The name of the cookie.
     */
    cookieName: null,
    
    /**
     * The value of the cookie.
     */
    cookieValue: null,
    
    /**
     * When the cookie will expire.
     */
    cookieExpires: null,
    
    /**
     * Domain of the cookie
     */
    cookieDomain: null,
    
    /**
     * Array of cookie parameters
     */
    cookieParams: new Hash(),
    
    /**
     * Constructor.
     * @constructor
     * @throws exception if Prototype 1.6.0 or greater is not loaded.
     */
    initialize: function(cookieName)
    {
        this.cookieName = cookieName;
        
        // Make sure Prototype is included.
        var exception = "Prototype version 1.6.0 or greater is required.";
        if(typeof Prototype == "undefined")
        {
            throw(exception);
        }
        // Make sure it is version 1.6.0 or greater.
        var ver = Prototype.Version.split(".");
        var major = ver[0];
        var minor = ver[1];
        if(major < 1 || minor < 6)
        {
            throw(exception);
        }

        this.loadCookieValue();
        this.buildParamsArrayFromCookieValue();
    },
    
    loadCookieValue: function()
    {
        this.cookieValue = this.getCookie(this.cookieName);
        
        // console.log("loadCookieValue called for cookieValue: " + this.cookieValue);
    },
    
    setExpireDays: function(days)
    {
        this.cookieExpires = days;
    },
    
    setDomain: function(domain)
    {
        this.cookieDomain = domain;
    },
    
    save: function()
    {
        // console.log("save cookie called for cookieName: " + this.cookieName + "; cookieValue: " + this.cookieValue + "; cookieExpires: " + this.cookieExpires);
        this.setCookie(this.cookieName, this.cookieValue, this.cookieExpires);
    },
    
    getParam: function(name)
    {
        return this.cookieParams.get(name);
    },
    
    setParam: function(name, value)
    {
        this.cookieParams.set(name, value);
        
        this.updateCookieValueFromParamsArray();
    },
    
    updateCookieValueFromParamsArray: function()
    { 
        this.cookieValue = this.cookieParams.toQueryString();
        
        // console.log("updateCookieValueFromParamsArray this.cookieValue: " + this.cookieValue);
    },
    
    buildParamsArrayFromCookieValue: function()
    {
        // console.log("buildParamsArrayFromCookieValue called for cookieValue: " + this.cookieValue);

        var cookieParams = new Hash();
        
        var value = this.cookieValue;
        
        var cookieValueNameValues = value.split("&"); 
        
        // console.log("cookieValueNameValues: " + cookieValueNameValues.inspect());
        
        cookieValueNameValues.each(function(nameValue)
        {
            if ((nameValue != null) && (nameValue != ""))
            {
                // console.log("cookieValueNameValue: " + nameValue);
                
                var name = nameValue.split("=")[0];
                var value = nameValue.split("=")[1];
                
                cookieParams.set(name, value)
            }
        });
        
        // console.log("cookieParams: " + cookieParams.inspect());
        
        this.cookieParams = cookieParams;
    },

    /**
     * Creates a Cookie
     * Taken from http://www.w3schools.com/JS/js_cookies.asp
     */
    setCookie: function(c_name, value, expiredays)
    {
        var exdate = new Date(); 
        exdate.setDate(exdate.getDate()+expiredays);
        
        var expires = (expiredays == null) ? "" : ";expires="+exdate.toGMTString();
        var path    = ";path=/";
        var domain  = (this.cookieDomain == null) ? "" : ";domain=" + this.cookieDomain;  
        
        document.cookie = c_name+ "=" + escape(value) + expires + path + domain;
    },
    
    /**
     * Gets a Cookie's value
     * Taken from http://www.w3schools.com/JS/js_cookies.asp
     */
    getCookie: function(c_name)
    {
        if (document.cookie.length>0)
        {
            c_start=document.cookie.indexOf(c_name + "=");
            
            if (c_start!=-1)
            { 
                c_start=c_start + c_name.length+1; 
                c_end=document.cookie.indexOf(";",c_start);
                
                if (c_end==-1) c_end=document.cookie.length;
                
                return unescape(document.cookie.substring(c_start,c_end));
            } 
        }
        
        return "";
    } 
}
