﻿// Shorthand for document.getElementByID:
var get$ = function(el) { return document.getElementById(el); };
var getN$ = function(el) { return document.getElementsByName(el); };

// For the purpose of this script, it's sufficient to find out if the browser is IE or not.
var browser = "";
if (navigator.userAgent.indexOf("MSIE") >= 0) {
   browser = "ie";
}

SetupMore();
AddNoteEvents();
SetupNavNagivate();

function SetupMore() {
   var items = hasClass("morewrap", "span");
   // var itemsContent = hasClass("morecontent", "span");
   var content = [];
   //debugger;
   for (var i = 0; i < items.length; i++) {
      (function() {
      	var item = items[i];
      	item.style.display = "inline";

      	// Find the first span within the 'more' span and assign it to content.
      	var content = item.getElementsByTagName("span")[0];
      	content.style.display = "none";

      	item.onclick = function() {
      		var disp = content.style.display;

      		if (disp == "none") {
      			content.style.display = "inline";
      		} else {
      			content.style.display = "none";
      		}
      	};
      })();
   }
  }

	// Setup the top navigation cells to call the doNavNavigate function on a click.
	function SetupNavNagivate() { 
		var items = hasClass("navButton", "td");

		for (var i = 0; i < items.length; i++) {
			// Create a closure so we refer to the correct element			
			(function() {
				var item = items[i];

				item.onclick = function() {
//					debugger;
					doNavNavigate(item);
				}

			})();		
		}
	}

// Function from Pro Javascript Techniques Pgs 91-92
// Finds elements based on their class name.
function hasClass(name, type) {
   var r = [];
   
   //Limit search by type or all
   var e = document.getElementsByTagName(type || "*");

   for (var j = 0; j < e.length; j++) {
      if ( e[j].className.search(name) >= 0 ) {
         r.push(e[j]);
      }
   }
   return r;
  }

// Functions for showing and hiding help boxes.
	function AddNoteEvents() {
//  	debugger;
		// Pop notes are stored in a notes division on the page
		var noteWrapper = get$("notes");

		if (noteWrapper) {
			// Get all the div's within the noteWrapper
			var notes = noteWrapper.getElementsByTagName("div");

			for (var i = 0; i < notes.length; i++) {
	//			debugger;
				// Wrap this section in a closure so we still have access to the correct
				// notes item. The enclosing anon' function creates a reference to the notes[i] item.
				// That reference points us to the correct item when the function is eventually called.
				(function() {
					var note = notes[i];

					// If the div has a name that contains the word 'note', assign the display events.
					if (note.id.indexOf("note") >= 0) {
	//					debugger;
						note.onmouseover = function() { KeepHelpOpen(note.id); }

						notes[i].onmouseout = function() { HideHelp(note.id); }

						notes[i].onclick = function() { ToggleVisible(note.id, event); }
					}
				})();
			}
		}
	}

  function ToggleVisible(elm, evt) {
  	var me = get$(elm);
//debugger;
  	if (me.style.display == "block") {
  		me.style.display = "none";
  	} else {
  		me.style.display = "block";
  		me.style.left = (evt.clientX + -10 + ScrollX()) + "px";
  		me.style.top = (evt.clientY + -10 + ScrollY()) + "px";
  	}
  	return false;
  }

  function HideHelp(elm) {
  	var me = get$(elm);

  	me.style.display = "none";
  	document.body.style.cursor = "auto";
  }

  function ShowHelp(elm, evt) {
  	var me = get$(elm);
//  	alert("Here");
//  	debugger;
  	me.style.display = "block";
  	me.style.left = (evt.clientX + -10 + ScrollX()) + "px";
  	me.style.top = (evt.clientY + -10 + ScrollY()) + "px";
  	document.body.style.cursor = "help";

  	return false;
  }

  function KeepHelpOpen(elm) {
  	//debugger;
  	var me = get$(elm);
  	me.style.display = "block";
  	document.body.style.cursor = "help";
  }
	
	function ScrollY() {
		var de = document.documentElement;
		return self.pageYOffset || (de && de.scrollTop) || document.body.scrollTop;
	}
	function ScrollX() {
		var de = document.documentElement;
		return self.pageXOffset || (de && de.scrollLeft) || document.body.scrollLeft;
	}

	// This function is intended to work with the navigation structure at the top of each page.
	// It assumes the element being clicked is the cell containing the navigation link.
	// It assumes there is only one link and it is therefore in the 0 position.
	function doNavNavigate(elm) {

		// Get the first link inside this element
		var target = elm.getElementsByTagName('a')[0].href;

		// If there was a link, navigate to it.
		if (target !== undefined) { window.location.href = target; }
		
	}
