// http://simonwillison.net/2004/May/26/addLoadEvent/
function addLoadEvent(f)
{
	var prevEvent = window.onload;
	if(typeof(window.onload) != 'function')
	{
		window.onload = f;
	}
	else
	{
		window.onload = function ()
		{
			if(prevEvent) { prevEvent(); }
			f();
		}
	}
}

// written by Dean Edwards, 2005
// with input from Tino Zijdel, Matthias Miller, Diego Perini

// http://dean.edwards.name/weblog/2005/10/add-event/

function addEvent(element, type, handler) {
	if (element.addEventListener) {
		element.addEventListener(type, handler, false);
	} else {
		// assign each event handler a unique ID
		if (!handler.$$guid) handler.$$guid = addEvent.guid++;
		// create a hash table of event types for the element
		if (!element.events) element.events = {};
		// create a hash table of event handlers for each element/event pair
		var handlers = element.events[type];
		if (!handlers) {
			handlers = element.events[type] = {};
			// store the existing event handler (if there is one)
			if (element["on" + type]) {
				handlers[0] = element["on" + type];
			}
		}
		// store the event handler in the hash table
		handlers[handler.$$guid] = handler;
		// assign a global event handler to do all the work
		element["on" + type] = handleEvent;
	}
};
// a counter used to create unique IDs
addEvent.guid = 1;

function removeEvent(element, type, handler) {
	if (element.removeEventListener) {
		element.removeEventListener(type, handler, false);
	} else {
		// delete the event handler from the hash table
		if (element.events && element.events[type]) {
			delete element.events[type][handler.$$guid];
		}
	}
};

function handleEvent(event) {
	var returnValue = true;
	// grab the event object (IE uses a global event object)
	event = event || fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event);
	// get a reference to the hash table of event handlers
	var handlers = this.events[event.type];
	// execute each event handler
	for (var i in handlers) {
		this.$$handleEvent = handlers[i];
		if (this.$$handleEvent(event) === false) {
			returnValue = false;
		}
	}
	return returnValue;
};

function fixEvent(event) {
	// add W3C standard event methods
	event.preventDefault = fixEvent.preventDefault;
	event.stopPropagation = fixEvent.stopPropagation;
	return event;
};
fixEvent.preventDefault = function() {
	this.returnValue = false;
};
fixEvent.stopPropagation = function() {
	this.cancelBubble = true;
};

// PRESERVE ditherdog browser checking -- then redefine getElementById
//  browser sniffer
var IE4 = (document.all && !document.getElementById) ? true : false;
var NS4 = (document.layers) ? true : false;
var IE5 = (document.all && document.getElementById) ? true : false;
var N6 = (document.getElementById && !document.all) ? true : false;

// http://webbugtrack.blogspot.com/2007/08/bug-152-getelementbyid-returns.html
var isOpera = false, isIE = false;
if(typeof(window.opera) != 'undefined') { isOpera = true; }
if(!isOpera && navigator.userAgent.indexOf('Internet Explorer')) { isIE = true; }

if(isOpera || isIE)
{
	document.nativeGetElementById = document.getElementById;
	document.getElementById = function (id)
	{
		var e = document.nativeGetElementById(id);
		if(e)
		{
			// verify we got an id match and not something else
			if(e.attributes['id'] && e.attributes['id'].value == id)
			{
				return e;
			}
			else
			{
				// the first match wasn't an id match!
				// using the non-standard document.all search for a match
				// skipping the first element, since we already know its not a match
				for(var i=1; i<da[id].length; i++)
				{
					var e = document.all[id][i];
					if(e.attributes['id'] && e.attributes['id'].value == id)
					{
						return e;
					}
				}
			}
		}
		return null;
	};
}

// define a cross-browser getElementById
function fromId(id)
{
	if(document.getElementById)
	{
		return document.getElementById(id);
	}
	else if(document.all)
	{
		return document.all[id];
	}
	else if(document.layers)
	{
		return document.layers[id];
	}
	return null;
}

