var modalActive = null;
var modalCallback = null;
var modalSpeed = 150;
var modalThrobber = document.createElement("img"); //new Image();

modalThrobber.src = "/images/throbber.gif";
modalThrobber.alt = "Loading";
modalThrobber.title = modalThrobber.alt;

function queueModal(id, callback) {
    jQuery(document).ready(function(e) {
        showModal(id, callback);
    });
}

function showModal(id, callback) {
    var toggle = function() {
        var modal = jQuery("#" + id);
        var border = jQuery("#modalBorder");
        modalActive = id;
        
        if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
            border.css("opacity", "0.75");
        }
        if (border.css("position") == "absolute") {
            setScrollPosition(id);
        }
        if (modal.find(".footer").length > 0) {
            modal.height("auto");
        }
        
        calculateModalBounds(id);
        
        modal.fadeIn(modalSpeed);
        border.fadeIn(modalSpeed);
        
        if (callback != undefined || callback != null)
            modalCallback = callback;
    }
    if(modalActive != null) {
        closeModal();
        setTimeout(toggle, modalSpeed + 25); // +25 gives us a little extra time for the animation to complete.
    } else {
        toggle();
    }
}

function calculateModalBounds(id)
{
    var modal = jQuery("#" + id);
    var border = jQuery("#modalBorder");

    if (modal.find(".footer").length > 0) {
        modal.height("auto");
    }
    border.css("left", "50%");
    border.css("top", "40%");
    modal.css("left", "50%");
    modal.css("top", "40%");

    border.height(modal.height() + 40);
    border.width(modal.width() + 30);
    border.margin({ left: -(border.width() / 2), top: -(border.height() / 2) });
    modal.margin({ left: -((modal.width() / 2) + 4), top: -((modal.height() / 2)) });
}

function keepScrolled() {
    setScrollPosition("modalBorder");
    if (modalActive != null) {
        setScrollPosition(modalActive);
    }
}

function setScrollPosition(id) {
    var windowHeight = window.innerHeight || document.body.clientHeight;
    var windowWidth = window.innerWidth || document.body.clientWidth;
    jQuery("#" + id).css("left", jQuery(window).scrollLeft() + windowWidth / 2);
    jQuery("#" + id).css("top", jQuery(window).scrollTop() + windowHeight / 2);
}

function closeModal(id) { /* id is optional, if passed it will only close the modal if the current ID is active */
    if (modalActive == null || modalActive == id)
        return;
        
    jQuery("#" + modalActive).fadeOut(modalSpeed).unbind(keepScrolled);
    jQuery("#modalBorder").fadeOut(modalSpeed);
    modalActive = null;
    if (modalCallback != null) {
        callback();
    }
    modalCallback = null;
}

function certPreview(itemid) {
    /* If the itemid is over 8 characters, it's probably a encrypted key */
    var url = "/aGQaCommon/showCert.aspx?mode=front&scale=0.5&ID=" + itemid;
    
    if (itemid.toString().length > 8) {
        url = "/aGQaCommon/showCert.aspx?mode=front&scale=0.5&key=" + itemid;
    }
    
    if (jQuery("#certPreview").length == 0)
        return;
    
    var show = function() {
        var img = jQuery("#certPreviewImage");
        img.attr("src", modalThrobber.src);
        img.attr("src", url);
        showModal("certPreview");
    }
    if (modalActive != null) {
        closeModal();
        setTimeout(show, modalSpeed);
    } else {
        show();
    }
}

function previewImage(src, serial)
{
    var maxWidth = 800;

    if (jQuery("#imagePreview").length == 0)
        return;
        
    var show = function() {
        var img = jQuery("#imagePreviewImage");
        var loader = new Image();
        img.attr("src", modalThrobber.src);
        loader.onload = function() {
            img.attr("src", loader.src);
            img.attr("alt", serial);
            img.attr("title", serial);

            var aspect = parseFloat(loader.height) / parseFloat(loader.width);
            var width = Math.min(maxWidth, loader.width);
            var height = loader.width * aspect;

            img.width(width);
            img.height(height);            
            
            calculateModalBounds("imagePreview");
        }
        loader.src = src;
        showModal("imagePreview");
    };
    if (modalActive != null) {
        closeModal();
        setTimeout(show, modalSpeed);
    } else {
        show();
    }
}

jQuery(document).ready(function(e) {
    var div = document.createElement("div");
    var anchor = document.createElement("a");
    var page = jQuery("#page");
    
    div.id = "modalBorder";
    anchor.href="javascript:closeModal();";
    anchor.appendChild(document.createTextNode("Close"));
    div.appendChild(anchor);
    
    if (page.length > 0)
        page.append(jQuery(div));
    else
        document.body.appendChild(div);

    if (jQuery("#modalBorder").css("position") == "absolute") {
        jQuery(window).scroll(keepScrolled);
    }
});
