/*
 * Customised Cookie plugin
 *
 * Varun Sood (19th May 2009)
 *
 * Set cookie:  $.cookie('Cookie_Name', 'Cookie_Value');
 * Get cookie:  $.cookie('Cookie_Name');
 * Delete a cookie: $.cookie('Cookie_Name', null);
 *
 */

/*
jQuery.cookie = function(name, value) 
{ 
    if (typeof value != 'undefined' || value === null)  
    {
        // Delete a cookie: $.cookie('Cookie_Name', null); 
        if (value === null) 
            var expiredays = -1; 
    
        // Set cookie: $.cookie('Cookie_Name', 'Cookie_Value');
        if (typeof value != 'undefined') 
            var expiredays = 30; // Days
         
        var exdate=new Date();
        exdate.setDate(exdate.getDate()+expiredays);    
        document.cookie= name + "=" +escape(value)+
        ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
    }
    
    // Get cookie: $.cookie('Cookie_Name');
    if (typeof value == 'undefined') 
    { 
        if (document.cookie.length > 0)
        { 
            c_start=document.cookie.indexOf(name + "=");
            
            if (c_start!=-1)
            { 
                c_start=c_start + 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));
            }
        }
        // Note: Setting the return value to "false" closes all panels where as 
        // setting it to "0" or "null" will leave the first panel open if cookie 
        // is not available. 
        return "false";
    }
    
};
*/

/*
 * Get cookie:        $.cookie('Cookie_Name');     
 * Set cookie:        $.cookie('Cookie_Name', 'Cookie_Value'); 
 * Set cookie expire: $.cookie('Cookie_Name', 'Cookie_Value', { expires: 30 }); 
 * Delete cookie:     $.cookie('Cookie_Name', '', { expires: -1 }); 
 */
jQuery.cookie = function(name, value, options) 
{ 
    
    
    if (typeof value != 'undefined') 
    { // name and value given, set cookie
        options = options || {};
        if (value === null) 
        {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) 
        {
            var date;
            if (typeof options.expires == 'number') 
            {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } 
            else 
            {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        //var path = options.path ? '; path=' + (options.path) : '';
        //var domain = options.domain ? '; domain=' + (options.domain) : '';
        //var secure = options.secure ? '; secure' : '';        
        
        //document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
        
        date = new Date();
        date.setTime(date.getTime() + (30 * 24 * 60 * 60 * 1000));
        expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE

        document.cookie = [name, '=', encodeURIComponent(value), expires, ';path=/', '', ''].join('');
    } 
    else 
    {  // only name given, get cookie 
        
        var cookieValue = null;
        if (document.cookie && document.cookie != '') 
        {
            var cookies = document.cookie.split(';');
            
            for (var i = 0; i < cookies.length; i++) 
            {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                 
                if (cookie.substring(0, name.length + 1) == (name + '=')) 
                {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
       
        return cookieValue;
    }
};