var constants = {
	DROP_DOWN_ON_DELAY: 250,
	DROP_DOWN_OFF_DELAY: 150,
	DROP_DOWN_CLEANUP_INTERVAL: .08
}

/**
  * Tests object for safeness to itterate
  * <p>
  * Prototype.js adds utility functions to all objects (even enumerable objects). This will allow
  * functions the itterate over objects (incl arrays) to make sure that the itteration does
  * not process prototype (or anything else's) added functions
  * </p>
  * @param obj The object to be tested
  * @return boolean true or false whether it is itterable
  */
function isPrototypeSafe(obj) {
	if (typeof obj != 'function') {
		return true;
	} else {
		return false;
	}
}

// global var to track mouse position
var mouseP = {x:0,y:0}

// watching mouse movement (IE & standard)
document.onmousemove = function watchMouse(e) {
	//IE
	if (document.all) {
    	//make sure document body has loaded
    	if (document.body) {
    		mouseP.x = event.clientX + document.body.scrollLeft
   	 		mouseP.y = event.clientY + document.body.scrollTop
   	 	}
  	} else {  // grab the x-y pos.s if browser is NS
    	mouseP.x = e.pageX
    	mouseP.y = e.pageY
  	}
}

// watch for onload of the window objects, add functions to execution stack
Event.observe(window, 'load', enableDropDowns);
Event.observe(window, 'load', enableRollovers);

//global var holds the top nav drop-downs key: ul id, value: js object
var dropDownHash = new Hash();

/**
  * Itterates over the DOM top-nav drop-downs and creates corresponding js objects to control and
  * position. Adds js dropdowns to global var dropDownHash.
  * 
  * @see dropDownHash
  * @see dropDown
  * @see enableRollovers
  */
function enableDropDowns() {
	var domDropDowns = new Array();
	domDropDowns = $$('ul.navLvl2');
	
	for (x in domDropDowns) {
		if (isPrototypeSafe(domDropDowns[x])) {
			var tempDropDown = new dropDown(domDropDowns[x]);
			dropDownHash.set(domDropDowns[x].identify(), tempDropDown);
		}
	}
}

/**
  * Itterates over the DOM top-nav images and creates corresponding js objects to control mouse
  * over, mouse out, pre-loading, and showing of drop downs.
  * 
  * @see enableDropDowns
  */
function enableRollovers() {
	var imgRollovers = new Array();
	imgRollovers = $$('img.navLvl1');
	
	for (x in imgRollovers) {
		if (isPrototypeSafe(imgRollovers[x])) {
			
			var roImage = imgRollovers[x];
			if (!roImage.on) {
				function _preload(sSrc) {
					var newImg = new Image;
					newImg.src = sSrc;
					return newImg;
				}
				roImage.off = _preload(roImage.src.valueOf());
				roImage.on = _preload(roImage.src.replace(/_off/, "_on"));
			}
			roImage.onmouseover = function() {
				this.src = this.on.src;
				var childDropDownId = this.identify().replace("Img", "Lvl2");
				dropDownHash.get(childDropDownId).show();
			}
			roImage.onmouseout = function() {
				this.src = this.off.src;
			}
		}
	}
}


/**
  * The top-nav drop-down js object. This controls the corresponding DOM drop-down 
  * object
  * <p>Internal methods for show/hide/position (based on parent top-nav image)
  * 
  *
  *
  * @param domObject a single drop-down UL element
  *
  *
  */
function dropDown(domObject) {
	
	this.id = domObject.identify();
	this.catRef = this.id.replace("Lvl2", "");
	this.parentImg = $(this.catRef+"Img");
	this.name = this.parentImg.alt;
	
	function position(e) {
		// separate positioning of sale drop-down from rest of the drop-downs
		if (this.parentImg.alt == "Sale") {
			var saleLeft = this.parentImg.positionedOffset().left;
			e.style.left = (saleLeft - this.parentImg.getWidth()) + "px";
		} else {
			var parentLeft = this.parentImg.positionedOffset().left;
			e.style.left = parentLeft;
		}
		var parentTop = this.parentImg.positionedOffset().top;
		e.style.top = (parentTop + this.parentImg.getHeight()) + "px";
	}
	this.position = position;
	
	function hide() {
		//console.info("Hide Drop Down " + this.name);
		domObject.style.display = "none";
	}
	this.hide = hide;
	
	function show() {
		//console.info("Show Drop Down " + this.name);
		this.position(domObject);
		
		new PeriodicalExecuter(function(pe) {
  			var hideDropDown = new dropDown(domObject);
  			
  			if (hideDropDown.isMouseOutside()) {
				hideDropDown.hide();
				pe.stop();
			}
		}, constants.DROP_DOWN_CLEANUP_INTERVAL);
		domObject.style.display = 'block';
	}
	this.show = show;
	
	function isMouseOutside() {
		if (
			(mouseP.x < dropDownCoords.left || mouseP.x > dropDownCoords.right || mouseP.y < dropDownCoords.top || mouseP.y > dropDownCoords.bottom) &&
			(mouseP.x < imgCoords.left || mouseP.x > imgCoords.right || mouseP.y < imgCoords.top || mouseP.y > imgCoords.bottom)
			) {
			//console.info("isMouseOutside return true");
			return true;
		} else {
			//console.info("isMouseOutside return false");
			return false;
		}
	}
	this.isMouseOutside = isMouseOutside;
	
	var imgCoords = {
		left: this.parentImg.positionedOffset().left,
		right: this.parentImg.positionedOffset().left + this.parentImg.getWidth(),
		top: this.parentImg.positionedOffset().top,
		bottom: this.parentImg.positionedOffset().top + this.parentImg.getHeight()
	}
	
	var dropDownCoords = {
		left: this.parentImg.positionedOffset().left,
		right: this.parentImg.positionedOffset().left + domObject.getWidth(),
		top: this.parentImg.positionedOffset().top + this.parentImg.getHeight(),
		bottom: this.parentImg.positionedOffset().top + this.parentImg.getHeight() + domObject.getHeight()
	}
	
	if (this.name == "Sale") {
		dropDownCoords.left = dropDownCoords.left - this.parentImg.getWidth();
		dropDownCoords.right = dropDownCoords.right - this.parentImg.getWidth();
	}
}

function include_dom(script_type, script_language, script_filename) {
    var html_doc = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
    js.setAttribute('language', script_language);
    js.setAttribute('type', script_type);
    js.setAttribute('src', script_filename);
    html_doc.appendChild(js);
    return false;
}

/*include_dom("text/javascript", "javascript", "/estore/js/ua.js");
include_dom("text/javascript", "javascript", "/estore/js/windowPop.js");*/
