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._mainNavigationId = -1;
	this._clientTemplatePath = '';
	this._mainNavDepthLimit = -1;  //-1 = no limit on depth 
	this._mainNavTreeRootLevel = 0;  //level in the trail that main nav tree roots start (count from 0)
	this._trail = new Array();
	this._menuCount = 0;
	this._mainNavDOMId = "mainNav";
	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.buildMainNavigation = function() {
		var out = '';

		out = this._buildMainNavigationRC(this._mainNavigationId, 0);
		return out;
	}
	
	this._enableMainNavigation = function() {
		this._sfHover();
		document.getElementById(this._mainNavDOMId).style.display = "block";
	}
	
	this.buildSelectedNavTitle = function() {
		var out = '';
		
		if (this._isSelectedNavIdValid()) {
			out = '<h1>'+getNavItem(this._selectedNavId)[DISPLAY_TEXT]+'</h1>';
		}
		
		return out;		
	}
	
	this.buildSubNavigation = function() {
		var out = '';
		
		out += "<ul id='subNav'>";
		for (var i=0; i<NavItems.length; i++) {
			if (NavItems[i][PARENT_NODE] == this._selectedNavId) {
				out += '<li>';
		    	out += '<img src="'+this._clientTemplatePath+'images/mm_tlc.gif" width="4" height="4" class="topLeftCurve1px" />';
				out += '<a href="'+NavItems[i][URL]+'" '+(NavItems[i][URL_TARGET] != '' ? 'target="'+NavItems[i][URL_TARGET]+'"' : '')+'>'+NavItems[i][DISPLAY_TEXT]+'</a>';
				out += '<img src="'+this._clientTemplatePath+'images/mm_blc.gif" width="4" height="4" class="bottomLeftCurve1px" />';
				out += '</li>';
			}
		}
		out += "</ul>";
		
		//alert(out);
		return out;
	}
	
	this.subscribeSubmit = function(formElement) {
		with (formElement) {
		
			this.subscribeFirstNameClick(PlugletFirstName);
			this.subscribeLastNameClick(PlugletLastName);
			this.subscribeEmailClick(PlugletEmail);
	
			if ((PlugletFirstName.value == '') ||
			   (PlugletLastName.value == '') ||
			   (PlugletEmail.value == '')) {
				
				if (PlugletFirstName.value == '') {PlugletFirstName.value = 'Your first name';}
				if (PlugletLastName.value == '') {PlugletLastName.value = 'Your last name';}
				if (PlugletEmail.value == '') {PlugletEmail.value = 'Your email address';}
				
				alert ('Please include your first name, last name and email address...');
				return false;
				
			} else {
				return true;
			}
		}
	}
	
	this.subscribeFirstNameClick = function(textInputElement) {
		if (textInputElement.value == 'Your first name') {
			textInputElement.value = '';
		}
	}

	this.subscribeLastNameClick = function(textInputElement) {
		if (textInputElement.value == 'Your last name') {
			textInputElement.value = '';
		}
	}
	
	this.subscribeEmailClick = function(textInputElement) {
		if (textInputElement.value == 'Your email address') {
			textInputElement.value = '';
		}
	}
	
	this.buildTrail = function(){
		var out = Array();
		var foundMainNav = false;
		
		for (var i=0; i<this._trail.length; i++) {
			if (foundMainNav) {
				var navItem = getNavItem(this._trail[i]);
				out.push('<a href="'+navItem[URL]+'">'+navItem[DISPLAY_TEXT]+'</a>');
			} else if (this._trail[i] == this._mainNavigationId) {
				foundMainNav = true;
			}
		}
		
		return out.join(' >> ');
	}
	
	this._sfHover = function() {
		if (!BIsIE) {return false;}
		
		var mainNavDOMId = siteNavigation._mainNavDOMId;
		var sfEls = document.getElementById(mainNavDOMId).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(mainNavDOMId) && BIsIE) {
				//sfEls[i].style.width = '1px';
			}
		}
	}

	this._buildMainNavigationRC = function(navId, depth){
		var out = '';
	
		if (!hasChildren(navId) || (depth == this._mainNavDepthLimit)){
			return out;
		}
	
		if (this._menuCount == 0){
			out += '<ul id="'+this._mainNavDOMId+'" style="display:none">';
			this._menuCount++;
		} else {
			out += "<ul>";
		}
		
		depth++;
	
		for (var i=0; i<= NavItems.length-1; i++){
			if (NavItems[i][PARENT_NODE] == navId) {
				out += "<li>";
				out += "<a href=\""+NavItems[i][URL]+"\" "+(NavItems[i][URL_TARGET] != '' ? 'target="'+NavItems[i][URL_TARGET]+'"' : null)+">"+NavItems[i][DISPLAY_TEXT]+"</a>";
				out += this._buildMainNavigationRC(NavItems[i][NODE_ID], depth);
				out += "</li>";
			}
		}
		
		/* -- this makes the curvy edges on the bottom of the submenues-- */
		if (depth > 1) {
			out += '<img src="'+this._clientTemplatePath+'images/mm_blc.gif" width="4" height="4" class="bottomLeftCurve1px" />';
			out += '<img src="'+this._clientTemplatePath+'images/mm_brc.gif" width="4" height="4" class="bottomRightCurve1px" />';
		}
		/* ----- */

		out += "</ul>";
		return out;
	}
	
	this._isSelectedNavIdValid = function(){
		return ((this._selectedNavId > 0) && (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;
}
