// Constant definitions for array indexes

var SUB_ID    = 0;
var ITEM_HREF = 0;
var ITEM_TRGT = 1;
var ITEM_TEXT = 2;
var ITEM_SHOW = 3;
var ITEM_ROOT_FORECOLOR = '#FFFFFF';
var ITEM_ROOT_BACKCOLOR = '#963514';
var ITEM_FORECOLOR = '#FFFFFF';
var ITEM_BACKCOLOR = '#5D1C08';
var ITEM_UPPERCASE = true;
var TIMER_INTERVAL = 500;

var NUM_IFRAMES = 2;

// Variables for script

var sSpacerSrc = '';
var sArrowSrc = '';

var bExpandRight = true;
var aChangeDirSub = new Array();
//var oForcedLeft = null;

var nTimerId = null;

var oRootItmActive = null;
var aSubActive = new Array();
var aItmActive = new Array();
var aIFrames = new Array();

// Create the IFrame for the IE - Flash problem

function createIFrames( nAmount ) {

	var i = 0;
	var sOutput = '';
	for( i = 0; i < nAmount; i++ ) {
		sOutput += '<iframe class="submnu_iframe" src="javascript:false;" id="iframe_' + i + '" scroll="no" frameborder="0"></iframe>';
	}
	document.write( sOutput );
}

function showIFrame( nLeft, nTop, nWidth, nHeight ) {

	var oIFrame = null;
	if( oIFrame = document.getElementById( 'iframe_' + aIFrames.length ) ) {
		oIFrame.style.visibility = 'visible';
		oIFrame.style.left = nLeft;
		oIFrame.style.top = nTop;
		oIFrame.style.width = nWidth;
		oIFrame.style.height = nHeight - 2;		// Subtract 2 from the height otherwise IE shows the bottom of the frame below the menu
		aIFrames.push( oIFrame );
		return true;
	}
	return false;
}

function hideIFrame() {

	var oIFrame = null;

	// We have already hidden the sub so the IFrame array should be one in front
	if( aIFrames.length > aSubActive.length ) {
		oIFrame = aIFrames.pop();
		oIFrame.style.visibility = 'hidden';
		oIFrame.style.left = 0;
		oIFrame.style.top = 0;
		oIFrame.style.width = 0;
		oIFrame.style.height = 0;
		return true;
	}
	return false;
}

// Create the lists, table items and links

function createMenuStructure( sMenuSpacerSrc, sMenuArrowSrc ) {

	//if( !is_ie5_5up && !is_nav6up && !is_safari && !is_opera6up && !( is_gecko && ( is_moz || is_fb || is_fx ) ) ) { return; }


	// Only work on IE5.5 and up
	if( !is_ie5_5up ) { return; }

	// If menu array exists
	if( window.aSubStructure ) {

		// Create the IFrames
		createIFrames( NUM_IFRAMES );

		sSpacerSrc = sMenuSpacerSrc;
		sArrowSrc = sMenuArrowSrc;

		var sOutput = '';

		// Loop through first level of array
		for( var i = 0; i < aSubStructure.length; i++ ) {

			var sSubId = aSubStructure[i][SUB_ID];
			sOutput += '<ul id="' + sSubId + '" class="submnu"><li>';
			sOutput += '<table cellpadding="0" cellspacing="0" border="0">';

			// Loop through second level of array
			for( var j = 1; j < aSubStructure[i].length; j++ ) {

				var sHref = aSubStructure[i][j][ITEM_HREF];
				var sTrgt = aSubStructure[i][j][ITEM_TRGT];
				var sText = aSubStructure[i][j][ITEM_TEXT];
				var sShow = aSubStructure[i][j][ITEM_SHOW];
				var sArrow = sShow == '' ? '&nbsp;' : '<img src="' + sArrowSrc + '" border="0" /><br />';
				var sSpacer = '<img src="' + sSpacerSrc + '" width="9" height="1" border="0" />';

				if( ITEM_UPPERCASE ) {
					sText = sText.toUpperCase();
				}

				sTrgt = sTrgt == '' ? '' : ' target="' + sTrgt + '"';
				
				sOutput += '<tr onmouseover="itmOver(this,\'' + sShow + '\')" onmouseout="itmOut(this)"><td class="subleft">';
				sOutput += '<a href="' + sHref + '"' + sTrgt + '>';
				sOutput += sSpacer + '</a>';
				sOutput += '</td><td class="subtext">';
				sOutput += '<a href="' + sHref + '"' + sTrgt + '>';
				sOutput += sText + '</a>';
				sOutput += '</td><td class="subarrow">';
				sOutput += '<a href="' + sHref + '"' + sTrgt + '>';
				sOutput += sArrow + '</a>';
				sOutput += '</td></tr>';
			}

			sOutput += '</table>';
			sOutput += '</li></ul>';
		}
		document.write( sOutput );
	}
}

