

var default_menu_filter = 'progid:DXImageTransform.Microsoft.Shadow(color=#000000,direction=135,strength=4)';
var navigationArray = new Array();

/*These variables can be overridden by the calling page*/
var menu_location = 'S'; // Options: S or E
var menu_position_mod_x = 0;
var menu_position_mod_y = 0;
var menu_filter = default_menu_filter;
var menu_fadeout = true;
var hideTime = 500;
/* end*/


var useIFrames = false;
var ua = navigator.userAgent;
var isMSIE = (navigator.appName == "Microsoft Internet Explorer");
var isOpera = ua.indexOf('Opera') != -1;

var loadedPage = false;

var getPopupsWindow = function() {
	return window;
}
var getPopupsBody = function() {
	popupWindow = getPopupsWindow();
	return popupWindow.document.body;
}
var getPopupsDocument = function() {
	popupWindow = getPopupsWindow();
	return popupWindow.document;
}

var popupWindow = getPopupsWindow();
var popupsBody = getPopupsBody();
var popupsDocument = getPopupsDocument();


var oldOnLoad = window.onload;
window.onload = function() {
	loadedPage = true;
	if( typeof oldOnLoad == "function" )
		oldOnLoad();
}

if( isOpera )
	isMSIE = false;

if( isMSIE ) {
	useIFrames = true;
}

