﻿var Browser = {
    Version: function () {
        var version = 999; // we assume a sane browser
        if (navigator.appVersion.indexOf("MSIE") != -1)
        // bah, IE again, lets downgrade version number
            version = parseFloat(navigator.appVersion.split("MSIE")[1]);
        return version;
    }
}


var popup1Status = 0;

//loading popup with jQuery magic!  
function loadPopup() {
    //loads popup only if it is disabled  
    if (popup1Status == 0) {
        if (Browser.Version() > 6) {
            $("#backgroundPopup").css({
                "opacity": "0.7"
            });
            $("#backgroundPopup").fadeIn("slow");
        } else {
            fixTheIE6Bugs(1, 1);
        }
        $("#popupContact").fadeIn("slow");
        popup1Status = 1;
    }
}

function fixTheIE6Bugs(status) {
    if (status == 1) {
        // Activate a iFrame for fixing some bugs in IE6
        var windowWidth = getSize()[0];
        var windowHeight = getSize()[1];
        var scrollX = getScrollXY()[0];
        var scrollY = getScrollXY()[1];

        var popupHeight = $("#popupContact").height();
        var popupWidth = $("#popupContact").width();
       
        $("#ie6fixFrame").css({
            "position": "absolute",
            "top": (windowHeight / 2 - popupHeight / 2) + 0,
            "left": (windowWidth / 2 - popupWidth / 2) + 0,
            "height": popupHeight,
            "width": popupWidth,
            "display": "block"
        });
        $("#ie6fixFrame").show();
    } else {
        $("#ie6fixFrame").fadeOut("fast");
    }
}

//disabling popup with jQuery magic!  
function disablePopup() {
    //disables popup only if it is enabled  
    if (popup1Status == 1) {
        if (Browser.Version() > 6) {
            $("#backgroundPopup").fadeOut("slow");
        } else {
            fixTheIE6Bugs(0, 1);
        }
        $("#popupContact").fadeOut("slow");
        $('#picviewer_image').attr("src", "images/ajax-loader.gif");
        popup1Status = 0;
    }
}

function getSize() {
    var myWidth = 0, myHeight = 0;

    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    return [myWidth, myHeight];
}

function getScrollXY() {
    var scrOfX = 0, scrOfY = 0;

    if (typeof (window.pageYOffset) == 'number') {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    return [scrOfX, scrOfY];
}


//centering popup  
function centerPopup() {
    //request data for centering
    var windowWidth = $(window).width();
    var windowHeight = $(window).height();
    var scrollX = getScrollXY()[0];
    var scrollY = getScrollXY()[1];
    var popupHeight = $("#popupContact").height();
    var popupWidth = $("#popupContact").width();
    //centering  
    $("#popupContact").css({
        "position": "fixed",
        "top": (windowHeight / 2 - popupHeight / 2) + 0,
        "left": (windowWidth / 2 - popupWidth / 2) + 0
    });
    //only need force for IE6  

    $("#backgroundPopup").css({
        "height": windowHeight
    });
}

$(document).ready(function () {

    //LOADING POPUP
    //Click the button event!
    $("#popupbutton").click(function () {
        //centering with css
        centerPopup();
        //load popup
        loadPopup();
    });



    //CLOSING POPUP
    //Click the x event!
    $("#popupContactClose").click(function () {
        disablePopup();
    });
    //Click out event!
    $("#backgroundPopup").click(function () {
        disablePopup();
    });   
    $("#disablePopupButton").click(function () {
        disablePopup();
    });
    //Press Escape event!
    $(document).keypress(function (e) {
        if (e.keyCode == 27) {
            disablePopup();
        }
    });
});

function showPopup(imagePath, imageURL, thbLast, thbCurrent, thbNext, sizes, imgFormat) {
    //centering with css
    if ($(window).width() > 1400) {
        var popupwidth = $(window).width() - 400
        $("#popupContact").css({
            "height": $(window).height() - 60,
            "width": popupwidth
        });
    } else {
        var popupwidth = $(window).width() - 150
        $("#popupContact").css({
            "height": $(window).height() - 60,
            "width": popupwidth
        });
    }
     
    $('#picviewer_image').attr("src", imagePath + imageURL);

    if (popupwidth >= 700 && popupwidth < 1024) {
        if (sizes >= 1) {
            $('#picviewer_image').attr("src", imagePath + "m_" + imageURL);
        }
    } else if (popupwidth >= 1024 && popupwidth < 2000) {
        if (sizes >= 2) {
            if (imgFormat == 0) {
                $('#picviewer_image').css("width", "100%");
            } else {
                $('#picviewer_image').css("height", "100%");
            }
            $('#picviewer_image').attr("src", imagePath + "b_" + imageURL);
        } else if (sizes == 1) {
            $('#picviewer_image').attr("src", imagePath + "m_" + imageURL);
        }
    } else if (popupwidth > 2000) {
        if (sizes >= 3) {
            if (imgFormat == 0) {
                $('#picviewer_image').css("width", "100%");
            } else {
                $('#picviewer_image').css("height", "100%");
            }
            $('#picviewer_image').attr("src", imagePath + "h_" + imageURL);
        } else if (sizes == 2) {
            $('#picviewer_image').attr("src", imagePath + "b_" + imageURL);
        } else if (sizes == 1) {
            $('#picviewer_image').attr("src", imagePath + "m_" + imageURL);
        }
    }



    if (thbLast != "") {
        $('#picviewer_imgLast').attr("src", imagePath + thbLast);
    } else {
        $('#picviewer_imgLast').css({ "display": "none" });
    }
    $('#picviewer_imgCurrent').attr("src", imagePath + thbCurrent);
    if (thbNext != "") {
        $('#picviewer_imgNext').attr("src", imagePath + thbNext);
    } else {
        $('#picviewer_imgNext').css({ "display": "none" });
    }
    centerPopup();
    //load popup
    loadPopup();
}

