1//Drop Down/ Overlapping Content: http://www.dynamicdrive.com
2//**Updated: Dec 19th, 07': Added ability to dynamically populate a Drop Down content using an external file (Ajax feature)
3//**Updated: Feb 29th, 08':
4				//1) Added ability to reveal drop down content via "click" of anchor link (instead of default "mouseover")
5				//2) Added ability to disable drop down content from auto hiding when mouse rolls out of it
6				//3) Added hidediv(id) public function to directly hide drop down div dynamically
7
8//**Updated: Sept 11th, 08': Fixed bug whereby drop down content isn't revealed onClick of anchor in Safari/ Google Chrome
9//**Updated: April 9th, 10': Minor change
10
11var dropdowncontent={
12	disableanchorlink: true, //when user clicks on anchor link, should link itself be disabled (always true if "revealbehavior" above set to "click")
13 hidedivmouseout: [true, 200], //Set hiding behavior within Drop Down DIV itself: [hide_div_onmouseover?, miliseconds_before_hiding]
14	ajaxloadingmsg: "Loading content. Please wait...", //HTML to show while ajax page is being feched, if applicable
15	ajaxbustcache: true, //Bust cache when fetching Ajax pages?
16
17	getposOffset:function(what, offsettype){
18		return (what.offsetParent)? what[offsettype]+this.getposOffset(what.offsetParent, offsettype) : what[offsettype]
19	},
20
21	isContained:function(m, e){
22		var e=window.event || e
23		var c=e.relatedTarget || ((e.type=="mouseover")? e.fromElement : e.toElement)
24		while (c && c!=m)try {c=c.parentNode} catch(e){c=m}
25		if (c==m)
26			return true
27		else
28			return false
29	},
30
31	show:function(anchorobj, subobj, e){
32		if (!this.isContained(anchorobj, e) || (e && e.type=="click")){
33			var e=window.event || e
34			if (e.type=="click" && subobj.style.visibility=="visible"){
35				subobj.style.visibility="hidden"
36				return
37			}
38			var horizontaloffset=(subobj.dropposition[0]=="left")? -(subobj.offsetWidth-anchorobj.offsetWidth) : 0 //calculate user added horizontal offset
39			var verticaloffset=(subobj.dropposition[1]=="top")? -subobj.offsetHeight : anchorobj.offsetHeight //calculate user added vertical offset
40			subobj.style.left=this.getposOffset(anchorobj, "offsetLeft") + horizontaloffset + "px"
41			subobj.style.top=this.getposOffset(anchorobj, "offsetTop")+verticaloffset+"px"
42			subobj.style.clip=(subobj.dropposition[1]=="top")? "rect(auto auto auto 0)" : "rect(0 auto 0 0)" //hide drop down box initially via clipping
43			subobj.style.visibility="visible"
44			subobj.startTime=new Date().getTime()
45			subobj.contentheight=parseInt(subobj.offsetHeight)
46			if (typeof window["hidetimer_"+subobj.id]!="undefined") //clear timer that hides drop down box?
47				clearTimeout(window["hidetimer_"+subobj.id])
48			this.slideengine(subobj, (subobj.dropposition[1]=="top")? "up" : "down")
49		}
50	},
51
52	curveincrement:function(percent){
53		return (1-Math.cos(percent*Math.PI)) / 2 //return cos curve based value from a percentage input
54	},
55
56	slideengine:function(obj, direction){
57		var elapsed=new Date().getTime()-obj.startTime //get time animation has run
58		if (elapsed<obj.glidetime){ //if time run is less than specified length
59			var distancepercent=(direction=="down")? this.curveincrement(elapsed/obj.glidetime) : 1-this.curveincrement(elapsed/obj.glidetime)
60			var currentclip=(distancepercent*obj.contentheight)+"px"
61			obj.style.clip=(direction=="down")? "rect(0 auto "+currentclip+" 0)" : "rect("+currentclip+" auto auto 0)"
62			window["glidetimer_"+obj.id]=setTimeout(function(){dropdowncontent.slideengine(obj, direction)}, 10)
63		}
64		else{ //if animation finished
65			obj.style.clip="rect(0 auto auto 0)"
66		}
67	},
68
69	hide:function(activeobj, subobj, e){
70		if (!dropdowncontent.isContained(activeobj, e)){
71			window["hidetimer_"+subobj.id]=setTimeout(function(){
72				subobj.style.visibility="hidden"
73				subobj.style.left=subobj.style.top=0
74				clearTimeout(window["glidetimer_"+subobj.id])
75			}, dropdowncontent.hidedivmouseout[1])
76		}
77	},
78
79	hidediv:function(subobjid){
80		document.getElementById(subobjid).style.visibility="hidden"
81	},
82
83	ajaxconnect:function(pageurl, divId){
84		var page_request = false
85		var bustcacheparameter=""
86		if (window.XMLHttpRequest) // if Mozilla, IE7, Safari etc
87			page_request = new XMLHttpRequest()
88		else if (window.ActiveXObject){ // if IE6 or below
89			try {
90			page_request = new ActiveXObject("Msxml2.XMLHTTP")
91			}
92			catch (e){
93				try{
94				page_request = new ActiveXObject("Microsoft.XMLHTTP")
95				}
96				catch (e){}
97			}
98		}
99		else
100			return false
101		document.getElementById(divId).innerHTML=this.ajaxloadingmsg //Display "fetching page message"
102		page_request.onreadystatechange=function(){dropdowncontent.loadpage(page_request, divId)}
103		if (this.ajaxbustcache) //if bust caching of external page
104			bustcacheparameter=(pageurl.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
105		page_request.open('GET', pageurl+bustcacheparameter, true)
106		page_request.send(null)
107	},
108
109	loadpage:function(page_request, divId){
110		if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)){
111			document.getElementById(divId).innerHTML=page_request.responseText
112		}
113	},
114
115 init:function(anchorid, pos, glidetime, revealbehavior){
116		var anchorobj=document.getElementById(anchorid)
117		if (anchorobj)
118			var subobj=document.getElementById(anchorobj.getAttribute("rel"))
119		if (!anchorobj || !subobj)
120			return
121		var subobjsource=anchorobj.getAttribute("rev")
122		if (subobjsource!=null && subobjsource!="")
123			this.ajaxconnect(subobjsource, anchorobj.getAttribute("rel"))
124		subobj.dropposition=pos.split("-")
125		subobj.glidetime=glidetime || 1000
126		subobj.style.left=subobj.style.top=0
127		if (typeof revealbehavior=="undefined" || revealbehavior=="mouseover"){
128			anchorobj.onmouseover=function(e){dropdowncontent.show(this, subobj, e)}
129			anchorobj.onmouseout=function(e){dropdowncontent.hide(subobj, subobj, e)}
130			if (this.disableanchorlink) anchorobj.onclick=function(){return false}
131		}
132		else
133			anchorobj.onclick=function(e){dropdowncontent.show(this, subobj, e); return false}
134		if (this.hidedivmouseout[0]==true) //hide drop down DIV when mouse rolls out of it?
135			subobj.onmouseout=function(e){dropdowncontent.hide(this, subobj, e)}
136	}
137}