function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj) { 
			curleft += obj.offsetLeft; 
			obj = obj.offsetParent; 
		} 
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent)
	{
		curtop += obj.offsetTop
		while (obj.offsetParent)
		{
			obj = obj.offsetParent;
			curtop += obj.offsetTop
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function getWindowWidth() {
	var myWidth = 0;
	
	popupsBody = getPopupsBody();
	popupsDocument = getPopupsDocument();
	popupWindow = getPopupsWindow();
	
	if( typeof( popupWindow.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = popupWindow.innerWidth;
	} else if( popupsDocument.documentElement &&
	( popupsDocument.documentElement.clientWidth || popupsDocument.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = popupsDocument.documentElement.clientWidth;
	} else if( popupsBody && ( popupsBody.clientWidth || popupsBody.clientHeight ) ) {
		//IE 4 compatible
		myWidth = popupsBody.clientWidth;
	}
	return myWidth;
}

function getWindowHeight() {
	var myHeight = 0;
	
	popupsBody = getPopupsBody();
	popupsDocument = getPopupsDocument();
	popupWindow = getPopupsWindow();
	
	if( typeof( popupWindow.innerWidth ) == 'number' ) {
		//Non-IE
		myHeight = popupWindow.innerHeight;
	} else if( popupsDocument.documentElement &&
	( popupsDocument.documentElement.clientWidth || popupsDocument.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myHeight = popupsDocument.documentElement.clientHeight;
	} else if( popupsBody && ( popupsBody.clientWidth || popupsBody.clientHeight ) ) {
		//IE 4 compatible
		myHeight = popupsBody.clientHeight;
	}
	return  myHeight;
}

function getScrollY(){
	popupsDocument = getPopupsDocument();
	popupWindow = getPopupsWindow();
	
	var iebody=(popupsDocument.compatMode && popupsDocument.compatMode != "BackCompat")? popupsDocument.documentElement : popupsDocument.body;
	return popupsDocument.all? iebody.scrollTop : popupWindow.pageYOffset;
}

function getScrollX(){
	popupsDocument = getPopupsDocument();
	popupWindow = getPopupsWindow();
	
	var iebody=(popupsDocument.compatMode && popupsDocument.compatMode != "BackCompat")? popupsDocument.documentElement : popupsDocument.body;
	return popupsDocument.all? iebody.scrollLeft : popupWindow.pageXOffset;
}

var iframes = new Array();
var currentMenu = new Array();
var unhideArray = new Array();

function checkValidMenu() {
	popupsDocument = getPopupsDocument();
	if( currentMenu.length ) {
		try {
			//try to access the element and see if we get an error !
			currentMenu[0].id;
		} catch ( err ) {
			iframes = new Array();
			currentMenu = new Array();
			unhideArray = new Array();
		}
	}
}

function showIFrame( level, childObj) {
	popupsBody = getPopupsBody();
	popupsDocument = getPopupsDocument();
	
	var id = childObj.id;
	
	if( !loadedPage || !useIFrames )
		return false;

	var iframe;

		if( typeof iframes[level] != "undefined" ) {
			iframe = iframes[level];
		} else {
			//create new iframe to go over selects and other flash
			iframe = popupsDocument.createElement( 'iframe' );
			iframe.src='/blank.html';
			iframe.scrolling = 'no';
			iframe.frameborder = 0;
			iframe.style.position = 'absolute';
			iframe.style.border = 'none';
			iframe.style.zIndex = 1;
			iframe.style.left = 1;
			iframe.style.top = 1;
			iframe.style.height = 10;
			iframe.style.width = 10;
			iframe.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=0)";
			iframe.style.MozOpacity = level/100;
			iframe.style.opacity = level/100;

			popupsBody.insertBefore( iframe );
			iframes[level] = iframe;
		}
		iframe.id = id+'_iframe';

//			position the iframe
		iframe.style.border = childObj.style.border;
		iframe.style.margin = childObj.style.margin;
		iframe.style.padding = childObj.style.padding;
		
		iframe.style.left = childObj.style.left;
		iframe.style.top = childObj.style.top;
		if( childObj.style.height )
			iframe.style.height = childObj.style.height;
		else
			iframe.style.height = childObj.offsetHeight+'px';
		if( childObj.style.width )
			iframe.style.width = childObj.style.width;
		else
			iframe.style.width = childObj.offsetWidth+'px';
		iframe.style.display = 'block';

}

function mouseOver( parentObj, childObj, level, customMenuLocation ) {
	if( typeof level == "undefined" ) {
		level = 0;
	}
	
	var currentMenuLocation;
	
	if( typeof customMenuLocation != "undefined" ) {
		currentMenuLocation = customMenuLocation;
	} else {
		currentMenuLocation = menu_location;
	}
	
	//if there is an open menu at this level AND the current open menu is not the parent of the new menu AND the current open menu is not the new menu to open
	if( typeof currentMenu[level] != "undefined" && currentMenu[level] != parentObj && currentMenu[level] != childObj ) {
		//hide it
		currentMenu[level].style.display = 'none';
		hideStatsDivs( currentMenu[level].id );
		
		//hide iframe
		var iframe = popupsDocument.getElementById(currentMenu[level].id+'_iframe');
		if( iframe )
			iframe.style.display = 'none';
		
		clearTimeout( unhideArray[currentMenu[level].id] );
		//hide any menus above this one
		hideLevel = level+1;
		while( typeof(currentMenu[hideLevel]) != 'undefined' ) {
			clearTimeout( unhideArray[currentMenu[hideLevel].id] );
			currentMenu[hideLevel].style.display = 'none';
			hideStatsDivs( currentMenu[hideLevel].id );
			
			//hide iframe
			var iframe = popupsDocument.getElementById(currentMenu[hideLevel].id+'_iframe');
			if( iframe )
				iframe.style.display = 'none';
			
			hideLevel++;
		}
	}
	if( typeof childObj != "undefined" && childObj != null) {
		currentMenu[level] = childObj;
		childObj.style.display = 'block';
		childObj.style.filter = menu_filter;
		childObj.style.MozOpacity = 1;
		childObj.style.opacity = 1;

		
		if( currentMenuLocation == 'S' ) {
			//find a suitable left pos for menu.
			
			//try down and left from current item
			if( (findPosX( parentObj ) + parentObj.offsetWidth + childObj.offsetWidth + menu_position_mod_x - getScrollX() ) <= getWindowWidth() ) {
				leftPos = (findPosX( parentObj ) + menu_position_mod_x);
			} else {
				//else move left however much is required to fit
				leftPos = getWindowWidth() + getScrollX() - childObj.offsetWidth;
			}
			childObj.style.left = leftPos+'px';
			childObj.style.top = (findPosY( parentObj ) + parentObj.offsetHeight + menu_position_mod_y)+'px';
		} else
		if( currentMenuLocation == 'N' ) {
			//find a suitable left pos for menu.
			
			//try up and left from current item
			if( (findPosX( parentObj ) + parentObj.offsetWidth + childObj.offsetWidth + menu_position_mod_x - getScrollX() ) <= getWindowWidth() ) {
				leftPos = (findPosX( parentObj ) + menu_position_mod_x);
			} else {
				//else move left however much is required to fit
				leftPos = getWindowWidth() + getScrollX() - childObj.offsetWidth;
			}
			childObj.style.left = leftPos+'px';
			childObj.style.top = (findPosY( parentObj ) - childObj.offsetHeight + menu_position_mod_y)+'px';
		} else
		if( currentMenuLocation == 'E' ) {
			childObj.style.left = (findPosX( parentObj ) + parentObj.offsetWidth + menu_position_mod_x)+'px';
			childObj.style.top = (findPosY( parentObj ) + menu_position_mod_y)+'px';
		} else
		if( currentMenuLocation == 'EW' ) {
			//try east
			if( (findPosX( parentObj ) + parentObj.offsetWidth + childObj.offsetWidth + menu_position_mod_x - getScrollX()) <= getWindowWidth() ) {
				childObj.style.left = (findPosX( parentObj ) + parentObj.offsetWidth + menu_position_mod_x)+'px';
			} else {
				//try west
				//adjust if default shadow filter is on
				childObj.style.left = (findPosX( parentObj ) - childObj.offsetWidth + menu_position_mod_x + ( menu_filter == default_menu_filter ? 4 : 0 ) )+'px';
			}
			
			//Check fits vertically
			if( (findPosY( parentObj ) + parentObj.offsetHeight + childObj.offsetHeight + menu_position_mod_y - getScrollY() ) <= getWindowHeight() ) {
				childObj.style.top = (findPosY( parentObj ) + menu_position_mod_y)+'px';
			} else {
				//try moving it up
				//adjust if default shadow filter is on
				var top = (findPosY( parentObj ) + parentObj.offsetHeight - childObj.offsetHeight + menu_position_mod_y + ( menu_filter == default_menu_filter ? 4 : 0 ) );
				if( top - getScrollY() < 0 ) {
					top = getScrollY();
				}
				childObj.style.top = top+'px';
			}
		}
		clearTimeout( unhideArray[childObj.id] );
		
		childObj.style.zIndex = 2;
		
		showStatsDivs( childObj.id );

		//if we are positioning the sub menu and its not fixed position
		if( currentMenuLocation != '' )
			showIFrame( level, childObj );
	
	}
	clearTimeout( unhideArray[parentObj.id] );
}

function mouseOverChild( childObj ) {
	clearTimeout( unhideArray[childObj.id] );
	childObj.style.filter = menu_filter;
	childObj.style.MozOpacity = 1;
	childObj.style.opacity = 1;

	showStatsDivs( childObj.id );
}

function mouseOverChildLevel( level ) {
	popupsDocument = getPopupsDocument();
	
	// unset any timers to close windows of lower level menus
	for( var menuI = 0; menuI <= level; menuI++ ) {
		if( typeof currentMenu[menuI] != "undefined" ) {
			clearTimeout( unhideArray[currentMenu[menuI].id] );
			//make sure its not half faded
			childObj = popupsDocument.getElementById(currentMenu[menuI].id);
			childObj.style.filter = menu_filter;
			childObj.style.MozOpacity = 1;
			childObj.style.opacity = 1;
			showStatsDivs( childObj.id );
		}
	}

};

function mouseOut( childObj ) {
	if( typeof childObj == "undefined" || childObj == null )
		return;

	if( menu_fadeout ) {
		clearTimeout( unhideArray[childObj.id] );
		unhideArray[childObj.id] = setTimeout( 'fadeDiv("'+childObj.id+'", 90)', hideTime );
	} else {
		clearTimeout( unhideArray[childObj.id] );
		unhideArray[childObj.id] = setTimeout( 'fadeDiv("'+childObj.id+'", 0)', hideTime );
	}
}

function mouseOutLevel( level ) {
	level++;
	//set timers to close windows of lower level menus
	for( var menuI = 0; menuI <= level; menuI++ ) {
		if( typeof currentMenu[menuI] != "undefined" ) {
			if( menu_fadeout ) {
				clearTimeout( unhideArray[currentMenu[menuI].id] );
				unhideArray[currentMenu[menuI].id] = setTimeout( 'fadeDiv("'+currentMenu[menuI].id+'", 90)', (hideTime - (menuI*100)) );
			} else {
				clearTimeout( unhideArray[currentMenu[menuI].id] );
				unhideArray[currentMenu[menuI].id] = setTimeout( 'fadeDiv("'+currentMenu[menuI].id+'", 0)', (hideTime - (menuI*100)) );
			}
		}
	}
}

function showStatsDivs( objId ) {
	popupsDocument = getPopupsDocument();
	
	//get array of stats divs
	if( typeof( navigationArray[objId] ) == 'object' ){
		//statsDivs[ objId ]
		//loop over them all to position and display
		if( navigationArray[objId].length > 0 ){
			for( i = 0; i<navigationArray[objId].length ; i++){
				var id = navigationArray[objId][i];
				// get the element
				var element = popupsDocument.getElementById( 'anchor_' + id );
	
				// get absolute position of current item
				var pos = getAbsolutePosition( element );
				
				// position the div in the correct place
				var divRef = popupsDocument.getElementById( 'div_' + id );
				divRef.style.top = ( pos.absTop + 15 ) + 'px';
				divRef.style.left = ( pos.absLeft ) + 'px';
				// show the div
			  	divRef.style.display = 'block';
			  	divRef.style.zIndex = 3;
			   	divRef.style.display = '';
			   	
			   	// position the stats div near by too
			   	var divStatsRef = popupsDocument.getElementById( 'div_' + id + '_stats' );
				divStatsRef.style.top = ( pos.absTop + 27 ) + 'px';
				
				//store current show state
				var oldDisplayValue = divStatsRef.style.display;
				// show div so it takes notice of any moving that has to be done
				divStatsRef.style.display = '';
			  	// calculate if we need to adjust the positioning so its not off the page
				if( (findPosX( element ) + divStatsRef.offsetWidth - getScrollX() ) <= getWindowWidth() ) {
					leftPos2 = (findPosX( element ));
				} else {
					//else move left however much is required to fit
					leftPos2 = getWindowWidth() + getScrollX() - divStatsRef.offsetWidth;
				}
				divStatsRef.style.left = ( leftPos2 ) + 'px';
				divStatsRef.style.zIndex = 4;
				divStatsRef.style.display = oldDisplayValue;
			}
		}
	}
}

function hideStatsDivs( objId ) {
	popupsDocument = getPopupsDocument();
	
	if( typeof( navigationArray[objId] ) == 'object' ){
		//loop over them all to position and display
		if( navigationArray[objId].length > 0 ){
			for( i = 0; i<navigationArray[objId].length ; i++){
				var id = navigationArray[objId][i];
				// hide the div in the correct place
				var divRef = popupsDocument.getElementById( 'div_' + id );
			   	divRef.style.display = 'none';
			   	// hide the stats div near by too
			   	var divStatsRef = popupsDocument.getElementById( 'div_' + id + '_stats' );
				divStatsRef.style.display = 'none';
			}
		}
	}
}

function fadeDiv( objId, level ) {
	popupsDocument = getPopupsDocument();
	
	var obj = popupsDocument.getElementById(objId);
	
	if( useIFrames )
		var iframe = popupsDocument.getElementById(objId+'_iframe');
		
	obj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity="+level+")";
	obj.style.MozOpacity = level/100;
	obj.style.opacity = level/100;

	if( level <= 50 && iframe  && typeof iframe != "undefined" && typeof iframe.style != "undefined" ){
		iframe.style.display = 'none';		
		hideStatsDivs( objId );
	}

	if( level <= 10 ) {
		obj.style.display = 'none';
	} else {
		unhideArray[objId] = setTimeout( 'fadeDiv("'+objId+'", '+(level-15)+')', 75 );
	}
}
