sgSt={
// CSS classes
	dynamicClass:'sgSt',
	showClass:'show',
	currentClass:'current',
	hideClass:'hide',
// Page section IDs
	expButt:'expButt',
	boundaryId:'boundary',
	detailsContainer:'flatdetails',
	detailsNav:'detailnav',
// global parameters
	currentLink:null,
	currentSection:null,

/* Initialise functionality */
	init:function(){
		if(!document.getElementById || !document.createTextNode){return;}
		var container=document.getElementById(sgSt.boundaryId);
		if(!container){return;}
		sgSt.cssjs('add',container,sgSt.dynamicClass);
		sgSt.initTabs();
		sgSt.initExpAll();
	},
	initExpAll:function(){
		var expButt=document.getElementById(sgSt.expButt);
		if(expButt && expButt.getElementsByTagName('a')[0]){
			var link=expButt.getElementsByTagName('a')[0];
			sgSt.addEvent(link,'click',sgSt.expAll,false);
		}
	},
	initTabs:function(){
		var nav=document.getElementById(sgSt.detailsNav);
		if(nav && nav.getElementsByTagName('a')[0]){
			var links=nav.getElementsByTagName('a');
			var firstSection=links[0].href.toString().match(/#(.*)/)[1];
			firstSection=document.getElementById(firstSection);
			if(firstSection){
				var parentDIV=firstSection.parentNode.parentNode;
				sgSt.cssjs('add',parentDIV,sgSt.showClass);
				sgSt.currentSection=firstSection;
				sgSt.cssjs('add',links[0],sgSt.currentClass);
				sgSt.currentLink=links[0];
				for(var i=0;i<links.length;i++){
					sgSt.addEvent(links[i],'click',sgSt.showTab,false);
				}
			}
		}
	},
	expAll:function(e){

		var nav=document.getElementById('boundary');
		if(sgSt.cssjs('check',nav,'sgSt')){
			sgSt.cssjs('remove',nav,'sgSt');
		} else {
			sgSt.cssjs('add',nav,'sgSt');
		}


		sgSt.cancelClick(e);
	},
	showTab:function(e){
		var section=sgSt.getTarget(e);
		var toshow=section.getAttribute('href').toString().match(/#(.*)/)[1];
		if(sgSt.currentSection && sgSt.currentLink){
			sgSt.cssjs('remove',sgSt.currentSection.parentNode.parentNode,sgSt.showClass);
			sgSt.cssjs('remove',sgSt.currentLink,sgSt.currentClass);
		}
		if(document.getElementById(toshow)){
			toshow=document.getElementById(toshow);
			sgSt.cssjs('add',toshow.parentNode.parentNode,sgSt.showClass);
			sgSt.cssjs('add',section,sgSt.currentClass);
			sgSt.currentSection=toshow;
			sgSt.currentLink=section;
		}
		sgSt.cancelClick(e);
	},
/* helper methods */
	getTarget:function(e){
		var target = window.event ? window.event.srcElement : e ? e.target : null;
		if (!target){return false;}
		if (target.nodeName.toLowerCase() != 'a'){target = target.parentNode;}
		return target;
	},
	cancelClick:function(e){
		if (window.event){
			window.event.cancelBubble = true;
			window.event.returnValue = false;
			return;
		}
		if (e){
			e.stopPropagation();
			e.preventDefault();
		}
	},
	addEvent: function(elm, evType, fn, useCapture){
		if (elm.addEventListener)
		{
			elm.addEventListener(evType, fn, useCapture);
			return true;
		} else if (elm.attachEvent) {
			var r = elm.attachEvent('on' + evType, fn);
			return r;
		} else {
			elm['on' + evType] = fn;
		}
	},
	// cssjs tests
	cssjs:function(a,o,c1,c2){
		switch (a){
			case 'swap':
				o.className=!sgSt.cssjs('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
			break;
			case 'add':
				if(!sgSt.cssjs('check',o,c1)){o.className+=o.className?' '+c1:c1;}
			break;
			case 'remove':
				var rep=o.className.match(' '+c1)?' '+c1:c1;
				o.className=o.className.replace(rep,'');
			break;
			case 'check':
				return new RegExp("(^|\\s)" + c1 + "(\\s|$)").test(o.className)
			break;
		}
	}
}
// start the show.
sgSt.addEvent(window, 'load', sgSt.init, false);

