﻿//---------------------------------------------
// JetshopjQuery.js 
// Copyright (C) Jetshop AB 2010-12-16
//---------------------------------------------
$(document).ready(function () {
    klarnaKontoInfoPopup();

    $('.divModalUpdateContainer').corner('8px');
    $('.divModalUpdateContainerLogin').corner('8px');
    $('.StatusOrderBtnActive').corner('5px');

    if ($('.fastcartconfirmationpage').length) {
        $('.fastcartconfirmationpage .iteminfo-comment > span').tooltip({
            track: true,
            delay: 0,
            showURL: false,
            showBody: " - ",
            extraClass: "pretty",
            fixPNG: true,
            left: -120
        });
    }

    $('.upFastCart').panelReady(function () { /* Måste finnas */
        $('.emptycart .optionslist a').corner('8px');
    });

    $('.upFastFreightSelector').panelReady(function () { /* Måste finnas */

        $('.freight-cost').corner('5px');

        $(function () {
            $('.freight-name').each(function () {
                if ($(this).height() > 15) {
                    $(this).css('top', '16px');
                }
            });
        });

        $('.optionbox-nooptions,.optionbox-nooptions-selected').tooltip({
            track: false,
            delay: 500,
            showURL: false,
            showBody: " - ",
            extraClass: "testertester",
            fixPNG: true,
            left: 0
        });

    });


    $('.upFastCartSummary').panelReady(function () { /* Måste finnas */
        $('.FastCartSummaryDiscountTitleCssClass').tooltip({
            track: true,
            delay: 0,
            showURL: false,
            showBody: " - ",
            extraClass: "pretty",
            fixPNG: true,
            left: -120
        });


    });
    $('.upFastOrderConfirmation').panelReady(function () { /* Måste finnas */
        $(function () {
            $('.fast-submit-btn-text').each(function () {
                if ($(this).height() > 18) {
                    $(this).css('padding-top', '13px');
                }
            });
        });
    });
    $('.upFastDiscount').panelReady(function () {
        // Active form field
        $('input[type="text"],input[type="password"]').addClass("idleField");
        $('input[type="text"]').focus(function () {
            $(this).removeClass("idleField").addClass("focusField");
        });
        $('input[type="text"],input[type="password"]').blur(function () {
            $(this).removeClass("focusField").addClass("idleField");
        });
    });

    //
    $('.upFastCartDetails').panelReady(function () {
        $('.btn-delete').hide();
        $('[id$=_divGrandtotal]').corner('5px');
        $('.fast-cart-summary').corner('5px');
        $('.amounterror-inner').corner('3px');

        $('.iteminfo-comment > span').tooltip({
            track: true,
            delay: 0,
            showURL: false,
            showBody: " - ",
            extraClass: "pretty",
            fixPNG: true,
            left: -120
        });

        $('.basketitem').hover(function () {
            var active = $(this);
            $('.btn-delete', active).stop(true, true).fadeIn(1000);
            $('.btn-delete', active).click(function () {
                $(active).slideUp('slow');
                $(active).removeClass('basketitem');
                $(active).addClass('basketitem-hidden');
            });
        }, function () {
            $('.btn-delete', this).hide();
        });
    });

    $('.upGetCustomerInfo').panelReady(function () {
        $('input[type="text"],input[type="password"]').addClass("idleField");
        $('input[type="text"]').focus(function () {
            $(this).removeClass("idleField").addClass("focusField");
        });
        $('input[type="text"],input[type="password"]').blur(function () {
            $(this).removeClass("focusField").addClass("idleField");
        });
    });
    
    //Extra addresrow
    $('.upCustomerInfoLeftPart').panelReady(function () {

        $('[id$=_pCOAddress].showthis').slideToggle(300, function () {
            $(this).removeClass('showthis');
        });

        $('[id$=_pCOAddress].hidethis').slideToggle(300, function () {
            $(this).removeClass('hidethis');
        });

        $('[id$=_pAlternativeCOAddress].showthis').slideToggle(300, function () {
            $(this).removeClass('showthis');
        });

        $('[id$=_pAlternativeCOAddress].hidethis').slideToggle(300, function () {
            $(this).removeClass('hidethis');
        });

        $('[id$=_pnlAlternativeDeliveryAddress].showthis').slideToggle(300, function () {
            $(this).removeClass('showthis');
        });

        $('[id$=_pnlAlternativeDeliveryAddress].hidethis').slideToggle(300, function () {
            $(this).removeClass('hidethis');
        });

        /*
        $('div.showthis').slideToggle(300, function () {
            //var altheight = $('.showthis').hide().height();
            $('div.showthis').removeClass('showthis');
            $('html, body').delay(500).animate({ scrollTop: '+=' + (altheight + 200) }, 500);
        });

        $('div.hidethis').slideToggle(300, function () {
            //var altheight = $('.showthis').hide().height();
            $('div.hidethis').delay(1500).removeClass('hidethis');
            //$('html, body').animate({ scrollTop: '-=' + (altheight + 300) }, 500);
        });
        */

        $('.subtlebutton').corner('5px');

        // Active form field - LEFT COL
        $('input[type="text"],input[type="password"]').addClass("idleField");
        $('input[type="text"]').focus(function () {
        $(this).removeClass("idleField").addClass("focusField");
        });

        $('input[type="text"],input[type="password"]').blur(function () {
        $(this).removeClass("focusField").addClass("idleField");
        });

    });
    

    $('.upFastCartComment').panelReady(function () {
        $('[id$=_pnlPasswords].showthis').slideToggle(300, function () {
            $('[id$=_pnlPasswords].showthis').removeClass('showthis');
        });

        $('[id$=_pnlPasswords].hidethis').slideToggle(300, function () {
            $('[id$=_pnlPasswords].hidethis').removeClass('hidethis');
        });

        // Active form field - RIGHT COL
        $('input[type="text"],input[type="password"]').addClass("idleField");
        $('input[type="password"]').focus(function () {
            $(this).removeClass("idleField").addClass("focusField");
        });
        $('input[type="text"],input[type="password"]').blur(function () {
            $(this).removeClass("focusField").addClass("idleField");
        });

    });

    //Helptabs
    $('.help').hide();

    //Helptabs
    $('.box:not(.emptycart)').mouseover(function () {
        $('.boxinner', this).css('border-color', '#cfd8f0');
        $('.help', this).show();
    }).mouseout(function () {
        $('.boxinner', this).css('border-color', '#cccccc');
        $('.help', this).hide();
    });

    //Fancybox
    $('.fancybox').fancybox();
    $('.fancybox-iframe').fancybox({ type: 'iframe' });
    $('.fancybox-iframe-login').fancybox({ type: 'iframe', height: 300 });

    //PanelReady
    $('.updatepanel').panelReady(function (el, b, c) {

        //Helptabs
        $('.help').hide();

        //Corner
        $('.grandtotal, .getinfo, .loggain').corner('5px');
        $('.emptycart .optionslist a').corner('8px');


        //Fancybox
        $('.fancybox', el).fancybox();
        $('.fancybox-iframe', el).fancybox({ type: 'iframe' });
        $('.fancybox-iframe-login', el).fancybox({ type: 'iframe', height: 300 });
        $('.fancybox-iframe-terms').fancybox({ type: 'iframe', height: 460 });

        //Box hover
        $('.box', el).mouseover(function () {
            $('.boxinner', this).css('border-color', '#cfd8f0');
            $('.help', this).show();
        }).mouseout(function () {
            $('.boxinner', this).css('border-color', '#cccccc');
            $('.help', this).hide();
        });

        //Fit checkout button-texts
        var o = $('.upFastCountrySelect .optionslist a span, .upFastPaymentSelect .optionslist a span').not('.flagicon, .paymenticon');
        fit(o, 120, 15);
    });

    $('.upFastLogin').panelReady(function () {
        $('form:first *:input[type!=hidden]:first').focus();

        if ($('#reloadparent').length) {
            if (location.protocol == 'https:') {
                var url = window.location.search.substring(1);
                url = url.replace('q=', '');
                parent.location.href = url;
            }
            else {
                parent.location.reload();
            }
        }
    });



    /*
    var effectSlidePaymentOptions = false;
    $('.upFastPaymentSelect').panelUpdated(function () {
    if (effectSlidePaymentOptions) {
    $('.payment-options .payment-options-inner').fadeOut().fadeIn();
    effectSlidePaymentOptions = false;
    }
    });
    */

    /*******************************
    *            EVENTS            *
    *******************************/

    //PaymentOptions_Click
    $('.paymentoptionbox-item1,.paymentoptionbox-item2,.paymentoptionbox-item3,.paymentoptionbox-item4,.paymentoptionbox-item5').live('click', function () {
        var paymentoptionclicked = $(this);
        //effectSlidePaymentOptions = true;
    });


    $(".external_search_provider_textbox").autocomplete({
        source: function (request, response) {

            $.ajax({
                type: "POST",
                url: "/Services/ExternalSearchWS.asmx/GetAutoComplete",
                data: '{ searchPhrase:  "' + $(".external_search_provider_textbox").val() + '" }',
                contentType: "application/json; charset=utf-8",
                dataType: "json",

                success: function (data) {
                    if (data.d.trim() == "") {
                        response();
                    } else {
                        splitted = data.d.trim().split("\r\n");
                        response(splitted);
                    }
                }
            });
        },
        close: function (event, ui) {
            if (event.type == "autocompleteclose") {
                if (event.which == 1 || event.which == 13) {
                    /*pageSize = getGeneralSetting("searchpagesize");*/
                    location.href = document.getElementById('ctl00_searchBox_SearchBoxButton').href;
                    /*DoSearch('ShowSearchResult.aspx', 'ctl00_searchBox_tbSearch', alertText, 'True', pageSize);*/
                }
            }
        }
    });

});

