var timeoutId = 0;
var APP_XOFFSET  = 2;
var APP_YOFFSET  = 2;
var APP_TIPBG    = "#F5F4EA";
var APP_WIDTH    = 250;
var APP_TIMEIN   = 10;
var APP_TIMEOUT  = 1000;


/* Determine browser type */
var NS6 = (!document.all && document.getElementById)? 1:0;
var NS4 = (document.layers) ? 1:0;
var IE4 = (document.all) ? 1:0;

var NS6_Safari = false;
if (NS6) {
	var detect = navigator.userAgent.toLowerCase();
	var place = detect.indexOf('safari') + 1;
	if (place) NS6_Safari = true;
}

//if(NS4) {
//	alert("you appear to be using netscape 4");
//}
//if(NS6) {
//	alert("you appear to be using netscape 6");
//}
//if(IE4) {
//	alert("you appear to be using IE4 or similar");
//}
//if(NS6_Safari) {
//	alert("you appear to be using Safari or similar");
//}

var divname = "toolmenudiv";
var x = 0;
var y = 0;
var Y_SHIFT		  = 0;
var X_SHIFT		  = 0;
var window_width    = 800;
var window_height   = 600;
var isTimeOut		= 1;

var myWidth = 0, myHeight = 0; 
var scrOfX = 0, scrOfY = 0;
var visibility = 0;

var menuindex = 0;
var menu = new Array;
currentmainnav = new Array;
currentmainnavEx = new Array;

/* Add position to leaf 
input:
id   - leaf id
name - Row label
url  - Row url or action  
returns: Row id
*/ 
function AddToLeaf(id, name, url) {
	var rid = menu[id + ".menuindex"];
	menu[id + ".Rows." + menu[id + ".menuindex"] + ".Name"] = name;
	menu[id + ".Rows." + menu[id + ".menuindex"]+ ".Url"] = url; 
	menu[id + ".menuindex"]++;	
	return rid;
}

/* Create new tree leaf 
input: 
name - Leaf name 
returns:  Leaf id
*/
function CreateNewleaf(name) {
	var id = name;
	menu[name + ".menuindex"] = 0;
	menu[name + ".Name"] = name;
	return id;
}

/* Mousemove event handler  */
if(NS4 || IE4 || NS6) {
	document.onmousemove = mouseMove;
	
	if(NS4 || NS6) {
		document.captureEvents(Event.MOUSEMOVE);
		window_width = window.innerWidth;
	}
}

