/**
 * Library for manipulation with cookies.
 *
 * @package      TenisPortal
 * @subpackage   JavaScript
 * @author       Jiří Švec {@link jiri.svec@livesport.cz}
 * @copyright    (c) 2007 Livesport, s.r.o.; {@link http://www.livesport.cz/}
 * @since        2006
 */

/**
 * Constrructor of object for manipulation with cookies.
 * 
 * @constructor
 */
function Cookie()
{};

/**
 * Add / edit cookie.
 * 
 * @param  {String}  name    The cookie's name.
 * @param  {Mixed}   value   The cookie's value.
 * @param  {Integer} days    Cookie is valid for next number of days.
 * @return {Cookie}  Returns a fluent interface.
 */
Cookie.prototype.add = function(name, value, days)
{
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    } else {
        var expires = "";
    }
    
    document.cookie = name + "=" + value + expires + "; path=/";
    
    return this;
};

/**
 * Get a cookie value.
 * 
 * @param  {String} name   The cookie name.
 * @return {Mixed}  Returns a cookie value.
 */
Cookie.prototype.get = function(name)
{
    var nameEQ = name + "=";
    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 c.substring(nameEQ.length,c.length);
        }
    }
    
    return null;
};

/**
 * Delete the cookie.
 * 
 * @param  {String} name   The cookie name.
 * @return {Cookie} Returns a fluent interface;
 */
Cookie.prototype.erase = function(name)
{
    this.add(name, '', -1);
    
    return this;
};