//  This function swaps images, using the "img" array, defined below.
function menuHi(num, imgname) {
	if (menusReady==false) return;
	if (num/2==currentSection) return;  //  do nothing if highlight is for current section
	if (num%2==0 && currMenu!=-1) {  //  hide current menu after 1 sec.
		del = (menus[currMenu].length > 0) ? 100 : 1;
		menuTimeout = setTimeout('hideMenu()',del);
	}
	if (num%2==0 && num/2==currMenu && menus[currMenu].length > 0) return;  //  leave highlight on if it's the current menu and menu has items
	if (num%2==1) hideMenu();  //  hide last menu
	if (num>=menus.length*2) {  //  rollover swap for non-menu nav items
		if (num%2==0) document.images['navImg'+currentSection].src = img[1+currentSection*2].src;
		else document.images['navImg'+currentSection].src = img[currentSection*2].src;
	}
	imgname.src = img[num].src;
}

//  Define on and off menu IMG SRC here.  "off" state first, "on" state second
imgsrc = new Array(
	'/images/nav/oem.gif',
	'/images/nav/oem-on.gif',
  '/images/nav/contact.gif',
  '/images/nav/contact-on.gif'
);

//  This loop converts the "imgsrc" array into IMG objects.
img = new Array();
for (i = 0; i < imgsrc.length; i++) {
	img[i] = new Image();
	img[i].src = imgsrc[i];
}

