var compare = 
    {
        items : new Array(),
        
        load : function()
            {   
                var loadCookie = this.getCookieItems();
                
                if( loadCookie )
                {
                    $('.empty').hide();
                    $('#comparesubmit').fadeIn();
                    $('#comparetruncate').fadeIn();
                    
                    this.items = this.decodeCookieItems( loadCookie );
                    for(var i = 0; i < this.items.length; i++)
                    {   
                        this.objadd( this.items[i][0] , this.items[i][1] , this.items[i][2] );
                    }
                }
                else
                {
                    $('#comparesubmit').hide();
                    $('#comparetruncate').hide();
                }
                
                                    
                $('.compareaddbuttom').each( function (i)
                {
                    if( compare.exist( this.id ) )
                    {
                        compare.objsubDisable( this.id );
                    }
                    else
                    {
                        compare.objsubEneble( this.id );
                    }
                });
                
            },
        exist : function ( id )
            {
                for(var i = 0; i < this.items.length; i++)
                {   
                    if( this.items[i][0] == id )
                    {
                        return true;
                    }
                }
                return false;
            },
        add : function( id , name , href , fade )
            {   
                $('.empty').hide();
                $('#comparesubmit').fadeIn();
                $('#comparetruncate').fadeIn();
                
                for(var i = 0; i < this.items.length; i++)
                {
                    if( this.items[i][0] == id ) return false;
                }

                if( !name || !href ) 
                {
                    return this.getParams( id );
                }
                else
                {   
                    this.items[this.items.length] = new Array( id , name , href );
                    this.saveCookieItems();
                    this.objadd( id , name , href , fade );
                }
                
                this.objsubDisable( id );
            },
        remove : function( id )
            {
                if( this.items.length == 1 ) return this.truncate();
                
                for(var i = 0; i < this.items.length; i++)
                {
                    if( this.items[i][0] == id )
                    {
                        this.items.splice( i , 1 );
                        break;
                    }
                }
                this.saveCookieItems();
                this.objremove( id );
                
                this.objsubEneble( id );
            },
        truncate : function()
            {   
                $('#comparesubmit').fadeOut();
                $('#comparetruncate').fadeOut();
                
                this.items = new Array();
                this.saveCookieItems();
                this.objtruncate();
                $('.compareaddbuttom').each( function (i)
                {
                    compare.objsubEneble( this.id );
                });
            },
        submit : function()
            {
                var compare = new String();
                
                for(var i = 0; i < this.items.length; i++)
                {
                    if( i < this.items.length - 1 )
                    {
                        compare = compare + this.items[i][0] + ','
                    }
                    else
                    {
                        compare = compare + this.items[i][0];
                    }
                }
                document.location.href = '/compare.html?compare=' + compare;
            },
        objsubEneble : function( id )
            {
                var inner = '<span class="active" onclick="compare.add('+id+')">добавить в корзину</span>';
                $('.compareaddbuttom[id='+id+']').html( inner );
            },
        objsubDisable : function( id )
            {
                var inner = '<span class="disable">в корзине</span>';
                $('.compareaddbuttom[id='+id+']').html( inner );
            },
        objadd : function( id , name , href , fade )
            {   
                if( name.length > 28 )
                {
                    var trimname = name.substr( 0 , 24 )+' ...';
                    var innerstring = '<div class="item" id="compare'+id+'"><div class="remove"><img src="/misc/images/compare/delete.gif" onclick="compare.remove('+id+')" /></div><div class="name" onclick="location.href=\'/laptop/'+href+'.html\';">'+trimname+'</div></div>';
                }
                else
                {
                    var innerstring = '<div class="item" id="compare'+id+'"><div class="remove"><img src="/misc/images/compare/delete.gif" onclick="compare.remove('+id+')" /></div><div class="name" onclick="location.href=\'/laptop/'+href+'.html\';">'+name+'</div></div>';
                }

                $('.compareconteiner').html( $('.compareconteiner').html()+innerstring );
                
                if( fade == true )
                {
                    $('#compare'+id).css( { display: 'none' } );
                    $('#compare'+id).fadeIn( 1000 );
                }
            },
        objremove : function( id )
            {
                $('#compare'+id).fadeOut(1000, function () { $(this).remove(); });
            },
        objtruncate : function()
            {
                $('.compareconteiner').fadeOut(1000, function ()
                { 
                    $('.empty').fadeIn( 1000 );
                    $(this).html('');
                    $(this).show(); 
                });
            },
        saveCookieItems : function()
            {
                this.cookie( 'compare' , null , { path: '/' });

                var cookiestring = this.encodeCookieItems();

                if( cookiestring )
                {
                    this.cookie( 'compare' , cookiestring , { path: '/', expires: 10 });
                }
                else
                {
                    this.cookie( 'compare' , null , { path: '/' });
                }
            },
        encodeCookieItems : function()
            {   
                var cookiestring = new Array();
                
                var id   = new String();
                var name = new String();
                var href = new String();
                
                for(var i = 0; i < this.items.length; i++)
                {
                    id   = this.items[i][0];
                    name = this.items[i][1];
                    href = this.items[i][2];

                    cookiestring[ cookiestring.length ] = id+'%'+name+'%'+href;
                }
                cookiestring = cookiestring.join(";");
                return cookiestring;
            },
        getCookieItems : function ()
            {   
                return this.cookie('compare');
            },
        decodeCookieItems : function( cookiestring )
            {   
                var items = new Array();

                items = cookiestring.split(';');

                for(var i = 0; i < items.length; i++)
                {
                    var itemcookiestring = items[i];
                    items[i] = itemcookiestring.split('%');
                }
                return items;
            },
        getParams : function( id )
            {
                $.post("/ajax.html?ajax=laptops.getShort", { id: id },
                  function( data ){
                    compare.add( id , data.name , data.href , true );
                  }, "json");
            },
        cookie : function(name, value, options)
            {
                if (typeof value != 'undefined') {
                    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();
                    }
                    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('');
                }
                else
                {
                    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]);
                            if (cookie.substring(0, name.length + 1) == (name + '=')) {
                                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                                break;
                            }
                        }
                    }
                    return cookieValue;
                }
            },
        scroll : function ()
            {
                
            }
        

    }
