﻿//move the overlay back to the middle of the screen on scrolling
var colorBoxClosing = false;

$(document).bind('cbox_complete', function () {
    parent.$(window).scroll(function () {
        setTimeout('reposition(true);', 500);
    });
    parent.$(window).resize(function () {
        setTimeout('reposition(true);', 500);
    });
});

$(document).bind('cbox_closed', function () {
    parent.$(window).unbind("scroll");
    parent.$(window).unbind("resize");
    colorBoxClosing = false;
});

$(document).bind('cbox_cleanup', function () {
    colorBoxClosing = true;
});

function reposition(animate) {
    if (!colorBoxClosing) {
        //stop any previous animation
        parent.$('#colorbox').stop();

        var win = parent.$(window);
        //only move the overlay if the overlay height is smaller than the window
        if (win.height() > parent.$('#colorbox').outerHeight()) {

            var cssTop = Math.max(win.height() - $("#cboxLoadedContent").height() - eval($("#colorbox").css("padding-bottom").replace("px", "")), 0) / 2 + win.scrollTop();
            var cssLeft = Math.max(win.width() - $("#cboxLoadedContent").width() - eval($("#colorbox").css("padding-right").replace("px", "")), 0) / 2 + win.scrollLeft();

            if (animate) {
                parent.$('#colorbox').animate({ top: cssTop, left: cssLeft }, 300);
            }
            else {
                parent.$('#colorbox').css({ top: cssTop, left: cssLeft });
            }
        }
    }
}

function setupColorBoxHeight() {
    var h;

    try {
        if ($('iframe#cboxIframe').contents().find('form').height() != null) {
            h = $('iframe#cboxIframe').contents().find('form').height();
            $.colorbox.resize({ innerHeight: h + 20 + 'px' });
        }
        else {
            h = parent.$('iframe#cboxIframe').contents().find('form').height();
            parent.$.colorbox.resize({ innerHeight: h + 20 + 'px' });
        }
    }
    catch (err) {
    }
}
