var AgntUsr=navigator.userAgent.toLowerCase();

var NavYes=AgntUsr.indexOf('mozilla')!=-1&&AgntUsr.indexOf('compatible')==-1?1:0;



var isDOM = (document.getElementById ? true : false); 

var isIE4 = ((document.all && !isDOM) ? true : false);

var isNS4 = (document.layers ? true : false);



function getRef(id) {

	if (isDOM) return document.getElementById(id);

	if (isIE4) return document.all[id];

	if (isNS4) return document.layers[id];

}



function getSty(id) {

	return (isNS4 ? getRef(id) : getRef(id).style);

} 



// Hide timeout.

var popTimer = 0;

// Array showing highlighted menu items.

var litNow = new Array();

var hiddenselects;

var visMenus = new Array(0);

var actMenu = -1;

var actItem = -1;



function getPageOffsetLeft(el) {



  var x;



  // Return the x coordinate of an element relative to the page.



  x = el.offsetLeft;

  if (el.offsetParent != null)

    x += getPageOffsetLeft(el.offsetParent);



  return x;

}







function getPageOffsetTop(el) {



  var y;



  // Return the x coordinate of an element relative to the page.



  y = el.offsetTop;

  if (el.offsetParent != null)

    y += getPageOffsetTop(el.offsetParent);



  return y;

}



function toggleNastyTagsVisibility(target, vis) {



    if(!isDOM) return;

    if (!hiddenselects) {

        hiddenselects=new Array(menu.length);

        for (count = 0; count < menu.length; count++) 

            hiddenselects[count] = new Array(0);

    }

    var n = document.getElementsByTagName("select");    

    var i;

    if (vis) {

        for(i=hiddenselects[target].length-1; i>=0; i--) {

            //node = hiddenselects[target].pop();

            node = arrayPop(hiddenselects[target]);

            node.style.visibility = "visible";

        }

        // menu over flash workaround

        if (typeof(detectFlash) != 'undefined' && detectFlash()) {   

            var flashHP = document.getElementById('flashHP');

            var gifHP = document.getElementById('gifHP');

            if (flashHP != null && gifHP != null) {

                flashHP.style.display = 'inline';

                gifHP.style.display = 'none';    

            }

        }           

    } else {

        var div = document.getElementById(target);



        // menu over flash workaround

        if (typeof(detectFlash) != 'undefined' && detectFlash()) {   

            var flashHP = document.getElementById('flashHP');

            var gifHP = document.getElementById('gifHP');                                    

            if (flashHP != null && gifHP != null) {            

                flashHP.style.display = 'none';

                gifHP.style.display = 'inline';  

                gifHP.style.visibility = 'visible';  

            }

        }



        if (div) {

            var dx1 = getPageOffsetLeft(div);

            var dx2 = dx1 + div.offsetWidth;

            var dy1 = getPageOffsetTop(div) - 4;            

            var dy2 = dy1 + div.offsetHeight; 



            var ox1;

            var ox2;

            var oy1;

            var oy2;



            var j=0;

            hiddenselects[target].length = 0;



            for(i=0; i<n.length; i++) {

                oy1 = getPageOffsetTop(n[i]);

                oy2 = oy1 + n[i].offsetHeight;



                if (dy2 > oy1 && dy1 < oy2) {

                    ox1 = getPageOffsetLeft(n[i]);

                    ox2 = ox1 + n[i].offsetWidth;

                    if (dx2 > ox1 && dx1 < ox2) {



                        n[i].style.visibility = "hidden";

                        hiddenselects[target][j] = n[i]; j++;

                    }

                }

            }

        }

    }



   /* n = document.getElementsByTagName("iframe");

    if(!n)

        return;



    for(i=0; i<n.length; i++)

        n[i].style.visibility = vis ? "visible" : "hidden";*/

}



