
var myTopMenu = null;
var spanHoverTopTD = null;

var menuOverImg = new Image();
var menuOutImg = new Image();
var menuSpacer = new Image();
var menuSpacerHover = new Image();

menuOverImg.src = "/images/top_menu/menuitem_bg_hover.gif";
menuOutImg.src = "/images/top_menu/menuitem_bg.gif";
menuSpacer.src = "/images/top_menu/menuspacer.gif";
menuSpacerHover.src = "/images/top_menu/menuspacer_hover.gif";

var isWaiting = false;
var allowClicks = true;
var SelectedMenuTD = null;

function SubMenuItem( Index, Title, PageURL ){

	this.ref = null;
	this.index = Index;
	this.title = Title;
	this.selected = 0;
	this.pageURL = PageURL;

	this.anch = null;
	this.td = null;

	this.create = function CreateSubMenuItem(){

		this.anch = document.createElement("A");
		this.anch.href = this.pageURL;
		this.anch.innerHTML = this.title;
		this.anch.style.color = (this.index == 0)?('#362600'):('#69A9A9');
		this.anch.style.fontFamily = ' "Gill Sans MT", Arial, Helvetica, sans-serif ';
		this.anch.style.textDecoration = 'none';
		this.anch.style.fontSize = '13';
		this.anch.style.fontWeight = 'bold';
		this.anch.style.zIndex = 999;
		this.anch.ref = this;

		this.anch.onclick = function anonymous(){ return false; }

		this.td = document.createElement("TD");
		this.td.noWrap = true;
		this.td.height = 23;
		this.td.style.backgroundColor = (this.index == 0)?('#A2C6D2'):('#D0E1E8');
		this.td.style.paddingLeft = '10px';
		this.td.style.paddingRight = '10px';
		this.td.style.paddingBottom = '2px';
		this.td.style.cursor = 'hand';
		this.td.style.position = 'relative';
		this.td.style.top = 0;
		this.td.anch = this.anch;
		this.td.ref = this;

		this.td.onclick = function anonymous(){

			if( !this.ref.selected ){

				this.ref.selected = 1;

				for(var s=0;s<this.ref.ref.subitems.length;s++){
					if( this.ref.ref.subitems[s].td != this ){
						this.ref.ref.subitems[s].td.style.backgroundColor = '#D0E1E8';
						this.ref.ref.subitems[s].anch.style.color = '#69A9A9';
						this.ref.ref.subitems[s].selected = 0;
					}
				}
				this.style.backgroundColor = '#A2C6D2';
				this.anch.style.color = '#362600';

				document.getElementById("MainContentiFrameID").src = this.anch.href;
			}
			else
			{
				currentPage = '' + window.frames[0].document.location;
				correctPage = currentPage.substring( 0, currentPage.lastIndexOf('/')+1 ) + this.ref.anch.href;

				//compare page and if page is not correct then change to correct page
				if( currentPage != correctPage ){

					this.ref.selected = 1;

					for(var s=0;s<this.ref.ref.subitems.length;s++){
						if( this.ref.ref.subitems[s].td != this ){
							this.ref.ref.subitems[s].td.style.backgroundColor = '#D0E1E8';
							this.ref.ref.subitems[s].anch.style.color = '#69A9A9';
							this.ref.ref.subitems[s].selected = 0;
						}
					}
					this.style.backgroundColor = '#A2C6D2';
					this.anch.style.color = '#362600';

					document.getElementById("MainContentiFrameID").src = this.anch.href;
				}
			}
		}

		this.td.appendChild( this.anch );

		return this.td;
	}
}

