﻿var pcd; //Initialized with the page.

//NOTE: Consider moving this object into the namespace
//Precondition: Object constructor is not called until DOM has loaded.
function ProductToCartDialog(productId, productName, productCatImageUrl, productUnitPrice, numAddedSkuInCart, totalNumSkusInCart, cartSubtotal, dialogShowFunction) {
    this.ProductId = productId;
    this.ProductName = productName;
    this.ProductCatImageUrl = productCatImageUrl;
    this.ProductUnitPrice = productUnitPrice;
    this.NumAddedSkuInCart = numAddedSkuInCart;
    this.TotalNumSkusInCart = totalNumSkusInCart;
    this.CartSubtotal = cartSubtotal;
    this.DialogShow = function() { dialogShowFunction(); };

    this.DialogInit = function() {
        //hardcoded container name
        $('.SkuList input').val("0"); //reset qty textboxes now that they have added the items to their cart
        this.LoadDialog();
        this.DialogShow();

        //Requires hotkeys plugin. Unbind when closing the dialog.
        $(document).bind('keydown', 'return', this.EnterKeyPress);

        //turn off the scrollbar when dialog is shown and then revert on close
        $('html').css('overflow-y','hidden');
    };

    this.EnterKeyPress = function() {
        pcd.Checkout();
        return false;
    }

    this.CloseDialog = function() {
        $(document).unbind('keydown', 'return', this.EnterKeyPress);
        $('html').css('overflow-y','scroll');
        dialog.Close(); return false;
    }

    this.Checkout = function() {
        $(".divRedirecting").css("visibility", "visible");
        window.location.href = '../Basket/ShoppingCart.aspx';
    };

    this.LoadDialog = function() {
        //Load Product Summary
        $('.tdProductAddSummary img').attr("src", this.ProductCatImageUrl);
        $('.tdProductAddSummary .name').html(this.ProductName);
        $('.tdProductAddSummary .price').html(this.ProductUnitPrice);
        $('.tdProductAddSummary .quantity').html(this.NumAddedSkuInCart);

        //Load Cart Summary
        $('.tdCartSummary .itemsInCart').html(this.TotalNumSkusInCart);
        $('.tdCartSummary .subtotal').html(this.CartSubtotal);
    }


    this.DialogInit();
}
