var favorites = (function(){
    var _currentId;
    
    return {
        setCurrentId: function(id) {
            _currentId = id;
        },
        add: function(vehicleID) {
            var vehicleIds = new Array();
            var cookieString = $.cookie('favorites');
            if (cookieString != null) {
                $.map(cookieString.split(','), function(val) {
                    var intVal = parseInt(val);
                    if (!isNaN(intVal)) {
                        vehicleIds.push(intVal);
                    }
                });
                
                var idx = $.inArray(vehicleID, vehicleIds);
                if (idx >= 0) {
                    return;
                }
                
                if (vehicleIds.length >= 6) {
                    $.colorbox({
                        href: '/auto/favorites?id=' + vehicleID,
                        iframe: true,
                        width: 820,
                        height: 310
                    });
                    return;
                }
            }
            vehicleIds.push(vehicleID);
            $.cookie('favorites', vehicleIds.join(','),
                     { expires: 30, path: '/' });
            
            if (vehicleID == _currentId) {
                $('#remove-from-favorite').show();
                $('#add-to-favorite').hide();
            }
            this._createNode(vehicleID);
        },
        
        remove: function(vehicleID) {
            var vehicleIds = new Array();
            var cookieString = $.cookie('favorites');
            if (cookieString == null) {
                return;
            }
            
            $.map(cookieString.split(','), function(val) {
                vehicleIds.push(parseInt(val));
            });
            

            var idx = $.inArray(vehicleID, vehicleIds);
            if (idx >= 0) {
                vehicleIds.splice(idx, 1);
            }
            $.cookie('favorites', vehicleIds.join(','),
                     { expires: 30, path: '/' });
            
            var elm = $('#favorite-vehicle-'+ vehicleID);
            elm.fadeOut("normal", function(){
                $(this).remove();
            });
            
            if (vehicleID == _currentId) {
                $('#remove-from-favorite').hide();
                $('#add-to-favorite').show();
            }				
        },
        
        _createNode: function(id)
        {
            var self = this;
            $.get('/vehicle/fragment/' + id, null, function(data, status){
                
                // Search empty container
                var $container = $('#favorites ol');
                if ($container.length > 0) {
                    var $node = $(data);
                    $node.hide();
                    
                    $node.find('a.delete').click(function(){
                        self.remove(id);
                        return false;
                    });
                    $container.append($node);
                    $node.fadeIn();
                }
            });
                  
        }
    }
}());

