var pages = new Array(
	"News", "Applications", "Extras", "About", "Contact"
);
	
var subPages = new Array(
	
);

var preloadAnimTimer = 0, preloadFrame = 1;

function loadPage() {
	for (i = 1; i <= 12; i++)
		new Image(48, 48).src = '/images/load/'+i+'.png';
	
	preloadAnimation(true);
	
	new Image(56, 56).src = "/images/loading-spinner.png";
	new Image(1, 24).src = "/images/filter-bar-gradient.png";
	new Image(110, 17).src = "/images/nav-current.png";
	new Image(110, 17).src = "/images/nav-down.png";
	new Image(110, 17).src = "/images/nav-highlight.png";
	new Image(1, 121).src = "/images/app-highlight-well.png";
	new Image(1, 121).src = "/images/odd-background-gradient.png";
	new Image(10, 10).src = "/images/bottom-left-corner.png";
	new Image(10, 10).src = "/images/bottom-right-corner.png";
	
	var splitURL = window.location.href.split('/');
	var urlEle = splitURL.length - 1;
	if (splitURL[urlEle] == '') urlEle--;
	var thisPage = splitURL[urlEle].split('.')[0];
	var subPage = '';
	
	for (var aSubPage in subPages) {
		if (thisPage == subPages[aSubPage])
		{
			subPage = thisPage;
			thisPage = splitURL[urlEle-1].split('.')[0];
			break;
		}
	}
	
	if (!document.getElementById(thisPage + '_content')) {
		possiblePage = splitURL[urlEle-1].split('.')[0]
		if (document.getElementById(possiblePage + '_content'))
			thisPage = possiblePage;
		else
			thisPage = 'news';
	}
	
	var subPageURL = '/' + subPage;
	if (subPageURL == '/') subPageURL = '';
	
	window.location.href = 'http://' + splitURL[2] + '/#/' + thisPage + subPageURL;
	
	goto(thisPage, subPage);
	
	setTimeout('finishLoad()', 400);
}

function finishLoad() {
	preloadAnimation(false);
	
	var loading = document.getElementById('loading');
	loading.style.display = 'none';
	
	var wrapper = document.getElementById('wrapper');
	wrapper.style.display = '';
}

function goto(page, subPage) {
	if (page == 'external')
	{
		window.location.href = subPage;
		return true;
	}
	
	if (document.getElementById(page + '_content'))
	{
		for (var aPage in pages) {
			thisPage = pages[aPage].toLowerCase();
			thisNav = document.getElementById(thisPage + '_nav');
			thisContent = document.getElementById(thisPage + '_content');
			
			thisNavCurrent = "<a href=\"#/" + thisPage + "\" onclick=\"goto(\'" + thisPage + "\', '');\" class=\"current\">" + pages[aPage] + "</a>";
			thisNavLink = "<a href=\"#/" + thisPage + "\" onclick=\"goto(\'" + thisPage + "\', '');\">" + pages[aPage] + "</a>";
			
			if (thisContent)
				if (page == thisPage) {
					document.title = 'random applications \u00B7 ' + thisPage;
					thisNav.innerHTML = thisNavCurrent;
					if (!subPage ) thisContent.style.display = '';
					else  thisContent.style.display = 'none';
				}
				else {
					thisNav.innerHTML = thisNavLink;
					thisContent.style.display = 'none';
				}
		}
		
		for (aSubPage in subPages) {
			thisSubPage = document.getElementById(subPages[aSubPage] + '_content');
			if (subPage == subPages[aSubPage]) {
				document.title += ' \u00B7 ' + subPage;
				thisSubPage.style.display = '';
			}
			else thisSubPage.style.display = 'none';
		}
	}
	
	return true;
}

function preloadAnimation(spin) {
	if (spin) preloadAnimTimer = setInterval("doPreloadAnimation()", 100);
	else clearInterval(preloadAnimTimer);
}

function doPreloadAnimation(from) {
	document.getElementById("SpinImage").src = '/images/load/'+preloadFrame+'.png';
	preloadFrame++;
	if (preloadFrame > 12) preloadFrame = 1;
}