//General
//for example: instead of each module writing out script found in moduleMaxMin_OnClick have the functionality cached
//

var MDS_COL_DELIMITER = String.fromCharCode(16);
var MDS_ROW_DELIMITER = String.fromCharCode(15);
var __MDS_m_bPageLoaded = false;

window.onload = __MDS_Page_OnLoad;

function __MDS_ClientAPIEnabled()
{
	return typeof(MDS) != 'undefined';
}


function __MDS_Page_OnLoad()
{
	if (__MDS_ClientAPIEnabled())
	{
		var sLoadHandlers = MDS.getVar('__MDS_pageload');
		if (sLoadHandlers != null)
			eval(sLoadHandlers);
		
		MDS.dom.attachEvent(window, 'onscroll', __MDS_bodyscroll);
	}
	__MDS_m_bPageLoaded = true;
}

function __MDS_KeyDown(iKeyCode, sFunc, e)
{
	if (e == null)
		e = window.event;

	if (e.keyCode == iKeyCode)
	{
		eval(unescape(sFunc));
		return false;
	}
}

function __MDS_bodyscroll() 
{
	var oF=document.forms[0];	
	if (__MDS_ClientAPIEnabled() && __MDS_m_bPageLoaded)
		oF.ScrollTop.value=document.documentElement.scrollTop ? document.documentElement.scrollTop : MDS.dom.getByTagName("body")[0].scrollTop;
}

function __MDS_setScrollTop(iTop)
{
	if (__MDS_ClientAPIEnabled())
	{
		if (iTop == null)
			iTop = document.forms[0].ScrollTop.value;
	
		var sID = MDS.getVar('ScrollToControl');
		if (sID != null && sID.length > 0)
		{
			var oCtl = MDS.dom.getById(sID);
			if (oCtl != null)
			{
				iTop = MDS.dom.positioning.elementTop(oCtl);
				MDS.setVar('ScrollToControl', '');
			}
		}
		window.scrollTo(0, iTop);
	}
}

//Focus logic
function __MDS_SetInitialFocus(sID)
{
	var oCtl = MDS.dom.getById(sID);	
	if (oCtl != null && __MDS_CanReceiveFocus(oCtl))
		oCtl.focus();
}	

function __MDS_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 __MDS_ContainerMaxMin_OnClick(oLnk, sContentID)
{
	var oContent = MDS.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 (MDS.getVar('min_icon_' + sContainerID))
			sMinIcon = MDS.getVar('min_icon_' + sContainerID);
		else
			sMinIcon = MDS.getVar('min_icon');

		if (MDS.getVar('max_icon_' + sContainerID))
			sMaxIcon = MDS.getVar('max_icon_' + sContainerID);
		else
			sMaxIcon = MDS.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 = '';
			MDS.dom.expandElement(oContent, iNum);
			oBtn.title = MDS.getVar('min_text');
			if (sCookieID != null)
			{
				if (MDS.getVar('__MDS_' + sContainerID + ':defminimized') == 'true')
					MDS.dom.setCookie(sCookieID, 'true', 365);
				else
					MDS.dom.deleteCookie(sCookieID);
			}
			else
				MDS.setVar('__MDS_' + sContainerID + '_Visible', 'true');
		}
		else
		{
			oBtn.src = sMaxIcon;				
			//oContent.style.display = 'none';
			MDS.dom.collapseElement(oContent, iNum);
			oBtn.title = MDS.getVar('max_text');
			if (sCookieID != null)
			{
				if (MDS.getVar('__MDS_' + sContainerID + ':defminimized') == 'true')
					MDS.dom.deleteCookie(sCookieID);
				else
					MDS.dom.setCookie(sCookieID, 'false', 365);				
			}
			else
				MDS.setVar('__MDS_' + sContainerID + '_Visible', 'false');			
		}
		
		return true;	//cancel postback
	}
	return false;	//failed so do postback
}

