// JavaScript Document
<!--
var toggleMenu = {
	init : function(sContainerClass, sHiddenClass) {
		if (!document.getElementById || !document.createTextNode) {
			return;
		} // Check for DOM support

		// setting up the actual page url with NO variables attached to it
		// check only the url itself ignoring the variables in the URL
		// added by rogcoli@ig.com.br

		var CompleteUrl 		= window.document.location.toString();
		var splitCompleteUrl 	= CompleteUrl.split("/");
		var splitSplitUrl		= splitCompleteUrl[splitCompleteUrl.length-1].split("?");
		var actualPage			= splitSplitUrl[0];

        // Add by Lawrence 2008/09/04 (begin)

		var splitSplitUrl2	= splitCompleteUrl[splitCompleteUrl.length-2].split("?");
		var actualPage2			= splitSplitUrl2[0];

		actualLinkObj = this.stripLink(CompleteUrl);


// Add by Lawrence 2008/09/04 (end)

		// END setting up the actual page url with NO variables attached to it

		var arrMenus = this.getElementsByClassName(document, 'ul', sContainerClass);
		var arrSubMenus, oSubMenu, oLink;

		for (var i = 0; i < arrMenus.length; i++) {

			// ULs - second level
			arrSubMenus = arrMenus[i].getElementsByTagName('ul');

			for (var j = 0; j < arrSubMenus.length; j++) {
				// ULs - second level
				oSubMenu = arrSubMenus[j];

				// links(URL) first level
				oLink = oSubMenu.parentNode.getElementsByTagName('a')[0];

				// element 'A' - second level
				oSubA = oSubMenu.getElementsByTagName('a');

				// added by rogcoli@ig.com.br
				//arrSubA = [];
				//added by Lawrence 2008/09/04
				//arrSubA2 = [];

				//
				menuLinkObjArray = [];

				for (var k = 0; k < oSubA.length; k++) {
					// to correct a IE bug wich returns the full URL as ths href atribute
					var splitLink = oSubA[k].attributes['href'].value.split("/")
					//arrSubA[k] = splitLink[splitLink.length-1];
					//arrSubA2[k] = splitLink[splitLink.length-2];

					var tempObj = this.stripLink(oSubA[k].attributes['href'].value);
					if (tempObj['domain'] == null)
						tempObj['domain'] = actualLinkObj['domain'];

					menuLinkObjArray[k] = tempObj

					// page, level, domain, depth
					//alert("depth: "+tempObj['depth']+"\ndomain: "+tempObj['domain']+"\nlevel: "+tempObj['level']+"\npage: "+tempObj['page']);

				}

				//arraySubA2[k]+arrSubA[k] = /xxxx/xxxx.php

				// added by rogcoli@ig.com.br
				// test if there is a actualPage variable in the links array
				// actualPage - pagename of current page
				// actualPage2 - sublevel of current page
				// arrSubA - array of pagename of current menu
				// arrSubA2 - array of sublevel of current menu

				//alert("actualPage:"+actualPage+"\nactualPage2:"+actualPage2);

				//searchResult = this.arraySearch(actualPage,actualPage2, arrSubA,arrSubA2);

/*
				if(searchResult == -1000){

					oLink.onclick = function(){
						//Lawrence Added 2008/09/04 (begin)
						document.location.href = this.href;
						//Lawrence Added 2008/09/04  (end)


						toggleMenu.toggle(this.parentNode.getElementsByTagName('ul')[0], sHiddenClass);
						return false;
					}
					this.toggle(oSubMenu, sHiddenClass);
				}
				else if (searchResult == -100) {
					oLink.onclick = function(){

						//Lawrence Added 2008/09/04 (begin)
						document.location.href = this.href;
						//Lawrence Added 2008/09/04  (end)

						toggleMenu.toggle(this.parentNode.getElementsByTagName('ul')[0], sHiddenClass);
						return false;
					}
					this.toggle(oSubMenu, 'block');
					// disabling the actualpage link
					oSubA[searchResult].onclick = function(){
						return false;
					}
				}
				// actual link 100% found
				else {
					oLink.onclick = function(){

						//Lawrence Added 2008/09/04 (begin)
						document.location.href = this.href;
						//Lawrence Added 2008/09/04  (end)

						toggleMenu.toggle(this.parentNode.getElementsByTagName('ul')[0], sHiddenClass);
						return false;
					}
					this.toggle(oSubMenu, 'block');
					// disabling the actualpage link
					oSubA[searchResult].onclick = function(){
						return false;
					}
					// changing the class of the parent element
					oSubA[searchResult].parentNode.className = 'actualpage';
				}

*/
				searchedIndex = this.linkMatch(actualLinkObj, menuLinkObjArray);

				if(searchedIndex == -2){

					oLink.onclick = function(){
						//Lawrence Added 2008/09/04 (begin)
						document.location.href = this.href;
						//Lawrence Added 2008/09/04  (end)


						toggleMenu.toggle(this.parentNode.getElementsByTagName('ul')[0], sHiddenClass);
						return false;
					}
					this.toggle(oSubMenu, sHiddenClass);
				}
				else if (searchedIndex == -1) {
					oLink.onclick = function(){

						//Lawrence Added 2008/09/04 (begin)
						document.location.href = this.href;
						//Lawrence Added 2008/09/04  (end)

						toggleMenu.toggle(this.parentNode.getElementsByTagName('ul')[0], sHiddenClass);
						return false;
					}
					this.toggle(oSubMenu, 'block');
				}
				// actual link 100% found
				else {
					oLink.onclick = function(){

						//Lawrence Added 2008/09/04 (begin)
						document.location.href = this.href;
						//Lawrence Added 2008/09/04  (end)

						toggleMenu.toggle(this.parentNode.getElementsByTagName('ul')[0], sHiddenClass);
						return false;
					}
					this.toggle(oSubMenu, 'block');
					// disabling the actualpage link
					oSubA[searchedIndex].onclick = function(){
						return false;
					}
					// changing the class of the parent element
					oSubA[searchedIndex].parentNode.className = 'actualpage';
				}

			}
		}
	},
	toggle : function(el, sHiddenClass) {
		var oRegExp = new RegExp("(^|\\s)" + sHiddenClass + "(\\s|$)");
		 // Add or remove the class name that hides the element
		el.className = (oRegExp.test(el.className)) ? el.className.replace(oRegExp, '') : el.className + ' ' + sHiddenClass;
	},
	stripLink : function(hrefValue) {
		var linkObj = new Object();

		temp = hrefValue.split("?");
		if (temp.length > 1)
			temp=temp[1];
		else
			temp=temp[0];

		// handle http://domain parts
		if (temp.indexOf("://")!=-1) {
			linkObj['domain'] = temp.substring(0, temp.indexOf("/", temp.indexOf("://")+3));
			linkObj['domain'] = linkObj['domain'].toLowerCase();

			// remove the domain
			temp = temp.split("://");
			if (temp.length > 1)
				temp=temp[1];
			else
				temp=temp[0];

			temp = temp.substring(temp.indexOf("/"));
		}
		else
			linkObj['domain'] = null;

		if (temp.lastIndexOf("/") != temp.length-1) {
			linkObj['page'] = temp.substring(temp.lastIndexOf("/")+1);
			linkObj['level'] = temp.substring(0, temp.lastIndexOf("/")+1);
		}
		else {
			linkObj['level'] = temp;
			linkObj['page'] = "";
		}
		var dirArray = linkObj['level'].split("/");

		linkObj['lastdir'] = dirArray[dirArray.length-2];

		linkObj['depth'] = (linkObj['level'].split("/")).length-2;
		linkObj['level'] = linkObj['level'].toLowerCase();
		linkObj['page'] = linkObj['page'].toLowerCase();

		return linkObj;
	},
	linkMatch : function(linkObject, linkObjectArray) {
		returnCode = -2;
		var menufound = false;
		var menufoundIndex = -1;
		var linkfound = false;
		var linkfoundIndex = -1;

		/*****
		 * Do not attemp to do menu expand logic if current page is not within sublevel
		 *****/
		//if (linkObject['page']=="") return returnCode;

		for (var i=0; i < linkObjectArray.length; i++) {
			// return index to make index menu link as active page and show menu
			// return -1 to show menu
			// return -2 to hide menu

			if (linkObject['level'] == linkObjectArray[i]['level']) {

				menufound = true;
				menufoundIndex = i;
				returnCode = -1;

				// remove menu expand if either page is only a holder #
				if (linkObject['page'].charAt(linkObject['page'].length-1)=='#' || linkObjectArray[i]['page'].charAt(linkObjectArray[i]['page'].length-1)=='#') {
					returnCode = -2;
					menufound = false;
					menufoundIndex = -1;
				}
				else if (linkObject['page'] == linkObjectArray[i]['page']) {
					linkfound = true;
					linkfoundIndex = i;
					returnCode = i;
				}
			}
			// else only check the last dir
			else if (linkObject['lastdir'] == linkObjectArray[i]['lastdir']) {
				if (linkObject['page'] == linkObjectArray[i]['page']) {
					linkfound = true;
					linkfoundIndex = i;
					returnCode = i;
				}
			}

			str = "";
			str += "Link depth: "+linkObject['depth']+"\ndomain: "+linkObject['domain']+"\nlevel: "+linkObject['level']+"\nlastdir: "+linkObject['lastdir']+"\npage: "+linkObject['page']+"\n\n";
			str += "Comp depth: "+linkObjectArray[i]['depth']+"\ndomain: "+linkObjectArray[i]['domain']+"\nlevel: "+linkObjectArray[i]['level']+"\nlastdir: "+linkObjectArray[i]['lastdir']+"\npage: "+linkObjectArray[i]['page']+"\n\n";
			str += "menufound:\t" + menufound;
			str += "\nlinkfound:\t " + linkfound;
			str += "\nreturnCode:\t" + returnCode;
			//alert(str);

			if (linkfound) break;
		}
		return returnCode;
	},
	// added by rogcoli@ig.com.br
	arraySearch : function(searchS,searchS2, arraySA, arraySA2) {
		 var I = 0;
		 var minI = 0;
		 var maxI = arraySA.length - 1;
		 //
		 var s = "";
		 //
		 var foundB = false;
		 var foundSublevel = false;
		 //
		 I = minI - 1;
		 while ((I <= maxI) && (!(foundB))) {
		  I = I + 1;
		  s = arraySA[I];

          //Modify by Lawrence 2008/09/04 begin

           s2 = arraySA2[I];
           foundB = (searchS == s && searchS2==s2);

		  //Modify by Lawrence 2008/09/25 begin

		   if (s=="*" && s2==searchS2){
		   foundB = true;
		   }

			/*
		   if (!foundB && s2==searchS2)
		   	foundSublevel = true;
			*/
		//Modify by Lawrence 2008/09/25 end

        //Modify by Lawrence 2008/09/04 end

		  }
		 if (foundB) {
		  return(I);
		 }
		 else if (foundSublevel) {
		 	return(-100);
		 }
		 else {
		  return(-1000); // some negative number indicating not found
		 }
	},
/* addEvent function from http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html */
	addEvent : function(obj, type, fn) {
		if (obj.addEventListener)
			obj.addEventListener(type, fn, false);
		else if (obj.attachEvent) {
			obj["e"+type+fn] = fn;
			obj[type+fn] = function() {obj["e"+type+fn](window.event);}
			obj.attachEvent("on"+type, obj[type+fn]);
		}
	},
/*
Written by Jonathan Snook, http://www.snook.ca/jonathan
Add-ons by Robert Nyman, http://www.robertnyman.com
*/
	getElementsByClassName : function(oElm, strTagName, strClassName){
	    var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
	    var arrReturnElements = new Array();
	    strClassName = strClassName.replace(/\-/g, "\\-");
	    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	    var oElement;
	    for(var i=0; i<arrElements.length; i++){
	        oElement = arrElements[i];
	        if(oRegExp.test(oElement.className)){
	            arrReturnElements.push(oElement);
	        }
	    }
	    return (arrReturnElements)
	}
};
toggleMenu.addEvent(window, 'load', function(){toggleMenu.init('menu','hidden');});
//-->

