﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("BandSite");

BandSite.Shop = function(element) {
    BandSite.Shop.initializeBase(this, [element]);
    
    this._items = [];    
}

BandSite.Shop.prototype = {
    initialize: function() {
        BandSite.Shop.callBaseMethod(this, 'initialize');
        
        // Add custom initialization here
        var that = this;
        
        this._listView.add_eventHandler(
            "AddToCartButton",
            "buttonClicked",
            function(sender, e, item) {                
//                if ($find('ShoppingCartSection')) {
//                    $find('ShoppingCartSection').addItemToCart(item);
//                }
                that.addItemToCart(item);
            }
        );

        this._checkoutButton.add_buttonClicked(
            function(sender, e) {
                $get("PayPalForm").submit();
            }
        );
        
        this._shoppingCartListView.add_eventHandler(
            "RemoveButton",
            "buttonClicked",
            function(sender, e, item) {
                // alert("Remove item " + item.ID);
                var newItemList = [];
                for(var i = 0; i < that._items.length; i++) {
                    if (that._items[i].ID != item.ID) {
                        that._items[i].ID = newItemList.length + 1;
                        newItemList.push(that._items[i]);
                    }
                }
                that._items = newItemList;
                that.reloadItems();
            }
        );
        
        this._totalAndCheckoutButtonDiv.style.display = 'none';
        
    },
    dispose: function() {        
        //Add custom dispose actions here
        BandSite.Shop.callBaseMethod(this, 'dispose');
    },
    
    addItemToCart: function(item) {
    
        // The item ID is irrelevant here so we will assign a new,
        // unique one in case they want to add two of an item

        // Create a new item so changing the id does not affect original item    
        var cartItem = {};                    
        cartItem.ID = this._items.length + 1;        
        cartItem.Title = item.Title;
        cartItem.Description = item.Description;
        cartItem.ImageFileName = item.ImageFileName;
        cartItem.Price = item.Price;
    
        this._items.push(cartItem);

        this.reloadItems();
//        if ($find("BandSite")) {
//            $find("BandSite").get_menuTabSet().showTab("ShoppingCartTab");
//        }
    },
    
    reloadItems: function() {
        this._shoppingCartListView.set_itemList(this._items);

        // Use JQuery here to remove the relevant hidden
        // fields and add the appropriate new ones
        $("#PayPalForm").children("input:hidden[name*='item_name_']").remove();
        $("#PayPalForm").children("input:hidden[name*='amount_']").remove();

        var total = 0;
        for (var i = 0; i < this._items.length; i++) {
            var item = this._items[i];
            total += item.Price;            
            var hiddenNameStr = '<input type="hidden" value="' + item.Title + '" name="item_name_' + (i + 1) + '"/>';
            $("#PayPalForm").append(hiddenNameStr);
            var hiddenAmountStr = '<input type="hidden" value="' + item.Price + '" name="amount_' + (i + 1) + '"/>';
            $("#PayPalForm").append(hiddenAmountStr);            
        }
        this._totalLabel.set_value(total);
        
        if (this._items.length > 0) {
            this._totalAndCheckoutButtonDiv.style.display = '';
        } else {
            this._totalAndCheckoutButtonDiv.style.display = 'none';
        }        
    }
    
}
BandSite.Shop.createProperty("bandID");
BandSite.Shop.createProperty("listView");
BandSite.Shop.createProperty('shoppingCartListView');
BandSite.Shop.createProperty('totalLabel');
BandSite.Shop.createProperty('checkoutButton');
BandSite.Shop.createProperty('totalAndCheckoutButtonDiv');
BandSite.Shop.createProperty('currency');
BandSite.Shop.registerClass('BandSite.Shop', Sys.UI.Control);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
