/**
 * Manages favorite products selected by the user.
 * Data stores as cookie of the user's browser.
 *
 * @requires        jquery.cookie.js
 * @requires        json2.min.js
 */
var FavoriteProducts = new Object();
var FavoriteProductsHelper = new Object();
(function ()
{
    // Settings of the favorite products.
    var settings = new Object();

    // The cookie settings.
    settings.cookie =
    {
        'expires' : 365,    // 1 year as default
        'name' : 'fp'
    }; 
    
    // Identifiers of the user's favorite products.
    var productIds;
    
    // -------------------------------------------------------------------------
    
    /**
     * Favorite Products
     */
    (function ()
    {
        /**
         * Adds a product to the user's favorites.
         *
         * @param       number      a product ID
         */
        FavoriteProducts.addProduct = function (productId)
        {
            // Convert the product ID to an integer number.
            productId = parseInt(productId);
        
            // A product ID must be a number that is greater than zero.
            if ( ! (productId > 0))
            {
                return;
            }
            
            
            // Don't add the product if it's already a favorite one.
            if (FavoriteProducts.isFavoriteProduct(productId))
            {
                return;
            }
            
            
            // Add the product to the favorites.
            productIds.push(productId);
            
            // Rewrite the cookie.
            FavoriteProducts.rewriteCookie();
        
        };  // END FavoriteProducts.addProduct = function (productId)
        
        // ---------------------------------------------------------------------
        
        /**
         * Deletes all products from favorites.
         */
        FavoriteProducts.deleteAllProducts = function ()
        {
            if (productIds.length > 0)
            {
                productIds = new Array();
                
                // Rewrite the cookie.
                FavoriteProducts.rewriteCookie();
            }
        
        };  // END FavoriteProducts.deleteAllProducts = function ()
        
        // ---------------------------------------------------------------------
        
        /**
         * Deletes a product from the user's favorites.
         *
         * @param       number      a product ID
         */
        FavoriteProducts.deleteProduct = function (productId)
        {
            // Convert the product ID to an integer number.
            productId = parseInt(productId);
        
            // A product ID must be a number that is greater than zero.
            if ( ! (productId > 0))
            {
                return;
            }
            
            
            // Skip the product if it's not a favorite one.
            if ( ! FavoriteProducts.isFavoriteProduct(productId))
            {
                return;
            }
            
            
            // Remove the product from the favorites.
            productIds.splice
            (
                jQuery.inArray(productId, productIds), 1
            );
            
            // Rewrite the cookie.
            FavoriteProducts.rewriteCookie();
            
        };  // END FavoriteProducts.deleteProduct = function (productId)
        
        // ---------------------------------------------------------------------
        
        /**
         * Gets identifiers of favorite products.
         *
         * @return      array
         */
        FavoriteProducts.getProductIds = function() {
            return productIds;
        }; // END FavoriteProducts.getProductIds = function ()
        
        // ---------------------------------------------------------------------
        
        /**
         * Checks whether a product is favorite one.
         *
         * @param       number      a product ID
         */
        FavoriteProducts.isFavoriteProduct = function (productId)
        {
            return jQuery.inArray(productId, productIds) > -1;
        
        };  // END FavoriteProducts.isFavoriteProduct = function (productId)
        
        // ---------------------------------------------------------------------
        
        /**
         * Obtains the list of the user's favorite products.
         */
        FavoriteProducts.reloadFromCookie = function ()
        {    
            // Read the encoded value from the cookie. 
            var encodedValue = jQuery.cookie
            (
                settings.cookie['name']
            );
            
            
            // Process the encoded value.
            if (encodedValue)
            {
                // If the cookie value is set, it must be a JSON encoded array.
                try
                {
                    // Try to decode the JSON encoded value.
                    decodedValue = JSON.parse(encodedValue);
                    
                    // If the value isn't array.
                    if (decodedValue instanceof Array)
                    {
                        productIds = decodedValue;
                    }
                    else
                    {
                        // No product IDs specified.
                        productIds = new Array();
                    }
                }
                catch (e)
                {
                    // No product IDs specified.
                    productIds = new Array();
                }
            }
            else
            {
                // No product IDs specified.
                productIds = new Array();
            }
            
            
            // Rewrite the cookie.
            FavoriteProducts.rewriteCookie();
            
        };  // END FavoriteProducts.reloadFromCookie = function ()
        
        // ---------------------------------------------------------------------
        
        /**
         * Rewrites the cookie that holds IDs of the user's favorite products.
         */
        FavoriteProducts.rewriteCookie = function ()
        {
            // The cookie options.
            var options =
            {
                'expires' : settings.cookie['expires'],
                'path' : '/'
            };
            
            // Rewrite the cookie.
            jQuery.cookie
            (
                settings.cookie['name'],
                JSON.stringify(productIds),
                options
            );
        
        };  // END FavoriteProducts.rewriteCookie = function ()
        
    })();   // END Favorite Products
    
    
    // Obtains the list of the user's favorite products.
    FavoriteProducts.reloadFromCookie();
    
    // -------------------------------------------------------------------------
    
    /**
     * Favorite Products Helper
     */
    (function ()
    {
        /**
         * Refresh the counter of favorite products.
         */
        FavoriteProductsHelper.refreshCounter = function ()
        {
            // Refresh the counter.
            jQuery('#number-of-favorite-products').text(productIds.length);
            
            // Update the link to the favorites.
            if (productIds.length > 0)
            {
                jQuery('#link-to-favorites')
                    .removeClass('no-items')
                    .addClass('has-items')
                ;
            }
            else
            {
                jQuery('#link-to-favorites')
                    .removeClass('has-items')
                    .addClass('no-items')
                ;
            }
        
        };  // END FavoriteProductsHelper.refreshCounter = function ()
    
    })();   // END Favorite Products Helper

})();   // END Favorite Products and Favorite Products Helper

