function ApOnShoppingCart() {

}

ApOnShoppingCart.init = function()
{
    if (ApOnShoppingCart.initialized) {
        return;
    }
    ApOnShoppingCart.courtain = createFloatingElement("div", "ApOnShoppingCartCourtain");
    ApOnShoppingCart.courtain.style.display = "none";

    ApOnShoppingCart.lightbox = createFloatingElement("div", "ApOnShoppingCartLightbox");
    ApOnShoppingCart.lightbox.style.display = "none";
    ApOnShoppingCart.lightbox.innerHTML = "loading...";


    ApOnShoppingCart.courtain.onclick = ApOnShoppingCart.hide;

    ApOnShoppingCart.initialized = true;
}

ApOnShoppingCart.show = function(instance)
{
    ApOnShoppingCart.instance = instance;
    ApOnShoppingCart.init();

    ApOnShoppingCart.courtain.style.display = "block";
    ApOnShoppingCart.lightbox.style.display = "block";
    ApOnShoppingCart.lightbox.innerHTML = "loading...";

    var maxFrame = 250;

    var animationFunction = function (frame) {
        var opacity = 0.7 * frame / maxFrame;
        if (document.all) {
            //only ie
            ApOnShoppingCart.courtain.style.filter = "alpha(opacity=" + Math.floor(opacity * 100) + ")";
        } else {
            ApOnShoppingCart.courtain.style.opacity = opacity;
        }
        var width = 700 * (frame/maxFrame);
        var height = 500 * (frame/maxFrame);
        var pageWidth = ApOnShoppingCart.courtain.offsetWidth;
        var pageHeight = ApOnShoppingCart.courtain.offsetHeight;
        setElementSizeAndPosition(ApOnShoppingCart.lightbox, (pageWidth - width) / 2, (pageHeight - height) / 2, width, height);
        if (frame == maxFrame) {
            ApOnShoppingCart.loadShoppingCart();
        }
    }

    var animation = new _aponAnimation(0, maxFrame, 25, "fading", animationFunction);
    animation.start();
}

ApOnShoppingCart.hide = function()
{
    ApOnShoppingCart.courtain.style.display = "none";
    ApOnShoppingCart.lightbox.style.display = "none";
}

ApOnShoppingCart.loadShoppingCart = function ()
{
    var additionalData = new Array();
    additionalData["call"] = "shopping_cart";
    ApOnWebpart.putInnerHTML("shop", "_base", ApOnShoppingCart.instance, additionalData, ApOnShoppingCart.loadShoppingCartCallback)
}

ApOnShoppingCart.loadShoppingCartCallback = function (response)
{
    ApOnShoppingCart.lightbox.innerHTML = response.text;
}

ApOnShoppingCart.PickMethod = function()
{
    if ($("shoppingCartEmpty")) {
        return;
    }
    ApOnShoppingCart.selectedMethod = "";
    if ($("paymentGoogleCheckout") && $("paymentGoogleCheckout").checked) {
        ApOnShoppingCart.selectedMethod = "google_checkout";
    }
    if ($("paymentPaypal") && $("paymentPaypal").checked) {
        ApOnShoppingCart.selectedMethod = "paypal";
    }
    if ($("paymentCreditCard") && $("paymentCreditCard").checked) {
        ApOnShoppingCart.selectedMethod = "credit_card";
    }
    if ($("paymentBankTransfer") && $("paymentBankTransfer").checked) {
        ApOnShoppingCart.selectedMethod = "bank_transfer";
    }
    if (ApOnShoppingCart.selectedMethod == "") {
        $("shoppingCartChooseMethod").style.display = "none";
        $("shoppingCartChooseMethodAlert").style.display = "inline";
        return;
    }

    $("shoppingCartChangeMethodButton").style.display = "block";
    $("shoppingCartPickMethodButton").style.display = "none";
    $("shoppingCartOnToPaymentButton").style.display = "block";

    $("shoppingCartPaymentChoices").style.display = "none";
    if (ApOnShoppingCart.selectedMethod == "google_checkout" && $("shoppingCartPaymentMethodGoogleCheckout")) {
        $("shoppingCartPaymentMethodGoogleCheckout").style.display = "block";
    } else if (ApOnShoppingCart.selectedMethod == "paypal" && $("shoppingCartPaymentMethodPaypal")) {
        $("shoppingCartPaymentMethodPaypal").style.display = "block";
    } else if (ApOnShoppingCart.selectedMethod == "credit_card" && $("shoppingCartPaymentMethodCreditCard")) {
        $("shoppingCartPaymentMethodCreditCard").style.display = "block";
    } else if (ApOnShoppingCart.selectedMethod == "bank_transfer" && $("shoppingCartPaymentMethodBankTransfer")) {
        $("shoppingCartPaymentMethodBankTransfer").style.display = "block";
    }
}

