var dragElement = null;
var elY;
var mouseDownY;
var oTr;
var oMt;
var dropLoc;
var scrollMenuOffset = 210;

var pullMenu;
var drawMenu;
var boundryR = 10;
var boundryL = -160;
var fLayoutChanged = false;

function sizeDragWindow (menuElement) {
	document.getElementById("protectLayer").style.display = '';
	oPane = document.getElementById("windowPane").style
	if ( moz || saf) oPane.opacity = 0.2;
	oPane.display = '';

	oTr = menuElement.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;

	oMt = document.getElementById("MenuTable");
 
	// Size the drag window using the row selected
	oPane.top = oTr.offsetTop+oMt.offsetTop+15;
	oPane.left = oTr.offsetLeft+oMt.offsetLeft;
	oPane.width = oTr.offsetWidth-5;
	oPane.height = oTr.offsetHeight-2;
 }

function startDrag (element, e) {
	dragElement = element;
	mouseDownY = e.clientY;
	document.onmousemove = drag;
	document.onmouseup = stopDrag;
}

function drag (e) {
	if (document.all) {
	window.event.returnValue = false;
		menuPos = event.clientY + window.document.body.scrollTop;
		dragElement.style.pixelTop = menuPos;
		if (event.clientY > window.document.body.clientHeight - 20)
			window.scrollBy(0, 10)
		else if (event.clientY < 10)
			window.scrollBy(event.clientY, -10);

	} else if (document.getElementById) {
		menuPos = e.clientY + self.pageYOffset;
		dragElement.style.top = menuPos + 'px';
		if (mouseDownY > self.innerHeight - 20) {
			window.scrollBy(0, 10);
			dragElement.style.top = e.clientY + self.pageYOffset + 10;
		} else if (mouseDownY < 20)
			window.scrollBy(mouseDownY, -10);
    }
}

function stopDrag () {
	var myPage = self.location.toString();
	if (myPage.indexOf("StartPage") != -1) {
		document.onmousemove = checkPos;
	}
    else {
		document.onmousemove = null;
    }
	document.onmouseup = null;
	
	dropLoc = dragElement.offsetTop-oMt.offsetTop;  
	dragElement.style.display = 'none';
	document.getElementById("protectLayer").style.display = 'none'; 
	if ( moz ) setFocus("protectLayer");
	insertMenu(); 
}

function collection () {
    // Extract positions of all row and collect in array
	var oPlacement = new Array();
	var oRows = document.getElementById('TableLayout').rows;
	for (j=0; j<oRows.length; j++){
		oPlacement[j]= oRows[j].offsetTop+13;
		}
	oPlacement[oRows.length]=oRows[oRows.length-1].offsetTop+oRows[oRows.length-1].offsetHeight+20;
	return oPlacement;
} 

function insertMenu (){
	oPos = collection();
	if (dropLoc < 0+20){
		moveMenuRow(oTr.rowIndex, 0);
		}
	else{
		for (j=0; j<oPos.length; j++){		
			if (dropLoc > oPos[j+1]-60 && dropLoc < oPos[j+1]) {
				moveMenuRow(oTr.rowIndex, j+1);
				}
			}
		}	
}

function moveMenuRow(RowIndex, NewPosition) {
	var oTable = document.getElementById("TableLayout");
	var sHTML, sStyle;
	
	sHTML = oTable.rows[RowIndex].cells[0].innerHTML;
	sStyle = oTable.rows[RowIndex].style.display;
	trNewRow = oTable.insertRow(NewPosition);
	trNewRow.insertCell(0);
	oTable.rows[NewPosition].cells[0].innerHTML = sHTML;
	oTable.rows[NewPosition].style.display = sStyle;
	if (RowIndex < NewPosition)
		oTable.deleteRow(RowIndex)
	else
		oTable.deleteRow(RowIndex+1);
	
	fLayoutChanged = true;
}

if ( ie )
{
	if(document.getElementById('ContentWindow')){
		document.getElementById('ContentWindow').style.width = '80%';
	}
}

function pull() {
	//clearInterval(drawMenu); ' do not delete commented out codes
	//pullMenu = setInterval("pullTLMSession()", 10);
	pullTLMSession();
	posContentR();
}

