function init() {
	runSpeed = 50;
	
	HTMLElement.prototype.getElementsByQuality = getElementsByQuality;
	HTMLElement.prototype.fadeTo = fadeTo;
	
	document.getElementsByQuality = getElementsByQuality;
	content = document.getElementById('content');
	spacer = document.getElementById('spacer');
	allSections = document.getElementsByQuality('class','section');
	allSectionLinks = document.getElementsByQuality('rel','section');
	allSectionAnchors = [];
	var node;
	i = 0;while(node = allSectionLinks[i++])
		if(node.tagName.toLowerCase() == 'a') {
			allSectionAnchors.push(node);
			node.onclick = newSection;
			node.onmouseover = flicker;
			node.goTo = node.href;
			node.href = 'javascript:;';
		}
		
	window.scroll(0,0);
	content.style.opacity = '0';
	document.styleSheets[0].deleteRule(0);
	newSection();
}

document.styleSheets[0].insertRule("#spacer {visibility : hidden }", 0); 
function flicker() {
	var obj = this;
	obj.style.color = '#e0f0ff';
	setTimeout(function(){obj.style.color = 'black';},100);
	setTimeout(function(){obj.style.color = '#e0f0ff';},200);
	setTimeout(function(){obj.style.color = 'black';},300);
	setTimeout(function(){obj.style.color = '#b0b0b0';},400);
}
function getElementsByQuality(quality,clss) {
	var returnList = [];
	var all = this.getElementsByTagName("*");
	var node;
	var i = 0; while(node = all[i++]) {
		var classes = (node.getAttribute(quality) +"").split(' ');
			var j = 0;
			var singleclass;
			while (singleclass = classes[j++])
				if(singleclass == clss) {
				returnList.push(node);
				break;
			}
		}
	return returnList;
}

function fadeTo(goal,toCall) {
	clearInterval(this.interval);
	obj = this;
	this.interval = setInterval(function(){
		if(Math.abs(parseFloat(obj.style.opacity) - goal) < 0.02) {
			clearInterval(this.interval);
			if(toCall)
				toCall();
		} else
			obj.style.opacity = (parseFloat(obj.style.opacity) + (goal - parseFloat(obj.style.opacity)) * 0.20) + "";
			//obj.style.paddingTop = (10 + parseFloat(obj.style.opacity)) + 'pc';
			obj.style.paddingLeft = (3 - parseFloat(obj.style.opacity) * 2) + 'pc';
			obj.style.paddingRight = (3 - parseFloat(obj.style.opacity) * 2) + 'pc';
			
		
	},runSpeed);
}

function newSection() {
	var currentscroll = window.pageYOffset;
	if(this.goTo)
		window.location.href = this.goTo;
	window.scroll(0,currentscroll);
	content.style.opacity = '0.6';
	content.style.background= 'white';
	content.fadeTo(0,setSection);
}

function setSection() {
	var hash = getHash();
	if(!hash)
		hash = 'somnia';
	
	var section
	var i = 0;while(section = allSections[i++])
		if(section.getAttribute('id') == hash)
			section.style.height = 'auto';
		else
			section.style.height = '5.5em';
			
	content.style.background = "#0a0a0a url('background.png') no-repeat scroll top center";
	content.fadeTo(0.6,null);
}

function getHash() {
	var hash = window.location.hashf;
	
	if(!hash)
		return window.location.hash.substring(1);
	else {
		return window.location.hashf.substring(1);
	}
}

window.onload = init;