﻿// Lightbox functionality

$(document).ready(function () {

    // Create the overlay (background fade) element
    $(".page").append("<div class=\"overlay\"></div>");
    $(".overlay").height($("body").height());

    $('.popup').click(function () {
        var target = $(this).attr("href");

        var elem;
        elem = $(GetUrlVars(target)[0]);
        
        if (target == "#fullBio")
            OpenLightbox(elem);
        else
            OpenLightbox(elem, "center");

        return false;
    });

    $('.close').click(function () {
        close_box();
    });

    $('.overlay').click(function () {
        close_box();
    });

});

function OpenLightbox(elem, center) {
    if (jQuery.type(elem) == "string")
        elem = $("#" + elem);
    $('.overlay').animate({ 'opacity': '.70' }, 300, 'linear');
    elem.animate({ 'opacity': '1' }, 300, 'linear');
    $('.overlay').css('display', 'block');
    elem.css('display', 'block');

    if (center == 'center') {
        elem.css({
            "left": "50%",
            "top": "50%",
            "margin-left": -(elem.width() * 0.5),
            "margin-top": -(elem.height() * 0.5)
        });
    }
}

function QuickDialog(msg, title) {
    var elem = $("#_quickDialog");

    if (title != null)
        elem.append("<h1>" + title + "</h1>");

    elem.append("<p>" + msg + "</p>");
    OpenLightbox(elem, "center");
}

function close_box()
{
    $('.overlay, .lightbox, .lightboxImage').animate({ 'opacity': '0' }, 300, 'linear', function () {
        $('.overlay, .lightbox, .lightboxImage').css('display', 'none');
        $("#_quickDialog").empty();
    });
}

function GetUrlVars(url) {
    return url.slice(url.indexOf('#')).split(/[&?]{1}[\w\d]+=/);
}
