var shadow;  // shadow behind modal window.
var mWin;    // modal window object.
var mwTopMargin = 30;           // Distance of modal window from top of browser window.
var jsPathPrefix = 'js/mw-';    // Path to js files + any prefix.
var mwLinkRel = 'modal_window'; // rel attribute for window links should start with this.
var mwContentScript;            // Script to be loaded that will contain content for modal window.

function show_modal_window(e) {
	
    // Get event object.
    e = e ? e : window.event;

    // For when we initiate this via onclick()
    if (!e) {
        e = {};
    }

    // Prevent default and stop propagation of event.
    if (e.preventDefault) {
        e.preventDefault();
        e.stopPropagation();
    }
    else {
        e.returnValue = false;
        e.cancelBubble = true;
    }

    // Create shadow and mWin document elements if they don't already exist.
    if (!shadow) {
        shadow = document.createElement('div');
        shadow.className = 'shadow';
        shadow.style.width = document.body.offsetWidth + 'px';
        shadow.style.height = ((document.body.offsetHeight > 580) ? document.body.offsetHeight : '580') + 'px';

        mWin = document.createElement('div');
        mWin.className = 'mWin';
        if (mWin.addEventListener) {
            mWin.addEventListener('click', prevent_bubble, false);
        }
        else {
            mWin.onclick = prevent_bubble;
        }

        shadow.appendChild(mWin);
        document.body.appendChild(shadow);
    }

    // Insert window content and position vertically.
    mwContentScript = document.createElement('script');
    mwContentScript.type = 'text/javascript';
    rel = e.currentTarget ? e.currentTarget.rel : this.rel;
            
    mwContentScript.src = HTTP + LAYOUTS + jsPathPrefix + rel.slice(mwLinkRel.length + 1) + '.js';
               
    document.body.appendChild(mwContentScript);

    if (window.pageYOffset != undefined) {
        mWin.style.marginTop = (window.pageYOffset + mwTopMargin) + 'px';
    }
    else {
        if (document.documentElement && document.documentElement.scrollTop) {
            mWin.style.marginTop = (document.documentElement.scrollTop + mwTopMargin) + 'px';
        }
        else {
            mWin.style.marginTop = (document.body.scrollTop + mwTopMargin) + 'px';
        }
    }

    // Show modal window.
    shadow.style.display = 'block';

    // Hide modal window when clicking outside it.
    if (document.body.addEventListener) {
        document.body.addEventListener('click', hide_modal_window, false);
    }
    else {
        document.body.onclick = hide_modal_window;
    }
}

function hide_modal_window(e) {

    // Hide modal window.
    shadow.style.display = 'none';

    // Remove associated event listeners.
    if (document.body.removeEventListener) {
        document.body.removeEventListener('click', hide_modal_window, false);
    }
    else {
        document.body.onclick = null;
    }

    // Remove modal window content.
    document.body.removeChild(mwContentScript);
    mwContentScript = null;
    mWin.innerHTML = '';
}

function prevent_bubble(e) {
    e = e ? e : window.event;
    if (e.stopPropagation) {
        e.stopPropagation();
    }
    else {
        e.cancelBubble = true;
    }
}

function init_modal_window_links() {
    // Add click events for all links with rel="modal_window.*
    for (var i = 0; i < document.links.length; i++) {
        link = document.links[i];
        if (!link.rel) {
            continue;
        }
        if (link.rel.match(RegExp('^' + mwLinkRel))) {
            if (link.addEventListener) {
                link.addEventListener('click', show_modal_window, false);
            }
            else {
                link.onclick = show_modal_window;
            }
        }
    }
}

// Init modal window links on load.
if (window.addEventListener) {
    window.addEventListener('load', init_modal_window_links, false);
}
else {
    window.onload = init_modal_window_links;
}