function rootOver( oSource, sId ) {
	if( nTimerId ) {
		clearTimeout( nTimerId );
		nTimerId = null;
	}
	var oSub = null;

	if( ( oRootItmActive ) && ( oSource != oRootItmActive ) ) {
		hideSubAll();
		oRootItmActive = null;
	}

	// Apply the rollover effect
	changeItm( oSource, true, true );

	if( ( sId != '' ) && ( oSub = document.getElementById( sId ) ) ) {

		// Make sure root itm is not already active
		if( oSource != oRootItmActive ) {
			showSub( oSub, oSource, true );
		}
	}
}

function rootOut( oSource ) {
	if( !nTimerId ) {
		nTimerId = setTimeout( 'hideSubAll()', TIMER_INTERVAL );
	}
	if( oSource != oRootItmActive ) {
		changeItm( oSource, true, false );
	}
}

function itmOver( oItm, sId ) {

	if( nTimerId ) {
		clearTimeout( nTimerId );
		nTimerId = null;
	}

	var oSub = null;
	var oThisSub = oItm.parentNode.parentNode.parentNode.parentNode;

	// Hide all subs until this one or the one to show
	for( var i = ( aSubActive.length - 1 ); i >= 0; i-- ) {
		if( aItmActive.length && aItmActive[aItmActive.length-1] == oItm ) {
			return;
		}
		if( oThisSub == aSubActive[i] ) {
			break;
		}
		hideSub();
	}

	// Apply the rollover effect
	changeItm( oItm, false, true );

	if( ( sId != '' ) && ( oSub = document.getElementById( sId ) ) ) {

		// Make sure itm is not already active
		if( ( aSubActive.length == 0 ) || ( aSubActive[aSubActive.length - 1] != oSub ) ) {
			showSub( oSub, oItm, false );
		}
	}
}

function itmOut( oItm ) {
	if( !nTimerId ) {
		nTimerId = setTimeout( 'hideSubAll()', TIMER_INTERVAL );
	}
	if( ( aItmActive.length == 0 ) || ( aItmActive[aItmActive.length - 1] != oItm ) ) {
		changeItm( oItm, false, false );
	}
}

