
function contains (array, element) 
  {
          for (var i = 0; i < array.length; i++) 
       {
              if (array[i].tagName == element) 
          {
                      return array[i];
              }
          }
          return null;
  };
//if(window.event)
	//window.event.cancelBubble = true;

addEvent(window, 'load', setMenus);
//document.addEventListener('click', resetAllLis, true);
var isIE;
//attachMyOnClickEvent(document, resetAllLis, true);
var lis;

function setMenus()
{
	isIE= (navigator.appName.indexOf("Microsoft")!=-1)?1:0;

	var menu = document.getElementById("menu");
	
	lis = menu.getElementsByTagName("LI");
	
	for(var i = 0; i<lis.length;i++)
	{
//		lis[i].firstChild.addEventListener('click',liClicked,true) ;	
		attachMyOnClickEvent(lis[i].firstChild, liClicked, true);
		if(lis[i].firstChild.href!="#")
		{
			lis[i].firstChild.oldHref=lis[i].firstChild.href;		
			lis[i].firstChild.href="#";
		}
		if(lis[i].className.indexOf('current_page')>=0)
		{
			lis[i].className+=" clicked";
		}
		var ul = lis[i].parentNode || lis[i].parentElement;
		//if(lis[i].parentNode.parentNode.parentNode.tagName =="UL" && lis[i].className =="page_item page-item-54 current_page_item clicked")
		if(isIE)
		{
			if(lis[i].parentElement.parentElement.parentElement.parentElement.parentElement.tagName=="UL" && lis[i].parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.tagName!="UL")
				repositionUL(ul);
			
		}
		else
		{
			if(lis[i].parentNode.parentNode.parentNode.parentNode.parentNode.tagName=="UL" && lis[i].parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.tagName!="UL")
				repositionUL(ul);
			else if(lis[i].parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.tagName=="UL")
			{
				repositionLeftUL(ul);
			}
		}
	}
}

function resetAllLis()
{	

	for(var i = lis.length-1; i>=0;i--)
	{
		//if(lis[i].clicked!=1)
			lis[i].className = lis[i].className.replace('clicked', '');
		
	}	
}

function liClicked()
{
	
	var li = this.parentNode || this.parentElement;
	
	var ul = contains(li.childNodes,"UL");
	
	if(li.className.indexOf('clicked' )>=0 || ul==null)
	{
		location.href=this.oldHref;
		resetAllLis();
		
		//return true;	
		
	}
	else
	{
		resetAllLis();
		li.className = li.className + ' clicked';
		setAllParentLis(li);
		li.clicked=1;
	
		//  alert('clicked2'); 
		if(!isIE)
			showMenu(ul);
		//return false;
	}
	return false;
}

function setAllParentLis(obj)
{
	var parentParent = obj.parentNode.parentNode || obj.parentElement.parentElement;
	
	if(parentParent.tagName=="LI")
	{
		if(parentParent.className.indexOf(' clicked' )<0)
			parentParent.className += ' clicked';
		setAllParentLis(parentParent);
	}
	else
	{
		return;
	}
}
function repositionUL(ul)
{
	var parentParent = ul.parentNode.parentNode.parentNode.parentNode || ul.parentElement.parentElement.parentElement.parentElement;
	if(ul.p!=1 && ul!=null && parentParent!=null && parentParent.tagName=="UL")
	{
		var parentul = ul.parentNode.parentNode || ul.parentElement.parentElement;
		var pos = getPos(parentul);
		var top = pos.height-20 +2;
		ul.style.top = top + 'px' ;
		ul.p = 1;
	}	
}
function repositionLeftUL(ul)
{
	var parentParent = ul.parentNode || ul.parentElement;
	if(ul.p!=1 && ul!=null && parentParent!=null && parentParent.tagName=="LI")
	{
		//parentParent.style.width = '250px';
		//parentParent.firstChild.style.display = 'inline';
		var pos = getPos(parentParent.firstChild);
		var left = (parentParent.firstChild.innerHTML.length*7)+2;
		ul.style.left = left + 'px' ;
		//parentParent.firstChild.style.display = 'inline';
		//alert(parentParent.firstChild.innerHTML.length*20);
		ul.p = 1;
	}	
}
function showMenu(obj)
{	
	time = 500;
	p=50;
	t=0;
	s= 100/(time/p);
	o=0;
	changeOpac(o,obj);
	
	setTimeout(function(){
		changeOpac(10, obj);				
	}, 20);
	setTimeout(function(){
		changeOpac(30, obj);				
	}, 90);
	setTimeout(function(){
		changeOpac(50, obj);				
	}, 160);
	setTimeout(function(){
		changeOpac(70, obj);				
	}, 230);
	setTimeout(function(){
		changeOpac(80, obj);				
	}, 300);
	setTimeout(function(){
		changeOpac(100, obj);				
	}, 370);
	
}
function changeOpac(opacity, obj) {
	var object = obj.style;
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}
function getPos(theObj){
	  x = y = 0;
	  	var oldstyle = theObj.style.display;	
	  	theObj.style.display = 'block';
	  h = theObj.offsetHeight;
	  w = theObj.offsetWidth;
  		theObj.style.display = oldstyle;
	  while(theObj){
	    x += theObj.offsetLeft;
	    y += theObj.offsetTop;
	    theObj = theObj.offsetParent;
	  }
	  return {height:h,width:w,x:x,y:y}
	}
function attachMyOnClickEvent(el, myFunction, bubbling)
{
	if (el.addEventListener) {
		el.addEventListener ("click",myFunction,bubbling);	
	//} else if (el.attachEvent) {
		//el.attachEvent ("onclick",myFunction);
	} else {
		el.onclick = myFunction;
	}	
}
function addEvent( obj, type, fn ) {
  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] );
  } else
    obj.addEventListener( type, fn, false );
}