function TopMenuItem( Index, DocID, Title, PageURL, Width, MoveLeftOnTransitionAmount ){

	this.selected = false;
	this.leftSpacer = null;
	this.rightSpacer = null;
	this.index = Index;
	this.ref = null;
	this.title = Title;

	this.subitems = new Array();
	this.addSub = function AddSubTopMenuItem( Index, Title, PageURL ){

		sIndex = this.subitems.length;
		this.subitems[sIndex] = new SubMenuItem( Index, Title, PageURL );
		this.subitems[sIndex].ref = this;
	}
	this.showSubs = function ShowSubsTopMenuItem(){

		if( this.subitems.length > 0 ){

			for(var si=0;si<this.subitems.length;si++){
				this.subRow.appendChild( this.subitems[si].create() );
			}
			this.lastSubItem = document.createElement("TD");
			this.lastSubItem.width = '100%';
			this.lastSubItem.id = 'SelectedMenuEnd';
			this.lastSubItem.style.backgroundRepeat = 'no-repeat';
			this.lastSubItem.style.backgroundPosition = 'right';
			this.lastSubItem.style.backgroundImage = 'url(/images/bottom_menu/menuend.gif)';
			this.subRow.appendChild( this.lastSubItem );
		}
	}
	this.hideSubs = function HideSubsTopMenuItem(){

		if( this.subitems.length > 0 ){

			for(var si=0;si<this.subitems.length;si++){
				this.subitems[si].selected = 0;
				this.subRow.removeChild( this.subitems[si].td );
			}
			this.subitems[0].selected = 1;
			this.subRow.removeChild( this.lastSubItem );
		}
	}

	this.subRow = document.getElementById("BottomMenuRow");
	this.lastSubItem = null;

	this.anch = document.createElement("A");
	this.anch.id = DocID + 'A';
	this.anch.href = PageURL;
	this.anch.innerHTML = Title;
	this.anch.style.color = '#FFFFFF';
	this.anch.style.fontFamily = 'Verdana';
	this.anch.style.textDecoration = 'none';
	this.anch.style.fontSize = '10';
	this.anch.style.fontWeight = 'bold';
	this.anch.style.position = 'relative';
	this.anch.style.top = 0;
	this.anch.style.left = 0;
	this.anch.style.zIndex = 1000;
	this.anch.ref = this;

	this.anch.onclick = function anonymous(){ return false; }

	this.td = document.createElement("TD");
	this.td.ref = this;
	this.td.id = DocID;
	this.td.width = Width;
	this.td.height = 28;
	this.td.align = 'center';
	this.td.vAlign = 'top';
	this.td.noWrap = true;
	this.td.style.backgroundColor = '#D0E1E8';
	this.td.style.backgroundImage = 'url(/images/top_menu/menuitem_bg.gif)';
	this.td.style.backgroundRepeat = 'repeat-x';
	this.td.style.paddingTop = '8px';
	this.td.style.cursor = 'hand';
	this.td.anch = this.anch;
	this.td.moveLeftAmount = (MoveLeftOnTransitionAmount)?(MoveLeftOnTransitionAmount):(null);
	this.td.onmouseover = function anonymous(){

		if( !this.ref.selected ){

			myTdObject = this;
			if(menuOverImg)
				myTdObject.style.backgroundImage = 'url('+menuOverImg.src+')';
			else
				timerOVR = setTimeout( "myTdObject.onmouseover();", 500 );

			leftAmt = ( this.ref.index * 3 ) + 45 + ( (this.moveLeftAmount)?(this.moveLeftAmount):(0) );
			widthAmt = parseInt( this.width ) + 6;

			spanHoverTopTD.style.width = widthAmt;
			spanHoverTopTD.style.left = leftAmt;
			spanHoverTopTD.style.visibility = 'visible';

			if( this.ref.leftSpacer ) this.ref.leftSpacer.td.style.backgroundImage = 'url(' + menuSpacerHover.src + ')';
			if( this.ref.rightSpacer ) this.ref.rightSpacer.td.style.backgroundImage = 'url(' + menuSpacerHover.src + ')';
		}
	}
	this.td.onmouseout = function anonymous(){

		if( !this.ref.selected ){

			myTdObject = this;
			if(menuOutImg)
				myTdObject.style.backgroundImage = 'url('+menuOutImg.src+')';
			else
				timerOVR = setTimeout( "myTdObject.onmouseout();", 500 );

			spanHoverTopTD.style.visibility = 'hidden';

			if( this.ref.leftSpacer ) this.ref.leftSpacer.td.style.backgroundImage = 'url(' + menuSpacer.src + ')';
			if( this.ref.rightSpacer ) this.ref.rightSpacer.td.style.backgroundImage = 'url(' + menuSpacer.src + ')';
		}
	}
	this.td.onclick = function anonymous(){

		if( allowClicks && !this.ref.selected ){

			this.onmouseout();
			this.ref.selected = true;

			document.getElementById("MainContentiFrameID").src = this.ref.anch.href;

			if( this.ref.ref.selected >= 0 && this.ref.ref.selected < this.ref.ref.menuitems.length ){

				this.ref.ref.menuitems[this.ref.ref.selected].setUnselected();
			}

			allowClicks = false;
			isWaiting = true;

			this.anch.style.fontFamily = ' "Gill Sans MT" ';

			TransitionObjectDownTo( this.anch, 19, ( -1*this.moveLeftAmount ) );
			fade2Color( this.anch, "#362600" );
			HideMenuText( this, this.anch );

			//set the first sub item as selected
			if( this.ref.subitems.length > 0 ) this.ref.subitems[0].selected = 1;
		}
	}

	this.td.appendChild( this.anch );

	this.setSelected = function SetSelectedTopMenuItem(){

		this.selected = true;
		this.ref.selected = this.index;

		this.anch.style.top = 0;
		this.anch.style.left = 0;
		this.anch.innerHTML = "<img src='/images/top_menu/selectedmenuitem.gif' border=0>";

		this.td.style.backgroundImage = 'url(/images/top_menu/menuitem_bg_selected.gif)';
		this.showSubs();
	}

	this.setUnselected = function SetUnselectedTopMenuItem(){

		this.selected = false;

		this.anch.innerHTML = this.title;
		this.anch.style.color = '#FFFFFF';
		this.anch.style.fontFamily = 'Verdana';
		this.anch.style.textDecoration = 'none';
		this.anch.style.fontSize = '10';
		this.anch.style.fontWeight = 'bold';
		this.anch.style.position = 'relative';
		this.anch.style.top = 0;
		this.anch.style.left = 0;

		this.td.style.backgroundImage = 'url(/images/top_menu/menuitem_bg.gif)';
		this.hideSubs();
	}
}