function popOver(menuNum, itemNum) {

	clearTimeout(popTimer);	

	if (menuNum == actMenu && itemNum == actItem) {	  

	    return;

	} else {        

           actMenu = menuNum;

	   actItem = itemNum;

	}

	hideAllBut(menuNum);

	litNow = getTree(menuNum, itemNum);

	for (count = 0; count < litNow.length; count++) {

            if (!hiddenselects || hiddenselects[litNow[count][0]].length == 0) {

                toggleNastyTagsVisibility(litNow[count][0], false);			

	    }

	}

	changeCol(litNow, true);

	targetNum = menu[menuNum][itemNum].target;

	if (targetNum > 0) {

		thisX = parseInt(menu[menuNum][0].ref.left) + parseInt(menu[menuNum][itemNum].ref.left);

		thisY = parseInt(menu[menuNum][0].ref.top) + parseInt(menu[menuNum][itemNum].ref.top);

                isPushed = false;

                // create menu on demand 

		if (!menu[targetNum][0].ref) {

		    writeMenus(targetNum);

		    //visMenus.push(targetNum);

		    arrayPush(visMenus,targetNum);

		    isPushed = true;  

		}

		with (menu[targetNum][0].ref) {

			left = parseInt(thisX + menu[targetNum][0].x);

			top = parseInt(thisY + menu[targetNum][0].y);

			visibility = 'visible';			

			if (!isPushed && visMenus[visMenus.length -1] != targetNum) 

			    //visMenus.push(targetNum);

			    arrayPush(visMenus,targetNum);

		}

		// hide all intersepted select elements

		toggleNastyTagsVisibility(targetNum, false);		

	}

}



function popOut(menuNum, itemNum) {

	if ((menuNum == 0) && !menu[menuNum][itemNum].target) 

		hideAllBut(0)

	else

		popTimer = setTimeout('hideAllBut(0,true)', 250);

}



function getTree(menuNum, itemNum) {

	// Array index is the menu number. The contents are null (if that menu is not a parent)

	// or the item number in that menu that is an ancestor (to light it up).

	//itemArray = new Array(menu.length);

	var itemArray = new Array(0);

        var item



	while(1) {

		/*itemArray[menuNum] = itemNum;

		// If we've reached the top of the hierarchy, return.

		if (menuNum == 0) return itemArray;

		itemNum = menu[menuNum][0].parentItem;

		menuNum = menu[menuNum][0].parentMenu;*/		

		item = new Array(2);

		item[0] = menuNum;

		item[1] = itemNum;		

		//itemArray.push(item);		

		arrayPush(itemArray,item);		

		if (menuNum == 0) return itemArray;		

		itemNum = menu[menuNum][0].parentItem;

		menuNum = menu[menuNum][0].parentMenu;

	}

}



function getTree2(menuNum, itemNum) {

	// Array index is the menu number. The contents are null (if that menu is not a parent)

	// or the item number in that menu that is an ancestor (to light it up).

	//itemArray = new Array(menu.length);

	itemArray = new Array(0);



	while(1) {	        

	        //if (menuNum != 0) itemArray.push(menuNum);

	        if (menuNum != 0) arrayPush(itemArray,menuNum);

		// If we've reached the top of the hierarchy, return.

		if (menuNum == 0) return itemArray;

		menuNum = menu[menuNum][0].parentMenu;

	}

}





// Pass an array and a boolean to specify colour change, true = over colour.

function changeCol(changeArray, isOver) {

        var menuNum;

	for (menuCount = 0; menuCount < changeArray.length; menuCount++) {

		        menuNum = changeArray[menuCount][0];

		//if (changeArray[menuCount]) {

			newCol = isOver ? menu[menuNum][0].overCol : menu[menuNum][0].backCol;

			textNewCol = isOver ? menu[menuNum][0].textOverCol : menu[menuNum][0].textBackCol;

			// Change the colours of the div/layer background.

			with (menu[menuNum][changeArray[menuCount][1]]) {

				if (isNS4) {

					ref.bgColor = newCol;

					ref.color = textNewCol;

					lnk.style.color = textNewCol;

				} else {

					ref.backgroundColor = newCol;

					ref.color = textNewCol;

					lnk.style.color = textNewCol;

				}

			}

		//}

	}

}



function hideAllBut(menuNum, timeout) {  

        if (timeout) {

            actMenu = -1;

            actItem = -1;            

        }

	var keepMenus = getTree2(menuNum, 1);

	var newVisMenus = new Array(0);		

        for (count1 = 0; count1 < visMenus.length; count1++) {

            pop = true;

            for (count2 = 0; count2 < keepMenus.length; count2++) {   

                if (keepMenus[count2] == visMenus[count1]) {

                   pop = false;

    	        }    	        

	    }

            if (pop) {

	            menu[visMenus[count1]][0].ref.visibility = 'hidden';			    			    

	            toggleNastyTagsVisibility(visMenus[count1], true);

	    } else {

	        //newVisMenus.push(visMenus[count1]);   

	        arrayPush(newVisMenus,visMenus[count1]);   

	    } 

        }

        visMenus = newVisMenus;

	changeCol(litNow, false);

}



