/* Greybox Redux
* Required: http://jquery.com/
* Written by: John Resig
* Based on code by: 4mir Salihefendic (http://amix.dk)
* License: LGPL (read more in LGPL.txt)
*/

var GB_DONE = false;
var GB_TOTAL_BORDER_WIDTH = 12;
var GB_HEIGHT;
var GB_WIDTH;
var GB_ANIMATION = true;

function GB_show(caption, url, width, height) {
    GB_HEIGHT = height || $(window).height() - 20;
    GB_WIDTH = width || $(window).width();
    $("#GB_overlay").remove();
    $(document.body).append("<div id='GB_overlay'></div><div id='GB_window'><div id='GB_caption'></div><div id='GB_close'>Close</div></div>");
    $("#GB_close").click(GB_hide);
    if (!GB_DONE) {
        $(window).resize(GB_position);
        GB_DONE = true;
    }

    // show loading
    //$("#backgroundPopup,#loading").remove();
    //$("#GB_window").append("<div id='backgroundPopup'></div><div id='loading'>Loading...</div>");

    // Add the background fade and the loading message to cover the iframe until it is done loading
    $("#GB_loading,#GB_loadingBg").remove();
    $("#GB_window").append("<div id='GB_loading'>Loading...</div>");
    $("#GB_window").append("<div id='GB_loadingBg'></div>");
    $("#GB_loadingBg").fadeTo(1, .6);

    $("#GB_frame").remove();
    $("#GB_window").append("<iframe id='GB_frame' frameborder='0' src='" + url + "'></iframe>");
    $("#GB_caption").html(caption);
    GB_position();
    $("#GB_overlay").fadeTo(200, .6);

    // when the content is loaded, hide the loading message
    $("#GB_frame").load(GB_hideLoading);

    if (GB_ANIMATION)
        $("#GB_window").slideDown("slow");
    else
        $("#GB_window").show();

    return false;
}

function GB_ShowCustomContent(caption, content, width, height) {
    GB_show(caption, '/js/greybox/blank.htm', width, height);
    $("#GB_frame").remove();
    $("#GB_window").append('<div id="GB_frame" class="GB_customContent">' + content + '</div>');
    $("#GB_frame").width(width - ($('#GB_frame').outerWidth(true) - $('#GB_frame').width()));
    GB_position();
    GB_hideLoading();
}

function GB_ShowCustomContentNoHeader(content, width, height) {
    GB_ShowCustomContent('', content, width, height);
    GB_HideHeader();
}

function GB_ShowNoHeader(caption, url, width, height) {
    GB_show(caption, url, width, height);
    GB_position();
    GB_hideLoading();
    GB_HideHeader();
}

function GB_HideHeader() {
    $("#GB_caption").remove();
    $("#GB_close").remove();
    $('#GB_frame').css('height', GB_HEIGHT + 'px');
}

function GB_hide() {
    $("#GB_window,#GB_overlay").hide();
}

function GB_hideLoading(e) {
    $("#GB_loading").fadeTo(300, 0, function (e) { $("#GB_loading").hide() });
    $("#GB_loadingBg").fadeTo(300, 0, function (e) { $("#GB_loadingBg").hide() });
}

function GB_position() {
    var de = document.documentElement;

    $('#GB_window').css({
        'width': GB_WIDTH + 'px',
        'height': GB_HEIGHT + 'px',
        'left': (($(window).width() - GB_WIDTH - GB_TOTAL_BORDER_WIDTH) / 2) + 'px',
        'top': (($(window).height() - GB_HEIGHT - GB_TOTAL_BORDER_WIDTH) / 2) + 'px'
    });
    $('#GB_frame').css('height', GB_HEIGHT - 32 + 'px');
    $('#GB_overlay').css({ 'width': $(window).width() + 'px', 'height': $(window).height() + 'px' });

    $('#GB_loading').css({
        'top': ($('#GB_window').height() - 28) / 2 + 'px',
        'left': ($('#GB_window').width() - 58) / 2 + 'px'
    });
    $('#GB_loadingBg').css({ 'height': GB_HEIGHT - 32 + 'px', 'top': '32px' });

    // if IE6... we need to specifically position the popup
    if ($.browser.msie && $.browser.version <= 6) {
        $('#GB_overlay').css({ 'top': $(window).scrollTop() + 'px' });
        var iTopMargin = $(window).scrollTop() > 0 ? 19 : 0;
        $('#GB_window').css({ 'top': ($(window).scrollTop() + iTopMargin) + 'px' });
    }
}

// MoJo Custom Functions
function CloseGB() {
    GB_hide();
}

function CloseGBandPost() {
    GB_hide();

    // based on which button was clicked
    if (curID == '')
        alert('Unknown error. Please try again.');
    else
        causePostback(curID);
}

function CloseGBandRedirect(url) {
    if (window.top.location != window.location)
        GB_hide();

    window.location = url;

    return false;
}

function CloseGBandRefresh() {
    if (window.top.location != window.location)
        GB_hide();

    window.location = window.location;

    return false;
}

function causePostback(id) {
    eval($('#' + id).attr('href'));
}

$(function () {
    if ($.browser.msie && $.browser.version <= 6) {
        $(window).scroll(GB_position);
    }

    $('a.greybox').click(function ()
    {
        if (!$(this).hasClass('maintenance'))
            GB_show(this.title, this.href, 800);
        return false;
    });


    $('a.greybox.maintenance').click(function ()
    {
        var t = this.title || $(this).text() || this.href;
        GB_show("Please do NOT use your browser's Back button while editing content", this.href, 960);
        return false;
    });
});