//  This function is called when the page loads.  It writes the dHTML menus using the "menus" array, defined below.
//  The "prefix" variable is defined in the HEAD of each page.  See "menus" array (below) for more info.
function drawMenus(isHome) {
	for (i=0;i<menus.length;i++) {
		if (menus[i].length>0) {
			if (NS4) {
			// find widest menu item
			
				for (j=0;j<menus[i].length;j+=2) {
					with(document.widthcheck.document) {
						open();
						write('<SPAN CLASS="menuItem">'+menus[i][j]+'<\/SPAN>');
						close();
					}
					tempwidth = document.widthcheck.clip.width + 16;
					if (tempwidth > menuWidth[i]) menuWidth[i] = tempwidth;
				}
		
			// write menu rectangle and rollover highlights
			
				htmlStr = '';
				htmlStr += '<TABLE CELLSPACING="0" CELLPADDING="0" BORDER="0">\r';
				htmlStr += '<TR>\r';
				htmlStr += '<TD><IMG SRC="/images/dot-tran.gif" WIDTH="1" HEIGHT="1"><\/TD>\r';
				htmlStr += '<TD BGCOLOR="#FF4700" COLSPAN="3"><IMG SRC="/images/dot-tran.gif" WIDTH="1" HEIGHT="1"><\/TD>\r';
				htmlStr += '<\/TR>\r';
				for (j=0;j<menus[i].length;j+=2) {
					htmlStr += '<TR>\r';
					htmlStr += '<TD><IMG SRC="/images/dot-tran.gif" WIDTH="2" HEIGHT="18" NAME="menu'+i+'left'+j+'"><\/TD>\r';
					htmlStr += '<TD BGCOLOR="#FF4700"><IMG SRC="/images/dot-tran.gif" WIDTH="1" HEIGHT="1"><\/TD>\r';
					htmlStr += '<TD BGCOLOR="#FFFFFF"><IMG SRC="/images/dot-tran.gif" WIDTH="'+menuWidth[i]+'" HEIGHT="18" BORDER="0" NAME="menu'+i+'itemblock'+j+'"><\/TD>\r';
					htmlStr += '<TD BGCOLOR="#FF4700"><IMG SRC="/images/dot-tran.gif" WIDTH="1" HEIGHT="1"><\/TD>\r';
					htmlStr += '<TD><IMG SRC="/images/dot-tran.gif" WIDTH="2" HEIGHT="18" NAME="menu'+i+'right'+j+'"><\/TD>\r';
					htmlStr += '<\/TR>\r';
				}
				htmlStr += '<TR>\r';
				htmlStr += '<TD><IMG SRC="/images/dot-tran.gif" WIDTH="1" HEIGHT="1"><\/TD>\r';
				htmlStr += '<TD BGCOLOR="#FF4700" COLSPAN="3"><IMG SRC="/images/dot-tran.gif" WIDTH="1" HEIGHT="1"><\/TD>\r';
				htmlStr += '<\/TR>\r';
				htmlStr += '<\/TABLE>\r';
				menuObj = fromId('menu'+i+'blocks').document;
				menuObj.open();
				menuObj.write(htmlStr);
				menuObj.close();
				menuObj = fromId('menu'+i+'blocks');
				menuObj.left = menuPlace[i];
				if (isHome) menuObj.top = homeVpos;
			
			// write menu items
			
				htmlStr = '';
				htmlStr += '<TABLE CELLSPACING="0" CELLPADDING="0" BORDER="0">\r';
				for (j=0;j<menus[i].length;j+=2) {
					htmlStr += '<TR>\r';
					htmlStr += '<TD><IMG SRC="/images/dot-tran.gif" WIDTH="1" HEIGHT="18"><\/TD>\r';
					htmlStr += '<TD><A HREF="/'+menus[i][j+1]+'" onMouseOver="highlight('+j+',true)" onMouseOut="highlight('+j+',false)" CLASS="menuItem">'+menus[i][j]+'<\/A><\/TD>\r';
					htmlStr += '<\/TR>\r';
				}
				htmlStr += '<\/TABLE>\r';
				menuObj = fromId('menu'+i).document;
				menuObj.open();
				menuObj.write(htmlStr);
				menuObj.close();
				menuObj = fromId('menu'+i);
				menuObj.left = 8 + menuPlace[i];
				if (isHome) menuObj.top = homeVpos;  //  move menu lower on home page
				
			} else {
				htmlStr = '';
				htmlStr += '<TABLE CELLSPACING="0" CELLPADDING="0" BORDER="0">\r';
				htmlStr += '<TR>\r';
				htmlStr += '<TD><IMG SRC="/images/dot-tran.gif" WIDTH="1" HEIGHT="1"><\/TD>\r';
				htmlStr += '<TD BGCOLOR="#FF4700" COLSPAN="5"><IMG SRC="/images/dot-tran.gif" WIDTH="1" HEIGHT="1"><\/TD>\r';
				htmlStr += '<\/TR>\r';
				for (j=0;j<menus[i].length;j+=2) {
					htmlStr += '<TR>\r';
					htmlStr += '<TD ID="menu'+i+'left'+j+'"><IMG SRC="/images/dot-tran.gif" WIDTH="2" HEIGHT="18"><\/TD>\r';
					htmlStr += '<TD BGCOLOR="#FF4700"><IMG SRC="/images/dot-tran.gif" WIDTH="1" HEIGHT="1"><\/TD>\r';
					htmlStr += '<TD BGCOLOR="#FFFFFF" ID="menu'+i+'padleft'+j+'"><IMG SRC="/images/dot-tran.gif" WIDTH="5" HEIGHT="1"><\/TD>\r';
					htmlStr += '<TD BGCOLOR="#FFFFFF" NOWRAP ID="menu'+i+'item'+j+'" onMouseOver="highlight('+j+',true)" onMouseOut="highlight('+j+',false)" onClick="document.location=\'/'+menus[i][j+1]+'\'" STYLE="cursor:pointer"><A HREF="/'+menus[i][j+1]+'" CLASS="menuItem">'+menus[i][j]+'<\/A><\/TD>\r';
					htmlStr += '<TD BGCOLOR="#FFFFFF" ID="menu'+i+'padright'+j+'"><IMG SRC="/images/dot-tran.gif" WIDTH="5" HEIGHT="1"><\/TD>\r';
					htmlStr += '<TD BGCOLOR="#FF4700"><IMG SRC="/images/dot-tran.gif" WIDTH="1" HEIGHT="1"><\/TD>\r';
					htmlStr += '<TD ID="menu'+i+'right'+j+'"><IMG SRC="/images/dot-tran.gif" WIDTH="2" HEIGHT="18"><\/TD>\r';
					htmlStr += '<\/TR>\r';
				}
				htmlStr += '<TR>\r';
				htmlStr += '<TD><IMG SRC="/images/dot-tran.gif" WIDTH="1" HEIGHT="1"><\/TD>\r';
				htmlStr += '<TD BGCOLOR="#FF4700" COLSPAN="5"><IMG SRC="/images/dot-tran.gif" WIDTH="1" HEIGHT="1"><\/TD>\r';
				htmlStr += '<\/TR>\r';
				htmlStr += '<\/TABLE>\r';
				menuObj = fromId('menu'+i);
				menuObj.innerHTML = htmlStr;
				menuObj.style.left = menuPlace[i];
				if (isHome) menuObj.style.top = homeVpos;  //  move menu lower on home page
			}
		}
	}
}

