//General
//for example: instead of each module writing out script found in moduleMaxMin_OnClick have the functionality cached
//

var EIP_COL_DELIMITER = String.fromCharCode(16);
var EIP_ROW_DELIMITER = String.fromCharCode(15);
var __eip_m_bPageLoaded = false;

window.onload = __eip_Page_OnLoad;

function __eip_ClientAPIEnabled()
{
	return typeof(eip) != 'undefined';
}


function __eip_Page_OnLoad()
{
	if (__eip_ClientAPIEnabled())
	{
		var sLoadHandlers = eip.getVar('__eip_pageload');
		if (sLoadHandlers != null)
			eval(sLoadHandlers);
	}
	__eip_m_bPageLoaded = true;
}

function __eip_KeyDown(iKeyCode, sFunc, e)
{
	if (e == null)
		e = window.event;

	if (e.keyCode == iKeyCode)
	{
		eval(unescape(sFunc));
		return false;
	}
}

function __eip_bodyscroll() 
{
	var oF=document.forms[0];	
	if (__eip_ClientAPIEnabled() && __eip_m_bPageLoaded)
		oF.ScrollTop.value=eip.dom.getByTagName("body")[0].scrollTop;
}

function __eip_setScrollTop(iTop)
{
	if (__eip_ClientAPIEnabled())
	{
		if (iTop == null)
			iTop = document.forms[0].ScrollTop.value;
	
		var sID = eip.getVar('ScrollToControl');
		if (sID != null && sID.length > 0)
		{
			var oCtl = eip.dom.getById(sID);
			if (oCtl != null)
			{
				iTop = eip.dom.positioning.elementTop(oCtl);
				eip.setVar('ScrollToControl', '');
			}
		}
		eip.dom.getByTagName("body")[0].scrollTop = iTop;
	}
}

//Focus logic
function __eip_SetInitialFocus(sID)
{
	var oCtl = eip.dom.getById(sID);	
	if (oCtl != null && __eip_CanReceiveFocus(oCtl))
		oCtl.focus();
}	

function __eip_CanReceiveFocus(e)
{
	//probably should call getComputedStyle for classes that cause item to be hidden
	if (e.style.display != 'none' && e.tabIndex > -1 && e.disabled == false && e.style.visible != 'hidden')
	{
		var eParent = e.parentElement;
		while (eParent != null && eParent.tagName != 'BODY')
		{
			if (eParent.style.display == 'none' || eParent.disabled || eParent.style.visible == 'hidden')
				return false;
			eParent = eParent.parentElement;
		}
		return true;
	}
	else
		return false;
}

//Max/Min Script
function __eip_ContainerMaxMin_OnClick(oLnk, sContentID)
{
	var oContent = eip.dom.getById(sContentID);
	if (oContent != null)
	{
		var oBtn = oLnk.childNodes[0];
		var sContainerID = oLnk.getAttribute('containerid');
		var sCookieID = oLnk.getAttribute('cookieid');
		var sCurrentFile = oBtn.src.toLowerCase().substr(oBtn.src.lastIndexOf('/'));
		var sMaxFile;
		var sMaxIcon;
		var sMinIcon;

		if (eip.getVar('min_icon_' + sContainerID))
			sMinIcon = eip.getVar('min_icon_' + sContainerID);
		else
			sMinIcon = eip.getVar('min_icon');

		if (eip.getVar('max_icon_' + sContainerID))
			sMaxIcon = eip.getVar('max_icon_' + sContainerID);
		else
			sMaxIcon = eip.getVar('max_icon');

		sMaxFile = sMaxIcon.toLowerCase().substr(sMaxIcon.lastIndexOf('/'));

		var iNum = 5;
		if (oLnk.getAttribute('animf') != null)
			iNum = new Number(oLnk.getAttribute('animf'));
			
		if (sCurrentFile == sMaxFile)
		{
			oBtn.src = sMinIcon;				
			//oContent.style.display = '';
			eip.dom.expandElement(oContent, iNum);
			oBtn.title = eip.getVar('min_text');
			if (sCookieID != null)
			{
				if (eip.getVar('__eip_' + sContainerID + ':defminimized') == 'true')
					eip.dom.setCookie(sCookieID, 'true', 365);
				else
					eip.dom.deleteCookie(sCookieID);
			}
			else
				eip.setVar('__eip_' + sContainerID + '_Visible', 'true');
		}
		else
		{
			oBtn.src = sMaxIcon;				
			//oContent.style.display = 'none';
			eip.dom.collapseElement(oContent, iNum);
			oBtn.title = eip.getVar('max_text');
			if (sCookieID != null)
			{
				if (eip.getVar('__eip_' + sContainerID + ':defminimized') == 'true')
					eip.dom.deleteCookie(sCookieID);
				else
					eip.dom.setCookie(sCookieID, 'false', 365);				
			}
			else
				eip.setVar('__eip_' + sContainerID + '_Visible', 'false');			
		}
		
		return true;	//cancel postback
	}
	return false;	//failed so do postback
}