ApOnShoppingCart.ChangeMethod = function()
{
    //buttons
    $("shoppingCartChangeMethodButton").style.display = "none";
    $("shoppingCartPickMethodButton").style.display = "block";
    $("shoppingCartOnToPaymentButton").style.display = "none";
    //choices
    $("shoppingCartPaymentChoices").style.display = "block";
    //methods
    if ($("shoppingCartPaymentMethodGoogleCheckout")) {
        $("shoppingCartPaymentMethodGoogleCheckout").style.display = "none";
    }
    if ($("shoppingCartPaymentMethodPaypal")) {
        $("shoppingCartPaymentMethodPaypal").style.display = "none";
    }
    if ($("shoppingCartPaymentMethodCreditCard")) {
        $("shoppingCartPaymentMethodCreditCard").style.display = "none";
    }
    if ($("shoppingCartPaymentMethodBankTransfer")) {
        $("shoppingCartPaymentMethodBankTransfer").style.display = "none";
    }
}

ApOnShoppingCart.ProceedToPaymentGateway = function(method)
{
    if (method != "") {
        ApOnShoppingCart.selectedMethod = method;
    }
    if (ApOnShoppingCart.selectedMethod == "paypal") {
        if ($("shoppingCartPaypalForm")) {
            $("shoppingCartPaypalForm").submit();
        }
    } else if (ApOnShoppingCart.selectedMethod == "google_checkout") {
        if ($("shoppingCartGoogleCheckoutForm")) {
            $("shoppingCartGoogleCheckoutForm").submit();
        }
    }
}

 ApOnShoppingCart.increment = function(id, unitPrice)
{
    //convert to integer
    $("txtCart_" + id + "_Quantity").value++;
    if ($("shoppingCartPaypal_" + id + "_Quantity")) {
        $("shoppingCartPaypal_" + id + "_Quantity").value = $("txtCart_" + id + "_Quantity").value;
    }
    if ($("shoppingCartItems")) {
        $("shoppingCartItems").innerHTML = ($("shoppingCartItems").innerHTML - 0) + 1;
    }
    ApOnShoppingCart.updatePrice(id, unitPrice);
}

ApOnShoppingCart.decrement = function(id, unitPrice)
{
    if ($("txtCart_" + id + "_Quantity").value > 0) {
        if ($("txtCart_" + id + "_Quantity").value == 1) {
            if (!confirm("Would you like to remove this item from the cart?")) {
                return;
            }
        }
        $("txtCart_" + id + "_Quantity").value--;
        if ($("shoppingCartPaypal_" + id + "_Quantity")) {
            $("shoppingCartPaypal_" + id + "_Quantity").value = $("txtCart_" + id + "_Quantity").value;
        }
        if ($("shoppingCartItems") && ($("shoppingCartItems").innerHTML - 0) > 0) {
            $("shoppingCartItems").innerHTML = $("shoppingCartItems").innerHTML - 1;
        }
        ApOnShoppingCart.updatePrice(id, unitPrice);
    }
}

ApOnShoppingCart.updatePrice = function(id, unitPrice)
{
    var oldTotalPrice = $("txtCart_" + id + "_TotalPrice").innerHTML - 0;
    unitPrice = unitPrice - 0;
    var newQuantity = $("txtCart_" + id + "_Quantity").value - 0;
    $("txtCart_" + id + "_TotalPrice").innerHTML = newQuantity * unitPrice;
    $("txtCart_TotalCartPrice").innerHTML = ($("txtCart_TotalCartPrice").innerHTML - 0) - oldTotalPrice + newQuantity * unitPrice;
    if (newQuantity == 0) {
        ApOnAjax.updateQuantityOnCart(id, newQuantity, ApOnShoppingCart.loadShoppingCart);
    } else {
        ApOnAjax.updateQuantityOnCart(id, newQuantity);
    }
}

