
// Custom Hover & Off Functions
function sgMenuHover(el) {
	el.className = el.className + 'sgMenuActive';
}
function sgMenuOff(el) {
	el.className = el.className.replace('sgMenuActive','');;
}


var sgMenu = {

	//
	//  Setup the Variables
	//
	menuTimer : 0,
	menuActive : false,
	
	// 
	// Initialize
	init: function(obj, options) {
		
		if (!document.getElementById(obj)) {
			throw(obj + ' does not exist!');
			return false;
		}
		
		this.obj = obj;
		
		this.options = this.extend({
			orientation : 'horizontal',					   
			offsetTop : 0,
			offsetLeft : 0,
			offsetTopSub : 0,
			offsetLeftSub : 0,
			delay : 1000,
			urlBase : '',
			overFunction : function() {},
			offFunction : function() {}						 
		}, options || {});
	
	},
	
	//
	// Create the current hover / fly out state.
	show: function(el,depth,pinto) {
		
		var key = el.getAttribute('menuItem');	
		

		// Create the ul element if the top array is valid
		if (!this.isDefined('sgMenuSub' + key)) {
			sgMenu.remove();
			return;
		}
			
		if (typeof depth == 'undefined')
			depth = 0;
		
		this.remove('sgMenu-' + depth);
		
		if (depth == 0) {
			sgMenu.currentElement = el;
			
			sgMenu.menuActive = true;
			sgMenu.options.overFunction(sgMenu.currentElement);
			el.onmouseout = function() { sgMenu.startTimeout(); }	
			el.onmouseover = function() {
				sgMenu.show(this);
				sgMenu.killTimeout();
			}
		}
			
		var appendTo = (typeof pinto == 'undefined') ? document.body : pinto;
			
		var args = eval('sgMenuSub' + key);	
		
		// Create the list menu
		var sgMenuUl = document.createElement('ul');
		sgMenuUl.className = 'sgMenu';
		sgMenuUl.id = 'sgMenu-' + depth;
		sgMenuUl.style.visibility = 'hidden';
		sgMenuUl.style.left = '-1000px';
		sgMenuUl.style.top = '-1000px';
		sgMenuUl.onmouseover = function() { sgMenu.killTimeout(); }
		sgMenuUl.onmouseout = function() { sgMenu.startTimeout(); }
		appendTo.appendChild(sgMenuUl);
		
		// Assign the newly created list element
		sgMenu.currentTier = document.getElementById('sgMenu-' + depth);
	
		// Set the position of the list
		this.setPosition(el,depth);
		
		for (var i=0; i<args.length; i++) {

			var subArgs = ('sgMenuSub' + key + '_' + i);
			var hasSub = false;
			if (this.isDefined(subArgs)) {
				hasSub = (typeof eval(subArgs) == 'object' && subArgs.length > 0) ? true : false;
			}
			
			var li = document.createElement('li');
			sgMenuUl.appendChild(li);
			
			var a = document.createElement('a');
			a.href = sgMenu.options.urlBase + args[i][1];
			a.innerHTML = args[i][0];
			a.setAttribute('menuItem',key + '_' + i);
			a.onmouseover = function() { 
				sgMenu.remove('sgMenu-' + Math.round(depth+1)); 
				sgMenu.setActive(this,depth);
			}
			if (hasSub) { 
				a.className = 'sgMenuSub';	
				a.onmouseover = function() { 
					sgMenu.show(this,depth+1,this.parentNode); 
					sgMenu.setActive(this,depth);
				}
			}
			li.appendChild(a);
			
		}
		
		
		//
		// Positioning is finished, make the element visible
		sgMenu.currentTier.style.visibility = 'visible';
		
		
	},
	
	startTimeout: function() {
		if (sgMenu.menuTimer == 0) {
			sgMenu.menuTimer = setTimeout('sgMenu.remove()',sgMenu.options.delay);
		}
	},
	
	killTimeout: function() {
		if (sgMenu.menuTimer > 0) {
			clearTimeout(sgMenu.menuTimer);
			sgMenu.menuTimer = 0;
		}
	},
	
	setActive: function(el,depth) {
		if (!document.getElementById('sgMenu-' + depth)) return;
		var parent = document.getElementById('sgMenu-' + depth);
		var hrefs = parent.getElementsByTagName('a');
		for (var ii=0; ii<hrefs.length; ii++) {
			hrefs[ii].className = hrefs[ii].className.replace(/sgMenuActiveItem/ig,'');
		}
		el.className = el.className + ' sgMenuActiveItem';
	},
	
	remove: function(id) {
		var elId = (typeof id == 'undefined') ? 'sgMenu-0' : id;
		
		if (!document.getElementById(elId)) return;
		var el = document.getElementById(elId);
		el.parentNode.removeChild(el);
		if (!document.getElementById('sgMenu-0')) {
			sgMenu.menuActive = false;
			sgMenu.options.offFunction(sgMenu.currentElement);		
		}
	},
	
	isDefined: function(variable) {
		try { 
        	eval(variable);
		} catch (e) { 
			return false;
		}
		return true;
	},
	
	setPosition: function(parent,depth) {
		var el = document.getElementById('sgMenu-' + depth);
		
		if (depth == 0) {
			sgMenu.getOffsets(parent);
			el.style.left = Math.round(sgMenu.iLeft + sgMenu.options.offsetLeft) + 'px';
			el.style.top = Math.round(sgMenu.iTop + parent.offsetHeight + sgMenu.options.offsetTop) + 'px';			
		} else {
			var parentUl = document.getElementById('sgMenu-' + Math.round(depth - 1));
			el.style.left = Math.round(el.parentNode.offsetWidth + sgMenu.options.offsetLeftSub) + 'px';
			el.style.top = Math.round(el.parentNode.offsetTop + sgMenu.options.offsetTopSub) + 'px';
		}
	},
	
	getOffsets: function(el) {
	
		iLeft = iRight = 0;
		var obj = el;
		if (el.offsetParent) {
			iLeft = el.offsetLeft;
			iTop = el.offsetTop;
			while (obj = obj.offsetParent) {
				iLeft += obj.offsetLeft;
				iTop += obj.offsetTop;
			}
			
		}
		sgMenu.iTop = iTop;
		sgMenu.iLeft = iLeft;
		sgMenu.offsetWidth = el.offsetWidth;
		sgMenu.offsetHeight = el.offsetHeight;
		
	},
	
	extend: function(destination, source) {
	  for (var property in source) {
		destination[property] = source[property];
	  }
	  return destination;
	}
	
}


// Initiate the menu, and setup custom values
sgMenu.init('mainNav', {
					   orientation:'horizontal',
					   //offsetTop:-1,  
					   //offsetTopSub:-1,
					   overFunction:sgMenuHover, 
					   offFunction:sgMenuOff,
					   delay : 500,
					   urlBase : URL_BASE
					   });