function __MDS_Help_OnClick(sHelpID)
{
	var oHelp = MDS.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 __MDS_SectionMaxMin(oBtn, sContentID)
{
	var oContent = MDS.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 = '';
			MDS.setVar(oBtn.id + ':exp', 1);
		}
		else
		{
			oBtn.src = sMaxIcon;				
			oContent.style.display = 'none';
			MDS.setVar(oBtn.id + ':exp', 0);
		}
		return true;	//cancel postback
	}
	return false;	//failed so do postback
}

//Drag N Drop
function __MDS_enableDragDrop()
{
	var aryConts = MDS.getVar('__MDS_dragDrop').split(";");	
	var aryTitles;

	for (var i=0; i < aryConts.length; i++)
	{
		aryTitles = aryConts[i].split(" ");
		if (aryTitles[0].length > 0)
		{			
			var oCtr = MDS.dom.getById(aryTitles[0]);
			var oTitle = MDS.dom.getById(aryTitles[1]);
			if (oCtr != null && oTitle != null)
			{
				oCtr.setAttribute('moduleid', aryTitles[2]);
				MDS.dom.positioning.enableDragAndDrop(oCtr, oTitle, '__MDS_dragComplete()', '__MDS_dragOver()');
			}	
		}
	}
}

var __MDS_oPrevSelPane;
var __MDS_oPrevSelModule;
var __MDS_dragEventCount=0;
function __MDS_dragOver()
{
	__MDS_dragEventCount++;
	if (__MDS_dragEventCount % 75 != 0)	//only calculate position every 75 events
		return;
	
	var oCont = MDS.dom.getById(MDS.dom.positioning.dragCtr.contID);

	var oPane = __MDS_getMostSelectedPane(MDS.dom.positioning.dragCtr);
		
	if (__MDS_oPrevSelPane != null)	//reset previous pane's border
		__MDS_oPrevSelPane.pane.style.border = __MDS_oPrevSelPane.origBorder;

	if (oPane != null)
	{		
		__MDS_oPrevSelPane = oPane;
		oPane.pane.style.border = '4px double ' + MDS_HIGHLIGHT_COLOR;
		var iIndex = __MDS_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 (__MDS_oPrevSelModule != null)
			MDS.dom.getNonTextNode(__MDS_oPrevSelModule.control).style.border = __MDS_oPrevSelModule.origBorder;
			

		if (oNextCtl != null)
		{
			__MDS_oPrevSelModule = oNextCtl;
			MDS.dom.getNonTextNode(oNextCtl.control).style.borderTop = '5px groove ' + MDS_HIGHLIGHT_COLOR;
		}
		else if (oPrevCtl != null)
		{
			__MDS_oPrevSelModule = oPrevCtl;
			MDS.dom.getNonTextNode(oPrevCtl.control).style.borderBottom = '5px groove ' + MDS_HIGHLIGHT_COLOR;
		}
	}
}

function __MDS_dragComplete()
{
	var oCtl = MDS.dom.getById(MDS.dom.positioning.dragCtr.contID);
	var sModuleID = oCtl.getAttribute('moduleid');
	
	if (__MDS_oPrevSelPane != null)
		__MDS_oPrevSelPane.pane.style.border = __MDS_oPrevSelPane.origBorder;

	if (__MDS_oPrevSelModule != null)
		MDS.dom.getNonTextNode(__MDS_oPrevSelModule.control).style.border = __MDS_oPrevSelModule.origBorder;
		
	var oPane = __MDS_getMostSelectedPane(MDS.dom.positioning.dragCtr);
	var iIndex;
	if (oPane == null)
	{
		var oPanes = __MDS_Panes();
		for (var i=0; i<oPanes.length; i++)
		{
			if (oPanes[i].id == oCtl.parentNode.id)
				oPane = oPanes[i];
		}
	}	
	if (oPane != null)
	{
		iIndex = __MDS_getPaneControlIndex(oCtl, oPane);
		__MDS_MoveToPane(oPane, oCtl, iIndex);

		MDS.callPostBack('MoveToPane', 'moduleid=' + sModuleID, 'pane=' + oPane.paneName, 'order=' + iIndex * 2); 
	}
}

