function init() {	
	sectionDivs = getElementsByTagAndAttributeSearch('div','class','section');
	internalLinks = getElementsByTagAndAttributeSearch('a','rel',['prev','next','section','start']);
	registerToAll(internalLinks,'onclick',swapSection);
	
	HTMLElement.prototype.fadeTo = fadeTo;
	HTMLElement.prototype.fadeText = fadeText;
	var i = 0; while(obj = sectionDivs[i++]) {
		var child = obj.childNodes[2];
		while(child = child.nextSibling)
			if(child.nodeType == 1)
				child.fadeText();
	}
	
	swapSection();
}

function swapSection() {
	if(this.href) {
		window.location = this.href;
		obj = this.parentNode;
		while((obj = obj.parentNode) && !(obj.tagName.toLowerCase() == 'div' && (obj.getAttribute('class').indexOf('section') + 1)));
		obj.style.opacity='1';
		obj.fadeTo(0,false);
	}
	document.getElementById(currentId()).style.opacity='0';
	document.getElementById(currentId()).fadeTo(1,'block');
}

function fadeTo(goal,displayAs) {
	var obj = this;
	if(displayAs)
		obj.style.display = displayAs;
	var xrand = Math.random()*2 -1;
	var yrand = Math.random()*2 -1;
	
	var runner = parseFloat(obj.style.opacity);
	var interval = setInterval(function() {
		runner += (goal - runner) * 0.2;
		obj.style.opacity = runner.toString();
		obj.style.marginTop = (yrand*(-0.9 * runner + 0.9))+'pc';
		
		if(Math.abs(runner-goal) < 0.01) {
			if(!displayAs)
				obj.style.display='none';
			obj.style.opacity  = goal;
			clearInterval(interval);
			
		}
	},50);
}

function fadeText() {
	var obj = this;
			

	obj.onmouseout = function(){this.opacityGoal = 0.25};	
	obj.onmouseover = function(){this.opacityGoal = 0.9};
	
	obj.opacityGoal = 0.2;
	
	var runner = 0.2;
	var interval = setInterval(function() {
		runner += (obj.opacityGoal - runner) * 0.2;

		obj.style.opacity = runner.toString();
		
	},50);
}
function getElementsByTagAndAttributeSearch(tag,attribute,needle) {
	var nodeList = []
	var attrValue;
	
	var needles = (needle instanceof Array) ? needle : [needle];
	var i = 0; while(needle = needles[i++]) {
		var j = 0; while(obj = document.getElementsByTagName(tag)[j++])
			if((attrValue = obj.getAttribute(attribute)))
				
				if(attrValue.indexOf(needle)+1)
					nodeList.push(obj);
	}
	return nodeList;
}

function currentId() {
	var hash = window.location.hash.substring(1);
	if(!hash) 
	hash = 'splash';
	return hash;
}

function registerToAll(nodeList,handler,action) {
	var i = 0; while(obj = nodeList[i++])
		eval('obj.'+handler+'=action');
}

window.onload = init;