// Spin2 Fade popup support
// Displays a popup over a faded HTML document
// Example use:
//<script type="text/javascript" src="js/yui/build/yahoo/yahoo.js"></script>
//<script type="text/javascript" src="js/yui/build/dom/dom.js"></script>
//<script type="text/javascript" src="js/yui/build/event/event.js"></script>
//<script type="text/javascript" src="js/yui/build/animation/animation.js"></script>
//<script type="text/javascript" src="js/fade_popup.js"></script>
//
//<a href="#" onclick="show_fade_popup('This is a test',300,100);return false;">Click Here!</a>


// To use, include this javascript and the YUI javascript files
var fade_background = false;
var fade_popup = false;
var fade_popup_table = false;
var fade_popup_content_cell = false;
var fade_popup_close_cell = false;

function show_fade_popup(html, w, h, close_img)
{
    if (!fade_popup)
    {
        // Needed to allow background DIV to always cover everything
        document.body.style.height = "100%";

        fade_background = document.createElement("DIV");
        fade_background.id = "wdFadeBackground";
        fade_background.style.position = "absolute";
        fade_background.style.width = YAHOO.util.Dom.getDocumentWidth() + "px";
        fade_background.style.height = YAHOO.util.Dom.getDocumentHeight() + "px";
        fade_background.style.left = "0px";
        fade_background.style.top = "0px";
        fade_background.style.visibility = "hidden";
        fade_background.style.opacity = "0";
        if (typeof fade_background.style.filter == "string")
            fade_background.style.filter = "alpha(opacity:0)";
        fade_background.style.cursor = "pointer";
        fade_background.onclick = hide_fade_popup;
        document.body.appendChild(fade_background);

        fade_popup = document.createElement("DIV");
        fade_popup.id = "wdFadePopup";
        fade_popup.style.position = "absolute";
        fade_popup.style.visibility = "hidden";
        document.body.appendChild(fade_popup);

        fade_popup_table = document.createElement("TABLE");
        fade_popup_table.id = "wdFadeTable";
        fade_popup.appendChild(fade_popup_table);

        var row;
        row = fade_popup_table.insertRow(-1);
        fade_popup_content_cell = row.insertCell(-1);
        fade_popup_content_cell.id = "wdFadeContent";

        row = fade_popup_table.insertRow(-1);
        fade_popup_close_cell = row.insertCell(-1);
        fade_popup_close_cell.id = "wdFadeClose";
    }

    window.onresize = resize_fade_popup;

    close_html = "<a href='#' onclick='hide_fade_popup();return false;'>";
    if (close_img)
        close_html += "<img src='" + close_img + "' border='0' alt='Close'>";
    else
        close_html += "CLOSE";
    close_html += "</a>";

    fade_popup_content_cell.innerHTML = html;
    fade_popup_content_cell.style.width = w + "px";
    fade_popup_content_cell.style.height = h + "px";

    fade_popup_close_cell.innerHTML = close_html;

    fade_popup_table.style.width = w + "px";

    var table_region = YAHOO.util.Dom.getRegion(fade_popup_table);
//    fade_popup.style.width = w + "px";
//    fade_popup.style.height = (table_region.bottom - table_region.top) + "px";
    fade_popup.style.left = ((YAHOO.util.Dom.getDocumentWidth() - w) / 2) + "px";
    fade_popup.style.top = (Math.max(document.documentElement.scrollTop, document.body.scrollTop) + 20) + "px";
    fade_popup.style.visibility = "visible";

    fade_background.style.visibility = "visible";
    var anim = new YAHOO.util.Anim(fade_background, { opacity: {to: 0.8} }, 0.5, YAHOO.util.Easing.easeNone);
    anim.animate();
}

function show_fade_popup_url(url, w, h, close_img)
{
    var html = "<iframe frameborder='0' src='" + url + "' style='width: " + w + "px; height: " + h + "px;'>";
    show_fade_popup(html,w,h,close_img)
}

function hide_fade_popup()
{
    if (!fade_popup)
        return;

    fade_popup.style.visibility = "hidden";

    var anim = new YAHOO.util.Anim(fade_background, { opacity: {to: 0.0} }, 0.5, YAHOO.util.Easing.easeNone);
    anim.animate();
    anim.onComplete.subscribe(fade_out_complete);
}

function fade_out_complete()
{
    fade_background.style.visibility = "hidden";
    window.onresize = null;
}

function resize_fade_popup()
{
    if (!fade_background)
        return;

    fade_background.style.width = YAHOO.util.Dom.getDocumentWidth() + "px";
    fade_background.style.height = YAHOO.util.Dom.getDocumentHeight() + "px";
}
