// Modal Overlay Controls

var modal_overlay;
var modal_overlay_iframe;


function loadModalOverlay(args)
{
	var request = createXmlHttpRequest();
	var url = "/root/modal_overlay/load.phtml";
	request.open("GET", url, true);

	// once it's ready, eval the text into a JSON object and close the connection
	request.onreadystatechange = function() {
		if (request.readyState == 4)
		{
			html = request.responseText;
			$("modal_overlay").innerHTML = html;
			showModalOverlay(args);
		}
	};
	request.send(null);
	
	return true;
}


/*
 * if action is specified, we presume there will be an iframe
 * and we'll change the source accordingly
 * I haven't forced this arguement in order to keep the function flexible
 * As there's no other way for this to work currently,
 * the script alerts if action is not set
 */
function showModalOverlay(args)
{
	if (args["action"] != "")
	{
		modal_overlay_iframe = document.getElementById("modal_overlay_iframe");

		var url = "/" + module + "/";
		url += args["action"] + ".phtml?modal_overlay=1";
		
		for (var i in args)
		{
			if (i != "action") url += "&" + i + "=" + args[i];
		}

		modal_overlay_iframe.src = url;
	}
	else
	{
		alert("No action passed in the args array");
	}

	modal_overlay = new Dialog.Box("modal_overlay");
	$("modal_overlay").show();

	window.onresize = function() {
		$("modal_overlay").show();
	}

	css.removeClassFromElement(document.getElementById("modal_overlay"), "hidden");
	document.body.appendChild(document.getElementById("modal_overlay"));

	return false;
}


/*
 * when the user clicks the 'close', 'x' or 'close x' link,
 * close the modal overlay
 */
function closeModalOverlay()
{
	new Effect.Fade(modal_overlay.overlay, {duration: 0.1});
	modal_overlay.dialog_box.style.display = 'none';
	modal_overlay.selectBoxes('show');
	modal_overlay.flashObjects('show');
	window.onresize = function() {};

    return false;
}



/*
 * if the user clicks a link in the modal overlay
 * replace the parent window with it
 */
function redirect_parent_window(el)
{
	parent.document.location.href = el.href;
	return true;
}




function showFeedbackOverlay(html)
{
	feedback_overlay.innerHTML = html;

	$("feedback_overlay").hide();

	new Dialog.Box("feedback_overlay");
	$("feedback_overlay").show();

	window.onresize = function() {
		$("feedback_overlay").show();
	}

	css.removeClassFromElement(document.getElementById("feedback_overlay"), "hidden");
}

function hideFeedbackOverlay()
{
	$("feedback_overlay").hide();
}

function createXmlHttpRequest()
{
	if (window.ActiveXObject) request = new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest) request = new XMLHttpRequest();
	return request;
}