function posContentR() {
	oContentWindow.style.left =180;
	document.getElementById('lblPageTitleHelp').style.left = 180;
	document.getElementById('slidebuttonout').style.display = 'none';
	document.getElementById('slidebuttonin').style.display = '';
	if (document.getElementById('ScrollMenu')){
	document.getElementById('ScrollMenu').style.left = 181;
	}
	/* pixelLeft for IE6 only
	oContentWindow.style.pixelLeft =180;
	document.getElementById('lblPageTitleHelp').style.pixelLeft = 180;
	document.getElementById('slidebuttonout').style.display = 'none';
	document.getElementById('slidebuttonin').style.display = '';
	if (document.getElementById('ScrollMenu')){
	document.getElementById('ScrollMenu').style.pixelLeft = 181;
	}*/
}

function draw(Quick) {
	//clearInterval(pullMenu); ' do not delete commented out codes
	//if (Quick)
		drawTLMSession(true)
	//else
		//drawMenu = setInterval("drawTLMSession(false)", 10);
	posContentL();
}

function posContentL() {
	oContentWindow.style.left = 15;
	document.getElementById('lblPageTitleHelp').style.left = 15;
	document.getElementById('slidebuttonin').style.display = 'none';
	document.getElementById('slidebuttonout').style.display = '';
	if (document.getElementById('ScrollMenu')){
	document.getElementById('ScrollMenu').style.left = 16;
	}
	/* pixelLeft for IE6 only
	oContentWindow.style.pixelLeft = 15;
	document.getElementById('lblPageTitleHelp').style.pixelLeft = 15;
	document.getElementById('slidebuttonin').style.display = 'none';
	document.getElementById('slidebuttonout').style.display = '';
	if (document.getElementById('ScrollMenu')){
	document.getElementById('ScrollMenu').style.pixelLeft = 16;
	}
	*/
}

function pullTLMSession() {
    // IE6 may require pixelLeft
		document.getElementById('MenuTable').style.left = boundryR;
	//if (document.getElementById('MenuTable').style.pixelLeft < boundryR) ' do not delete commented out codes
	//	document.getElementById('MenuTable').style.pixelLeft += 5
	//else {
	//	clearInterval(pullMenu);
		if ( ie )
		{
			document.getElementById('ContentWindow').style.width = '80%';
		}	
		fLayoutChanged = true;
	//}
}

function drawTLMSession(Quick) {
	if (Quick) {
		// IE6 may require pixelLeft
		document.getElementById('MenuTable').style.left = boundryL;
		if ( ie )
		{
			document.getElementById('ContentWindow').style.width = '90%';
		}		
	} else {
		if (document.getElementById('MenuTable').style.left > boundryL)
			document.getElementById('MenuTable').style.left -= 5
		else {
			clearInterval(drawMenu);
			fLayoutChanged = true;
		}
	}
}

function menuCollapse () {
	var oRows = document.getElementById('TableLayout').rows;

	for (j=0; j<oRows.length; j++){
		menutrackTop = document.getElementById('TableLayout').rows[j].cells[0].getElementsByTagName("TABLE")[0];
		menutrackMid = menutrackTop.rows[1].cells[0].getElementsByTagName("TABLE")[0];
		if ( ie ) {
			menutrackFinal = menutrackMid.rows[0].cells[1].childNodes[0];
		}
		if ( moz || saf) {
			menutrackFinal = menutrackMid.rows[0].cells[1].childNodes[1];
		}
		menutrackFinal.style.display='none';
	}
	
	fLayoutChanged = true;
}

function menuExpand () {
	var oRows = document.getElementById('TableLayout').rows;
	for (j=0; j<oRows.length; j++){
		menutrackTop = document.getElementById('TableLayout').rows[j].cells[0].getElementsByTagName("TABLE")[0];
		menutrackMid = menutrackTop.rows[1].cells[0].getElementsByTagName("TABLE")[0];
		if ( ie ) {
			menutrackFinal = menutrackMid.rows[0].cells[1].childNodes[0];
		}
		if ( moz || saf ) {
			menutrackFinal = menutrackMid.rows[0].cells[1].childNodes[1];
		}
		menutrackFinal.style.display='';
	}
	
	fLayoutChanged = true;
}

function scrollPage(){
	scroll( 0, 0 );
	//window.focus();
}