/* Mousemove event handler function */
function mouseMove(e) {
	
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( typeof( self.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = self.innerWidth;
		myHeight =  self.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	
	window_width = myWidth;
	window_height = myHeight;
	
	if(NS4) {
		x = e.pageX + APP_XOFFSET;
		y = e.pageY + APP_YOFFSET;
	} else if(NS6_Safari) {
		x = e.clientX + APP_XOFFSET;
		y = e.clientY + APP_YOFFSET;
	} else if(NS6) {
		x = e.clientX + APP_XOFFSET + scrOfX;
		y = e.clientY + APP_YOFFSET + scrOfY;
	} else if(IE4) {
		x = event.x + APP_XOFFSET + scrOfX;
		y = event.y + APP_YOFFSET + scrOfY;
		//window.status = x + ' ' + y + ' ' + scrOfX + ' ' + scrOfY + ' ' + myWidth + ' ' + myHeight;
	}
}

/* Show menu at fx1, fy1 */
function multitool(fx1, fy1, name ) {
	currentmainnavEx[0] = '';
	currentmainnav[0] = name;
  	currentmainnav[1] = fx1 + X_SHIFT;
  	currentmainnav[2] = fy1 + Y_SHIFT;	
	toolsmenuex();
}

/* Show menu and coordinates (DEBUG) */
function multitoolCoord(fx1, fy1, name ) {
	currentmainnavEx[0] = (currentmainnav[1] - 225) + ' ' + (currentmainnav[2] - 330);
	currentmainnav[0] = name;
  	currentmainnav[1] = fx1 + X_SHIFT;
  	currentmainnav[2] = fy1 + Y_SHIFT;	
	toolsmenuex();
}

/* Show menu at currentmainnav */
function toolsmenuex() {
	var txt = "";
	var coordChange = 30;
	var name = currentmainnav[0];
	toolsoff();
	
	if( typeof menu[name + ".menuindex"] != 'number' || typeof menu[name + ".Name"] != 'string' || menu[name + ".menuindex"] < 1) { 
		// alert (menu[name + ".menuindex"] + menu[name + ".menuindex"] + ' ' + menu[name + ".Name"] + ' error'); 
		return true; 
	}
	var px = '';
	if (NS6) {px = 'px';}
	txt += '<div id="title"><table border="0" cellpadding="0" cellspacing="0" width="100%" >\n'
			+ '<tr><td bgcolor="#fff"><dl class="tools_menu">'
			+ '<dt><a href="javascript:void(toolsoff());" onmouseover="window.clearTimeout(timeoutId); window.status=\'\';return true;" onmouseout=""><p>'
				+ menu[name + ".Name"] + ' ' + currentmainnavEx[0] + '</p></a>' 
			+ '</dt>';
	
	for(i = 0; i < menu[name + ".menuindex"]; i++) {
		var link = "";
		if(menu[name + ".Rows." + i + ".Url"] != '') {
			var url = menu[name + ".Rows." + i + ".Url"];
			target = '';
			if(url.substr(0,1)=='@') {
				url = url.substr(1);
				target = ' target="_blank"';
			}
			link = '<a href="' + url + '"' + target+'>' + menu[name + ".Rows." + i + ".Name"] + '</a>';
		} else {
			link = menu[name + ".Rows." + i + ".Name"];
		}
		if(i == 1){
			txt += '<dd id="first_shadow">' + link + '</dd>';
		} else {   
			txt += '<dd>' + link + '</dd>';
		}  
		txt += '\n';
	}
	
	txt += '</dl></td></tr></table></div>';
	
	// txt = "<p>FOOO</p>";
	
	var app_height = menu[name + ".menuindex"] * 20 + 35;
	if(currentmainnav[1] + APP_WIDTH > window_width) { currentmainnav[1] -= APP_WIDTH; }
	if(currentmainnav[1] - coordChange < scrOfX) { currentmainnav[1] = scrOfX + coordChange; }

// The following lines were commented out because they move the menu too high when there is a lot of data.
// We have now added a scrollbar to the menu, which should take care of this case.
//	if(currentmainnav[2] + app_height > window_height) { currentmainnav[2] -= app_height;  }
//	if(currentmainnav[2] - coordChange < scrOfY) { currentmainnav[2] = scrOfY + coordChange;  }

	printDiv(currentmainnav[1] - coordChange, currentmainnav[2] - coordChange , APP_WIDTH, txt, divname);
	window.clearTimeout(timeoutId);
	timeoutId = window.setTimeout('divon(\'' + divname + '\')', APP_TIMEIN);
	
	return true;
}

function printDiv(dx, dy, dw, txt, newdiv) {
	var d = jQuery("#"+newdiv);
	d.html(txt).css({'color' : 'black', 'position' : 'absolute', 'left' : dx + 'px', 'top' : dy + 'px', 'width' : dw + 'px', 'z-index' : '200'}).show();	
	return true;
}

function divon(newdiv) {
	jQuery("#"+newdiv).show();
	window.clearTimeout(timeoutId);
	return true;
}

function divoff(newdiv) {
	if (!isTimeOut) return;
	jQuery("#"+newdiv).hide();
}

function toolson() {
	visibility = 1;
	jQuery("#"+divname).show();
	window.clearTimeout(timeoutId);
	timeoutId = window.setTimeout('toolsoff()', APP_TIMEOUT);
	return true;
}

function toolsoff() {
	jQuery("#"+divname).hide();
}

