// LAYER UI
var
	m_szElement = "",
	m_aoWindowList = new Array(),
	m_aoWindowListTitle = new Array(),
	m_iPositionTab = 0,
	m_iNumVisibleTab = 0,
	m_iWindowActive = -1,
	m_winModalWindow,
	m_iWidthTabBar = 700;

m_aoWindowList.length = 0;

// CROSS BROWSER MODAL WINDOW
function ignoreEvents(e)
{
	return false
}

function showModalWindow(_szUrl, _iWidth, _iHeight)
{
	if (window.showModalDialog)
	{
		window.showModalDialog(_szUrl,null,"dialogWidth="+_iWidth+"px;dialogHeight="+_iHeight+"px")
	}
	else
	{
		window.top.captureEvents (Event.CLICK|Event.FOCUS)
		window.top.onclick=ignoreEvents
		window.top.onfocus=handleFocus 
		m_winModalWindow = window.open(_szUrl, "ModalChild", "dependent=yes,width="+_iWidth+",height="+_iHeight+"")
		m_winModalWindow.focus()
	}
}

function handleFocus()
{
	if (m_winModalWindow)
	{
		if (!m_winModalWindow.closed)
		{
			m_winModalWindow.focus()
		}
		else
		{
			window.top.releaseEvents (Event.CLICK|Event.FOCUS)
			window.top.onclick = ""
		}
	}
	return false
}
// END CROSS BROWSER MODAL WINDOW
	
function setDocumentView(_szElement)
{
	m_szElement = _szElement;
}

function getTextWidth(_szText)
{
	var
		oObj = getElement("cellMesure");
		
	if (oObj)
	{
		oObj.innerHTML = _szText;
		iWidth = getElementWidth("mesureElement");
		return iWidth;
	}
	return -1;
}

function getTextWidthUseParent(_szText)
{
	var
		oObj = getElementParent("cellMesure");
		
	if (oObj)
	{
		oObj.innerHTML = _szText;
		oObj = getElementParent("mesureElement");
		iWidth = getElementWidth2(oObj);
		return iWidth;
	}
	return -1;
}

function drawTabs()
{
	var
		oObj = getElement("tabBarSDI");
	iTotalWidth = 0;
	while (oObj.rows[0].cells.length != 0)
	{
		for (i = 0; i < oObj.rows[0].cells.length; i++)
		{
			oObj.rows[0].deleteCell(i);
		}
	}
	m_iNumVisibleTab = 0;
	for (i = m_iPositionTab; i < m_aoWindowList.length; i++)
	{
		iWidth = getTextWidth(m_aoWindowListTitle[i]);
		iWidth += 20;
		if ((iTotalWidth + iWidth) < m_iWidthTabBar)
		{
			iTotalWidth += iWidth;
			var cell = oObj.rows[0].insertCell(oObj.rows[0].cells.length);
			var szInnerHtml = "";
			
			cell.noWrap = true;
			cell.className = "tabButton";
			cell.id = "cellTab_"+m_aoWindowList[i];
			if (m_aoWindowList[i] == m_aoWindowList[m_iWindowActive])
				cell.className = "tabButtonHighlighted";
			szInnerHtml += "<a href=\"javascript:setActiveWindow("+i+",'"+m_aoWindowList[i]+"');\">";
			szInnerHtml += "<span class=\"tabFont\">"+m_aoWindowListTitle[i]+"</span>";
			szInnerHtml += "</a>";
			cell.innerHTML = szInnerHtml;
			m_iNumVisibleTab++;
		}
		else
			break;
	}
}