function __MDS_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;

			MDS.dom.removeChild(oPane.controls[i].control);
		}
		MDS.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++)
		{
			MDS.dom.appendChild(oPane.pane, aryCtls[i]);
		}
		__MDS_RefreshPanes();
	}
	else
	{
		oCtl.style.top=0;
		oCtl.style.left=0;
		oCtl.style.position = 'relative';
	}
}

function __MDS_RefreshPanes()
{
	var aryPanes = MDS.getVar('__MDS_Panes').split(';');
	var aryPaneNames = MDS.getVar('__MDS_PaneNames').split(';');
	__MDS_m_aryPanes = new Array();
	for (var i=0; i<aryPanes.length; i++)
	{
		if (aryPanes[i].length > 0)
			__MDS_m_aryPanes[__MDS_m_aryPanes.length] = new __MDS_Pane(MDS.dom.getById(aryPanes[i]), aryPaneNames[i]);
	}
}

var __MDS_m_aryPanes;
var __MDS_m_aryModules;
function __MDS_Panes()
{
	if (__MDS_m_aryPanes == null)
	{
		__MDS_m_aryPanes = new Array();
		__MDS_RefreshPanes();
	}
	return __MDS_m_aryPanes;
}

function __MDS_Modules(sModuleID)
{
	if (__MDS_m_aryModules == null)
		__MDS_RefreshPanes();
	
	return __MDS_m_aryModules[sModuleID];
}

function __MDS_getMostSelectedPane(oContent)
{
	var oCDims = new MDS.dom.positioning.dims(oContent);
	var iTopScore=0;
	var iScore;
	var oTopPane;
	for (var i=0; i<__MDS_Panes().length; i++)
	{
		var oPane = __MDS_Panes()[i];
		var oPDims = new MDS.dom.positioning.dims(oPane.pane);
		iScore = MDS.dom.positioning.elementOverlapScore(oPDims, oCDims);
		
		if (iScore > iTopScore)
		{
			iTopScore = iScore;
			oTopPane = oPane;
		}
	}
	return oTopPane;
}

function __MDS_getPaneControlIndex(oContent, oPane)
{
	if (oPane == null)
		return;
	var oCDims = new MDS.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 MDS.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 __MDS_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 (MDS.dom.isNonTextNode(oNode))	
		{
			if (__MDS_m_aryModules == null)
				__MDS_m_aryModules = new Array();

			//if (oNode.tagName == 'A' && oNode.childNodes.length > 0)
			//	oNode = oNode.childNodes[0];	//MDS now embeds anchor tag 
				
			var sModuleID = oNode.getAttribute('moduleid');
			if (sModuleID != null && sModuleID.length > 0)
			{
				strModuleOrder += sModuleID + '~';
				this.controls[this.controls.length] = new __MDS_PaneControl(oNode, iIndex);
				__MDS_m_aryModules[sModuleID] = oNode.id;
				iIndex+=1;
			}
		}
	}
	this.moduleOrder = strModuleOrder;

}

function __MDS_PaneControl(ctl, iIndex)
{
	this.control = ctl;
	this.id = ctl.id;
	this.index = iIndex;
	this.origBorder = ctl.style.border;
	
}