// *** MENU CONSTRUCTION FUNCTIONS ***

function Menu(isVert, popInd, x, y, width, overCol, backCol, borderClass, textClass, textOverCol, textBackCol) {

	// True or false - a vertical menu?

	this.isVert = isVert;

	// The popout indicator used (if any) for this menu.

	this.popInd = popInd

	// Position and size settings.

	this.x = x;

	this.y = y;

	this.width = width;

	// Colours of menu and items.

	this.overCol = overCol;

	this.backCol = backCol;

	// Colours of text.

	this.textOverCol = textOverCol;

	this.textBackCol = textBackCol;

	// The stylesheet class used for item borders and the text within items.

	this.borderClass = borderClass;

	this.textClass = textClass;

	// Parent menu and item numbers, indexed later.

	this.parentMenu = null;

	this.parentItem = null;

	// Reference to the object's style properties (set later).

	this.ref = null;

	this.lnk = null;

}



function Item(text, href, frame, length, spacing, target) {

	this.text = text;

	this.href = href;

	this.frame = frame;

	this.length = length;

	this.spacing = spacing;

	this.target = target;

	// Reference to the object's style properties (set later).

	this.ref = null;

}



function writeMenus(currMenu) {

if (!isDOM && !isIE4 && !isNS4) return;



if (!menu || !menu[currMenu]) return;



// do not create all menus !! Create menu on demand

//for (currMenu = 0; currMenu < menu.length; currMenu++) 



	with (menu[currMenu][0]) {

	// Variable for holding HTML for items and positions of next item.

	var str = '', itemX = 0, itemY = 0;

	// Remember, items start from 1 in the array (0 is menu object itself, above).

	// Also use properties of each item nested in the other with() for construction.

	for (currItem = 1; currItem < menu[currMenu].length; currItem++) 

		with (menu[currMenu][currItem]) {

			var itemID = 'm' + currMenu + 'i' + currItem;

			var refID = 'h' + currMenu + 'i' + currItem;



			// The width and height of the menu item - dependent on orientation!

			var w = (isVert ? width : length);

			var h = (isVert ? length : width);

			// Create a div or layer text string with appropriate styles/properties.

			// Thanks to Paul Maden (www.paulmaden.com) for helping debug this in IE4, apparently

			// the width must be a miniumum of 3 for it to work in that browser.

			if (isDOM || isIE4) {

				str += '<div id="' + itemID + '" style="z-index:100; cursor:hand;position: absolute; left: ' + itemX + '; top: ' + itemY + '; width: ' + (NavYes?w-LeftPaddng-RightPaddng:w) + '; height: ' + h + '; visibility: inherit; padding-left:' + LeftPaddng + 'px; padding-top:' + TopPaddng + 'px; padding-right:' + RightPaddng + 'px;';

				if (backCol) str += 'background: ' + backCol + '; ';

				if (textBackCol) str += 'color: ' + textBackCol + '; ';

				str += '" ';

			}

			if (isNS4) {

				str += '<layer id="' + itemID + '" left="' + itemX + '" top="' + itemY + '" width="' +  (NavYes?w-LeftPaddng-RightPaddng:w) + '" height="' + h + '" visibility="inherit" ';

				if (backCol) str += 'bgcolor="' + backCol + '" ';

				if (textBackCol) str += 'color: ' + textBackCol + '; ';

			}

			if (borderClass) str += 'class="' + borderClass + '" ';

			// Add mouseover handlers and finish div/layer.

				str += 'onMouseOver="popOver(' + currMenu + ',' + currItem + ')" onMouseOut="popOut(' + currMenu + ',' + currItem + ')"  onClick="window.open(\'' + href + '\',\'' + (frame?frame:'_top') + '\')">';

				// Add contents of item (default: table with link inside).

				// In IE/NS6+, add padding if there's a border to emulate NS4's layer padding.

				// If a target frame is specified, also add that to the <a> tag.

				str +=  '<span style="position: absolute; display:block; width:' + (w-25) + 'px;"><a id="' + refID + '" href="' + href + '" style="color:' + textBackCol + '; text-decoration:none;">' + text +  '</a></span>';

				//str +=  '<a id="' + refID + '" href="' + href + '" style="color:' + textBackCol + '; text-decoration:none;float:left">' + text +  '</a>';;

				if (target > 0) {

					// Set target's parents to this menu item.

					menu[target][0].parentMenu = currMenu;

					menu[target][0].parentItem = currItem;

					// Add a popout indicator.

					if (isDOM || isIE4) {

						if (popInd) str += '<div style="z-index:100;position: absolute; width: 17px; height: 12px; top: 2px; left:' + (w-18) + 'px; color: #ffffff; font-weight: 600; border: 1px"><a href="' + href + '" style="color: #ffffff; text-decoration:none;">' + popInd + '</a></div>';

                                                //if (popInd) str += '<a href="' + href + '" style="color: #ffffff; text-decoration:none;float:right">' + popInd + '</a>';

					} 

					if (isNS4) {

						if (popInd) str += '<layer style="position: absolute; width: 12px; height: 12px; top: 2px; left:' + (w-18) + 'px; color: #ffffff; font-weight: 600"><a href="' + href + '" style="color: #ffffff; text-decoration:none;">' + popInd + '</a></layer>';

					}

				} else {

					if (isDOM || isIE4) {

						str += '<div style="z-index:100;position: absolute; width: 12px; height: 12px; top: 3px; left:' + (w-18) + 'px;"><a href="' + href + '"><img src=\'/file/PMELink/bt_seta01.gif \' border=0 /></a></div>';

                                                //str += '<a href="' + href + '" style="float:right"><img src=\'/file/PMELink/bt_seta01.gif \' border=0 /></a>';						

					}

					if (isNS4) {

						str += '<layer style="position: absolute; width: 12px; height: 12px; top: 3px; left:' + (w-18) + 'px;"><a href="' + href + '"><img src=\'/file/PMELink/bt_seta01.gif \' border=0 /></a></layer>';

					}

				}

				str += (isNS4 ? '</layer>' : '</div>');

				if (isVert) itemY += length + spacing + (NavYes?TopPaddng+1:0);

				else itemX += length + spacing;

			}

			if (isDOM) {			   

				var newDiv = document.createElement('div');

				document.getElementById('categoryMenu').appendChild(newDiv);				

				newDiv.id = currMenu;

				newDiv.innerHTML = str;

				ref = newDiv.style;

				//ref.width= 160 + "px";						

				ref.width= 153 + "px";

				totalLength = menu[currMenu].length - 1;

				totalHeight= (totalLength * defLength);

				ref.height= totalHeight+ "px";

				ref.position = 'absolute';

				ref.visibility = 'hidden';

			}

			// Insert a div tag to the end of the BODY with menu HTML in place for IE4.

			if (isIE4) {

				document.body.insertAdjacentHTML('beforeEnd', '<div id="m' + currMenu + 'div" ' + 'style="visibility: hidden">' + str + '</div>');

				ref = getSty('m' + currMenu + 'div');

			}

			// In NS4, create a reference to a new layer and write the items to it.

			if (isNS4) {

				ref = new Layer(0);

				ref.document.write(str);

				ref.document.close();

			}

			for (currItem = 1; currItem < menu[currMenu].length; currItem++) {

				itemName = 'm' + currMenu + 'i' + currItem;

				refName = 'h' + currMenu + 'i' + currItem;

				if (isDOM || isIE4) menu[currMenu][currItem].ref = getSty(itemName);

				if (isNS4) menu[currMenu][currItem].ref = ref.document[itemName];

				menu[currMenu][currItem].lnk = document.getElementById(refName);

			}

		if (currMenu == 0)

			document.getElementById('categoryMenu').style.height = itemY;

	}

with(menu[currMenu][0]) {

	ref.left = x;

	ref.top = y;

	ref.visibility = 'visible';	

}

}





// These two lines handle the window resize bug in NS4. See <body onResize="...">.

// I recommend you leave this here as otherwise when you resize NS4's width menus are hidden.

var popOldWidth = window.innerWidth;

nsResizeHandler = new Function('if (popOldWidth != window.innerWidth) location.reload()');





// This is a quick snippet that captures all clicks on the document and hides the menus

// every time you click. Use if you want.

if (isNS4) document.captureEvents(Event.CLICK);

document.onclick = clickHandle;



function clickHandle(evt) {

	if (isNS4) document.routeEvent(evt);

	//hideAllBut(0);

}





// This is just the moving command for the example.

function moveRoot() {

	with(menu[0][0].ref) left = ((parseInt(left) < 100) ? 100 : 5);

}



