﻿

//function resizeFancyBox() {
//    $.fancybox.resize();
//}

//Kräver en del html för att kunna visa resultatet.
//Så här bör det se ut för att få det hela att fungera
//
//<a id="produktdetalj" href="#result"></a>
//<div style="display:none;">
//<div id="result">
//</div>
//</div>
function XXXresizeFancyBox2() {
    $("#bilden")
    // once the image has loaded, execute this code    
    .load(function() {
        // set the image hidden by default          
        $(this).hide();
        // with the holding div #loader, apply:      
        //        $('#loader')
        //        // remove the loading class (so no background spinner),         
        //    .removeClass('loading')
        //        // then insert our image        
        //    .append(this);
        //        // fade our image in to create a nice effect
        $(this).fadeIn();
        $.fancybox.resize();
//        alert(document.bilden.complete);
    })
    // if there was an error loading the image, react accordingly    
    .error(function() {
        // notify the user that the image could not be loaded    
    })
}

function VisaDetalj(artnr, prubid, hideBuy) {

    $.ajax({
        type: "POST",
        url: "../Search/VisaArtikelDetalj",
        data: '{"Artnr":"' + artnr + '","PrubId":' + prubid + ',"HideBuy":"' + hideBuy + '"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        processData: false,
        success: function(data) {
            $("#result").html(data.statusMessage);
            if ($("#produktdetalj").html() != null) {
                $("#produktdetalj").fancybox({
                    'titlePosition': 'inside',
                    'transitionIn': 'none',
                    'transitionOut': 'none',
                    'autoScale': true,
                    'centerOnScroll': true,
                    'showCloseButton': false,
                    'onComplete': function() {
                        $("#fancybox-outer").prepend($("#FancyboxClose").html());
                        $("#FancyboxClose").html('');
                        //                    $.fancybox.resize();
                        //                    resizeFancyBox2();
                        //                        $("#fancybox-wrap").css({ 'top': '20px', 'bottom': 'auto' });
                        //                        $("#fancybox-inner").append("<div>hello world</div>");
                    }
                });
            }
            $("#produktdetalj").click();

            //            $.fancybox.resize();
            $("#result").oneTime("1500ms", function() {
                //                $("#fancybox-content").prepend('<b><a style="font-size: x-small" href="javascript:$.fancybox.close();">Stäng fönster</a></b>')
                try {

                    if ($("#fancybox-overlay").css("display") != 'none') {
                        $.fancybox.resize();
                        //                    $("#fancybox-overlay").css('display', 'none');
                    }
                }
                catch (e) {
                    //alert('An error has occurred: ' + e.message)
                    $("#fancybox-overlay").css('display', 'none');
                    //Handle errors here
                }
                //                $.fancybox.center();
                //                $("#fancybox-wrap").css({ 'top': '20px', 'bottom': 'auto' });
            });

            //                    'modal': true,
        }
    });
}

function CloseFancy() {
    $.fancybox.close();
    return false;
}

function UppdateraKundvagn() {

     $.ajax({
         type: "POST",
         url: "../Kundvagn/AntalIKundvagn",
         data: '',
         contentType: "application/json; charset=utf-8",
         dataType: "json",
         processData: false,
         success: function(dta) {
            $("#antalikundvagn").html("Kundvagn (" + dta.antal + ")")
         },
         error: function(error) {
             alert("Fel " + error);
         }
     });
    
}
    






$(function() {

    //Detta behövs för att resiza fancyboxen 
    //när den innehåller en länk till bild sätts storleken för tidigt,
//därav resize efter att bilden laddats
    
    //BNY Kommenterar bort nedan 15 rader för test ipad
//    etm = document.getElementById("result");
//    if (etm !== null) {
//        if (typeof window.addEventListener != 'undefined') {
//            etm.addEventListener("resize", resizeFancyBox, false);
//        }
//        else if (typeof document.addEventListener != 'undefined') {
//            etm.addEventListener("resize", resizeFancyBox, false);
//        }
//        else if (typeof window.attachEvent != 'undefined') {
//            etm.attachEvent("onresize", resizeFancyBox);
//        }
//        else {
//            window.alert('Failed to attach an Event Listener\n\n Please Report');
//        }
//    }


});

