function CCookie(exp)
{
    this.ethalon_expires = 31536000; //seconds, 1 year, 365 days
    this.default_expires = exp ? parseInt(exp*86400) : this.ethalon_expires;

    this.get = function(name)
    {
        var cookie = ' ' + document.cookie;
        var cname = ' ' + name + '=';
        var from  = cookie.indexOf(cname);
        if ( from != -1 )
        {
            from += cname.length;
            to    = cookie.indexOf(';', from );
            if ( to == -1 )
                to = cookie.length;
            return unescape( cookie.substring(from, to) );
        }
        return null;
    };

    this.set = function(name, data, path, domain, expires, secure)
    {
        if (!expires)
        {
            expires = new Date();
            expires.setTime( expires.getTime() + this.default_expires*1000);
        }
        document.cookie = name + "=" + escape(data)
            + ((expires == null) ? "" : "; expires=" + expires.toGMTString())
            + ((path == null)    ? "" : "; path=" + path)
            + ((domain == null)  ? "" : "; domain=" + domain)
            + ((secure == null)  ? "" : "; secure");
    };

    this.remove = function(name, path, domain)
    {
        if (this.get(name))
        {
            document.cookie = name + "=" + ((path == null) ? "" : "; path=" + path) + ((domain == null) ? "" : "; domain=" + domain) + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
        }
    };

    this.setDefaultExpires = function(days)
    {
    	 this.default_expires = days ? parseInt(days*86400) : this.ethalon_expires;
    };

    this.getFromGroup = function(group, name)
    {    	var s = this.get(group);
    	if (!s) return null;
    	s = s.split(';');
    	var len = s.length, r;
    	for(var i=0;i<len;i++)
    	{    		r = s[i].split('__');
    		if (r[0]==name) return r[1];    	}
    	return null;    };

    this.setToGroup = function(group, name, data, path, domain, expires, secure)
    {    	var s = this.get(group);
    	if (!s)
    	{
    	    data = name+'__'+data;
    	    this.set(group, data, path, domain, expires, secure);
    	    return;
    	}
    	s = s.split(';');
    	var len = s.length, isNew = true, r;
    	for(var i=0;i<len;i++)
    	{
    		r = s[i].split('__');
    		if (r[0]==name)
    		{
    		    s[i] = name+'__'+data;
    		    isNew = false;
    		    break;
    		}
    	}
    	if(isNew) s[i] = name+'__'+data;
   	    this.set(group, s.join(';'), path, domain, expires, secure);
    };

    this.removeFromGroup = function(group, name, path, domain, expires, secure)
    {    	var s = this.get(group);
    	if (!s) return;
    	s = s.split(';');
    	var len = s.length, d = [];
    	for(var i=0;i<len;i++)
    	{
    		if (s[i].split('__')[0]!=name) d.push(s[i]);
    	}
   	    this.set(group, d.join(';'), path, domain, expires, secure);
    };
};

window.coo = new CCookie();
