//*** This code is copyright 2003 by Gavin Kistner, gavin@refinery.com
//*** It is covered under the license viewable at http://phrogz.net/JS/_ReuseLicense.txt
//*** Reuse or modification is free provided you abide by the terms of that license.
//*** (Including the first two lines above in your source code satisfies the conditions.)
var moveSub;

function UL2Menu_ShowHead(){
	
	var li=this;
	AddClass(li,'active');
	
	var xy=FindXYWH(li);
	
	
	if (li.isTop){
		
		moveSub = FindPosSubMenu(li);
		li.sub.style.left=(xy.x+(!li.isHorizontal?xy.w:0))+'px';
		li.sub.style.top=(xy.y+(li.isHorizontal?xy.h:0)-(li.isTop?0:1))+'px';
	
	} else {
		
		if ( navigator.appName.indexOf("Microsoft") >= 0){
			
			if(moveSub < 400){
				li.sub.style.left=li.offsetWidth-405+'px';
			}else{
				li.sub.style.left=li.offsetWidth-40+'px';
			}
		
		}else{
			
			if(moveSub < 400){
				li.sub.style.left=li.offsetWidth-375+'px';
			}else{
				li.sub.style.left=li.offsetWidth+'px';
			}
		
		}
		
		li.sub.style.top=(li.offsetTop)+'px';
	
	}
	
	li.sub.style.visibility='visible';

}

function UL2Menu_HideHead(){
	var li=this;
	li.sub.style.visibility='hidden';
	KillClass(li,'active');
}

function UL2Menu_ConvertMenu(){
	var menu=document.getElementById('menu');
	if (!menu) return;
	var menuIsHorizontal=HasClass(menu,'horizontal');
	var lis = menu.getElementsByTagName('li');
	for (var i=0,len=lis.length;i<len;i++){
		var li=lis[i];
		var uls = li.getElementsByTagName('ul');
		if (!uls || uls.length==0) continue;
		var ul=uls[0];
		li.sub=ul;
		li.onmouseover=UL2Menu_ShowHead;
		li.onmouseout=UL2Menu_HideHead;
		li.isTop = li.parentNode==menu;
		li.isHorizontal = (menuIsHorizontal && li.isTop);
		if (li.addedArrow || li.isTop) continue;
		
	}
}

function UL2Menu_KillSubMenu(){
	
	var menu=document.getElementById('menu');
	
	if (!menu) return;
	
	var menuIsHorizontal=HasClass(menu,'horizontal');
	var lis = menu.getElementsByTagName('li');
	
	for (var i=0,len=lis.length;i<len;i++){
		
		var li=lis[i];
		KillClass(li,'active');
		
	}
}

AttachEvent(window,'load',UL2Menu_ConvertMenu,true);
//UL2Menu_KillSubMenu();


//***Generic Library Functions Follow

	function getDim(el){
		for (var lx=0,ly=0;el!=null;
			lx+=el.offsetLeft,ly+=el.offsetTop,el=el.offsetParent);
		return {x:lx,y:ly}
	}
	
	function FindXY(obj){
	    if ( navigator.appName.indexOf("Opera") >= 0 ) {
			x=obj.offsetLeft;
			y=obj.offsetTop;
		} else {
			
			if ( navigator.appName.indexOf("Microsoft") >= 0) {
				var objPos = getDim(document.getElementById("menu"));
				var x=objPos.x-43, y=objPos.y;

				var posicaoX = x + obj.offsetLeft;
				var tamPX = window.screen.width - posicaoX;
				
				if( tamPX < 250 )
					x=x - 100;

			} else {
				
				var x=10, y=8;
				var posicaoX = x + obj.offsetLeft;
				var tamPX = window.screen.width - posicaoX;
				
				if( tamPX < 250 )
					x=x - 100;
			}
			
			if (obj){
				x+=obj.offsetLeft - (obj.scrollLeft || 0);
				y+=obj.offsetTop - (obj.scrollTop || 0);
			}
		}
		return {x:x,y:y};
	}

	
	function FindPosSubMenu(obj){
		
		if ( navigator.appName.indexOf("Microsoft") >= 0) {
			
			var objPos = getDim(document.getElementById("menu"));
			var x=objPos.x-43, y=objPos.y;
			var posicaoX = x + obj.offsetLeft;
			var tamPX = window.screen.width - posicaoX;
			
		} else {
			var x=10, y=8;
			var posicaoX = x + obj.offsetLeft;
			var tamPX = window.screen.width - posicaoX;
		}
			
		return tamPX;
	}


	function FindXYWH(obj){
		if (!obj) return { x:0, y:0, w:0, h:0};
		var objXY = FindXY(obj);
		return { x:objXY.x, y:objXY.y, w:obj.offsetWidth||0, h:obj.offsetHeight||0};
	}

	function AttachEvent(obj,evt,fnc,useCapture){
		if (obj.addEventListener){
			obj.addEventListener(evt,fnc,useCapture);
			return true;
		} else if (obj.attachEvent) return obj.attachEvent("on"+evt,fnc);
		else obj['on'+evt]=fnc;
		return true;
	}

	function HasClass(obj,cName){ 
		
		return (!obj || !obj.className)?false:(new RegExp("\\b"+cName+"\\b")).test(obj.className) 
	}
	
	function AddClass(obj,cName){ 
		
		if (!obj) return; 
		if (obj.className==null) 
			obj.className=''; 
			
		return obj.className+=(obj.className.length>0?' ':'')+cName; 
	}
	
	function KillClass(obj,cName){ 
		
		if (!obj) return; 
		return obj.className=obj.className.replace(RegExp("^"+cName+"\\b\\s*|\\s*\\b"+cName+"\\b",'g'),''); 
	}

