window.onload=function(){
	$('boxSubMenu').isVisible=false;
	var links = $('boxMenu').childElements();
	
	//get current page name
	var sPath = window.location.pathname;
	var pageName = sPath.substring(sPath.lastIndexOf('/') + 1);
	
	var srcElem=null;
	for(var i=0;i<links.length;i++){
		if(links[i].tagName=='A'){
			links[i].onmouseover=function(){
				if(srcElem!=null){srcElem.style.backgroundImage='url(img/backgroundMenuM.jpg)';}
				srcElem=this;
				var subLinks=srcElem.readAttribute('subLinks');
				srcElem.style.backgroundImage='url(img/backgroundMenuMOver.jpg)';
				if(subLinks!=''){
					if(!$('boxSubMenu').isVisible){
						$('boxSubMenu').isVisible=false;
						expand('boxSubMenu',18);
						$('boxSubMenu').isVisible=true;
						
					}

					if($('boxSubMenu').autoHideTimeout){
						window.clearTimeout($('boxSubMenu').autoHideTimeout);
					}
					//$('boxSubMenu').autoHideTimeout=window.setTimeout(function(){hideSubMenu();srcElem.style.backgroundImage='url(img/backgroundMenuM.jpg)';},2500);
					
					
					$('menuArrow').style.left=(this.positionedOffset().left+Math.round(this.offsetWidth/2)-Math.round($('menuArrow').offsetWidth/2))+'px';
					$('menuArrow').style.top=(this.positionedOffset().top+this.offsetHeight-$('menuArrow').offsetHeight+3)+'px';
					$('menuArrow').style.visibility='visible';
					
					var newContent='';
					var tab=subLinks.split(new RegExp('[,]+', 'g'));
					for(var j=0;j<tab.length;j++){
						var temp=tab[j].split(new RegExp('[:]+', 'g'));
						
						if(temp[1]==pageName){
							newContent+='<div>'+temp[0]+'</div>';
						}else{
							newContent+='<a href="'+temp[1]+'">'+temp[0]+'</a>';
						}
					}
					
					$('boxSubMenu').update(newContent);
				}
				else{
					hideSubMenu();
				}
			}
			links[i].onmouseout=function(){
				if(!$('boxSubMenu').isVisible && srcElem!=null){
					srcElem.style.backgroundImage='url(img/backgroundMenuM.jpg)';
				}				
				else{
					$('boxSubMenu').autoHideTimeout=window.setTimeout(function(){hideSubMenu();srcElem.style.backgroundImage='url(img/backgroundMenuM.jpg)';},2000);
				}
			}
		}
	}
	
	$('boxSubMenu').onmouseover=function(){
		if($('boxSubMenu').autoHideTimeout){
			window.clearTimeout($('boxSubMenu').autoHideTimeout);
		}
	}
	
	$('boxSubMenu').onmouseout=function(){
		if($('boxSubMenu').autoHideTimeout){
			window.clearTimeout($('boxSubMenu').autoHideTimeout);
		}
		$('boxSubMenu').autoHideTimeout=window.setTimeout(function(){hideSubMenu();srcElem.style.backgroundImage='url(img/backgroundMenuM.jpg)';},2500);
	}
}

function hideSubMenu()
{
	if($('boxSubMenu').isVisible){					
		collapse('boxSubMenu');
		$('boxSubMenu').update('');
		$('boxSubMenu').isVisible=false;
		
		$('menuArrow').style.visibility='hidden';
	}
}

function expand(elemID, maxHeight){
	var elem=$(elemID);
	
	if(elem.offsetHeight==0 && !elem.style.height){
		elem.style.height='0px';
	}

	if(elem.collapseTimer!=null){
		window.clearInterval(elem.collapseTimer);
		elem.collapseTimer=null;
	}
	
	if(elem.expandTimer==null && elem.style.height=='0px')
	{			
		elem.expandTimer = window.setInterval(function(){
			var height=parseInt(elem.style.height.replace('px',''));
			var ratio=Math.max(5,(maxHeight-height)*0.15);
			
			var newHeight=Math.min(maxHeight,height+ratio);
			elem.style.height=newHeight+'px';
			
			if(newHeight==maxHeight){
				window.clearInterval(elem.expandTimer);
				elem.expandTimer=null;
			}					
		}, 1000/25);
	}
}

function collapse(elemID){
	var elem=$(elemID);
	
	if( elem.expandTimer!=null){
		window.clearInterval( elem.expandTimer);
		 elem.expandTimer=null;
	}

	if(elem.collapseTimer==null && elem.style.height!='0px')
	{			
		elem.collapseTimer = window.setInterval(function(){
			var height=parseInt(elem.style.height.replace('px',''));
			var ratio=Math.max(5,(height)*0.25);
			
			var newHeight=Math.max(0,height-ratio);
			elem.style.height=newHeight+'px';
			
			if(newHeight<=0){
				window.clearInterval(elem.collapseTimer);
				elem.collapseTimer=null;
			}					
		}, 1000/25);
	}
}