function openWindow(_szWindowName, _szWindowTitle, _szHRef)
{
	var
		bFind = false;
	
	if (!isBrowserOk())
		return;
	
	for (i = 0; i < m_aoWindowList.length; i++)
	{
		if (_szWindowName == m_aoWindowList[i])
		{
			bFind = true;
			break;
		}
	}
	if (!bFind && m_szElement.length != 0)
	{
		m_iWindowActive = m_aoWindowListTitle.length;
		createViewDocument(_szWindowName, _szHRef);
		m_aoWindowList[m_aoWindowList.length] = _szWindowName;
		m_aoWindowListTitle[m_aoWindowListTitle.length] = _szWindowTitle;
		iTotalWidth = 0;
		for (i = m_iPositionTab; i < m_aoWindowList.length; i++)
		{
			iWidth = getTextWidth(m_aoWindowListTitle[i]);
			iWidth += 20;
			iTotalWidth += iWidth;
			if (iTotalWidth > m_iWidthTabBar)
			{
				m_iPositionTab++;
				break;
			}
		}
		drawTabs();
	}
	else 
		if (bFind)
		{
			setActiveWindow(i, m_aoWindowList[i]);
			//createViewDocument(_szWindowName, _szHRef);
		}
}

function hideAllView()
{
	for (i = 0; i < m_aoWindowList.length; i++)
	{
		szIdElement = m_aoWindowList[i];
		oObjHide = getElement("div_"+szIdElement);
		if (oObjHide)
		{
			if (getBrowser() == "IE")
				oObjHide.style.display = "none";
			else
				oObjHide.style.visibility = "hidden";
			oObjHide.style.zindex = "-90";
			oObjHide.style.left = "2000px";
		}
	}
}

function restoreView()
{
	for (i = 0; i < m_aoWindowList.length; i++)
	{
		szIdElement = m_aoWindowList[i];
		oObjHide = getElement("div_"+szIdElement);
		if (oObjHide)
		{
			oObjHide.style.left = "0px";
		}
	}
	oObj = getElement("div_"+m_aoWindowList[m_iWindowActive]);
	if (oObj)
	{
		if (getBrowser() == "IE")
			oObj.style.display = "inline";
		else
			oObj.style.visibility = "visible";
		oObj.style.zindex = "100";
		oObj.style.left = "0px";
	}
}

function createViewDocument(_szFrameId, _szHRef)
{
	var
		szHtml = "",
		oObjHide = null,
		oObj = getElement(m_szElement),
		iTop = getElementTop(m_szElement),
		szIdElement,
		bFind = false;
	
	for (i = 0; i < m_aoWindowList.length; i++)
	{
		szIdElement = m_aoWindowList[i];
		if (szIdElement != _szFrameId)
		{
			oObjHide = getElement("div_"+szIdElement);
			if (oObjHide)
			{
				oObjHide.style.left = "2000px";
				if (getBrowser() == "IE")
					oObjHide.style.display = "none";
				else
					oObjHide.style.visibility = "hidden";
				oObjHide.style.zindex = "-90";
			}
		}
		else
			bFind = true;
	}
	if (!bFind)
	{
		var oObj2 = getElement("div_"+_szFrameId);
		
		if (!oObj2)
		{
			var
				iWidthContainer = getElementWidth(m_szElement)-1,
				iHeightContainer = getElementHeight(m_szElement)-1;
				
			szHtml += "<div id=\"div_"+_szFrameId+"\" style=\"position:absolute;top:"+iTop+"px;left:0px;visibility:visible;display:inline;z-index:100;\">";
			szHtml += "<iframe id=\"frame_"+_szFrameId+"\" name=\"frame_"+_szFrameId+"\" src=\""+_szHRef+"\" width=\""+iWidthContainer+"\" height=\""+iHeightContainer+"\" />";
			szHtml += "</div>";
			oObj.innerHTML += szHtml;
		}
		else
		{
			var
				iWidthContainer = getElementWidth(m_szElement)-1,
				iHeightContainer = getElementHeight(m_szElement)-1;
				
			szHtml += "<iframe id=\"frame_"+_szFrameId+"\" name=\"frame_"+_szFrameId+"\" src=\""+_szHRef+"\" width=\""+iWidthContainer+"\" height=\""+iHeightContainer+"\" />";
			oObj2.innerHTML += szHtml;
		}
	}
	else
	{
		oObj = getElement(_szFrameId);
		if (getBrowser() == "IE")
			oObj.style.display = "inline";
		else
			oObj.style.visibility = "visible";
		oObj.style.left = "0px";
		oObj.style.zindex = "100";
		oObj.src = _szHRef;
	}
}

