﻿//Author : Allen Chen
var popupStatusSLightBox;           //Tracks the status of popup    (Program)
var backgroundSLightBox;            //Show background               (Optional)
function showSLightBox() {
    if (popupStatusSLightBox == 0) {
        popupStatusSLightBox = 1;
        centerSLightBox();
        $("#BodySLightBox").show();
        if (backgroundSLightBox == 1) $("#BackgroundSLightBox").show();
    }
}
function hideSLightBox() {
    if (popupStatusSLightBox == 1) {
        $("#BodySLightBox").hide();
        if (backgroundSLightBox == 1) $("#BackgroundSLightBox").hide();
        popupStatusSLightBox = 0;
    }
}
function centerSLightBox() {
    if (popupStatusSLightBox == 1) {
        var windowHeight = $(window).height(); var windowWidth = $(window).width();
        var scrollTop = $(window).scrollTop(); var scrollLeft = $(window).scrollLeft();
        var popupHeight = $("#BodySLightBox").height(); var popupWidth = $("#BodySLightBox").width();
        $("#BodySLightBox").css({
            "position": "absolute",
            "top": (scrollTop >= 255) ? (windowHeight / 2 - popupHeight / 2) + scrollTop : 255,
            "left": (windowWidth / 2 - popupWidth / 2) + scrollLeft
        });
    }
}
function InitSetting() {
    popupStatusSLightBox = 0;
}
$(document).ready(function() {  //Main function
    InitSetting();
    $(document).keypress(function(e) {
        if (e.which == '27') {
            hideSLightBox();
        }
    });
//    $("#BackgroundSLightBox").click(function(){
//        hideSLightBox();
//    });
    $(window).resize(function() {
        centerSLightBox();
    });
    $(window).scroll(function() {
        centerSLightBox();
    });
});
function addOnClickRedirectURL(imgID, strUrl, blnNewWindow) {
    if (strUrl) {
        if (blnNewWindow) {
            $("#" + imgID).click(function() {
                window.open(strUrl);
                hideSLightBox();
            });
        }
        else
            $("#" + imgID).click(function() {
                window.location.href = strUrl;
                hideSLightBox();
            });
    }
    else 
    {
        $("#" + imgID).click(function() {
            hideSLightBox();
        });
    }
}
function addImage(imgID, strPath, strTitle, strCssStyleJSON) {
    $("#BodySLightBox").append("<img id='" + imgID + "' />");
    strTitle = (strTitle) ? strTitle : "";
    $("#" + imgID).attr({
        src: strPath
        , alt: strTitle
        , title: strTitle
    });
    setCSS(imgID, strCssStyleJSON);
}
function setCSS(id, strCssStyleJSON){
    $("#" + id).css(ConvertToJSON(strCssStyleJSON));
}
function setBackground(isOn){
    backgroundSLightBox = 1;
}
//Convert string to map
function ConvertToJSON(str) {
    var obj = jQuery.parseJSON(str);
    return obj;
}