function savePrefs() {
	var oTable = document.getElementById("TableLayout");
	var sGroupID = "";
	var sOrder = "";
	var sState = "";
	var j;
	
	if (!fLayoutChanged) return;
	
	
	for (j=0; j<oTable.rows.length; j++) {
		if ( ie ) {
			sGroupID = oTable.rows[j].cells[0].getElementsByTagName("TABLE")[0].rows[0].cells[1].childNodes[0].id.toString().substr(3);
		}
		if ( moz || saf ){
			sGroupID = oTable.rows[j].cells[0].getElementsByTagName("TABLE")[0].rows[0].cells[1].childNodes[1].id.toString().substr(3);
		}

		sOrder += "," + sGroupID + "-";
		
		if (getRowVis(sGroupID))
			sOrder += "1"
		else
			sOrder += "0";

	}
	
	sOrder = sOrder.substr(1);
	
	/* pixelLeft maybe required for IE6*/
	if (document.getElementById("MenuTable").style.left == boundryL)
		sState = "1;" + sOrder
	else
		sState = "0;" + sOrder;
	
	postHTTP("SaveMiscData.aspx?type=MenuBarState", sState);
}

	
function getRowVis( GroupID ) {
	RowObj = document.getElementById("Menu" + GroupID + "list").style;
		
	return (RowObj.display != "none");
}

function setRowVis(GroupID) {
	RowObj = document.getElementById("Menu" + GroupID + "list").style;
		
	if (getRowVis(GroupID)) {
		RowObj.display = "none";
	} else {
		RowObj.display = "";
	}
	
	fLayoutChanged = true;
}

function initMenuBar(GroupOrder, Drawn) {
	var asOrder;
	var j, asGroup;
	
	if (GroupOrder.length != 0) {
		// If length were 0, it would mean that the default settings were to be used
		// Hence, the following block needs to be executed only when length is not 0.
		asOrder = GroupOrder.split(",");
		for (j=0; j<asOrder.length; j++) {
			asGroup = asOrder[j].split("-");
			setGroupPosition(asGroup[0], j, asGroup[1]);
		}
	}
	
	if (Drawn==1) {
		draw(true);
	} else {
		document.getElementById("MenuTable").style.display = "";
		document.getElementById("lblPageTitleHelp").style.display = "";
		document.getElementById("ScrollMenu").style.display = "";
	}	
	
	//Reset fLayoutChanged after all the initializations
	fLayoutChanged = false;
}

function setGroupPosition(GroupID, NewPosition, State) {
	var oItem, oTR;

	oItem = document.getElementById('tab' + GroupID);
	oTR = oItem.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
	
	moveMenuRow(oTR.rowIndex, NewPosition);
	
	if (State==0) setRowVis(GroupID);
}

function quickScroll() {
	objS = document.getElementById('ScrollMenu');
	var oContentWindowSc = document.getElementById("ContentWindow");		
	var nContentBottom = oContentWindowSc.offsetHeight+oContentWindowSc.offsetTop;
	var incCheck = document.body.scrollTop+scrollMenuOffset;
	
	if (incCheck > nContentBottom-40)
		return;
	else
		objS.style.pixelTop=document.body.scrollTop+scrollMenuOffset;
	
	if (document.getElementById('toTop')) {
		if (document.body.scrollTop > 0) {
			document.getElementById('toTop').style.display = '';
			document.getElementById('noTop').style.display = 'none';
		} else {
			document.getElementById('toTop').style.display = 'none';
			document.getElementById('noTop').style.display = '';	
		}
	}
	
	//setTimeout("quickScroll()",1000);
	
	//save this code in case	
	/*if(document.body.scrollTop < oContentWindow.offsetHeight+oContentWindow.offsetTop){
		if ((document.body.scrollTop+200) > (oContentWindow.offsetHeight+oContentWindow.offsetTop))
			eval(document.getElementById('ScrollMenu').style.pixelTop=oContentWindow.offsetHeight+oContentWindow.offsetTop-100)
		else
			eval(document.getElementById('ScrollMenu').style.pixelTop=document.body.scrollTop+200);

		setTimeout("quickScroll()",1000);
	}

	if (document.body.scrollTop > oContentWindow.offsetHeight){
		eval(document.getElementById('ScrollMenu').style.pixelTop=oContentWindow.offsetHeight+oContentWindow.offsetTop-100);
		setTimeout("quickScroll()",1000);
	}
	For netscape should it ever be used
	if(!document.all && document.getElementById){
	eval(document.getElementById('ScrollMenu').style.top=window.pageYOffset+200);
	setTimeout("quickScroll()",1000);
	}*/
}

