var objectsOpen = new Array();
var navPlus = "Images/nav_plus.gif";
var navMinus = "Images/nav_minus.gif";
var navBlank = "Images/nav_blank.gif";
var staticNavId = "";

// This function gets called whenever one of the nav images gets clicked.
// It manages the array of objects that are displayed.
// The array is an array of arrays where each inner array is composed of:
// [0]: the nav object
// [1]: the image object
// navsToShow is passed in in the code behind.

// This function toggles the nav and keep track of which nav sections should be open.
// also takes the number of navigation div to show.
function toggleNav(obj, image, navsToShow){
	var newArray = createNavArray(obj, image);
	if($(obj) != null){
		if($(obj).style.display == 'none' && existsInArray(objectsOpen, obj) == -1){ // it's not open
			toggle.show(obj,image);
			// make this item the first item of the array
			if(obj != staticNavId){ // if it isn't the currently open page, add it
				objectsOpen.unshift(newArray);
			}
			while(objectsOpen.length > navsToShow){
				navOpen = objectsOpen.pop();
				var oldNav = navOpen[0];
				var oldImage = navOpen[1];
				toggle.hide(oldNav, oldImage);
			};
		}else{ // it is open, in which case not only must it be closed but it must be removed from the array of objects that are open
			toggle.hide(obj, image);
			// remove the object from the array

			for(i = objectsOpen.length;i>0;i--){
				var indexMinusOne = i - 1;
				if(objectsOpen[indexMinusOne][0] == newArray[0]){
					objectsOpen.splice(indexMinusOne,1);
					break;
				}
			}
		}
	}
}

function setStaticNavId(value){
	staticNavId = value;
}

// standard method for creating array that handles which nav divs are open
function createNavArray(obj, image){
	var navArray = new Array();
	navArray.push(obj);
	navArray.push(image);
	return navArray;
}

// Used to toggle the display property of the object. Also changes the image
var toggle = {
	show : function(obj, image) {
		$(obj).style.display = ($(obj).style.display != 'none' ? 'none' : '' );

		// change the image for the nav
		if($(image).src != location.protocol + "//" + location.host + navBlank){
			$(image).src = navMinus;
		}
	},
	hide : function(obj, image) {
		$(obj).style.display = 'none';

		// change the image for the nav
		if($(image).src != location.protocol + "//" + location.host + navBlank){
			$(image).src = navPlus;
		}
	}
};

///Returns the index of the object in the array or -1 if it does not exist.
function existsInArray(array, object){
	for(i=0;i<array.length;i++){
		if(array[i] == object){
			return i;
		}
	}
	return -1;
}

// Base function which gets the elements of passed in code.
function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}