/******************************************************************************
 The SI Object v2.0
 
 Stores a variety of functions localized to modules. Any module that requires
 initialization onload should have an onload handler. The SI.onload method will
 loop through all modules and run their respective onload handler. SI.onload
 can then be called in the window.onload event handler and all modules requiring
 initialization will be initialized. Does the same for onbeforeload and onresize.
 
 ******************************************************************************/
var SI = new Object();
SI.onbeforeload	= function() { for (var module in this) { if (this[module].onbeforeload) { this[module].onbeforeload(); };};};
SI.onload		= function() { for (var module in this) { if (this[module].onload) { this[module].onload(); };};};
SI.onresize		= function() { for (var module in this) { if (this[module].onresize) { this[module].onresize(); };};};


/******************************************************************************
 SI.MORE module v1.0
 
 Generates and activates multiple instances of one anchor-based navigation, then
 adds sliding onclick events (based on and including code originally created by 
 Travis Beckam of http://www.squidfingers.com | http://www.podlob.com)
 
 ******************************************************************************/
SI.MORE = {
	onbeforeload	: function() {
		if (!document.getElementsByTagName) return;
		// Get navigation
		var navigation = document.getElementById('navigation').innerHTML;
		
		// Find all of our navigation containers
		var navs = new Array();
		var divs = document.getElementsByTagName("div");
		for (var i=0; i<divs.length; i++) {
			var e = divs[i];
			if (e.className=='navigation') {
				e.innerHTML = navigation;
				navs[navs.length] = e;
				};
			};
		
		// Opera doesn't get the scoll feature at the moment
		if (window.opera) { return; }
		
		for (var j=0; j<navs.length; j++) {
			var lnks = navs[j].getElementsByTagName("a");
			
			for (var k=0; k<lnks.length; k++) {
				var a = lnks[k];
				if (k==j) {
					a.className = 'active';
					a.onclick = function() { return false; }
					continue;
					}
				a.onclick = function () {
					SI.MORE.Scroll.to(this.href.replace(/^[^#]*#/,''));
					return false;
					}
				}
			}
		},
	Scroll	: {
		scrollLoop 		: false, 
		scrollInterval	: null,
		getWindowHeight	: function() {
			if (document.all) {  return (document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.body.clientHeight; }
			else { return window.innerHeight; }
			},
		getScrollLeft	: function() {
			if (document.all) { return (document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft; }
			else { return window.pageXOffset; }
			},
		getScrollTop	: function() {
			if (document.all) { return (document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop; }
			else { return window.pageYOffset; }
			},
		getElementYpos	: function(el) {
			var y = 0;
			while(el.offsetParent){
				y += el.offsetTop
				el = el.offsetParent;
				}
			return y;
			},
		to : function(id){
			if(this.scrollLoop){
				clearInterval(this.scrollInterval);
				this.scrollLoop = false;
				this.scrollInterval = null;
				}
			var container = document.getElementById('container');
			var documentHeight = this.getElementYpos(container) + container.offsetHeight;
			var windowHeight = this.getWindowHeight();
			var ypos = this.getElementYpos(document.getElementById(id));
			if(ypos > documentHeight - windowHeight) ypos = documentHeight - windowHeight;
			this.scrollTo(0,ypos);
			},
		scrollTo : function(x,y) {
			if(this.scrollLoop) {
				var left = this.getScrollLeft();
				var top = this.getScrollTop();
				if(Math.abs(left-x) <= 1 && Math.abs(top-y) <= 1) {
					window.scrollTo(x,y);
					clearInterval(this.scrollInterval);
					this.scrollLoop = false;
					this.scrollInterval = null;
					}
				else {
					window.scrollTo(left+(x-left)/2, top+(y-top)/2);
					}
				}
			else {
				this.scrollInterval = setInterval("SI.MORE.Scroll.scrollTo("+x+","+y+")",100);
				this.scrollLoop = true;
				}
			}
		}
	}


/******************************************************************************
 SI.Clear module v2.0
 
 The Clear module takes care of clearing the footer below two or more 
 absolutely positioned columns. An extra function clearContained will clear the
 absolutely positioned contents of any element with a className 'clear-contents'
 
 ******************************************************************************/
SI.Clear = {
	container			: 'content',
	inner				: 'inner-content', // extendShallow does not apply to children of innerContainer
	footer				: 'footer',
	clear				: 'service', // (class-1|class-2|class-n)
	minHeight			: 410,
	extendShallow		: false,
	bottomOut			: false,
	getContainedHeight	: function(e) {
		var oh=[],h=0,c,k; oh[0]=0;
		for (var i=0; i<e.childNodes.length; i++) { var c=e.childNodes[i]; if (c.nodeType==1) { c.style.height = 'auto'; oh[oh.length] = c.offsetHeight+c.offsetTop; }; };
		k=oh.length; do { h = (oh[k]>h)?oh[k]:h; } while(--k);
		return h;
		},
	clearFooter			: function () {
		var d = document,w=window,dE=d.documentElement,dB=d.body,oh=[],h=0;
		if (!d.getElementById || !dB.offsetHeight) return;
		
		var c = d.getElementById(this.container);
		var f = d.getElementById(this.footer);
		if (!c) return;
		
		var t	= c.offsetTop;
		var fH	= (!f)?0:f.offsetHeight;
		var wH	= (typeof(w.innerHeight)=='number')?w.innerHeight:(dE&&dE.clientHeight)?dE.clientHeight:(dB&&dB.clientHeight)?dB.clientHeight:0;
		oh[0]	= this.minHeight-t-fH;	// Minimum height
		
		for (var i=0;i<c.childNodes.length;i++) { var e = c.childNodes[i]; if (e.nodeType==1) { e.style.height = (e.id==this.inner)?this.getContainedHeight(e)+'px':'auto'; oh[oh.length] = e.offsetHeight+e.offsetTop; }; };
		for (var j=oh.length-1; j>=0; j--) { h = (oh[j]>h)?oh[j]:h; };
		
		if (this.bottomOut) { h = ((t+h+fH) < wH)?wH-fH-t:h; };
		if (this.extendShallow) { for (var k=0;k<c.childNodes.length;k++) { var e = c.childNodes[k]; if (e.nodeType==1) { e.style.height = h+'px'; }; }; };
		
		c.style.height = h+'px';
		},
	clearContained		: function() {
		var d = document,oh=[],h=0;
		if (!d.getElementsByTagName) return;
		var all = d.getElementsByTagName("*");
		for (var i=all.length-1; i>0; i--) {
			var e = all[i];
			if (e.className.indexOf(this.clear)!=-1) { // update to use regex to allow for multiple classes
				e.style.height = this.getContainedHeight(e)+'px';
				};
			};
		},
	onbeforeload		: function() { this.clearContained(); this.clearFooter(); },
	onresize			: function() { this.clearContained(); if (this.bottomOut) { this.clearFooter(); };  }
	};


/******************************************************************************
 SI.Resize module v2.0
 
 Resize creates a hidden div and monitors changes in it's offsetHeight to 
 determine when the user resizes text. This function doesn't modify any other
 elements, it augments the browsers default resize detection.

 ******************************************************************************/
SI.Resize = {
	control			: null,
	height			: 0,
	onbeforeload	: function() {
		if ((document.all && window.print) || !document.createElement || !document.getElementsByTagName) { return; }
		
		var c=document.createElement('div'),s=c.style;
		s.position		= 'fixed';
		s.top			= '0';
		s.visibility	= 'hidden';
		s.width			= '1em';
		s.height		= '1em';
		
		this.control = document.body.appendChild(c);
		this.height = 0;
		window.setInterval('SI.Resize.watch()',50);
		},
	watch			: function() {
		var o = this.height;
		this.height = this.control.offsetHeight;
		if (o!=this.height) this.react();
		},
	react			: function() { SI.onresize(); }
	};


/******************************************************************************
 window.onload/onresize()
 
 This should only ever require SI.onload() which in turn initializes all modules
 that require it.
 
 ******************************************************************************/
window.onload	= function() { SI.onload(); };
window.onresize	= function() { SI.onresize(); };