function TopMenuSpacer(){

	this.td = document.createElement("TD");
	this.td.width = 3;
	this.td.height = 28;
	this.td.style.fontSize = 1;
	this.td.style.backgroundColor = '#D0E1E8';
	this.td.style.backgroundImage = 'url(/images/top_menu/menuspacer.gif)';
	this.td.style.backgroundRepeat = 'no-repeat';
	this.td.innerHTML = '&nbsp;';
}

function TopMenuObject( parentRowId, DocID ){

	this.row = document.getElementById( parentRowId );
	this.selected = 0;

	this.id = DocID;
	this.spacers = new Array();
	this.menuitems = new Array();

	//menu start TD
	this.startTd = document.createElement("TD");
	this.startTd.width = 45;
	this.startTd.height = 28;
	this.startTd.style.fontSize = 1;
	this.startTd.style.backgroundColor = '#D0E1E8';
	this.startTd.style.backgroundImage = 'url(/images/top_menu/menustart.gif)';
	this.startTd.style.backgroundRepeat = 'no-repeat';
	this.startTd.innerHTML = '&nbsp;';
	this.row.appendChild( this.startTd );

	//menu end TD
	this.endTd = document.createElement("TD");
	this.endTd.width = 2;
	this.endTd.height = 28;
	this.endTd.align = 'right';
	this.endTd.vAlign = 'top';
	this.endTd.noWrap = true;
	this.endTd.style.fontSize = 1;
	this.endTd.style.backgroundColor = '#D0E1E8';
	this.endTd.style.backgroundImage = 'url(/images/top_menu/menuitem_bg.gif)';
	this.endTd.style.backgroundRepeat = 'repeat-x';
	this.endTd.innerHTML = "<img src='/images/top_menu/menuend.gif' width=2 height=28 border=0>";

	this.add = function AddTopMenuObject( Title, PageURL, Width ){

		prevWidth = 0;
		for(var m=0;m<this.menuitems.length;m++){ prevWidth += parseInt( this.menuitems[m].td.width );}

		sIndex = this.spacers.length;
		this.spacers[sIndex] = new TopMenuSpacer();

		this.row.appendChild( this.spacers[sIndex].td );

		mIndex = this.menuitems.length;
		this.menuitems[mIndex] = new TopMenuItem( mIndex, this.id+'m'+mIndex, Title, PageURL, Width, prevWidth );
		this.menuitems[mIndex].ref = this;

		//set the selected menu item
		if( mIndex == this.selected ) this.menuitems[mIndex].setSelected();

		//associate the left side spacer with the menu item
		this.menuitems[mIndex].leftSpacer = this.spacers[sIndex];

		//associate the right side spacer with the menu item
		if( mIndex > 0 ) this.menuitems[mIndex-1].rightSpacer = this.spacers[sIndex];

		this.row.appendChild( this.menuitems[mIndex].td );

		this.addSub( mIndex, Title, PageURL );

		if( mIndex == 0 ) this.menuitems[mIndex].setSelected();
	}

	this.addSub = function AddSubTopMenuObject( ParentIndex, Title, PageURL ){

		childIndex = this.menuitems[ParentIndex].subitems.length;
		this.menuitems[ParentIndex].addSub( childIndex, Title, PageURL );
	}

	this.end = function EndTopMenuObject(){

		sIndex = this.spacers.length;
		this.spacers[sIndex] = new TopMenuSpacer();

		this.row.appendChild( this.spacers[sIndex].td );
		if( this.menuitems.length > 0 ) this.menuitems[this.menuitems.length-1].rightSpacer = this.spacers[sIndex];

		prevWidth = 0;
		for(var c=0;c<this.row.cells.length;c++){ prevWidth += parseInt( this.row.cells[c].width ); }
		this.endTd.width = 960 - prevWidth;

		this.row.appendChild( this.endTd );
	}

	this.selectMenu = function selectMenuMenuObject( topIndex, lowIndex ){

		//unselect the selected menu item
		if( this.selected >= 0 && this.selected < this.menuitems.length ) this.menuitems[this.selected].setUnselected();

		//set ref to menu objects
		topMenuObj = this.menuitems[topIndex];
		subMenuObj = this.menuitems[topIndex].subitems[lowIndex];

		//unselect all menu items
		for(var g=0;g<this.menuitems.length;g++){
			this.menuitems[g].selected = false;
			for(var h=0;h<this.menuitems[g].subitems.length;h++){
				this.menuitems[g].subitems[h].selected = 0;
			}
		}

		//setup the selected items
		this.selected = topIndex;
		topMenuObj.selected = true;

		//document.getElementById("MainContentiFrameID").src = subMenuObj.pageURL;

		allowClicks = false;
		isWaiting = true;

		topMenuObj.anch.style.fontFamily = ' "Gill Sans MT" ';

		TransitionObjectDownTo( topMenuObj.anch, 19, ( -1*topMenuObj.td.moveLeftAmount ) );
		fade2Color( topMenuObj.anch, "#362600" );

		HideMenuText( topMenuObj.td, topMenuObj.anch );

		this.selectSubMenu( topIndex, lowIndex );
	}

	this.selectSubMenu = function selectSubMenuMenuObject( topIndex, lowIndex ){

		curM = this;
		curT = topIndex;
		curL = lowIndex;

		if( !allowClicks ){
			SM = setTimeout( "curM.selectSubMenu( curT, curL);", 100 );
		} else {
			curM.menuitems[curT].subitems[curL].td.onclick();
		}
	}
}