//  This function is called when the mouse rolls over menu IMGs.  It hides/unhides the appropriate menu.
function showMenu(men,isVisible) {
	if (menusReady==false) return;
	if (men!=currMenu) hideMenu();  //  hide last menu if new menu is activated
	currMenu = men;
	if (NS4) {
		menuDiv = fromId('menu'+men);
		menublockDiv = fromId('menu'+men+'blocks');
		menuDiv.visibility = (isVisible==true) ? 'visible' : 'hidden';
		menublockDiv.visibility = menuDiv.visibility;
	} else {
		menuDiv = fromId('menu'+men);
		menuDiv.style.visibility = (isVisible==true) ? 'visible' : 'hidden';
	}
	if (currentSection!=-1) {  //  turn on/off menu highlight depending on mouse location
		if (isVisible==true && men!=currentSection) document.images['navImg'+currentSection].src = img[currentSection*2].src;
		else document.images['navImg'+currentSection].src = img[1+currentSection*2].src;
	}
	clearTimeout(menuTimeout);
}


//  This function makes the current menu item highlight in orange.
//  It is called when the mouse rolls over a HREF, written in "drawMenus()"
function highlight(itm, isOn) {
	if (NS4) {
		leftHilite = fromId('menu'+currMenu+'blocks').document.images['menu'+currMenu+'left'+itm];
		rightHilite = fromId('menu'+currMenu+'blocks').document.images['menu'+currMenu+'right'+itm];
		itemBlock = fromId('menu'+currMenu+'blocks').document.images['menu'+currMenu+'itemblock'+itm];
		if (isOn==true) {
			clearTimeout(menuTimeout);
			leftHilite.src = '/images/dot-oran.gif';
			rightHilite.src = '/images/dot-oran.gif';
			itemBlock.src = '/images/dot-pech.gif';
		} else {
			del = (menus[currMenu].length > 0) ? 100 : 1;
			menuTimeout = setTimeout('hideMenu()',del);
			leftHilite.src = '/images/dot-tran.gif';
			rightHilite.src = '/images/dot-tran.gif';
			itemBlock.src = '/images/dot-tran.gif';
		}
	} else {
		leftHilite = fromId('menu'+currMenu+'left'+itm);
		rightHilite = fromId('menu'+currMenu+'right'+itm);
		leftPad = fromId('menu'+currMenu+'padleft'+itm);
		rightPad = fromId('menu'+currMenu+'padright'+itm);
		itemTD = fromId('menu'+currMenu+'item'+itm);
		if (isOn==true) {
			clearTimeout(menuTimeout);
			leftHilite.bgColor = '#ff4700';
			rightHilite.bgColor = '#ff4700';
			leftPad.bgColor = '#ffdacc';
			rightPad.bgColor = '#ffdacc';
			itemTD.bgColor = '#ffdacc';
		} else {
			del = (menus[currMenu].length > 0) ? 100 : 1;
			menuTimeout = setTimeout('hideMenu()',del);
			leftHilite.bgColor = '';
			rightHilite.bgColor = '';
			leftPad.bgColor = '#ffffff';
			rightPad.bgColor = '#ffffff';
			itemTD.bgColor = '#ffffff';
		}
	}
}

//  This function hides the current menu.
function hideMenu() {
	if (menusReady==false) return;
	if (currMenu>-1) {
		showMenu(currMenu,false);
		if (currMenu!=currentSection) document.images['navImg'+currMenu].src = img[currMenu*2].src;
	}
	clearTimeout(menuTimeout);
}


