var dropdownmenu={
	delaybeforehide: 200, //set delay in milliseconds before content box disappears onMouseout (1000=1 sec)
	disableanchorlink: true, //when user clicks on anchor link, should it be disabled?
	ajaxloadingmsg: "Loading content. Please wait...", //HTML to show while ajax page is being feched
	ajaxbustcache: true, //Bust cache when fetching pages?

	getposOffset:function(what, offsettype){
		return (what.offsetParent)? what[offsettype]+this.getposOffset(what.offsetParent, offsettype) : what[offsettype]
	},

	isContained:function(m, e){
		var e=window.event || e
		var c=e.relatedTarget || ((e.type=="mouseover")? e.fromElement : e.toElement)
		while (c && c!=m)try {c=c.parentNode} catch(e){c=m}
		if (c==m)
			return true
		else
			return false
	},

	show:function(anchorobj, subobj, e){
		if (!this.isContained(anchorobj, e)){
			var dropposition = subobj.getAttribute("dropposition").toString().split(" ")

			//var horizontaloffset=(dropposition[0]=="left")? - (subobj.offsetWidth-anchorobj.offsetWidth) : 0 //calculate user added horizontal offset
			//var verticaloffset=(dropposition[1]=="top") ? -(subobj.offsetHeight-anchorobj.parentNode.parentNode.offsetHeight) :  //calculate user added vertical offset
			//subobj.style.left=this.getposOffset(anchorobj, "offsetLeft") + horizontaloffset + "px"
			//subobj.style.top = this.getposOffset(anchorobj.parentNode, "offsetTop") + "px"
			subobj.style.clip = (dropposition[1]=="top") ? "rect(auto auto 0 auto)" : "rect(0 auto 0 0)" //hide drop down box initially via clipping
			subobj.setAttribute("startTime", new Date().getTime())
			subobj.setAttribute("contentheight", parseInt(subobj.offsetHeight))
			if (typeof window["hidetimer_"+subobj.id]!="undefined") //clear timer that hides drop down box?
				clearTimeout(window["hidetimer_"+subobj.id])
			this.slideengine(subobj, (dropposition[1]=="top") ? "up" : "down")
			//anchorobj.setAttribute("class", "expandedOption")
			subobj.style.visibility = "visible"
			//anchorobj.className = "expandedOption"
		}
	},

	curveincrement:function(percent){
		return (1-Math.cos(percent*Math.PI)) / 2 //return cos curve based value from a percentage input
	},

	slideengine:function(obj, direction){
		var elapsed = new Date().getTime() - obj.getAttribute("startTime") //get time animation has run
		var glidetime = parseInt(obj.getAttribute("glidetime"))
		if (elapsed<glidetime){ //if time run is less than specified length
			//alert(this.curveincrement(elapsed/glidetime))
			var distancepercent = (direction=="up") ? this.curveincrement(elapsed/glidetime) : 1-this.curveincrement(elapsed/glidetime)
			var contentHeight = obj.getAttribute("contentheight")
			var currentclip = (distancepercent * contentHeight)
			var cliped = (direction == "up") ? "rect(auto, auto, " + currentclip + "px, 0)" : "rect(" + currentclip + "px, auto, auto, 0)"
			//alert(cliped)
			obj.style.top = (distancepercent * contentHeight * -1) + "px";
			obj.style.clip = cliped
			window["glidetimer_"+obj.id]=setTimeout(function(){dropdownmenu.slideengine(obj, direction)}, 10)
		}
		else{ //if animation finished
			obj.style.clip="rect(0 auto auto 0)"
		}
	},

	hide:function(activeobj, subobj, e, anchorobj){
		if (!dropdownmenu.isContained(activeobj, e)){
			window["hidetimer_"+subobj.id]=setTimeout(function(){
				subobj.style.visibility = "hidden"
				subobj.style.top = -500
				//anchorobj.setAttribute("class", "selectedOption")
				//anchorobj.className = "selectedOption"
				clearTimeout(window["glidetimer_"+subobj.id])
			}, dropdownmenu.delaybeforehide)
		}
	},

	ajaxconnect:function(pageurl, divId){
		var page_request = false
		var bustcacheparameter=""
		if (window.XMLHttpRequest) // if Mozilla, IE7, Safari etc
			page_request = new XMLHttpRequest()
		else if (window.ActiveXObject){ // if IE6 or below
			try {
			page_request = new ActiveXObject("Msxml2.XMLHTTP")
			} 
			catch (e){
				try{
				page_request = new ActiveXObject("Microsoft.XMLHTTP")
				}
				catch (e){}
			}
		}
		else
			return false
		document.getElementById(divId).innerHTML=this.ajaxloadingmsg //Display "fetching page message"
		page_request.onreadystatechange=function(){dropdownmenu.loadpage(page_request, divId)}
		if (this.ajaxbustcache) //if bust caching of external page
			bustcacheparameter=(pageurl.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
		page_request.open('GET', pageurl+bustcacheparameter, true)
		page_request.send(null)
	},

	loadpage:function(page_request, divId){
		if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)){
			document.getElementById(divId).innerHTML=page_request.responseText
		}
	},
	
	getObject:function(id) 
	{
		var object = null
		
		if( document.layers ) {   
			object = document.layers[id]
		} else if( document.all ) {
			object = document.all[id]
		} else if( document.getElementById ) {
			object = document.getElementById(id)
		}
		
		return object
	},

	init:function(anchorid, pos, glidetime){
		var anchorobj = dropdownmenu.getObject(anchorid)
		if (anchorobj != null)
		{
			var rel = anchorobj.getAttribute("rel")
			var subobj = null
			
			if (rel != null && rel != "undefined")
				subobj = dropdownmenu.getObject(rel)
			
			if (subobj != null)
			{
				/*if (subobjsource!=null && subobjsource!="")
					this.ajaxconnect(subobjsource, rel)
				*/
				var posSplit = pos.split("-")
				var posSplitStr = ""
					
				for (var i = 0; i < posSplit.length; i++)
				{
					posSplitStr = posSplitStr + posSplit[i]
					if (i + 1 != posSplit.length)
						 posSplitStr = posSplitStr + " "
				}
				
				subobj.setAttribute("dropposition", posSplitStr)
				subobj.setAttribute("glidetime", glidetime || 1000)
			
				if (subobj.style)
					subobj.style.top = -500 + "px"
				else
					subobj.setAttribute("style", "top: 0px;")
				
				subobj.onmouseout = function(e) { dropdownmenu.hide(this, subobj, e, anchorobj) }
				//subobj.onmouseover= function(e) { dropdownmenu.show(anchorobj, subobj, e) }
				anchorobj.onmouseover = function(e) { dropdownmenu.show(this, subobj, e) }
				var navegador = navigator.appName
				if (navigator.appVersion.match("MSIE 6.0") == null) {
					anchorobj.onmouseout = function(e) { dropdownmenu.hide(subobj, subobj, e, anchorobj) }
				}
			}
			
			if (this.disableanchorlink && subobj != null) 
				anchorobj.onclick = function() { return false }
		}
	}
}