/*
function __MDS_TableRowReorder(ctl, bUp, sKey)
{
	var oTR;
	var oPTR;
	var oP = ctl.parentNode;

	while (oP != null)
	{
		if  (oP.tagName.toLowerCase() == 'tr')
		{
			oTR = oP;
			break;
		}		
		oP = oP.parentNode;
	}
	if (oTR != null)
	{
		oPTR = oTR.parentNode;
		var iIndex = oTR.rowIndex;
		if (oTR.getAttribute('origidx') == '-1')
			__MDS_TableRowSetOrigIndex(oPTR);

		var iNextIdx;
		if (bUp)
			iNextIdx = __MDS_TableRowGetPrevNodeIdx(oPTR, iIndex-1);
		else
			iNextIdx = __MDS_TableRowGetNextNodeIdx(oPTR, iIndex+1);
		if (iNextIdx > -1)
		{
			MDS.dom.removeChild(oTR);
			if (oPTR.childNodes.length > iNextIdx)
				oPTR.insertBefore(oTR, oPTR.childNodes(iNextIdx));
			else
				oPTR.appendChild(oTR);
			
			MDS.setVar(sKey,__MDS_GetTableRowOrder(oPTR));
		}
		return true;//handled
	}
}

function __MDS_TableRowGetNextNodeIdx(oParent, iStartIndex)
{
	for (var i=iStartIndex; i<oParent.childNodes.length; i++)
	{
		var oCtl = oParent.childNodes(i);
		if (oCtl.getAttribute('origidx'))
			return i;
	}
	return -1;
}

function __MDS_TableRowGetPrevNodeIdx(oParent, iStartIndex)
{
	for (var i=iStartIndex; i>=0; i--)
	{
		var oCtl = oParent.childNodes(i);
		if (oCtl.getAttribute('origidx'))
			return i;
	}
	return -1;
}

function __MDS_TableRowSetOrigIndex(oParent)
{
	var iIdx=0;
	for (var i=0; i<oParent.childNodes.length; i++)
	{
		var oCtl = oParent.childNodes(i);
		if (oCtl.getAttribute('origidx'))
		{
			oCtl.setAttribute('origidx', iIdx.toString());
			iIdx++;
		}
	}
}

function __MDS_GetTableRowOrder(oParent)
{
	var sIdx;
	var sRet='';
	for (var i=0; i<oParent.childNodes.length; i++)
	{
		var oCtl = oParent.childNodes(i);
		sIdx = oCtl.getAttribute('origidx');
		if (sIdx)
			sRet+= (sRet.length>0 ? ',':'') + sIdx;
	}
	return sRet;
}
*/

//--------------20050429 tabpane

function __MDS_MouseOverTabPane(e){
	if (e != null){
		if (e.checkshow == 'false'){
			e.all.namedItem("left").className="TabPaneActiveLeft";
			e.all.namedItem("back").className="TabPaneActiveCenter";
			e.all.namedItem("right").className="TabPaneActiveRight";
		}
	}
}

function __MDS_MouseOutTabPane(e){
	if (e != null){
		if (e.checkshow == 'false'){
				e.all.namedItem("left").className="TabPaneNormalLeft";
				e.all.namedItem("back").className="TabPaneNormalCenter";
				e.all.namedItem("right").className="TabPaneNormalRight";

		}
	}
}

function __MDS_SetTabPane(sFirstTabPaneId, e){	

	var aryTabPanes=MDS.getVar(sFirstTabPaneId).split(";");
	for (var i = 0; i< aryTabPanes.length-1; i++){
		var oTabPane=MDS.dom.getById(aryTabPanes[i]);
               if (oTabPane!=null) 
               {
		var oSection=MDS.dom.getById(oTabPane.getAttribute("sectionid"));
		if(oTabPane.id==e.id)
		{
			oTabPane.className="TabPaneActive";
			oTabPane.all.namedItem("left").className="TabPaneActiveLeft";
			oTabPane.all.namedItem("back").className="TabPaneActiveCenter";
			oTabPane.all.namedItem("right").className="TabPaneActiveRight";
			
			oSection.style.display='';
			oTabPane.checkshow='true';
			MDS.setVar(sFirstTabPaneId + ':SelectedTabPaneId', e.id);
		}
		else
		{
			oTabPane.className="TabPaneNormal";
			oTabPane.all.namedItem("left").className="TabPaneNormalLeft";
			oTabPane.all.namedItem("back").className="TabPaneNormalCenter";
			oTabPane.all.namedItem("right").className="TabPaneNormalRight";
			
			oSection.style.display='none';
			oTabPane.checkshow='false';
			//MDS.SETVAR(oTabPane.id + ':exp', 0);
		}
		
	     }	
	}

}


//--------------------------