/*******************************************************************************
*File: menu.js
*Coded by: Alexandru Nedelcu
*Last modified: January 14th 2004
*******************************************************************************/

//In this implementation there are n possible levels of nesting

var TBSupported; //Flag set to true on browsers that the menu can be shown
var HTMLString = new String("<!--MENU_TOP_INSERTION_POINT-->"); 
var ieVersion; //The Internet Explorer Version
var thisMenu; //The current menu that is going to be shown

var message;

//POSITIONAL CORRECTIONS
var cX = -30; //Horizontal correction
var cY = -70; //Vertical correction

//STRING CONSTANTS
var STYLE_NAME_OFF = 'off';
var STYLE_NAME_ON = 'on';
var STYLE_NAME_CONTAINER = 'container';
var STYLE_NAME_CATEGORY = 'category';
var BASE_URL = "http://localhost"
var ContainerPrefix = "CONTAINER_";
var newLineChar = String.fromCharCode("\n");

if(navigator.userAgent.indexOf('MSIE') != -1)
{
 TBSupported = true;
 if(navigator.userAgent.indexOf('MSIE 6.0') != -1) ieVersion = 6;
 if(navigator.userAgent.indexOf('MSIE 5.0') != -1) ieVersion = 5;
 if(navigator.userAgent.indexOf('MSIE 4.0') != -1) ieVersion = 4;
}
else
{
 TBSupported = false;
}

if(TBSupported)
{
 var x = 0;
 var y = 0;
}

function addCategory(CategoryID, CategoryName, UrlStr, hasKids)
{
 var placeHolder = "<!--MENU_TOP_INSERTION_POINT-->";
 var identifier = ContainerPrefix + CategoryID;
 var category;
 if((UrlStr == null) || (UrlStr.length == 0))
 {
 	category = "<div class='" + STYLE_NAME_CATEGORY + "'>" 
				+ CategoryName + "</div>"
 }
 else
 {
	category = "<div class='" + STYLE_NAME_CATEGORY +
				"' onclick='goTo(\"" + UrlStr + "\",\"_self\"); event.cancelBubble = true;'" +
				" style='cursor:hand;'>" 
				+ CategoryName + "</div>"
 }
 
 if(hasKids)
 {
	category += "<!--" + identifier + "_INSERTION_POINT-->"
 }
 category += placeHolder;
 HTMLString = HTMLString.replace(placeHolder, category);
}

function addMenu(ParentNameStr, NameStr, DescriptionStr, UrlStr, target, hasKids)
{
 var TempStr;
 var menuID = "MENU_" + NameStr;
 var containerID = ContainerPrefix + menuID;
 var parentID = ContainerPrefix + ParentNameStr;
 
 var menu = "<div class='" + STYLE_NAME_OFF + "' nowrap=\'true\'  ID='" + menuID +	"'";
 
 if(hasKids)
 {
  	menu +=	"onmouseover=\"doMenu('in','" + containerID + "');";
	menu += "doHover('" + STYLE_NAME_ON + "','" + menuID + "');\"";
	menu += " onmouseout=\"doMenu('out','" + containerID + "');";
	menu += "doHover('" + STYLE_NAME_OFF + "','" + menuID + "');\" ";
	menu += " onclick='goTo(\"" + UrlStr + "\",\" " + target + "\"); event.cancelBubble = true;' >";
 } 
 else
 {
 	 menu += "onmouseover=\"doHover('" + STYLE_NAME_ON + "','" + menuID + "');\"";
	 menu += " onmouseout=\"doHover('off','" + menuID + "');\" ";
     menu += " onclick='goTo(\"" + UrlStr + "\",\"" + target + "\"); event.cancelBubble = true;' >" ;
 }
 menu +=  DescriptionStr;
 if(hasKids)
 {
  menu += "<div id='" + containerID + "' class='" + STYLE_NAME_CONTAINER + "' style='visibility:none;'";
	menu += "onmouseleave=\"doMenu('out','" + containerID + "');\">" ;
	menu += "<!--" + ContainerPrefix  + NameStr + "_INSERTION_POINT-->";
	menu +="</div>";
 }
 menu += "</div>";
 var PlaceHolder = "<!--" + ContainerPrefix + ParentNameStr + "_INSERTION_POINT-->";
 var TempStr = menu + PlaceHolder;
 HTMLString = HTMLString.replace(PlaceHolder, TempStr);
}