function __eip_Help_OnClick(sHelpID)
{
	var oHelp = eip.dom.getById(sHelpID);
	if (oHelp != null)
	{
		if (oHelp.style.display == 'none')
			oHelp.style.display = '';
		else
			oHelp.style.display = 'none';

		return true;	//cancel postback
	}
	return false;	//failed so do postback
}

function __eip_SectionMaxMin(oBtn, sContentID)
{
	var oContent = eip.dom.getById(sContentID);
	if (oContent != null)
	{
		var sMaxIcon = oBtn.getAttribute('max_icon');
		var sMinIcon = oBtn.getAttribute('min_icon');
		if (oContent.style.display == 'none')
		{
			oBtn.src = sMinIcon;				
			oContent.style.display = '';
			eip.setVar(oBtn.id + ':exp', 1);
		}
		else
		{
			oBtn.src = sMaxIcon;				
			oContent.style.display = 'none';
			eip.setVar(oBtn.id + ':exp', 0);
		}
		return true;	//cancel postback
	}
	return false;	//failed so do postback
}

//Drag N Drop
function __eip_enableDragDrop()
{
	var aryConts = eip.getVar('__eip_dragDrop').split(";");	
	var aryTitles;

	for (var i=0; i < aryConts.length; i++)
	{
		aryTitles = aryConts[i].split(" ");
		if (aryTitles[0].length > 0)
		{			
			var oCtr = eip.dom.getById(aryTitles[0]);
			var oTitle = eip.dom.getById(aryTitles[1]);
			if (oCtr != null && oTitle != null)
			{
				oCtr.setAttribute('moduleid', aryTitles[2]);
				eip.dom.positioning.enableDragAndDrop(oCtr, oTitle, '__eip_dragComplete()', '__eip_dragOver()');
			}	
		}
	}
}

var __eip_oPrevSelPane;
var __eip_oPrevSelModule;
var __eip_dragEventCount=0;
function __eip_dragOver()
{
	__eip_dragEventCount++;
	if (__eip_dragEventCount % 75 != 0)	//only calculate position every 75 events
		return;
	
	var oCont = eip.dom.getById(eip.dom.positioning.dragCtr.contID);

	var oPane = __eip_getMostSelectedPane(eip.dom.positioning.dragCtr);
		
	if (__eip_oPrevSelPane != null)	//reset previous pane's border
		__eip_oPrevSelPane.pane.style.border = __eip_oPrevSelPane.origBorder;

	if (oPane != null)
	{		
		__eip_oPrevSelPane = oPane;
		oPane.pane.style.border = '4px double ' + EIP_HIGHLIGHT_COLOR;
		var iIndex = __eip_getPaneControlIndex(oCont, oPane);

		var oPrevCtl;
		var oNextCtl;
		for (var i=0; i<oPane.controls.length; i++)
		{
			if (iIndex > i && oPane.controls[i].id != oCont.id)
				oPrevCtl = oPane.controls[i];
			if (iIndex <= i && oPane.controls[i].id != oCont.id)
			{
				oNextCtl = oPane.controls[i];
				break;
			}
		}			
		
		if (__eip_oPrevSelModule != null)
			eip.dom.getNonTextNode(__eip_oPrevSelModule.control).style.border = __eip_oPrevSelModule.origBorder;
			

		if (oNextCtl != null)
		{
			__eip_oPrevSelModule = oNextCtl;
			eip.dom.getNonTextNode(oNextCtl.control).style.borderTop = '5px groove ' + EIP_HIGHLIGHT_COLOR;
		}
		else if (oPrevCtl != null)
		{
			__eip_oPrevSelModule = oPrevCtl;
			eip.dom.getNonTextNode(oPrevCtl.control).style.borderBottom = '5px groove ' + EIP_HIGHLIGHT_COLOR;
		}
	}
}

function __eip_dragComplete()
{
	var oCtl = eip.dom.getById(eip.dom.positioning.dragCtr.contID);
	var sModuleID = oCtl.getAttribute('moduleid');
	
	if (__eip_oPrevSelPane != null)
		__eip_oPrevSelPane.pane.style.border = __eip_oPrevSelPane.origBorder;

	if (__eip_oPrevSelModule != null)
		eip.dom.getNonTextNode(__eip_oPrevSelModule.control).style.border = __eip_oPrevSelModule.origBorder;
		
	var oPane = __eip_getMostSelectedPane(eip.dom.positioning.dragCtr);
	var iIndex;
	if (oPane == null)
	{
		var oPanes = __eip_Panes();
		for (var i=0; i<oPanes.length; i++)
		{
			if (oPanes[i].id == oCtl.parentNode.id)
				oPane = oPanes[i];
		}
	}	
	if (oPane != null)
	{
		iIndex = __eip_getPaneControlIndex(oCtl, oPane);
		__eip_MoveToPane(oPane, oCtl, iIndex);

		eip.callPostBack('MoveToPane', 'moduleid=' + sModuleID, 'pane=' + oPane.paneName, 'order=' + iIndex * 2); 
	}
}

