/*
    SiteComponents version:
    6.8.0, tag SC_6_8_0, created Tue Jun 15 10:40:42 +0200 2010

    Disclaimer
    
    While we make every effort to ensure that this code is fit for its intended
    purpose, we make no guarantees as to its functionality. CoreTrek AS will
    accept no responsibility for the loss of data or any other damage or
    financial loss caused by use of this code.


    Copyright
    
    This programming code is copyright of CoreTrek AS. Permission to run this
    code is given to approved users of CoreTrek's publishing system CorePublish.
    
    This source code may not be copied, modified or otherwise repurposed for use
    by a third party without the written permission of CoreTrek AS.
    
    Contact webmaster@coretrek.com for information.
    
*/

/*jslint laxbreak: true, sub: true, white: false, browser: true,
onevar: false, nomen: false, noindent: true, eqeqeq: false */
/*global siteComponentsConfig: false, Ajax: false, $$: false, $: false,
Element: false, window: false, Class: false, Effect: false, lightbox: false */

/*

    ============================================================================
    IMPORTANT! This javascript is dependent on Prototype.
    ============================================================================
    
    CtCookie
    
*/    
var CtCookie = Class.create({

    /**
     * Set a named value in cookie storage. It is also possible to set how
     * long the cookie will be valid by providing the expireInDays parameter.
     *
     * @param name string
     * @param value string The value to store
     * @param expireInDays int Number of days the cookie will be available
     */
    set: function(name, value, expireInDays) {
        var expires;
        if(expireInDays) {
            var date = new Date();
            date.setTime(date.getTime() + (expireInDays*24*60*60*1000));
            expires = "; expires=" + date.toGMTString();
        } else {
           expires = "";
        }
        document.cookie = name + "=" + value + expires + "; path=/";
    },
    
    /**
     * Get a named value from cookie storage. If the value is not set, null
     * is returned.
     *
     * @param name string
     */  
    get: function(name) {
        var nameRe = new RegExp("^\\s?" + name + "=.*");
        var cookieVars = document.cookie.split(';')
           .grep(nameRe)
           .collect(function(item) {
               return item.strip();
           });
           
        if(cookieVars.size() > 0) {
           return cookieVars.first().substring(name.length + 1);
        } else {
           return null;
        }
    },
    
    /**
     * Clear a value from session storage.
     *
     * @param name string
     */    
    clear: function(name) {
        this.set(name, '', -1);
    }

});

