/*
  nav-flyout.js
  Author: Chad Lindstrom <webmaster[AT]chadlindstrom[DOT]ca>
*/

/* BEGIN: Utils Section */
var utils = {
	debug : 0,
	debugObj : null,
	getObj : function (items,c) {
	   var _s = items.length;
	   if( _s > 0 ){
		  for( var i = 0; i < _s; i++ ){
			 if( items[i].className == c ){
				return items[i];
			 }
		  }
	   }
	   this.trace("didn't find");
	   return null;
	},
	trace : function(m){
		if( 1 == this.debug ) {
			if( null == this.debugObj ) {
				this.debugObj = document.getElementById("trace");
				this.debugObj.style.visibility = "visible";
			}
			this.debugObj.value += m + "\n";
		}
	}
}
/* END: Utils Section */

/* BEGIN: Main Nav */

var flyNav = {
	visibleFlyout : null,	/* current flyout element */
	activeItem : null, /* current fly nav item (item that triggers flyout) */
	timer : null,	/* timer used to auto-hide */
	itemActivatedClass : "flyoutActive", /* the class used to assign to the activeItem */
	itemDefaultClass : "", /* default class for item, used to reset */
	flyoutClass : "flyout", /* the class used for the element that is toggled on/off */
	
	/* shows the flyout associated with t */
	show : function(t) { 

		utils.trace("showing");
		if( this.timer != null ){
			clearTimeout( this.timer );
		}
		
		this.itemDefaultClass = t.className;
		var tmp_f = this._getFlyout(t);
		
		if( this.activeItem != null || this.visibleFlyout != null ){
			if( this.activeItem === t || this.visibleFlyout === tmp_f ){
				return; /* we're already showing what we need */
			} else {
				flyNav._hideFlyout();
			}
		}
		
		this.activeItem = t;
		this.visibleFlyout = tmp_f;
		
		if( this.visibleFlyout != null ){
			this.visibleFlyout.style.display = "block";
		} else {
			throw new Error("'this.visibleFlyout' is null when it should not be.");
		}
		
		if( this.activeItem != null ){
			this.activeItem.className = this.itemDefaultClass + " " + this.itemActivatedClass;
		}
		this.hold();
	},
	
	
	/* helper function to get the child flyout menu */
	_getFlyout : function (t){
		
		utils.trace("get helper");
		return utils.getObj(t.parentNode.childNodes,this.flyoutClass);
	},
	
	/* this function holds the current flyout active */
	hold : function (){
		
		utils.trace("holding");
		if( this.timer != null ){
			clearTimeout( this.timer );
		}
	},
	
	/* this function hides the current flyout */
	hide : function(){
		
		utils.trace("hiding");
		if( this.visibleFlyout != null ){
			this.timer = setTimeout( "flyNav._hideFlyout();", 500 );
		}
	},
	
	/* helper function to hide the current flyout, static function */
	_hideFlyout : function (){
		
		utils.trace("hide helper");
		if( flyNav.activeItem != null ){
			this.activeItem.className = this.itemDefaultClass
			flyNav.activeItem = null;
		}
		if( flyNav.visibleFlyout != null ){
			flyNav.visibleFlyout.style.display = "none";
			flyNav.visibleFlyout = null;
		}
	}	
}
/* END: Main Nav */
