﻿/// <reference path="~/Resources/js/jquery-1.2.6-intellisense.js" />
/// <reference path="~/Resources/js/start.js" />

function Cart() {
    this.quantity = 0;
    this.subtotal = 0;
    this.items = [];

    this.getCookieValue = function(name) {
        var cookies = document.cookie.split("; ");
        for (i = 0; i < cookies.length; i++) {
            var values = cookies[i].split("=");
            if (values[0] == name) {
                return values[1];
            }
        }
        return null;
    };

    var cookieValue = this.getCookieValue("VvmCart");
    if (cookieValue !== null) {
        var cookie = cookieValue.split("!");
        if (cookie.length > 1) {
            this.subtotal = cookie[0];
            this.quantity = cookie[1];
        }
        if (cookie.length > 2) {
            for (var i = 2; i < cookie.length; i++) {
                var item = cookie[i].split("|");
                var product = {
                    id: item[0],
                    price: item[1],
                    priceListId: item[2],
                    qty: item[3]
                };
                this.items.push(product);
            }
        }
    }

    // Adds an item to the cart
    // @param   item: {id, price, priceListId[, qty]}
    this.add = function(item) {
        item.qty = item.qty * 1 || 1;

        var itemInCart = this.getItem(item);
        if (itemInCart === null) {
            this.items.push(item);
        } else {
            itemInCart.qty = (itemInCart.qty * 1) + item.qty;
            itemInCart.price = item.price;
        }

        this.persist();
    };

    // Removes the item from the cart
    // @param   item: {id, priceListId}
    this.remove = function(item) {
        if (!item.hasOwnProperty("id") ||
            !item.hasOwnProperty("priceListId")) {
            return;
        }
        var items = this.items;
        this.getItem(item, function(index) {
            items.splice(index, 1);
        });
        this.persist();
    };

    // Updates the specified item with the specified quantity
    // @remark  if qty is 0 the item is removed
    // @param   item: {id, priceListId}
    // @param   qty   the new quantity
    this.update = function(item, qty) {
        var itemToUpdate = this.getItem(item);
        if (itemToUpdate === null) {
            return;
        }
        if (qty == 0) {
            this.remove(item);
            return;
        }
        itemToUpdate.qty = qty * 1;
        this.persist();
    };

    // Gets the specified item
    // @param   itemToGet: {id, priceListId}
    // @param   fn  optional - if item found 
    //              fn is invoked with index as parameter
    this.getItem = function(itemToGet, fn) {
        for (var i in this.items) {
            var item = this.items[i];
            if (item.hasOwnProperty("id")) {
                if (item.id == itemToGet.id &&
                    item.priceListId == itemToGet.priceListId) {
                    if (fn) { fn(i); }
                    return item;
                }
            }
        }
        return null;
    };

    this.clear = function() {
        this.items.splice(0, this.items.length);
        this.persist();
    };

    this.persist = function() {
        var str = "",
            qty = 0,
            total = 0;
        for (var i in this.items) {
            var item = this.items[i];
            if (item.hasOwnProperty("id")) {
                str += "!" + item.id + "|" + item.price + "|" + item.priceListId + "|" + item.qty;
                qty += item.qty * 1;
                total += item.price * item.qty * 1;
            }
        }
        this.quantity = qty;
        this.subtotal = total;
        document.cookie = "VvmCart=" + this.subtotal + "!" + this.quantity + str + "; path=/;";
    };
}

VVM.cart = new Cart();
VVM.renderCart = function() {
    $("#mini-cart #quantity").text(VVM.cart.quantity);
    $("#mini-cart #total-cost").text(VVM.formatPrice(VVM.cart.subtotal));
};

