

// Variable to hold reference to any selected menu object
var selectedMenu = '';

// Variable to determine whether cursor is currently over menu
var isOverMenu = false;

// Variable to define the offset of the menus
var menuWidth = 59; //offset

// Variables to define the menu posititoning based upon location within screen.
// This will generally be the size of space between the menus and the left hand
// side of screen (same method used for top).
var offsetMenuWidth;
var offsetHeight;

// Event to be fired upon mouse movement around document
window.document.onmousemove = function()
{
	if (window.documentOnmousemove) {
		window.documentOnmousemove();
	}
}

// Event to be fired upon mouse movement out of document. This would resolve any
// issue relating to when the user moves the mouse pointer so quickly off the
// menu onto the browser toolbar, that the document move does not register an 
// event. However, as this event also fires when moving over menu (annoyingly),
// we cannot use it or the menu items will disappear all the time. TODO: find
// a way to implement this without the menu items hiding.
window.document.onmouseout = function()
{
	if (window.documentOnmousemove) {
		//window.documentOnmousemove();
	}
}


function getDocumentElement(itemID) {
/* Function that returns an element object depending on the ID that has	*/
/* been passed into the function. Different methods are used depending	*/
/* on the method available to the browser. Note that NS4 is				*/
/* deliberately not included, as we will not be providing support for	*/
/* the drop-downs due to the complexities involved.						*/
			
	// If we can use the getElementById method (NS6+ IE5+) 
	if (document.getElementById) {
		return document.getElementById(itemID);
	// Otherwise document.all (IE4)
	} else 
		return eval(itemID);
}


function documentOnmousemove() {
/* Function that is called during the mouse move event of the document.	*/
/* 07-Sep-2001 Matt Hall												*/

	// If a menu has been selected and we are not currently moving over the
	// menu i.e. anywhere else on the screen
	if (selectedMenu) {
		if (!isOverMenu) {
			// Hide the menu items and remove highlight from the menu
			oMenuItem = getDocumentElement(selectedMenu.id + "Items");
			oMenuItem.style.display = "none";
			ImageOff(selectedMenu.id + "Image");
					
			// Flag that a menu is no longer selected
			selectedMenu = null;
			showelements();					
		}
	}
}


function overMenu(obj,extraoffset) {
/* Function that is called when the mouse pointer moves over a menu	*/
/* item.															*/
/* 07-Sep-2001 Matt Hall											*/

	if(!offsetHeight){
		return;
	}

	// If a menu has been selected and it is not the one we are currently
	// moving over
	if (selectedMenu && obj != selectedMenu) {

		oMenuItem = getDocumentElement(selectedMenu.id + "Items");
			
		// Hide the items for that previous menu and remove highlighting
		oMenuItem.style.display = "none";
		ImageOff(selectedMenu.id + "Image");
		showelements();					
				
	}

	oMenuItem = getDocumentElement(obj.id + "Items");

	// Dynamically build the menu items, along with positioning etc and show
	ImageOn(obj.id + "Image");
	oMenuItem.style.display = "block";
//	var menuPos = obj.id.charAt(obj.id.length-1);
//	oMenuItem.style.left = menuWidth * menuPos + offsetMenuWidth*menuPos+27;
	oMenuItem.style.left = obj.offsetLeft+50; 
	oMenuItem.style.top = offsetHeight+extraoffset;
	selectedMenu = obj;
	isOverMenu = true;

	hideelements(oMenuItem);
}


function overMenuItem(obj) {
/* Function that is called when the mouse pointer moves over a menu	*/
/* item.															*/
/* 07-Sep-2001 Matt Hall											*/

	// Add highlight formatting to menu item and flag that we are 
	// currently over a menu item.
	obj.className = "clsMenuItemHighlight";
	isOverMenu = true;
}


function outMenuItem(obj) {
/* Function that is called when the mouse pointer moves off a menu	*/
/* item.															*/
/* 07-Sep-2001 Matt Hall											*/

	// Remove highlight formatting from menu item and flag that we are 
	// not over a menu item anymore.
	obj.className = "clsMenuItem";
	isOverMenu = false;
}

function ImageOn(imgName) {
			
	var ptrImage = eval('document.images.'+imgName);
	if (ptrImage.src.indexOf("_on") == -1) {
		var strImageSrc = ptrImage.src.toString();
		var strImageType = strImageSrc.slice(strImageSrc.lastIndexOf('.')); // e.g. '.gif', '.jpg', '.jpeg'
		ptrImage.src = ptrImage.src.toString().replace(strImageType,'_on'+strImageType);
	}
}
				
function ImageOff(imgName) {
	var ptrImage = eval('document.images.'+imgName);
			
	var strImageSrc = ptrImage.src.toString();
	var strImageType = strImageSrc.slice(strImageSrc.lastIndexOf('.')); // e.g. '.gif', '.jpg', '.jpeg'
	ptrImage.src = ptrImage.src.toString().replace('_on'+strImageType,strImageType);
}

//global variables for storing removed select boxes (drop downs)
var aHiddenobjects = new Array();
var intHiddenobjects=0;

function showelements() {
	//2002-02-06 Ola Servenius
	for (var i=1; i<=intHiddenobjects; i++) {
		aHiddenobjects[i].style.visibility='visible';
	}
	intHiddenobjects=0;

}

var Y1,X1,Y2,X2; //global variable containing the dropped down menu coordiantes

function hideelements(men) {
	//2002-02-06 Ola Servenius
	//get the corners of the menu
	Y2 = (Y1 = men.offsetTop)+ men.offsetHeight;
	X2 = (X1 = men.offsetLeft)+ men.offsetWidth;
	//alert('menu '+X1+','+Y1+'-'+X2+','+Y2)

	for (var j=0;j<document.forms.length;j++) {

		checkChildNodes(document.forms[j]);

	}
}


function checkChildNodes(n) {
	//explore the node tree, check selectboxes if they overlap menue
	if (n.type=='select-one') {

		//find the absolute corners of the selectbox

		offy = offx = 0;
		n_parent = n;
		while (n_parent!=null) {
			offy+=(n_parent.offsetTop);
			offx+=n_parent.offsetLeft;
			n_parent = n_parent.offsetParent;
		}

		y2 = (y1 = offy)+n.offsetHeight;
		x2 = (x1 = offx)+n.offsetWidth;
		//alert(n.name+' '+x1+','+y1+'-'+x2+','+y2)

		if (X1<x2 && x1<X2 && Y1<y2 && y1<Y2) {			//overlapping?
			aHiddenobjects.length =	++intHiddenobjects; // expand the storage array
			aHiddenobjects[intHiddenobjects]=n;			// store the object so we can unhide it later
			n.style.visibility='hidden';				// hide the select box
		}
	}

	//do the same for all child nodes recursively - only for IE!!!
	if (navigator.appName.indexOf("Microsoft") != -1 && navigator.appVersion > '4.0'){
		for (var k=0; k<n.childNodes.length; k++) {
			checkChildNodes(n.childNodes[k]);
		}
	}
}

function checkSelects(f) {
	//here for test
	//example: checkSelects(document.forms[j])
	ctags = f.tags('select');
	for (var j=0;j<ctags.length;j++) {
			alert(ctags[i].name);
	
	
	}

}




