﻿function openWin(url, productId, allowPutInBag, elm, doRedirect, isQuickBuy) {
    if (url.indexOf('.aspx') > -1) {
        var oWnd = radopen(url + "?product=" + productId + "&allowPutInBag=" + (allowPutInBag === true) + "&isQuickBuy=" + (isQuickBuy === true), null);
        window.top.dontRedirect = true;
        oWnd.set_behaviors(Telerik.Web.UI.WindowBehaviors.Pin + Telerik.Web.UI.WindowBehaviors.Move + Telerik.Web.UI.WindowBehaviors.Close + Telerik.Web.UI.WindowBehaviors.Resize + Telerik.Web.UI.WindowBehaviors.Maximize);
    } else {
        // Old style:
        var oWnd = radopen("/online-shop/prisliste.aspx?product=" + url + "&allowPutInBag=" + (productId == true) + "&isQuickBuy=" + (isQuickBuy === true), null);
        window.top.dontRedirect = true;
        oWnd.set_behaviors(Telerik.Web.UI.WindowBehaviors.Pin + Telerik.Web.UI.WindowBehaviors.Move + Telerik.Web.UI.WindowBehaviors.Close + Telerik.Web.UI.WindowBehaviors.Resize + Telerik.Web.UI.WindowBehaviors.Maximize);
    }
}

function OnClientclose(sender, eventArgs) {

}

function GoToBag() {
    window.top.location.href = "/indkoebskurv.aspx";
}

function CloseWindow() {
    try {


        var oWindow = null;
        if (window.radWindow) oWindow = window.radWindow;
        else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;

        var args = new Object();
        args.value = true;

        oWindow.close(args);
    } catch (e) {

    }
}

function PutInBag(prodId, countryId, amount) {
    var res = false;
    (function ($) {
        $.ajax({
            type: "POST",
            url: "/Handlers/Shop.asmx/AddToBag",
            data: '{ "prodId": "' + prodId + '", "countryId": "' + countryId + '", "amount":' + amount + '}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                res = msg.d == true;

                if (window.top.dontRedirect) {
                    window.top.jQuery('#updateBagBtn').click();
                    window.top.showToolTip();
                }
            },
            async: false
        });
    })(window.top.jQuery.noConflict());

    return res;
}

function showToolTip(e) {
    var toolTipId = jQuery('#toolTipId');
    if (toolTipId.length == 1) {
        setTimeout(function () {
            $find(toolTipId.text()).show();

            toolTipId.remove();
        }, 100);
    } else {
        setTimeout(showToolTip, 100);
    }
};

(function ($) {
    $(document).ready(function () {
        var BuyMoreBTN = $('#BuyMoreBTN'), ToBasketBTN = $('#ToBasketBTN'), ToFrontLNK = $('#ToFrontLNK');
        if (BuyMoreBTN.length === 1 && ToBasketBTN.length === 1 && ToFrontLNK.length === 1) {
            BuyMoreBTN.click(function (e) {
                e.preventDefault();
                CloseWindow();
            });
            ToBasketBTN.click(function (e) {
                e.preventDefault();
                GoToBag();
            });
            ToFrontLNK.click(function (e) {
                e.preventDefault();
                window.top.location.href = this.href;
            });
        };

        $('table.selectTable').on('click','a.button.selectForQuickBuy',function(e){
        var self = $(this), itemId = $('input[id*=pid]').val(), amount = self.closest('tr').find('td:eq(0)').text();
        window.top.jQuery.QuickBuy.UpdateItem(itemId,amount);
        CloseWindow();
        })
    });

} (jQuery.noConflict()));
