
var gModalCurrent = null;

function modal_show(inContentID,inTop,inLeft,inWidth)
{
    gModalCurrent = document.getElementById(inContentID);
    gModalCurrent.style.width = inWidth + "px";
    gModalCurrent.style.left = inLeft + "px";
    gModalCurrent.style.top = inTop + "px";

    var ieIFrame = $('modal_ie_iframe_hack');
    if(ieIFrame) {
        setTimeout(function() {
            ieIFrame.style.width = inWidth + "px";
            ieIFrame.style.height = gModalCurrent.offsetHeight;
            ieIFrame.style.left = inLeft + "px";
            ieIFrame.style.top = inTop + "px";
            ieIFrame.style.visibility = "visible";
        },1);
    }



    var bd = document.getElementById("modal_backdrop");
    bd.style.height = window.getScrollHeight() + "px";
    bd.style.visibility = "visible";
    var t = new OpacityTween(bd,Tween.regularEaseout,0,30,.25);
    t.start();

    gModalCurrent.style.visibility = "hidden";
    gModalCurrent.style.display = 'block';


    gModalCurrent.style.visibility = "visible";
    t = new OpacityTween(gModalCurrent,Tween.regularEaseout,0,100,.25);
    t.start();

}

function modal_show_centered(inContentID,inWidth)
{
    gModalCurrent = document.getElementById(inContentID);
    gModalCurrent.style.width = inWidth + "px";


    var bd = document.getElementById("modal_backdrop");
    bd.style.height = window.getScrollHeight() + "px";
    bd.style.visibility = "visible";
    var t = new OpacityTween(bd,Tween.regularEaseout,0,30,.25);
    t.start();

    gModalCurrent.style.visibility = "hidden";
    gModalCurrent.style.display = 'block';
    
    var pos = modal_determine_center(inWidth,gModalCurrent.offsetHeight);
    gModalCurrent.style.left = pos.left + "px";
    gModalCurrent.style.top = pos.top + "px";

    var ieIFrame = $('modal_ie_iframe_hack');
    if(ieIFrame) {
        setTimeout(function() {
            ieIFrame.style.width = inWidth + "px";
            ieIFrame.style.height = gModalCurrent.offsetHeight;
            ieIFrame.style.left = pos.left + "px";
            ieIFrame.style.top = pos.top + "px";
            ieIFrame.style.visibility = "visible";
        },1);
    }


    gModalCurrent.style.visibility = "visible";
    t = new OpacityTween(gModalCurrent,Tween.regularEaseout,0,100,.25);
    t.start();
}



// returns {top: x, left:x}
function modal_determine_center(inWidth,inHeight)
{
    var windowHeight = f_clientHeight();
    var windowWidth = f_clientWidth();
    var scrollLeft = f_scrollLeft();
    var scrollTop = f_scrollTop();
    
    var theResult =  {
        top: Math.round(scrollTop + (windowHeight / 3) - (inHeight / 3)),
        left: Math.round(scrollLeft + (windowWidth / 2) - (inWidth / 2))
    };
    
    if(theResult.top < 0) {
        theResult.top = 20;
    }
    
    return theResult;

}

function modal_hide() {
    var ieIFrame = $('modal_ie_iframe_hack');
    if(ieIFrame) {
        ieIFrame.style.visibility = "hidden";
    }
    
    if(gModalCurrent != null) {
        var t = new OpacityTween(gModalCurrent,Tween.regularEaseout,100,0,.25);
        t.addListener({onMotionFinished: function() {
            gModalCurrent.style.display = "none";
            gModalCurrent = null;
        }});
        t.start();
    }
    var bd = document.getElementById('modal_backdrop');
    var t = new OpacityTween(bd,Tween.regularEaseout,30,0,.25);
    t.addListener({onMotionFinished: function() {
        bd.style.visibility = "hidden";
    }});
    t.start();
}



function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}