//  This function makes slight changes, depending on different platforms and browsers.
//  It also sniffs what section the user is in, using the on/off menu IMGs.
//  Finally, it sets the "menusReady" flag to true, letting all the menu scripts run.
function adjustForPlatforms(isHome) {
	// find current section
	if (!isHome) {
		for (i=0;i<imgsrc.length/2;i++) {
			checkImg = document.images['navImg'+i];
			if (checkImg && checkImg.src.indexOf('-on.gif')!=-1) {
				currentSection = i;
				break;
			}
		}
	}

	// search box size
	if (IE4 || IE5) {
		if (navigator.appVersion.indexOf("Mac") != -1) document.forms['searchbox'].elements['query'].size = 22;
		else document.forms['searchbox'].elements['query'].size = 25;
	}
	
	// switch background image if screen has millions of colors
	if (!isHome && (IE4||IE5) && screen.colorDepth > 16 ) document.body.background = '/images/margin-bg.gif';

	menusReady = true;
}

//  This function removes the marquee around Windows IE HREFs, which appear when a link is clicked but the page does not refresh.
function blurry(obj) {
	if (IE4 || IE5) obj.blur();
}

//  This function is used to show larger images of thumbnails in a separate, specifically sized window.
function popImage(pathName,winTitle,w,h) {
	winWidth = w + 18
	winHeight = h + 28
	newWin = window.open(pathName,winTitle,'toolbar=no,location=no,directories=no,status=no,scrollbars=no,resizable=no,copyhistory=no,width='+winWidth+',height='+winHeight);
}

menusReady = false;  //  flag to prevent script activation before the page has initialized
currentSection = -1;  //  tracks what section the user is in
currMenu = -1;  //  tracks what menu is currently active
menuTimeout = -1;  //  initializes the menuTimeout object


/*
	The following two-dimensional array holds all the menu information, from left (menus[0]) to right (menus[5]).
	All menu-based HREF definitions are done here.
	The syntax is [menu item as displayed on screen],[relative location from root directory]
	The "prefix" variable, which is defined in the HEAD of each page, allows pages contained in subdirectories to access these relative locations correctly.
	Even though a menu-bar items has no menu items, it must have an empty array associated to it.
*/
menus = [ 
  [ /* OEM */
	'European and Arabic Linguistic Suite','oem/euro_lang.htm',
	'Asian Linguistic Suite','oem/asian_lang.htm',
	'Language & Character Encoding Identification','oem/encoding_id.htm',
	'Unicode Library','oem/unicode.htm',
	'Natural Language Queries','oem/natural_lang_queries.htm',	
	'Related Queries','oem/related_queries.htm',
	'Linguistic Pattern Match','oem/pattern_match.htm',
	'Knowledgebase Extraction Enhancements','oem/knowledgebase.htm',
	'Spelling Correction','oem/euro_lang.htm#spelling',
	'Hashing and Compression','oem/hashing_compression.htm',
	'Dictionary Builder and Classifier','oem/dictionary_builder.htm',
	'International Dictionaries','oem/international_dict.htm',
	'XML Parser','oem/xml_parser.htm'
  ],
  []  /* Contact */
];
menuWidth = new Array();  //  initialize menuWidth array to minimum 2 pixels
for (i=0;i<menus.length;i++) menuWidth[i]=2;

// menuPlace = new Array(66,145,237,305,369,369,497); old menu
menuPlace = new Array(66,145,212,304);  //  x-position of each menu item image
homeVpos = 186;  //  y-position of menu bar, which is different on the home page


/* 
Methods for resizing the flash stage at runtime (for the MiniChart on home).

setFlashWidth(divid, newW)
divid: id of the div containing the flash movie.
newW: new width for flash movie

setFlashWidth(divid, newH)
divid: id of the div containing the flash movie.
newH: new height for flash movie

setFlashSize(divid, newW, newH)
divid: id of the div containing the flash movie.
newW: new width for flash movie
newH: new height for flash movie

canResizeFlash()
returns true if browser supports resizing flash, false if not. 
*/
function setFlashWidth(divid, newW){
	fromId(divid).style.width = newW+"px";
}
function setFlashHeight(divid, newH){
	fromId(divid).style.height = newH+"px";		
}
function setFlashSize(divid, newW, newH){
	setFlashWidth(divid, newW);
	setFlashHeight(divid, newH);
}
function canResizeFlash(){
	var ua = navigator.userAgent.toLowerCase();
	var opera = ua.indexOf("opera");
	if( document.getElementById ){
		if(opera == -1) return true;
		else if(parseInt(ua.substr(opera+6, 1)) >= 7) return true;
	}
	return false;
}


