﻿/// <reference path="~/Resources/js/jquery-1.2.6-intellisense.js" />
/// <reference path="~/Resources/js/start.js" />

function Comparison() {
    this.count = 0;
    this.products = [];

    // Contructs the comparison from client storage
    var cookieValue = VVM.getCookieValue("VvmComparison");
    if (cookieValue !== null) {
        var cookie = cookieValue.split("!");
        for (var i = 0; i < cookie.length; i++) {
            var id = cookie[i];
            this.products.push({ id: id });
            this.count++;
        }
    }
    
    // applies the callback function on the i'th product
    this.apply = function(callback, i) {
        var product = this.products[i];
        if (product.hasOwnProperty("id")) {
            if (callback && typeof (callback) == 'function') {
                var result = callback(product, i);
                if (result) return true;
            }
        }
    };
    
    // iterates over all products in the comparison
    // invoking callback(product, index) on each product
    // @param   callback: function(product, index)
    // @remark  return true in the callback function to get an equivalent 
    // of a break behavior or false to get an equivalent of a continue behavior
    this.each = function(callback) {
        for (var i in this.products) {
            if (this.apply(callback, i)) return true;
        }
        return false;
    };

    // iterates over all products in the comparison in reverse order
    this.eachReverse = function(callback) {
        for (var i = this.products.length - 1; i >= 0; i--) {
            if (this.apply(callback, i)) return true;
        }
        return false;
    };

    // stores the comparison on the client
    this.persist = function() {
        var cookieValue = "";
        var count = 0;
        this.each(function(product) {
            cookieValue += (count > 0 ? "!" : "") + product.id;
            count++;
        });
        this.count = count;
        document.cookie = "VvmComparison=" + cookieValue + "; path=/;";
    };

    // returns true if the product is in the comparison
    this.contains = function(product) {
        if (!product || !product.hasOwnProperty("id"))
            return false;

        return this.each(function(_product, i) {
            return _product.id == product.id;
        });
    };

    // adds a product to the comparison
    // @param   product: { id: id } a product to compare
    // @post    contains(product)
    this.add = function(product) {
        if (this.contains(product))
            return;

        this.products.push(product);
        this.persist();
    };

    // removes a product from the comparison
    // @param (opt)   product: { id: id } product to remove
    //                if parameter is omitted the first product is removed
    this.remove = function(product) {
        if (typeof (product) == "undefined" || product == null) {
            this.products.splice(0, 1);
            this.persist()
            return;
        }
        if (!product.hasOwnProperty("id"))
            return;

        var products = this.products;
        this.each(function(_product, i) {
            if (_product.id == product.id) {
                products.splice(i, 1);
                return true;
            }
            return false;
        });
        this.persist();
    };
}

VVM.comparison = new Comparison();

$(function() {
    // Check all products in comparison handler
    $(".product-blurb .buy").each(function() {
        var pid = $(this).attr("rel");
        if (VVM.comparison.contains({ id: pid })) {
            $(".compare input", $(this).parents(".product-blurb:first")).attr("checked", true);
        }
    });

    // Check compare product handler
    $(".product-blurb .compare input").click(function() {
        var checked = $(this).attr("checked");
        var product = $(this).parent().siblings(".information").children(".buy").attr("rel");

        if (checked) {
            VVM.comparison.add({ id: product });
            // Remove all but the 3 most recently appended
            if (VVM.comparison.count > 3) {
                $("#compare" + VVM.comparison.products[0].id).attr("checked", false);
                VVM.comparison.remove();
            }
        } else {
            VVM.comparison.remove({ id: product });
        }
    });

    // Goto compare page handler
    $(".product-blurb .compare img").click(function() {
        if (VVM.comparison.count < 2) {
            alert("Du måste välja åtminstone två (2) artiklar att jämföra");
            return false;
        }

        var i = 0;
        var querystring = "";
        VVM.comparison.eachReverse(function(product) {
            querystring += (i > 0 ? "&" : "") + "p" + i + "=" + product.id;
            i++;
        });
        location.href = "/ComparePage.aspx?" + querystring;
    });
});
