var AUTOFORWARDER_ID = "autoForwardDialogTab";
var thisDialogFirstEl;
var modalFadeLayerLoaded;

function redirectFocus() {
	if (!is_ie || !thisDialogFirstEl)
		return;

	if (is_ie == true && thisDialogFirstEl.focus) {
		thisDialogFirstEl.focus();
	}
	else if (this.blur) {
		this.blur();
	}
}

function disableTab(el) {
	addCaptureEvent(el, "onfocus", redirectFocus);
	if (el.tagName) {
		if (el.tagName.toLowerCase() == "select" && el.style.visibility != "hidden") {
			el.hiddenByDialog = true;
			el.style.visibility = "hidden";
		}
	}
}

function enableTab(el) {
	removeCaptureEvent(el, "onfocus", redirectFocus);
	if (el.tagName) {
		if (el.tagName.toLowerCase() == "select" && el.hiddenByDialog == true) {
			el.style.visibility = "visible";
			el.hiddenByDialog = false;
		}
	}
}

function disableNonDialogTabs(dialog, firstTabEl) {
	if (dialog == null || firstTabEl == null) 
		return;
	
	thisDialogFirstEl =  firstTabEl;

	transformNonDialogElements(document, dialog.id, disableTab);

	// create a unique instance of the autoForwarder element for this dialog
	var maxTabIndex = getMaxTabIndex(dialog) + 1;
	var autoForwarder = document.getElementById(dialog.id + AUTOFORWARDER_ID);
	if (!autoForwarder) {
		autoForwarder = document.createElement("textarea");
		autoForwarder.id = dialog.id + AUTOFORWARDER_ID;
		dialog.appendChild(autoForwarder);
	}

	// position tabForwarderInput under the entire dialog, hidden 
	autoForwarder.style.position = "absolute";
	autoForwarder.style.left = "0"; 
	autoForwarder.style.top = "0"; 
	autoForwarder.style.visibility = "hidden"; 

	// set tabForwarder to be the last in the tab order, after all the visible fields
	autoForwarder.tabindex = maxTabIndex + 1;	
	autoForwarder.onfocus = function() {
								thisDialogFirstEl.focus();
							};
	thisDialogFirstEl.blur();
}

function transformNonDialogElements(thisNode, dialogID, callback) {
	if (thisNode == null) return;

	var children = thisNode.childNodes;
	for (var i=0; i < children.length; i++) {
		transformNonDialogElements(children[i], dialogID, callback);
	}

	// don't disable tab on any elements in the dialog
	var inDialog = false;
	if (thisNode.id == dialogID) {
		inDialog = true;
	}
	else {
		var parent = thisNode.parentNode;
		while (parent != null) {
			if (parent.id == dialogID) {
				inDialog = true;
				break;
			}
			parent = parent.parentNode;
		}
	}

	if (!inDialog) {
		callback(thisNode);
	}
}

function enableNonDialogTabs(dialog) {
	transformNonDialogElements(document, dialog.id, enableTab);
}

function getMaxTabIndex(thisNode) {
	var max = 0;
	if (thisNode.tabIndex) {
		max = thisNode.tabIndex;
		var msg = thisNode.id + " " + thisNode.tabIndex + "\n";
	}
	var children = thisNode.childNodes;
	for (var i=0; i < children.length; i++) {
		var maxCandidate = getMaxTabIndex(children[i]);
		if (maxCandidate > max) 
			max = maxCandidate;
	}
	return max;	
}

function initModalFadeLayer() {
	if (modalFadeLayerLoaded)
		return;

	var dialogFadeLayer = document.getElementById("dialogFadeLayer");
	resizeToClient();
	window.onresize = resizeToClient;

	setupFadeLayer(dialogFadeLayer);
	modalFadeLayerLoaded = true;
}

function setupFadeLayer(fadeObject) {
	if (!fadeObject) 
		return;
	
	var url = "/images/common/dialog/fadeLayer.png";
	if (is_mac && is_ie)
		url = "/images/common/dialog/fadeLayerClear.png";

	loadPNGIntoDiv(fadeObject, url);
}

function showModalFadeLayer() {
	if (!modalFadeLayerLoaded)
		initModalFadeLayer();

	var dialogFadeLayer = document.getElementById("dialogFadeLayer");
	if (dialogFadeLayer)
		dialogFadeLayer.style.display = "block";
}

function hideModalFadeLayer() {
	var dialogFadeLayer = document.getElementById("dialogFadeLayer");
	if (dialogFadeLayer)
		dialogFadeLayer.style.display = "none";	
}

function resizeToClient() {
	var dialogFadeLayer = document.getElementById("dialogFadeLayer");
	if (!dialogFadeLayer)
		return;
	
	var height = document.body.scrollHeight;
	if (height == 0) {
		height = document.documentElement.scrollHeight; // for Firefox
	}

	dialogFadeLayer.style.width = "746px";
	dialogFadeLayer.style.height = height + "px";
}

// doesn't work on Opera before v8
function addCaptureEvent(el, eventName, callbackHandler) {
	if (!el) 
		return;

	if (el.attachEvent) { // IE
		el.attachEvent(eventName, callbackHandler);
	}
	else if (el.addEventListener) {
		// note: removing 'on' from eventName
		el.addEventListener(eventName.substring(2, eventName.length), callbackHandler, true);
	}
}
// doesn't work on Opera before v8
function removeCaptureEvent(el, eventName, callbackHandler) {
	if (!el) 
		return;

	if (el.detachEvent) { // IE
		el.detachEvent(eventName, callbackHandler);
	}
	else if (el.removeEventListener) {
		// note: if the same captureEvents boolean (3rd arg)
		// is not passed to addEventListener and removeEventListener
		// it won't remove properly.
		el.removeEventListener(eventName.substring(2, eventName.length), callbackHandler, true);
	}
}

function setOpacity(el, opacity) {
	if (is_win && is_ie5_5up) {
		el.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + (opacity * 100) + ")";
		el.filters[0].Apply();
		el.filters[0].Play();
	}
	else if (is_win && is_ie5) {
		el.style.filter = "Alpha(opacity=" + (opacity * 100) + ")";
	}
	else {
		el.style.opacity = opacity;
		el.style.MozOpacity = opacity;
		el.style.KhtmlOpacity = opacity;
	}
}