function __eip_MoveToPane(oPane, oCtl, iIndex)
{

	if (oPane != null)
	{
		var aryCtls = new Array();
		for (var i=iIndex; i<oPane.controls.length; i++)
		{
			if (oPane.controls[i].control.id != oCtl.id)
				aryCtls[aryCtls.length] = oPane.controls[i].control;

			eip.dom.removeChild(oPane.controls[i].control);
		}
		eip.dom.appendChild(oPane.pane, oCtl);
		oCtl.style.top=0;
		oCtl.style.left=0;
		oCtl.style.position = 'relative';
		for (var i=0; i<aryCtls.length; i++)
		{
			eip.dom.appendChild(oPane.pane, aryCtls[i]);
		}
		__eip_RefreshPanes();
	}
	else
	{
		oCtl.style.top=0;
		oCtl.style.left=0;
		oCtl.style.position = 'relative';
	}
}

function __eip_RefreshPanes()
{
	var aryPanes = eip.getVar('__eip_Panes').split(';');
	var aryPaneNames = eip.getVar('__eip_PaneNames').split(';');
	__eip_m_aryPanes = new Array();
	for (var i=0; i<aryPanes.length; i++)
	{
		if (aryPanes[i].length > 0)
			__eip_m_aryPanes[__eip_m_aryPanes.length] = new __eip_Pane(eip.dom.getById(aryPanes[i]), aryPaneNames[i]);
	}
}

var __eip_m_aryPanes;
var __eip_m_aryModules;
function __eip_Panes()
{
	if (__eip_m_aryPanes == null)
	{
		__eip_m_aryPanes = new Array();
		__eip_RefreshPanes();
	}
	return __eip_m_aryPanes;
}

function __eip_Modules(sModuleID)
{
	if (__eip_m_aryModules == null)
		__eip_RefreshPanes();
	
	return __eip_m_aryModules[sModuleID];
}

function __eip_getMostSelectedPane(oContent)
{
	var oCDims = new eip.dom.positioning.dims(oContent);
	var iTopScore=0;
	var iScore;
	var oTopPane;
	for (var i=0; i<__eip_Panes().length; i++)
	{
		var oPane = __eip_Panes()[i];
		var oPDims = new eip.dom.positioning.dims(oPane.pane);
		iScore = eip.dom.positioning.elementOverlapScore(oPDims, oCDims);
		
		if (iScore > iTopScore)
		{
			iTopScore = iScore;
			oTopPane = oPane;
		}
	}
	return oTopPane;
}

function __eip_getPaneControlIndex(oContent, oPane)
{
	if (oPane == null)
		return;
	var oCDims = new eip.dom.positioning.dims(oContent);
	var oCtl;
	if (oPane.controls.length == 0)
		return 0;
	for (var i=0; i<oPane.controls.length; i++)
	{
		oCtl = oPane.controls[i];
		var oIDims = new eip.dom.positioning.dims(oCtl.control);
		if (oCDims.t < oIDims.t)
			return oCtl.index;
	}
	if (oCtl != null)
		return oCtl.index+1;
	else
		return 0;
}

//Objects
function __eip_Pane(ctl, sPaneName)
{
	this.pane = ctl;
	this.id = ctl.id;
	this.controls = new Array();
	this.origBorder = ctl.style.border;
	this.paneName = sPaneName;
	
	var iIndex = 0;
	var strModuleOrder='';
	for (var i=0; i<ctl.childNodes.length; i++)
	{
		var oNode = ctl.childNodes[i];
		if (eip.dom.isNonTextNode(oNode))	
		{
			if (__eip_m_aryModules == null)
				__eip_m_aryModules = new Array();

			//if (oNode.tagName == 'A' && oNode.childNodes.length > 0)
			//	oNode = oNode.childNodes[0];	//EIP now embeds anchor tag 
				
			var sModuleID = oNode.getAttribute('moduleid');
			if (sModuleID != null && sModuleID.length > 0)
			{
				strModuleOrder += sModuleID + '~';
				this.controls[this.controls.length] = new __eip_PaneControl(oNode, iIndex);
				__eip_m_aryModules[sModuleID] = oNode.id;
				iIndex+=1;
			}
		}
	}
	this.moduleOrder = strModuleOrder;

}

function __eip_PaneControl(ctl, iIndex)
{
	this.control = ctl;
	this.id = ctl.id;
	this.index = iIndex;
	this.origBorder = ctl.style.border;
	
}

// LCM page flash load
function runSwf_intro()
{
	document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="1050" height="320">\n')
    document.write('<param name="movie" value="/LCM/images/LCM/intro.swf">\n')
    document.write('<param name="quality" value="high"/>\n')
    document.write('<param name="menu" value="false">\n')	
    document.write('<embed src="/LCM/images/LCM/intro.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="1050" height="320"></embed>\n')
    document.write('</object>\n')
}