function showMenu()
{
 if(thisMenu != null)
 {
	if(thisMenu.style != null)
	{
  	thisMenu.style.left = x;
  	thisMenu.style.top = y;
   	thisMenu.style.display = 'block';
	}
 }
}

function hideMenu()
{
 if(thisMenu != null)
 {
  if(thisMenu.style != null)
	{
 	 thisMenu.style.display = 'none';
	}
 }
}

function doMenu(DirectionStr, NameStr)
{
 var obj;
 thisMenu = document.getElementById(NameStr);
 switch(DirectionStr)
 {
 	case 'in':
			   x = 0;
			   y = 0;
			   switch(ieVersion)
				 {
				  case 6:
					  //This code positions the menu on IE6.0
  				 	obj = event.srcElement;
  					x += obj.offsetWidth;
  					//x += obj.offsetLeft;
  					y += obj.offsetTop;
						while(obj.offsetParent)
						 {
						 	x += obj.offsetParent.offsetLeft;
							y += obj.offsetParent.offsetTop;
							obj = obj.offsetParent;
						 }
						break;
					case 5:
					  //TODO - This section of code position the menu on IE5.0
						//Replace this code with the correct version
  				 	obj = event.srcElement;
  					x += obj.offsetWidth;
  					x += obj.offsetLeft;
  					y += obj.offsetTop;						
						break;
					case 4:
					  //TODO - This section of code position the menu on IE4.0
					  //Replace this code with the correct version
  				 	obj = event.srcElement;
  					x += obj.offsetWidth;
  					x += obj.offsetLeft;
  					y += obj.offsetTop;											
					  break;
					default:
						break;						
				 }
				 x += cX;
				 y += cY;
				 
				 if(x < 0 ) x = 0;
				 if(y < 0 ) y = 0;
			 showMenu();
			 break;
	case 'out':
			 x = 0;
			 y = 0;
       if(shouldHide())	hideMenu();
			 break;
 }
 window.event.returnValue = false;
 window.event.cancelBubble= true;
}

//This function decides whether or not a menu should be hidden or remain visible.
//The menu should remain visible when the mouse mvoes to one of its child menus, or the event
//originates from an element it contains.
function shouldHide()
{
 var value;
 if(event.toElement)
 {
   if((event.toElement.id.indexOf("MENU_") != -1) || (event.toElement.id.indexOf("CN_") != -1)) 
   {
	  if(event.fromElement)
		{
     	if(event.fromElement.contains(event.toElement))
    	{
		 //The mouse was moved towards one of the child menus so the original menu should remain visible
    	 value = false;
    	}
    	else
    	{
    	 value = true;
    	}
		}
   }
   else
   {
   	value = true;
   }
 }
 else
 {
 	value = true;
 }
 return value;
}

//HIGHLIGHTS THE SPECIFIED ELEMENT ON MOUSEOVERS AND MOUSEOUTS
function doHover(state, menuID)
{
 var menu = document.getElementById(menuID);
 if(menu != null)
 {
 	 menu.className = state;
 }
 event.cancelBubble = true;
}

//Display the menu by writing it to the output HTML Stream
function drawMenu()
{
 document.write(HTMLString);
}

function goTo(UrlStr, target)
{
 if((target == null) || (target.toUpperCase() == "_SELF") || (target.toUpperCase() == " _SELF"))
	{
		if((UrlStr.indexOf("http://") != -1) || (UrlStr.indexOf(".aspx") != -1))
		{
			window.location = UrlStr;
		}
		else if(UrlStr.toUpperCase().indexOf("JAVASCRIPT") != -1)
		{
			eval(UrlStr);
		}
		else
		{
		    try
		    {
		        window.navigate(UrlStr);
		    }
		    catch(e)
		    {}
		}
	}
 else
	{
	    try
	    {
		    window.open(UrlStr, target, 'height=300px,width=400px,resizable=no,scrollbars=no,status=no,toolbar=no,top=100,left=200');
		}
		catch(e)
		{}
	}
 event.returnValue = false;
 
}
