///////////////////////////////////////////////////////////
//
//  jquery-1.3.2.min.js MUST be included BEFORE this file
//
///////////////////////////////////////////////////////////
var currentModalClass;
var roomForButton = new Array();


function convertModalLinks() {

    // setup the open behavior for regular modals
    $("[rel=modal]").each(function() {
        var t = $(this);
        //alert("modal link found: "+t.attr('href'));
        var modalUrl = t.attr('href');
        var modalClass = t.attr('rev').split('|')[0];
        var loadOnClose = '';
        if (t.attr('rev').split('|').length > 1) {
            loadOnClose = t.attr('rev').split('|')[1];
        }
        t.attr('href', '#');
        t.removeAttr('target');
        t.click(function(event) { event.preventDefault(); event.stopPropagation(); createNewModal(modalUrl, modalClass, "Close Window", loadOnClose); });
        t.attr('rel','modal|created');
    });

    // 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').split('|')[0];
        var loadOnClose = '';
        if (t.attr('rev').split('|').length > 1) {
            loadOnClose = t.attr('rev').split('|')[1];
        }
        t.attr('href', '#');
        t.removeAttr('target');
        t.click(function(event) { event.preventDefault(); event.stopPropagation(); createNewModal(modalUrl, modalClass, "Close Preview", loadOnClose); });
        t.attr('rel','modalPreview|created');
    });

}

function onModalWindowResize(modalNumber) {
    // adjusts the size of the iframe
    try 
    {
        if ($("#modalFrame" + modalNumber).length > 0) 
        {
            var windowWidth = $(window).width();
            var winLeft = $(window).scrollLeft();
            var winTop = $(window).scrollTop();
            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) + winLeft) + '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) + winTop) + 'px' });

            $("#modalWrapper" + modalNumber + " .newModalBackground").css({ 'width': $(top.document).width(), 'height': $(top.document).height() });
        }
    }
    catch (e) {
        alert("error: " + e.message);
    }
}


function createNewModal(srcUrl, modalClass, closeButtonText, loadOnClose) {
    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': 1001 + modalCount * 5 });
        modContent.css({ 'z-index': 1002 + modalCount * 5 });

        // setup the close button
        modButton.click(function() { hideNewModal(modalCount, loadOnClose); });

        // 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, loadOnClose) {
    // clears the src attribute for the modal frame,
    // then toggles visibility
    $('#modalWrapper' + modalNumber, top.document).remove();
    if(loadOnClose!=null && loadOnClose!=undefined && loadOnClose!='') window.top.location.href = loadOnClose;

    /*
    $(window).unbind("resize", onModalWindowResize);
    $("#modalWrapper").css({ 'display': 'none' });
    $("#modalFrame").attr('src', 'about:blank');
    $("#modalContent").removeClass(currentModalClass);
    */
}


$(document).ready(convertModalLinks);
