﻿$(document).ready(function () {
    $("#divBasket").qtip({
        content: '0 Items in basket.',
        position: { target: $("#divBasket"), corner: { target: 'rightMiddle' }, adjust: { x: -235, y: -20} },
        style: {
            tip: 'rightMiddle' // Notice the corner value is identical to the previously mentioned positioning corners
        }
    });

    $("input[id$='btnAddToCart']").qtip({
        content: {
            text: 'Item added to basket successfully. <a href="Basket.aspx">View Basket</a>',
            title: {
                text: 'Item added',
                button: 'Close'
            }
        },
        show: false, // We want to show only dynamically
        hide: true,
        position: { target: $("input[id$='btnAddToCart']"), corner: { target: 'leftMiddle' }, adjust: { x: 0, y: -70} },
        style: {
            tip: 'leftMiddle' // Notice the corner value is identical to the previously mentioned positioning corners
        }
    });

    $("input[id$='btnAddToCartPopup']").qtip({
        content: {
            text: 'Item added to basket successfully. <a href="Basket.aspx">View Basket</a>',
            title: {
                text: 'Item added',
                button: 'Close'
            }
        },
        show: false, // We want to show only dynamically
        hide: true,
        position: { target: $("input[id$='btnAddToCartPopup']"), corner: { target: 'rightMiddle' }, adjust: { x: -450, y: -80} },
        style: {
            tip: 'rightMiddle' // Notice the corner value is identical to the previously mentioned positioning corners
        }
    });

    // Update initial basket count
    updateBasketCount();
});

// Updates shopping basket with added item guid
function updateBasket(calledFromPopup) {

    // Show spinner
    $("#imgBasket").hide();
    $("#imgSpinner").show();

    // Set hidden field depends where this was called from
    if (calledFromPopup)
        $('#hdnAddedToBasketViaPopup').val("true");
    else
        $('#hdnAddedToBasketViaPopup').val("false");

    $guid = $("input[id$='hdnGUID']")
    var quantity = 1; // Default to 1 for now
    
    // Convert to JSON
    var list = [$guid.val(), quantity];
    var jsonText = JSON.stringify({ list: list });

    CallService("Services/BasketServ.asmx/AddItemToCart", jsonText, updateBasketSuccess, updateBasketError);
}

function updateBasketSuccess(result) {
    updateBasketCount();

    // Show tooltip
    if ($('#hdnAddedToBasketViaPopup').val() == "true")
        togglePurchaseTooltip($("input[id$='btnAddToCartPopup']"), true);
    else
        togglePurchaseTooltip($("input[id$='btnAddToCart']"), true);
}

function togglePurchaseTooltip(element, show) {
    qtip = element.qtip('api');

    if (show)
        qtip.show();
    else
        qtip.hide();
}

function updateBasketError(result) {
    resetBasketImage();
}

function resetBasketImage() {
    $("#imgSpinner").hide();
    $("#imgBasket").show();
}

function updateBasketCount() {
    // Show spinner
    $("#imgBasket").hide();
    $("#imgSpinner").show();

    CallService("Services/BasketServ.asmx/GetBasketCount", null, getBasketCountSuccess, getBasketCountError);
}

function getBasketCountSuccess(result) {
    if (result && result.d) {

        $label = "Items";
        if (result.d == 1)
            $label = "Item";

        $("a[id$='hypViewBasket']").text(result.d + " " + $label);

        // Update tooltip
        $("#divBasket").qtip('option', 'content.text', result.d + ' ' + $label + ' in basket.');
    }

    // Hide spinner
    resetBasketImage();
}

function getBasketCountError(result) {
    /* Nothing yet */
}