/***********************************************************************************************************************************************/


/***********************************************************************************************************************************************/


function px2Int( pxString ){ return parseInt( pxString.substring( 0, pxString.length - 2 ) ); }

function TransitionObjectDownTo( objToMove, intRestingPosition, intMoveLeftAmount ){

	curPosition = px2Int( objToMove.style.top );

	//increase the font size as it moves
	if( curPosition % 2 == 0 ){
		if( px2Int( objToMove.style.fontSize ) < 13 )
			objToMove.style.fontSize = px2Int( objToMove.style.fontSize ) + 1;
	}

	curObject = objToMove;
	curRestingPosition = intRestingPosition;
	curMoveLeftAmount = intMoveLeftAmount;

	if( curPosition < curRestingPosition ){

		if( curPosition + 2  < curRestingPosition ) objToMove.style.top = curPosition + 2;
		else objToMove.style.top = curRestingPosition;

		T = setTimeout( "TransitionObjectDownTo( curObject, curRestingPosition, curMoveLeftAmount );", 25 );
	} else {

		objToMove.style.top = curPosition + 1;

		if( intMoveLeftAmount < px2Int( objToMove.style.left ) ){

			if( objToMove.ref.index == 1 )
				leftSpeed = 5;
			else if( objToMove.ref.index == 2 )
				leftSpeed = 6;
			else if( objToMove.ref.index == 3 )
				leftSpeed = 9;
			else if( objToMove.ref.index == 4 )
				leftSpeed = 12;
			else if( objToMove.ref.index == 5 )
				leftSpeed = 18;
			else if( objToMove.ref.index == 6 )
				leftSpeed = 18;
			else
				leftSpeed = 4;

			T = setTimeout( "TransitionObjectLeftTo( curObject, curMoveLeftAmount, leftSpeed );", 25 );
		}
		else{
			isWaiting = false;
		}
	}
}

