/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++       author: Randy Weber           +++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++        title: popupDivs             +++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++      version: 1.0                   +++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++  last update: 05/09/08              +++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

// prototype inArray method to Array object class
Array.prototype.inArray = function( obj ) {
	
	// loop through this array
	for( var i = 0; i < this.length; i++ ) {
		
		// multi-dimensional array
		if( this[i].length > 1 ) {
			
			// call this function on the sub-array
			if( this[i].inArray( obj ) ) {
				
				// found our object
				return true;
				
			}
		
		// not a multi-dimensional array
		} else {
			
			// test each index
			if( this[i] == obj ) {
			
				// found our object
				return true;
		
			}
			
		}
			
	}
	
	// no object found
	return false;
};

// popupDivs class
var popupDivs = {
	
	// time delay before popup displays or is hidden
	delay : 500,
	
	// method that displays the popup menu
	show : function( x ) {
				
		// loop through the trigger array
		for( var i = 0; i < popupDivs.triggers.length; i++ ) {
			
			// is this target in either the trigger or popups array
			if( popupDivs.triggers[i] == x || popupDivs.popups[i][0] == x ) {
				
				// set popup display value to true
				popupDivs.popups[i][1] = true;
				
				// give it some time before displaying the popup
				setTimeout( function() {
					
					// loop through triggers array
					for( var i = 0; i < popupDivs.triggers.length; i++ ) {
						
						// is the target in the trigger array
						if( popupDivs.triggers[i] == x && popupDivs.popups[i][1] === true ) {
							
							// calculate coordinates for the popups
							popupDivs.calculateCoords( popupDivs.popups[i][0], popupDivs.triggers[i] );
							
							// display the popup
							popupDivs.popups[i][0].style.left = popupDivs.popups[i][3];
							
							//break;
							
						}
					
					}
				
				}, popupDivs.delay );
				
			}
			
		}
		
	},
	
	// method that hides the popup menu
	hide : function( x ) {
		
		// loop through the triggers array
		for( var i = 0; i < popupDivs.triggers.length; i++ ) {
			
			// is this target in either the triggers or popups array
			if( popupDivs.triggers[i] == x || popupDivs.popups[i][0] == x ) {
				
				// set popup display value to false
				popupDivs.popups[i][1] = false;
				
				// give it some time before hiding the popup
				setTimeout( function(){
					
					// loop through the popups array
					for( var i = 0; i < popupDivs.popups.length; i++ ) {
						
						// should this popup be hidden
						if( popupDivs.popups[i][1] === false ) {
							
							// hide the popup
							popupDivs.popups[i][0].style.left = "-100em";
														
						}
						
					 }
					
				 }, popupDivs.delay );
				
			}
			
		}
		
	},
	
	// method that calculates HTML element widths
	getWidth : function( obj ) {
				
		var xWidth = 0;
		
		// get the width of this HTML element
		if( obj.clientWidth > obj.offsetWidth ) {
			
			// client width is greatest value
			xWidth = obj.clientWidth;
			
		} else if( obj.scrollWidth > obj.clientWidth ) {
			
			// scroll width is greatest value
			xWidth = obj.scrollWidth;
			
		} else {
			
			// offset width is greatest value
			xWidth = obj.offsetWidth;
			
		}
						
		return xWidth;
		
	},
	
	// method that calculates popup coordinates
	calculateCoords : function( popup, trigger ) {
		
		var popups = popupDivs.popups;
		var triggers = popupDivs.triggers;
		var newTrigger = new Array();
		
		// loop through our triggers array
		for( var i = 0; i < triggers.length; i++ ) {
			
			// only calculate for our target popup and trigger
			if( trigger == triggers[i] ) {
			
				var positionX = 0;
				var positionY = 0;
				
				// new reference for this trigger
				newTrigger = triggers[i];
				
				// loop outwards to the DOM root
				while( newTrigger ) {
					
					// sum up target element and all parent element's left offset distance
					positionX += newTrigger.offsetLeft;
					
					// sum up target element and all parent element's top offset distance
					positionY += newTrigger.offsetTop;
					
					// traverse outwards to parent element
					newTrigger = newTrigger.offsetParent;
					
				}
				
				// is this an odd or even numbered popup
				if( i % 2 ) {
					
					// set the left absolute positioning offset for this popup
					// this should display to the left of the trigger								
					popupDivs.popups[i][3] = ( positionX - popupDivs.getWidth( popupDivs.popups[i][0] ) ) + "px";
					
				} else {
					
					// set the left absolute positioning offset for this popup
					// this should display to the right of the trigger								
					popupDivs.popups[i][3] = ( positionX + popupDivs.getWidth( popupDivs.triggers[i] ) ) + "px";
				
				}
							
				// set the top absolute positioning offset for this popup
				popups[i][0].style.top = ( positionY ) + "px";
				
				break;
				
			}
			
		}
				
	},
	
	// method that initializes the popups
	config : function( name ) {
				
		// initialize class' public popups array
		popupDivs.popups = new Array();
		
		// initialize class' public triggers array
		popupDivs.triggers = new Array();

				
		// get a collection of every HTML element in DOM
		var everything = document.getElementsByTagName("*");
		
		// loop through all HTML elements
		for( var i = 0; i < everything.length; i++ ) {
			
			// is this one of our popups or triggers?
			if( everything[i].onmouseover && String( everything[i].onmouseover ).indexOf("popupDivs") != -1 ) {
				
				// is this a popup?
				if( everything[i].className == name ) {
					
					// this is a popup, add it and it's display value to the class' public popups array
					popupDivs.popups[ popupDivs.popups.length ] = [ everything[i], false ];
					
				} else {
					
					// this is a trigger, add it to the class' public triggers array
					popupDivs.triggers[ popupDivs.triggers.length ] = everything[i];
					
				}
				
			}
			
		}
						
	}
};