//Drop Down/ Overlapping Content: http://www.dynamicdrive.com
//**Updated: Dec 19th, 07': Added ability to dynamically populate a Drop Down content using an external file (Ajax feature)
//**Updated: Feb 29th, 08':
				//1) Added ability to reveal drop down content via "click" of anchor link (instead of default "mouseover")
				//2) Added ability to disable drop down content from auto hiding when mouse rolls out of it
				//3) Added hidediv(id) public function to directly hide drop down div dynamically

//**Updated: Sept 11th, 08': Fixed bug whereby drop down content isn't revealed onClick of anchor in Safari/ Google Chrome

var dropdowncontent={
	// when user clicks on anchor link, should link itself be disabled (always true if "revealbehavior" above set to "click")
	disableanchorlink: true, 
	// Set hiding behavior within Drop Down DIV itself: [hide_div_onmouseover?, miliseconds_before_hiding]
 	hidedivmouseout: [true, 1500], 
	// HTML to show while ajax page is being feched, if applicable
	ajaxloadingmsg: '<div class="loading"><img src="/img/load.gif" alt="Loading" class="loadimg" /><span class="loadinfo"> Loading content. Please wait...</span></div>', 
	// Bust cache when fetching Ajax pages?
	ajaxbustcache: false, 
	// screen dimensions
	dimensions: {},
	
	getposOffset:function(what, offsettype){
		return (what.offsetParent)? what[offsettype]+this.getposOffset(what.offsetParent, offsettype) : what[offsettype]
	},

	getdimensions:function(anchorObj, dccObj){
		
		var stbody = (document.compatMode=="CSS1Compat")? document.documentElement : document.body
		
		this.dimensions={
			anchorW:anchorObj.offsetWidth, 
			anchorH:anchorObj.offsetHeight,
			dccW:dccObj.offsetWidth,
			dccH:dccObj.offsetHeight,
			docwidth:(window.innerWidth || stbody.clientWidth)-20,
			docheight:(window.innerHeight || stbody.clientHeight)-15,
			docscrollx:window.pageXOffset || stbody.scrollLeft,
			docscrolly:window.pageYOffset || stbody.scrollTop
		}
		
		var ainf  = ' anchor:' + this.dimensions.anchorW    + 'x' + this.dimensions.anchorH
		var dinf  = ' Dcc:'    + this.dimensions.dccW       + 'x' + this.dimensions.dccH
		var sinf  = ' Screen:' + this.dimensions.docwidth   + 'x' + this.dimensions.docheight
		var scinf = ' Screen:' + this.dimensions.docscrollx + 'x' + this.dimensions.docscrolly
		
		this.debuger('Dimension', ainf + dinf + sinf + scinf )

	},

	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) || (e && e.type=="click")){
			var e=window.event || e
			if (e.type=="click" && subobj.style.visibility=="visible"){
				subobj.style.visibility="hidden"
				return
			}
			// okreslamy id
			var elementID = anchorobj.id.replace('searchlink.','')
			//document.getElementById("info").innerHTML = anchorobj.getAttribute("alt") + ' (ID:' + elementID + ')'
			// calculate user added horizontal offset
			var horizontaloffset=(subobj.dropposition[0]=="left")? - (subobj.offsetWidth-anchorobj.offsetWidth) : 0 
			// calculate user added vertical offset
			var verticaloffset=(subobj.dropposition[1]=="top")? -subobj.offsetHeight : anchorobj.offsetHeight
			
			this.getdimensions(anchorobj,subobj)
			
			var sideV = this.dimensions.docwidth * 65 / 100
			var sideH = this.getposOffset(anchorobj, "offsetTop") + this.dimensions.dccH - this.dimensions.docscrolly
			// recalculate verical position 
			if(sideV > this.getposOffset(anchorobj, "offsetLeft"))
				subobj.style.left=this.getposOffset(anchorobj, "offsetLeft") + horizontaloffset + "px"
			else
				subobj.style.left=this.getposOffset(anchorobj, "offsetLeft") - this.dimensions.dccW + this.dimensions.anchorW + 2 + "px"
			// recalculate horizontal position
			if(sideH > this.dimensions.docheight)
				subobj.style.top=this.getposOffset(anchorobj, "offsetTop") - this.dimensions.dccH - this.dimensions.anchorH - 2 + "px"
			else	
				subobj.style.top=this.getposOffset(anchorobj, "offsetTop") + verticaloffset + "px"
			
			// hide drop down box initially via clipping
			subobj.style.clip=(subobj.dropposition[1]=="top")? "rect(auto auto auto 0)" : "rect(0 auto 0 0)" 
			subobj.style.visibility="visible"
			subobj.startTime=new Date().getTime()
			subobj.contentheight=parseInt(subobj.offsetHeight)
			//clear timer that hides drop down box?
			if (typeof window["hidetimer_"+subobj.id]!="undefined") 
				clearTimeout(window["hidetimer_"+subobj.id])
			this.slideengine(subobj, (subobj.dropposition[1]=="top")? "up" : "down")
			
			var subobjsource=anchorobj.getAttribute("rev")
			if (subobjsource!=null && subobjsource!="")
				this.ajaxconnect(subobjsource, anchorobj.getAttribute("rel"))
			}
	},

	curveincrement:function(percent){
		//return cos curve based value from a percentage input
		return (1-Math.cos(percent*Math.PI)) / 2 
	},

	slideengine:function(obj, direction){
		// get time animation has run
		var elapsed=new Date().getTime()-obj.startTime 
		// if time run is less than specified length
		if (elapsed<obj.glidetime){ 
			var distancepercent=(direction=="down")? this.curveincrement(elapsed/obj.glidetime) : 1-this.curveincrement(elapsed/obj.glidetime)
			var currentclip=(distancepercent*obj.contentheight)+"px"
			obj.style.clip=(direction=="down")? "rect(0 auto "+currentclip+" 0)" : "rect("+currentclip+" auto auto 0)"
			window["glidetimer_"+obj.id]=setTimeout(function(){dropdowncontent.slideengine(obj, direction)}, 10)
		}
		// if animation finished
		else{ 
			obj.style.clip="rect(0 auto auto 0)"
		}
	},
	
	// new func
	setopacity:function(obj, value){
		obj.style.opacity=value
		if (typeof obj.style.opacity!="string"){ //if it's not a string (ie: number instead), it means property not supported
			obj.style.MozOpacity=value
			if (obj.filters){
				obj.style.filter="progid:DXImageTransform.Microsoft.alpha(opacity="+ value*100 +")"
			}
		}
	},

	hide:function(activeobj, subobj, e){
		if (!dropdowncontent.isContained(activeobj, e)){
			window["hidetimer_"+subobj.id]=setTimeout(function(){
				subobj.style.visibility="hidden"
				subobj.style.left=subobj.style.top=0
				clearTimeout(window["glidetimer_"+subobj.id])
			}, dropdowncontent.hidedivmouseout[1])
		}
	},

	hidediv:function(subobjid){
		document.getElementById(subobjid).style.visibility="hidden"
	},

	ajaxconnect:function(pageurl, divId){
		var page_request = false
		var bustcacheparameter=""
		// if Mozilla, IE7, Safari etc
		if (window.XMLHttpRequest) 
			page_request = new XMLHttpRequest()
		// if IE6 or below
		else if (window.ActiveXObject){ 
			try { page_request = new ActiveXObject("Msxml2.XMLHTTP") } 
			catch (e){
				try{ page_request = new ActiveXObject("Microsoft.XMLHTTP") }
				catch (e){}
			}
		}
		else
			return false
		
		// Display "fetching page message"
		document.getElementById(divId).innerHTML = this.ajaxloadingmsg
		page_request.onreadystatechange=function(){dropdowncontent.loadpage(page_request, divId, pageurl)}
		// if bust caching of external page
		if (this.ajaxbustcache) 
			bustcacheparameter=(pageurl.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
		page_request.open('GET', pageurl+bustcacheparameter, true)
		page_request.send(null)
	},

	debuger:function( func, msg ){
		//document.getElementById("debuger").innerHTML = '<p>-><b>' +func + '</b>:' + msg + '</p>' + document.getElementById("debuger").innerHTML
	},
	
	loadpage:function(page_request, divId, pageurl){
		if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)){
			var dane = page_request.responseText
			document.getElementById(divId).innerHTML= dane //+ '<br>' + pageurl + '(' + divId + ')'
		}
	},
	
 
 init:function(anchorid, pos, glidetime, revealbehavior){
		var anchorobj=document.getElementById(anchorid)
		var subobj=document.getElementById(anchorobj.getAttribute("rel"))
		var subobjsource=anchorobj.getAttribute("rev")
		if (subobjsource!=null && subobjsource!="")
			this.ajaxconnect(subobjsource, anchorobj.getAttribute("rel"))
		subobj.dropposition=pos.split("-")
		subobj.glidetime=glidetime || 1000
		subobj.style.left=subobj.style.top=0
		if (typeof revealbehavior=="undefined" || revealbehavior=="mouseover"){
			anchorobj.onmouseover=function(e){dropdowncontent.show(this, subobj, e)}
			anchorobj.onmouseout=function(e){dropdowncontent.hide(subobj, subobj, e)}
			if (this.disableanchorlink) anchorobj.onclick=function(){return false}
		}
		else
			anchorobj.onclick=function(e){dropdowncontent.show(this, subobj, e); return false}
		// hide drop down DIV when mouse rolls out of it?		
		if (this.hidedivmouseout[0]==true) 
			subobj.onmouseout=function(e){dropdowncontent.hide(this, subobj, e)}
 },

 initall:function(classname, pos, glidetime, revealbehavior){
		
		// lyka wszystkie odnosniki w dokumencie
		var anchors = document.getElementsByTagName("a")
		// petla na elementy
		for (var i=0; i<anchors.length; i++) {
			// bierzemy pod uwage tylko te elementy, ktore posiadaja odpowiednia nazwe klasy	
			if(anchors[i].className == classname) dropdowncontent.init(anchors[i].id, pos, glidetime, revealbehavior)
		}
 }
}