var BUA = navigator.userAgent;
var BIE = BUA.indexOf("MSIE");
var BIsIE = BIE>=0;
var BIsMaccak = BUA.indexOf("Mac")!=-1;
var BVer = BIE>=0?parseFloat(BUA.substring(BIE+5, BIE+6)+"."+BUA.substring(BIE+7, BIE+8)):parseInt(navigator.appVersion.substring(0,1));


var NODE_ID = 0;
var PARENT_NODE = 1;
var DISPLAY_TEXT = 2;
var	HELP = 3;
var URL = 4;
var URL_TARGET = 5;

var siteNavigation = new SiteNavigation();


/* ----------------- browsers DOM extensions -------------------------*/
//this is included to implement the Array.push function in ieMac
if (typeof Array.prototype.push == 'undefined') {
	Array.prototype.push=function(){
		var i=0;
	    b=this.length,a=arguments;
		for(i;i<a.length;i++)this[b+i]=a[i];
	    return this.length
	}
}

//this is included to implement the Array.inArray function
if (typeof Array.prototype.inArray == 'undefined') {
	Array.prototype.inArray = function(value){
		for (var i=0; i<=this.length-1; i++) {
			if (this[i] == value) {
				return true;
			}
		}

		return false;
	}
}
/* -----------------  end browsers DOM extensions -------------------------*/


function SiteNavigation() {
	this._topNavigationId = -1;
	this._clientTemplatePath = '';
	this._trail = new Array();
	this._menuCount = 0;
	this._topNavDOMId = "topNav";
	this._selectedNavId = 0;
	this._rootNavId = -1;
	
	this.initBeforeLoad = function(){
		this._makeTrail();
	}
	
	this.initAfterLoad = function() {
		this._enableMainNavigation();
	}
	
	this.setSelectedNavId = function(selectedNavId){
		this._selectedNavId = selectedNavId;
	}

	this.setClientTemplatePath = function(path) {
		this._clientTemplatePath = path;		
	}	
	
	this._enableMainNavigation = function() {
		this._sfHover();
		document.getElementById(this._topNavDOMId).style.display = "block";
	}

	this.buildTopNavigation = function() {
		var out = '';

		out = this._buildTopNavigationRC(this._topNavigationId, 0);
		this._initiateTopNavigation();
		return out;
	}
	/*
	this.buildLeftNavigation = function(){
		var out = '';
		
		out += "<ul id='leftNav'>\n";
		for (var i=0; i<NavItems.length; i++) {
			if (NavItems[i][PARENT_NODE] == this._leftNavigationId) {
				out += "<li>\n";
				out += "<a href=\""+NavItems[i][URL]+"\">"+NavItems[i][DISPLAY_TEXT]+"</a>\n";
				out += "</li>\n";
			}
		}
		out += "</ul>\n";
		
		return out;
	}
	*/
	this._initiateTopNavigation = function() {
		if (window.attachEvent) {
			window.attachEvent("onload", this._sfHover);
					
		} else if (document.all && document.getElementById) {
			window.onload=this._sfHover;
		}
	}

	this._sfHover = function() {
		var topNavDOMId = siteNavigation._topNavDOMId;
		var sfEls = document.getElementById(topNavDOMId).getElementsByTagName("LI");
		
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
			
			if (sfEls[i].parentNode == document.getElementById(topNavDOMId) && BIsIE) {
				//sfEls[i].style.width = '1px';
			}
		}
	}

	this._buildTopNavigationRC = function(navId, depth){
		var out = '';
		var depthLimit = 2;
	
		if (!hasChildren(navId) || (depth == depthLimit)){
			return out;
		}
	
		if (this._menuCount == 0){
			out += '<ul id="'+this._topNavDOMId+'">'+"\n";
			this._menuCount++;
		} else {
			out += "<ul>\n";
		}
		
		depth++;
	
		for (var i=0; i<= NavItems.length-1; i++){
			if (NavItems[i][PARENT_NODE] == navId) {
				out += "<li>\n";
				out += "<a href=\""+NavItems[i][URL]+"\">"+NavItems[i][DISPLAY_TEXT]+"</a>\n";
				out += this._buildTopNavigationRC(NavItems[i][NODE_ID], depth);
				out += "</li>\n";
			}
		}
	
		out += "</ul>\n";
		return out;
	}
	
	this._isSelectedNavIdValid = function(){
		return (getNavItem(this._selectedNavId).length > 0);
	}
	
	this._makeTrail = function(){
		if (!this._isSelectedNavIdValid()) {return '';}
		this._makeTrailRc(this._selectedNavId);
		this._trail.reverse();
	}
	
	this._makeTrailRc = function(nodeId){
		if (nodeId != this._rootNavId) {
			this._trail.push(nodeId);
	
			var node = getNavItem(nodeId);
			this._makeTrailRc(node[PARENT_NODE]);
		}
	
		if (nodeId == this._rootNavId){
			this._trail.push(this._rootNavId);
		}
	}	
}

/* ----------------- functions for manipulating the NavItems array -------------------------*/
function hasChildren(navId){
	for (var i=0; i<=NavItems.length-1; i++) {
		if (NavItems[i][PARENT_NODE] == navId) {
			return true
		}
	}

	return false;
}


function getNavItem(id){
	//returns an array containing the data from NavItems for the given id
	//

	var navItem = Array();

	for (var i=0; i<NavItems.length; i++) {
		if (NavItems[i][NODE_ID] == id) {
			navItem[NODE_ID] = NavItems[i][NODE_ID];
			navItem[PARENT_NODE] = NavItems[i][PARENT_NODE];
			navItem[DISPLAY_TEXT] = NavItems[i][DISPLAY_TEXT];
			navItem[HELP] = NavItems[i][HELP];
			navItem[URL] = NavItems[i][URL];
			navItem[URL_TARGET] = NavItems[i][URL_TARGET];
		}
	}

	return navItem;
}