//var startingHeight = new Array();
//var startingWidth = new Array();
//var currentModalClass = new Array();
var currentModalClass;
var roomForButton = new Array();

$(document).ready(function() {

    // setup the open behavior for regular modals
    $("[rel=modal]").each(function() {
        var t = $(this);
        var modalUrl = t.attr('href');
        var modalClass = t.attr('rev');
        t.attr('href', '#');
        t.removeAttr('target');
        t.click(function(event) { event.preventDefault(); event.stopPropagation(); createNewModal(modalUrl, modalClass, "Close Window"); });
    });

    // setup the open behavior for image preview modals
    $("[rel=modalPreview]").each(function() {
        var t = $(this);
        var modalUrl = t.attr('href');
        var modalClass = t.attr('rev');
        t.attr('href', '#');
        t.removeAttr('target');
        t.click(function(event) { event.preventDefault(); event.stopPropagation(); createNewModal(modalUrl, modalClass, "Close Preview"); });
    });

});

function onModalWindowResize(modalNumber) {
    // adjusts the size of the iframe
    try 
    {
        if ($("#modalFrame" + modalNumber).length > 0) 
        {
            var windowWidth = $(window).width();
            var minWidth = parseInt($("#modalFrame" + modalNumber).css('min-width'));
            var maxWidth = parseInt($("#modalFrame" + modalNumber).css('max-width'));
            var frameWidth = windowWidth;
            var modalFrame = $("#modalFrame" + modalNumber);
            var modalContent = $("#modalContent" + modalNumber);
            var newWidth = windowWidth - 41;
            if (newWidth < minWidth) { newWidth = minWidth; }
            if (newWidth > maxWidth) { newWidth = maxWidth; }

            modalFrame.width(newWidth);
            frameWidth = modalFrame.width();

            modalContent.css({ 'left': Math.max(20, windowWidth / 2 - frameWidth / 2) + 'px' });

            var windowHeight = $(window).height();
            var minHeight = parseInt(modalFrame.css('min-height'));
            var maxHeight = parseInt(modalFrame.css('max-height'));
            var frameHeight = windowHeight;

            var newHeight = (windowHeight - (41 + roomForButton[modalNumber] * 2))
            if (newHeight < minHeight) { newHeight = minHeight; }
            if (newHeight > maxHeight) { newHeight = maxHeight; }

            modalFrame.height(newHeight);
            frameHeight = modalFrame.height();

            modalContent.css({ 'top': Math.max(20, windowHeight / 2 - frameHeight / 2) + 'px' });

            $("#modalWrapper" + modalNumber + " .newModalBackground").css({ 'width': $(top.document).width(), 'height': $(top.document).height() });
        }
    }
    catch (e) {
        alert("error: " + e.message);
    }
}

function createNewModal(srcUrl, modalClass, closeButtonText) {
    if (window.top != window) {
        window.top.createNewModal(srcUrl, modalClass, closeButtonText);
    }
    else {
        var modalCollection = $("#modalCollectionContainer", top.document);
        // if the collection holder doesn't exist, insert one
        if (modalCollection.length == 0) {
            $('body', top.document).append('<div id="modalCollectionContainer"></div>');
            modalCollection = $("#modalCollectionContainer", top.document);
        }
        var modalCount = $("#modalCollectionContainer .newModal", top.document).length;
        modalCollection.append(
            '<div id="modalWrapper' + modalCount + '" class="newModal">' +
            '<div class="newModalBackground"></div>' +
            '<div id="modalContent' + modalCount + '" class="newModalContent">' +
            '<iframe id="modalFrame' + modalCount + '" class="newModalFrame"></iframe>' +
            '<br/>' +
            '<input id="modalButton' + modalCount + '" type="button" class="button newModalButton" value="' + closeButtonText + '" />' +
            '</div>' +
            '</div>'
        );

        var modButton = $('#modalButton' + modalCount, top.document);
        var modContent = $('#modalContent' + modalCount, top.document);
        var modFrame = $('#modalFrame' + modalCount, top.document);
        var modBackground = $('#modalWrapper' + modalCount + ' .newModalBackground', top.document);

        // fix up the z-indexes
        modBackground.css({ 'z-index': 1 + modalCount * 5 });
        modContent.css({ 'z-index': 2 + modalCount * 5 });

        // setup the close button
        modButton.click(function() { hideNewModal(modalCount); });

        // get the starting sizes
        var buttonMarginTop = (modButton.css('margin-top') == 'inherit' || modButton.css('margin-top') == 'auto') ? 0 : parseInt(modButton.css('margin-top'));
        var buttonMarginBottom = (modButton.css('margin-bottom') == 'inherit' || modButton.css('margin-bottom') == 'auto') ? 0 : parseInt(modButton.css('margin-bottom'));

        roomForButton[modalCount] = (
            modButton.height() +
            (buttonMarginBottom != NaN ? buttonMarginBottom : 0) +
            (buttonMarginTop != NaN ? buttonMarginTop : 0)
        );

        // setup frame source and resize behavior
        modFrame.attr('src', srcUrl);
        modContent.addClass(modalClass);
        $(window.top).resize(function() { onModalWindowResize(modalCount) });
        $(window.top).resize();
        window.top.scrollTo(0, 0);
    }
}

function hideNewModal(modalNumber) {
    // clears the src attribute for the modal frame,
    // then toggles visibility
    $('#modalWrapper' + modalNumber, top.document).remove();

    /*
    $(window).unbind("resize", onModalWindowResize);
    $("#modalWrapper").css({ 'display': 'none' });
    $("#modalFrame").attr('src', 'about:blank');
    $("#modalContent").removeClass(currentModalClass);
    */
}