function TransitionObjectLeftTo( objToMove, intRestingPosition, intSpeed ){

	curPosition = px2Int( objToMove.style.left );

	curObject = objToMove;
	curRestingPosition = intRestingPosition;
	curSpeed = intSpeed;

	if( curPosition > curRestingPosition ){

		if( curPosition - curSpeed > curRestingPosition ) objToMove.style.left = curPosition - curSpeed;
		else objToMove.style.left = curRestingPosition;

		T = setTimeout( "TransitionObjectLeftTo( curObject, curRestingPosition, curSpeed );", 25 );
	} else {
		isWaiting = false;
	}
}

function HideMenuText( tdObjectPassed, aObjectPassed ){

	curTDObject = tdObjectPassed;
	curAObject = aObjectPassed;

	if( isWaiting ){
		W = setTimeout( "HideMenuText(curTDObject,curAObject);", 100 );
	} else {

		allowClicks = true;
		tdObjectPassed.onmouseout();
		tdObjectPassed.ref.setSelected();
	}
}
function fade2Color( obj, targetHexColor ){

	myObj = obj;

	curHexValue = ( myObj.style.color.substring( 0, 1 ) == "#" ) ? myObj.style.color : getHexFromCssRgb(myObj.style.color);
	curTargetHexColor = targetHexColor;

	currentColors = getRgb( curHexValue );
	targetColor = getRgb( targetHexColor );

	if( currentColors[0] > targetColor[0] ) currentColors[0] = currentColors[0] - 5;
	else currentColors[0] = targetColor[0];

	if( currentColors[1] > targetColor[1] ) currentColors[1] = currentColors[1] - 5;
	else currentColors[1] = targetColor[1];

	if( currentColors[2] > targetColor[2] ) currentColors[2] = currentColors[2] - 5;
	else currentColors[1] = targetColor[1];

	NextColor = "#" + getHex( currentColors[0] ) + getHex( currentColors[1] ) + getHex( currentColors[2] );
	obj.style.color = NextColor;

	if( currentColors[0] > targetColor[0] || currentColors[1] > targetColor[1] || currentColors[2] > targetColor[2] ){
		timerUsed=setTimeout("fade2Color(myObj,curTargetHexColor)", 25);
	}
}

function getRgb(hex) {
	var reHex		= new RegExp("^#([a-z0-9]{2})([a-z0-9]{2})([a-z0-9]{2})$", "i");
	var aValue		= reHex.exec(hex);
	var aRgb		= [ parseInt(aValue[1], 16), parseInt(aValue[2], 16), parseInt(aValue[3], 16) ];
	return aRgb;
}
function getHexFromCssRgb(rgb) {
	var reHex		= new RegExp("^rgb\\(([0-9]{1,3}), ([0-9]{1,3}), ([0-9]{1,3})\\)$", "i");
	var aValue		= reHex.exec(rgb);
	return "#" + getHex(aValue[1]) + getHex(aValue[2]) + getHex(aValue[3]);
}
function getHex(rgbValue) {
	rgbValue		= parseInt(rgbValue);
	var hexValue	= new String("0" + rgbValue.toString(16));
	hexValue		= hexValue.substring(hexValue.length - 2, hexValue.length);
	return hexValue.toUpperCase();
}

/************************************* CHECKING *******************************************/


/************************************************************************************************/
