
var SM = new Object();
SM.onbeforeload	= function() { for (var module in this) { if (this[module].onbeforeload) { this[module].onbeforeload(); };};};
SM.onload		= function() { for (var module in this) { if (this[module].onload) { this[module].onload(); };};};
SM.onresize		= function() { for (var module in this) { if (this[module].onresize) { this[module].onresize(); };};};



SM.justmargo = {
	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 () {
					SM.justmargo.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("SM.justmargo.Scroll.scrollTo("+x+","+y+")",100);
				this.scrollLoop = true;
				}
			}
		}
	}



SM.Clear = {
	container			: 'content',
	inner				: 'inner-content', 
	footer				: 'footer',
	clear				: 'service', 
	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(); };  }
	};



SM.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('SM.Resize.watch()',50);
		},
	watch			: function() {
		var o = this.height;
		this.height = this.control.offsetHeight;
		if (o!=this.height) this.react();
		},
	react			: function() { SM.onresize(); }
	};



window.onload	= function() { SM.onload(); };
window.onresize	= function() { SM.onresize(); };