function showSub( oSub, oItm, bRoot ) {

	var nItmX = findX( oItm );
	var nItmY = findY( oItm );
	var nSubWidth = oSub.offsetWidth;
	var nItmWidth = oItm.offsetWidth;
	var nScrollWidth = document.body.scrollWidth;

	var nShowRightX = 0;
	var nShowLeftX = 0;

	var nTmpLeft = 0;
	var nTmpTop = 0;

	oSub.style.zIndex = 100 + aSubActive.length;

	if( bRoot ) {

		//oSub.style.visibility = 'visible';
		//oSub.style.top  = nItmY + oItm.offsetHeight + 'px';
		nTmpTop = nItmY + 'px';
//		nTmpTop = nItmY + oItm.offsetHeight + 'px';

		nShowRightX = nItmX + nItmWidth;
		nShowLeftX = nItmX - nSubWidth;

//		nShowRightX = nItmX;
//		nShowLeftX = nItmX + nItmWidth - nSubWidth;

		if( ( nShowRightX + nSubWidth > nScrollWidth ) && ( nShowLeftX > 0 ) ) {
			bExpandRight = false;
			aChangeDirSub.push( oSub );
			//oSub.style.left = nShowLeftX + 'px';
			nTmpLeft = nShowLeftX + 'px';
		} else {
			//oSub.style.left = nShowRightX + 'px';
			nTmpLeft = nShowRightX + 'px';
		}

		oRootItmActive = oItm;
	} else {

		//oSub.style.visibility = 'visible';
		//oSub.style.top  = nItmY + 'px';
		nTmpTop = nItmY + 'px';

		nShowRightX = nItmX + nItmWidth;
		nShowLeftX = nItmX - nSubWidth;

		if( bExpandRight ) {
			if( ( nShowRightX + nSubWidth > nScrollWidth ) && ( nShowLeftX > 0 ) ) {
				bExpandRight = false;
				aChangeDirSub.push( oSub );
				//oSub.style.left = nShowLeftX + 'px';
				nTmpLeft = nShowLeftX + 'px';
			} else {
				//oSub.style.left = nShowRightX + 'px';
				nTmpLeft = nShowRightX + 'px';
			}
		} else {
			if( nShowLeftX < 0 ) {
				bExpandRight = true;
				aChangeDirSub.push( oSub );
				//oSub.style.left = nShowRightX + 'px';
				nTmpLeft = nShowRightX + 'px';
			} else {
				//oSub.style.left = nShowLeftX + 'px';
				nTmpLeft = nShowLeftX + 'px';
			}
		}

		aItmActive.push( oItm );
	}

	showIFrame( nTmpLeft, nTmpTop, oSub.offsetWidth, oSub.offsetHeight );

	oSub.style.visibility = 'visible';
	oSub.style.top = nTmpTop;
	oSub.style.left = nTmpLeft;

	aSubActive.push( oSub );
}

function hideSub() {
	var oSub = aSubActive.pop();
	oSub.style.visibility = 'hidden';
	oSub.style.top = '0px';
	oSub.style.left = '0px';

	hideIFrame();

	if( aItmActive.length ) {
		var oItm = aItmActive.pop();
		changeItm( oItm, false, false );
	} else {
		changeItm( oRootItmActive, true, false );
		oRootItmActive = null;
	}
	if( aChangeDirSub.length && aChangeDirSub[aChangeDirSub.length-1] == oSub ) {
		bExpandRight = bExpandRight ? false : true;
		aChangeDirSub.pop();
	}
}

function hideSubAll() {
	while( aSubActive.length ) {
		hideSub();
	}
}

function changeItm( oItm, bRoot, bOver ) {

	if( bOver ) {
		if( bRoot ) {
			oItm.childNodes[0].style.color = ITEM_ROOT_FORECOLOR;
			oItm.style.backgroundColor = ITEM_ROOT_BACKCOLOR;
		} else {
			oItm.childNodes[0].childNodes[0].style.color = ITEM_FORECOLOR;
			oItm.style.backgroundColor = ITEM_BACKCOLOR;
		}
	} else {
		if( bRoot ) {
			oItm.childNodes[0].style.color = '';
			oItm.style.backgroundColor = '';
		} else {
			oItm.childNodes[0].childNodes[0].style.color = '';
			oItm.style.backgroundColor = '';
		}
	}
}

function findX( oObject ) {
	var nObjectLeft = 0;
	if ( document.getElementById || document.all ) {
		while ( oObject.offsetParent )
		{
			nObjectLeft += oObject.offsetLeft;
			oObject = oObject.offsetParent;
		}

	} else if ( document.layers ) {
		nObjectLeft += oObject.x;
	}
	return nObjectLeft;
}

function findY( oObject ) {
	var nObjectTop = 0;
	if ( document.getElementById || document.all ) {
		while (oObject.offsetParent)
		{
			nObjectTop += oObject.offsetTop;
			oObject = oObject.offsetParent;
		}

	} else if ( document.layers ) {
		nObjectTop += oObject.y;
	}
	return nObjectTop;
}