/* DropMenu
 * Uses Prototype and Scriptaculous
 * Serisys Solutions
 * */
 
// when entrySelector(css selector) is moused over, blinds down (dropdown) immediate 
// children childMenuselectors(css selector).  On mouseout, blinds up.
var dropMenu=function(entrySelector,childMenuSelector){
	// lists are entries that could possibly have lists.
	var lists = $$(entrySelector);
	var curOn = new Array();
	
	lists.each( function(listEntry) {
		var children = listEntry.childElements();
		
		var numChildren=0;
		//Find children
		children.each( function(child) {
			if(child.match(childMenuSelector)) {
				numChildren+=1;
			}
		});
		//Only create mouseovers if has children.
		if(numChildren>0) {
			curOn[listEntry.identify()]=new Array();
			// for identifying what stage the dropdown is, so doesn't try to dropdown if already dropping down
			curOn[listEntry.identify()]['on']=0;
			// IMPORTANT: timer, that delays processing of 'mouseout' 'blindup' function
			// Otherwise, mouseout blindup will continually interfere with blinddown, 
			// as mouseout is fired all the time when moving over subelements, followed immediately
			// by mouseover. So delayed mouseout ignores these instances unless a 'real' mouseout
			// occurs.
			curOn[listEntry.identify()]['timer']=0;
		
		
			listEntry.observe('mouseover',
				function() {
					clearTimeout(curOn[listEntry.identify()]['timer']);
					children.each( function(child) {
						if(child.match(childMenuSelector)) {
								if(curOn[listEntry.identify()]['on']<1) {
									curOn[listEntry.identify()]['on']=1;
										// add to queue for child so that 
										// different children and multiple effects
										// don't collide and screw up incompleted
										// effects
									var curEffect = new Effect.BlindDown(child,
									{
										duration:0.2,
										queue:{position:'end', scope:child.identify()+'scope'}
									});
								}
							
						}
					});
				}
			);
			listEntry.observe('mouseout',
				function() {
				
					//delay blindUp, so that don't BlindUp when moving over sub-elements
					// that fire a "mouseout-mouseover" combo. This gives time for 'mouseover' to cancel
					// the blindUp.
					curOn[listEntry.identify()]['timer']= window.setTimeout(function() {
						children.each( function(child) {
							if(child.match(childMenuSelector)) {
									if(curOn[listEntry.identify()]['on']>0) {
										curOn[listEntry.identify()]['on']=0;
										var curEffect = new Effect.BlindUp(child,
										{	
											duration:0.2,
											queue:{position:'end', scope:child.identify()+'scope'}
										});
									}
							}
						});
					}, 100);
				}
			);
			//Start Faded.
			children.each( function(child) {
				if(child.match(childMenuSelector)) {
					child.hide();
				}
			});
		}	
	});
};




