

var jasper = new Object();

jasper.sections = new Object();

jasper.activeSection = null;

jasper.keepSection = null;


jasper.maxHeight = 465;


notice_section = function(elementId) {
	var n = (typeof elementId == 'string') ? document.getElementById(elementId) : elementId;
	
	if (n && n.style) {
		n.style.padding = '1em 0';
		/*
		if (n.offsetHeight < jasper.maxHeight) {
			// center content
			n.style.paddingTop = '' + ((jasper.maxHeight - n.offsetHeight) / 2) + 'px';
		}
		*/
		
		jasper.sections[n.id] = n;

		if (n.id != 'section_') {
			n.style.display = 'none';
		} else {
			n.style.display = 'block';
			jasper.activeSection = n;
			jasper.keepSection = '';
		}
		
	}
	
};





function init_magic()
{
	if (!cssIsEnabled()) {
		start_magic = function() { };
	} else {
		start_magic = function(exitElement)
		{
			var a = document.getElementsByTagName('a');
			
			var b = new Array();
			
			for (var i in a) {
				// alert(dump(a[i]));
				if (a[i].className && a[i].className.indexOf('magic_link') >= 0) {
					b.push(a[i]);
				}
			}
			
			for (var i in b) {
				b[i].onmouseover = magic_onmouseover;
				b[i].onclick = magic_onclick;
				b[i].onmouseout = magic_onmouseout;
			}
			
			if (exitElement) {
				var n = (typeof exitElement == 'string') ? document.getElementById(exitElement) : exitElement;
				if (n) {
					n.onmouseout = magic_onmouseout;
				}
			}
		};
		
		dump = function(a) {
			var ret = '';
			for (var i in a) {
				if (typeof(a[i]) != "function") {
					ret += '[' + i + ']: ' + a[i] + "\n";
				}
			}
			return ret;
		};
		
		keep_section = function(section_name)
		{
			switch_section(section_name);
			if (jasper.sections['section_' + section_name]) {
				jasper.keepSection = section_name;
				return false;
			} else {
				return true;
			}
		};
		


		switch_section = function(section_name)
		{
			if (jasper.sections['section_' + section_name]) {
				var newSection = jasper.sections['section_' + section_name];
				if (newSection != jasper.activeSection) {
					if (jasper.activeSection) {
						jasper.activeSection.style.display = 'none';
					}
					jasper.activeSection = newSection;
					if (jasper.activeSection) {
						jasper.activeSection.style.display = 'block';
						
					}
				}
			}
		};
			
		magic_onmouseover = function()
		{
			/*
			// old method - replaced because it doesn't show text for actual links
			if (this.href && this.href.indexOf('#') >= 0) {
				var section_name = this.href.substr(this.href.indexOf('#') + 1);
				switch_section(section_name);
			}
			*/
			/*
			// old method 2 - replaced because it's not too flexible
			if (this.href && this.href.indexOf('#') >= 0) {
				section_name = this.href.substr(this.href.indexOf('#') + 1);
			} else {
				section_name = this.href.substr(0, this.href.length - 1);
				section_name = section_name.substr(section_name.lastIndexOf('/') + 1);
			}
			*/
			
			var section_name = '';
			var curRel = this.getAttribute('rel');
			if (curRel && curRel.indexOf('magic[') == 0) {
				var res = curRel.match(/magic\[([^\]]*)\]/);
				if (res && res.length > 0) {
					section_name = res[1];
				}
			}
			if (section_name) {
				switch_section(section_name);
			}
		};
		
		
		magic_onmouseout = function()
		{
			switch_section(jasper.keepSection);
		};
		
		
		magic_onclick = function()
		{
			if (this.href && this.href.indexOf('#') >= 0) {
				var section_name = this.href.substr(this.href.indexOf('#') + 1);
				return keep_section(section_name);
			}
			
			
			// else
			return true;
		};
		
		/* add Array.push if needed (ie5) */
		if (Array.prototype.push == null) { Array.prototype.push = function(item) { this[this.length] = item; return this.length; }}
		
	}
	
	start_magic('menu_wrapper');
}

//check that nav styles are enabled
function cssIsEnabled()
{
	//reference to test link
	var linkObj = document.getElementById("home_link");
	if (!linkObj) {
		return false;
	}
	
	//link display
	var linkDisplay = "inline";
	
	//retrieve the computedStyle display of the link
	//this is mozilla and opera 7.2+
	if(typeof document.defaultView != "undefined" && typeof document.defaultView.getComputedStyle != "undefined")
	{
		linkDisplay = document.defaultView.getComputedStyle(linkObj,"").getPropertyValue("display");
	}
	//retrieve the currentStyle display of the link
	//this is internet explorer
	else if(typeof linkObj.currentStyle != "undefined")
	{
		linkDisplay = linkObj.currentStyle.display;
	}
	
	//if display is not 'inline' then CSS is deemed to be enabled
	//this is also the fallback position if no property is detected
	//which is Safari and Opera <7.2
	return (linkDisplay != "inline");
};




function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}


function addUnloadEvent(func) {
  var oldonunload = window.onunload;
  if (typeof window.onunload != 'function') {
    window.onunload = func;
  } else {
    window.onunload = function() {
      if (oldonunload) {
        oldonunload();
      }
      func();
    }
  }
}

addLoadEvent(init_magic);

