addEvent(window,"load",addHoverListeners);

	 


//place dropdowns in divs and add hover behaviour
function addHoverListeners() {
	if (!document.body.style) return false;
	var items = document.getElementById("nav").getElementsByTagName("UL");
	
	for (var i=0;i<items.length;i++) {
	 
		var div = document.createElement('div');
		items[i].parentNode.insertBefore(div,items[i]);
		var ul = items[i].parentNode.removeChild(items[i]);
		div.appendChild(ul);
		ul.style.display = "block";
		var a = div.previousSibling.previousSibling;
		var list = div.getElementsByTagName("UL")[0];
		a.list = list;
		
		
							
	//magma menu
	if (items[i].getAttribute("rel")=="open"){	
	  // addEvent(a,"mouseover",startHover); 
	 
   
	}else{
		addEvent(a,"mouseover",startHover);
		addEvent(a,"mouseout",stopHover);
		addEvent(a,"focus",startHover);								//tab browsing
		addEvent(a,"blur",stopHover);	
	}
	
		
		
		var listitems = ul.getElementsByTagName("LI");
		
		for (var n=0;n<listitems.length;n++) {
			listitems[n].firstChild.list = list;
			
			//same story
			if (items[i].getAttribute("rel")=="open"){
			//nothing	
			}else{
			
			addEvent(listitems[n],"mouseover",startHover);
			addEvent(listitems[n],"mouseout",stopHover);
			addEvent(listitems[n].firstChild,"focus",startHover);	//tab browsing
			addEvent(listitems[n].firstChild,"blur",stopHover);		//tab browsing
			
			}
						
		}
		
		
		ul.listheight = ul.offsetHeight;
		ul.ctop = -1*ul.listheight;
		ul.style.top = ul.ctop-5+"px";
		ul.style.left = "0";
	}
	
			
	
		
	
}




//begin pulling out the dropdown
function startHover(e) {
	var targ = getTarget(e);
	var ul = targ.list;
	clearInterval(ul.intdown);
	clearInterval(ul.intup);
	ul.intdown = setInterval(function(){slidedown(ul)},10);
}

//begin pulling in dropdown
function stopHover(e) {
	var targ = getTarget(e);
	var ul = targ.list;
	clearInterval(ul.intdown);
	clearInterval(ul.intup);
	ul.intup = setInterval(function(){slideup(ul)},10);
}

//lower dropdown a bit
function slidedown(ul) {
	ul.speed = 0.1*(ul.ctop-2);
	ul.ctop-=ul.speed;
	if (ul.ctop>0) {
		ul.ctop=0;
		clearInterval(ul.intdown);
	}
	ul.style.top = ul.ctop+"px";
}

//raise dropdown a bit
function slideup(ul) {
	ul.speed = 0.1*(ul.ctop-2);
	ul.ctop+=ul.speed;
	if (ul.ctop<-1*ul.listheight) {
		ul.ctop=-1*ul.listheight;
		clearInterval(ul.intup);
	}
	ul.style.top = ul.ctop+"px";
}

function getTarget(e) {
	
	e = e || window.event;	
	return (e.target || e.srcElement);
	
}

function addEvent( 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] );
	}
}