/*******************************
*            METHODS           *
*******************************/

function fit(o, w, h) {
    o.each(function () {
        if (notFit($(this), w, h))
            $(this).css('font-size', '9px');

        if (notFit($(this), w, h)) {
            for (var i = 0; i < 100; i++)
                if (notFit($(this), w, h))
                    $(this).html($(this).html().substring(0, $(this).html().length - 5).trim() + '...');
                else
                    break;
        }
    });
}

function notFit(o, w, h) {
    return (o.width() > w || o.height() > h);
}

function loadjQueryFix() {
    klarnaKontoInfoPopup();
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
}

function EndRequestHandler() {
    klarnaKontoInfoPopup();
}

function klarnaKontoInfoPopup() {
    var popupStatus = 0;
    var popupCartStatus = 0;

    $("div.popupInfoBoxLink").click(function () {
        var leftPos = parseFloat($(this).offset().left);
        var topPos = parseFloat($(this).offset().top) + 25;

        var offset = $(window).width() - (leftPos + $("div.popupInfoBox").width());

        if (offset < $("div.popupInfoBox").width()) {
            leftPos = leftPos - $("div.popupInfoBox").width();
        }

        $("div.popupInfoBox").stop()
		.animate({ opacity: 100 }, "fast")
		.css("display", "block")
		.css("left", leftPos)
		.css("top", topPos)

        $.ajax({
            type: "POST",
            url: "/Services/General.asmx/GetKlarnaKontoInfoBoxText",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                $("div.popupInfoBoxContent").html(msg.d);
            }
        })

        popupStatus = 1;

//    }, function () {
//        $("div.popupInfoBox").stop().fadeTo(650, 1, function () {
//            //$(this).fadeOut("fast");
//        })
    });

    $("div.popupInfoBoxLinkClose").click(function () {
        $("div.popupInfoBox").fadeOut("fast");
    });

    $("div.popupInfoBoxLinkCart").click(function () {

        var leftPos = parseFloat($(this).offset().left);
        var topPos = parseFloat($(this).offset().top) + 25;

        var offset = $(window).width() - (leftPos + $("div.popupInfoBoxCart").width());

        if (offset < $("div.popupInfoBoxCart").width()) {
            leftPos = leftPos - $("div.popupInfoBoxCart").width();
        }

        $("div.popupInfoBoxCart").stop()
		.animate({ opacity: 100 }, "fast")
		.css("display", "block")
		.css("left", leftPos)
		.css("top", topPos)

        $.ajax({
            type: "POST",
            url: "/Services/General.asmx/GetKlarnaKontoInfoBoxText",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                $("div.popupInfoBoxContentCart").html(msg.d);
            }
        })

        popupCartStatus = 1;

//    }, function () {
//        $("div.popupInfoBoxCart").stop().fadeTo(650, 1, function () {
//            $(this).fadeOut("fast");
//        })
    });

    $("div.popupInfoBoxLinkCloseCart").click(function () {
        $("div.popupInfoBoxCart").fadeOut("fast");
    });

    $('body').click(function() {
        if (popupStatus == 1) {
            popupStatus = 2;
        }
        else if (popupStatus == 2) {
            $("div.popupInfoBox").fadeOut("fast");
            popupStatus = 0;
        }
    });

    $(window).resize(function() {
        $("div.popupInfoBox").fadeOut("fast");
    });

    $('body').click(function () {
        if (popupCartStatus == 1) {
            popupCartStatus = 2;
        }
        else if (popupCartStatus == 2) {
            $("div.popupInfoBoxCart").fadeOut("fast");
            popupCartStatus = 0;
        }
    });

    $(window).resize(function () {
        $("div.popupInfoBoxCart").fadeOut("fast");
    });
}

function notifySelectToExternalSearchProvider(productID) {

    $.ajax({
        type: "POST",
        url: "/Services/ExternalSearchWS.asmx/Notify_Select",
        data: '{ productID:  "' + productID + '" }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            /*alert('notify select');*/
        }
    });
}

function notifyPurchaseToExternalSearchProvider(productID) {

    $.ajax({
        type: "POST",
        url: "/Services/ExternalSearchWS.asmx/Notify_Purchase",
        data: '{ productID:  "' + productID + '" }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            /*alert('notify purchase');*/
        }
    });
}