function prevTab()
{
	if (m_iPositionTab > 0)
	{
		m_iPositionTab--;
		drawTabs();
	}
}

function nextTab()
{
	if ((m_iPositionTab+m_iNumVisibleTab) < m_aoWindowList.length)
	{
		m_iPositionTab++;
		drawTabs();
	}
}

function setActiveWindow(_iWindow, _szWindowName)
{
	m_iWindowActive = _iWindow;
	szIdElement = "div_"+_szWindowName;
	for (i = 0; i < m_aoWindowList.length; i++)
	{
		szIdElement2 = "div_"+m_aoWindowList[i];
		if (szIdElement2 != szIdElement)
		{
			oObj = getElement(szIdElement2);
			if (oObj)
			{
				if (getBrowser() == "IE")
					oObj.style.display = "none";
				else
					oObj.style.visibility = "hidden";
				oObj.style.zindex = "-90";
				oObj.style.left = "2000px";
				oObj = getElement("cellTab_"+m_aoWindowList[i]);
				if (oObj != null)
					oObj.className = "tabButton";
			}
		}
	}
	oObj = getElement("div_"+_szWindowName);
	if (oObj)
	{
		oObj.style.zindex = "100";
		oObj.style.left = "0px";
		if (getBrowser() == "IE")
			oObj.style.display = "inline";
		else
			oObj.style.visibility = "visible";
		oObj = getElement("cellTab_"+_szWindowName);
		oObj.className = "tabButtonHighlighted";
	}
}

function closeActiveWindow()
{
	var
		oObj = getElement("div_"+m_aoWindowList[m_iWindowActive]);
	
	if (oObj == null)
		return;
		
	if (oObj)
	{
		oObj.innerHTML = "";
	}
	for (i = m_iWindowActive; i < m_aoWindowList.length-1; i++)
	{
		m_aoWindowList[i] = m_aoWindowList[i+1];
		m_aoWindowListTitle[i] = m_aoWindowListTitle[i+1];
	}
	if (m_iWindowActive > 0)
		m_iWindowActive--;
	m_aoWindowList.length--;
	m_aoWindowListTitle.length--;
	drawTabs();
	setActiveWindow(m_iWindowActive, m_aoWindowList[m_iWindowActive]);
}

function changeTabTitleFromChild(_szTitle)
{
	for (i = 0; i < window.parent.frames.length; i++)
	{
		if (window.parent.frames[i] == window.self)
		{
			var
				szWindowName = window.parent.frames[i].name;
			szWindowName = szWindowName.substr(6);
			for (t = 0; t < window.parent.m_aoWindowList.length; t++)
			{
				if (window.parent.m_aoWindowList[t] == szWindowName)
				{
					window.parent.m_aoWindowListTitle[t] = _szTitle;
					window.parent.drawTabs();
				}
			}
		}
	}
}

function drawWindow(_oParent, _iId, _szWindowTitle, _iLeft, _iTop, _iWidth, _iHeight)
{
	if (_oParent)
	{
		szHtml = "<table class=\"windowStyle\" cellpadding=0 cellspacing=0 width="+_iWidth+" height="+_iHeight+" style=\"position:absolute;top:"+_iTop+";left:"+_iLeft+";\">";
		szHtml += "<tr>";
		szHtml += "<td class=\"windowTitleStyle\">"+_szWindowTitle+"</td>";
		szHtml += "</tr>";
		szHtml += "<tr>";
		szHtml += "<td class=\"windowViewStyle\" id=\""+_iId+"\" name=\""+_iId+"\">&nbsp;</td>";
		szHtml += "</tr>";
		szHtml += "</table>";
		_oParent.innerHTML += szHtml;
	}
}

function closeWindow(_oParent)
{
	if (_oParent)
	{
		_oParent.innerHTML = "";
	}
}