 /********************************************************
 *	
 *	Developed by Michael P. Schmidt for Lockheed Martin 2007.
 *
 *	DropDown.js
 * 		will find all elements with a class name of dropDownActivator
 *  	will then find those elements attribute "dropDownName" which corresponds
 *		with the ID for  the drop down to hide.
 *		
 *	Requirements:
 *		prototype.js 
 *			 Version: '1.6.0_rc1',
 *
 *	Extra Features:
 *		If navigate off of all drop downs and is not hovering over activator
 *		then all drop downs will close after 1000ms (	hideTimeout ).
 *		If the drop down goes outside of the page horizontal border then the system
 *		will try to locate the drop down as close to the border as possible currently 
 *		set to 40 (leftPadding) to account for the firefox scroll bar.
 *
 *		Drop Down width needs to be defined in CSS and in the variable widthDropDown 
 *		currently set to 200.
 *
 *		Methods to be called:
 *			DropDownItems.show("ddName"); 	will show the drop down.
 *			DropDownItems.hide("ddName"); 	will hide the drop down stated.
 *			DropDownItems.hideall(); 		will hide all drop downs	
 *********************************************************/
 
 var DropDownItems = 
 {
 timer: undefined,
 Version : '0.0.1',
 dropDowns: new Array(),
 widthDropDown: 130,
 leftPadding : 40,
 hideTimeout: 250,
 initialize: function() {
	var ddName = "";
	var elems = $$(".dropDownActivator");
	for (var i = 0; i < elems.length; i++)
	{
		var elm = elems[i];
		ddName = elm.getAttribute("dropDownName")
		var parent = elm.getAttribute("parent");
		if ( $(ddName ) !=  undefined )
		{
			this.hide(ddName);
			Event.observe(elm, 'focus', this.show.bindAsEventListener(this));
			//Event.observe(elm, 'blur', this.prepareHide.bindAsEventListener(this));
			Event.observe(elm, 'mouseover', this.show.bindAsEventListener(this));
			Event.observe(elm, 'mouseout', this.prepareHide.bindAsEventListener(this));
			
			
			
			Event.observe(ddName, 'mouseover', this.clearTimeout.bindAsEventListener(this));
			Event.observe(ddName, 'mouseout', this.prepareHide.bindAsEventListener(this));
			Event.observe(ddName, 'focus', this.show.bindAsEventListener(this));
			//Event.observe(ddName, 'blur', this.prepareHide.bindAsEventListener(this));
			
			this.dropDowns.push({ name: ddName, parent:parent});
		} 
		
			Event.observe(elm, 'mouseover', this.hoverOn.bindAsEventListener(this));
			Event.observe(elm, 'mouseout', this.hoverOut.bindAsEventListener(this));
														
	} 
 },
 
 selectedItems : {},
 
 hoverOn: function(obj) {
 	var tmp = this.getObj(obj);
	
	if (tmp.ddName != "" && tmp.ddName != undefined) {
		var parent = tmp.parent;
		
		if (parent != 0) {
			if ( this.selectedItems[parent] )
				Element.removeClassName(this.selectedItems[parent], "dropDownHover");
			this.selectedItems[parent] = tmp.obj;
			Element.addClassName(tmp.obj, "dropDownHover");
			for ( var i = 0; i < this.dropDowns.length; i++ )
			{
				if ( this.dropDowns[i].name != tmp.ddName )
				{
					if ( this.dropDowns[i].parent == tmp.parent || tmp.parent == 0)
						this.hide(this.dropDowns[i].name);
				}
			}
	
		}
	}
	
	
 },
 
 hoverOut: function(obj)
 {
 	var tmp = this.getObj(obj);
	
 },
 clearTimeout : function(obj)
 {
 	if ( this.timer != undefined)
 	{
 		window.clearTimeout(this.timer);
 		this.timer = undefined;
 	}
 },
 prepareHide: function(obj)
 {
	 if ( this.timer != undefined)
	 {
	 	window.clearTimeout(this.timer);
	 	this.timer = undefined;
	 }
	 this.timer = window.setTimeout(this.hideAll.bindAsEventListener(this), this.hideTimeout);
 },
 hideAll : function()
 {
 	for ( var i = 0; i < this.dropDowns.length; i++)
 	{
		
 		Element.hide(this.dropDowns[i].name);
 	}
 },
 hide: function(ddName){
 
 		Element.hide(ddName);
 }
 ,
 
 getObj: function(obj)
 {
 	var ddName = "";
	if ( !obj )
	return {ddName:undefined, obj:undefined, parent:undefined};
 		if ( obj.element)
		{
			obj = obj.element() ;
			ddName = obj.getAttribute("dropDownName");
		} else {
			if ( obj.getAttribute )
				ddName = obj.getAttribute("dropDownName");
			else 
				ddName = false;
				
			if (! ddName )
			{
				ddName = $(obj).id;
				var elems = $$(".dropDownActivator");
				for (var i = 0; i < elems.length; i++)
				{
					if ( elems[i].getAttribute("dropDownName") == ddName )
					{
						obj = elems[i];
						break;
					}
				}
			}
		}
		var parent = obj.getAttribute("parent");
	return { ddName: ddName, obj: obj, parent: parent};
	
 },
 getParent: function(ddName) {
 		var parent = 0;
	for ( var i = 0; i < this.dropDowns.length; i++ )
	{
		if ( this.dropDowns[i].name == ddName )
		{
			parent = this.dropDowns[i].parent;
		}
	}
	return parent;
	
 },
 show: function(obj, ddName)
 { 
 	if ( this.timer != undefined)
 	{
 		window.clearTimeout(this.timer);
 	}
 	
 	if ( ddName == undefined)
	{
		var tmp = this.getObj(obj);
		ddName = tmp.ddName;
		obj = tmp.obj;
	} 
	if (! $(ddName) )
		return;
	var parent = this.getParent(ddName);
	
	
	for ( var i = 0; i < this.dropDowns.length; i++ )
	{
		if ( this.dropDowns[i].name != ddName )
		{
			if ( this.dropDowns[i].parent == parent || parent == 0)
				this.hide(this.dropDowns[i].name);
		}
	}
	Element.show(ddName);
	
	if ( parent != 0 && ! Element.hasClassName(ddName, "secondaryDropDown"))
	{
		Element.addClassName(ddName, "secondaryDropDown");
	}
	offset = Position.page(obj);
	
	if ( Prototype.Browser.IE )
		offset.left -= 40;
	if ( parent != 0)
	{
		if ( Prototype.Browser.IE )
		offset.left += this.widthDropDown + 20;
		else
		offset.left += this.widthDropDown + 25;
		
	}
	Element.absolutize(ddName);
	
	
	var pgWidth= document.viewport.getWidth();
	if ( pgWidth == 0)
	{		
		pgWidth = document.body['clientWidth'];
	
		pgWidth = pgWidth;

	} 
	pgWidth -= this.leftPadding;
	var scoffset = document.viewport.getScrollOffsets();
	
	if ( (offset.left +  this.widthDropDown ) > pgWidth )
	{
		offset.left = pgWidth - this.widthDropDown + scoffset.left;
	} else {
		
		offset.left += scoffset.left;
	}
	
	
		if (parent == 0)
		{
			if (Prototype.Browser.IE) 
				$(ddName).style.top = (offset.top + 20 + scoffset.top) + "px";
			else
				$(ddName).style.top = (offset.top + 20 + scoffset.top -10) + "px";
		}
		else 
			if (Prototype.Browser.IE) 
				$(ddName).style.top = (offset.top + scoffset.top) + "px";
			else 
				$(ddName).style.top = (offset.top + scoffset.top- 10 ) + "px";
		$(ddName).style.left = offset.left + "px";
		$(ddName).style.height = '';
		$(ddName).style.width = '';
 }
};

Event.observe(window, "load", DropDownItems.initialize.bindAsEventListener(DropDownItems));