actionDuration = 750;
currentMode = "home";
sitelock = false;

siteLookupTable = {'cafepress':'http://cafepress.com/khakionion',
	'resume':'http://khakionion.com/imagedump/Resume.pdf',
	'blogger':'http://khakionion.blogspot.com',
	'twitter':'http://twitter.com/khakionion',
	'facebook':'http://facebook.com/khakionion',
	'linkedin':'http://www.linkedin.com/in/khakionion',
	'gamertag':'http://live.xbox.com/en-US/profile/profile.aspx?pp=0&GamerTag=Khakionion'
}

initPage = function() 
{
	//mootools special effects initialization
	fxUpperhalf = new Fx.Tween($("Upperhalf"),{property:'height',link:'ignore',duration:actionDuration,unit:'px'});
	fxLowerhalf = new Fx.Tween($("Lowerhalf"),{property:'height',link:'ignore',duration:actionDuration,unit:'px'});
	fxSitename = new Fx.Tween($("Sitename"),{property:'left',link:'ignore',duration:actionDuration,unit:'%'});
	fxRandomquote = new Fx.Tween($("Randomquote"),{property:'opacity',link:'chain',duration:actionDuration});
	fxActualcontent = new Fx.Tween($("Actualcontent"),{property:'opacity',link:'chain',duration:actionDuration,onComplete:checkForUnlock});
	fxSitelinks = new Fx.Tween($("Sitelinks"),{property:'left',link:'ignore',duration:actionDuration,unit:'%'});
	
	//eh, it's a hack but I'm lazy
	newContent = 0;

	//base page dims on browser size
	initDims();
	window.onresize = initDims;

	//fading hovers for the badges
	$each($$("#Sitebadges img"),siteBadgeInit);
	//random witticisms in the lower right
	setRandomQuote();
	fxRandomquote.set(0);
	setTimeout(cycleRandomQuote,1000);

	//initial pos for dynamic elems
	fxSitename.set(30);
	fxSitelinks.set(40);
	loadContent("home",true);
}

window.addEvent('domready', initPage);

checkForUnlock = function() {
	if(sitelock && $("Actualcontent").style.opacity == 1)
	{
		sitelock = false;
	}
}

siteBadgeInit = function(item, index, object) {
	setTimeout(function(){item.parentNode.parentNode.getElement(".Icontitle").fade('out')},$random(2000,3100));
	item.addEvent('mouseover',function(event){item.parentNode.parentNode.getElement(".Icontitle").fade('in')});
	item.addEvent('mouseout',function(event){item.parentNode.parentNode.getElement(".Icontitle").fade('out')});
}

setRandomQuote = function() {
	$("Randomquote").textContent = randomQuotes[$random(0,randomQuotes.length-1)];
}

goHome = function() {
	changeMode("home");
	loadContent("home",true);
}

loadContent = function(name,home) {
	if(sitelock)
		return;
	sitelock = true;
	if(!home)
		changeMode("navigation");
	contentRequest = new Request({url:"xml/"+name+".xml",
									 link:"cancel",
									 onSuccess:parseNewContent});
	contentRequest.send();
	setTimeout(displayContent,actionDuration);
	fxActualcontent.start(0);
}


parseNewContent = function(responseText,responseXML) {
	newContent = new Element('div');
	newTitle = new Element('div');
	newTitle.setAttribute('class','Contenttitle');
	newTitle.setStyle('color',responseXML.firstChild.getAttribute("color"));
	newTitle.set('text',responseXML.firstChild.getAttribute("title"));
	newTitle.inject(newContent);
	newIntro = new Element('div');
	newIntro.setAttribute('class','Contentintro');
	newIntro.set('text',responseXML.firstChild.getElementsByTagName("intro")[0].textContent);
	newIntro.inject(newContent);
	newItems = responseXML.getElementsByTagName("item");
	for(i = 0;i<newItems.length;i++)
	{
		item = new Element('div');
		item.setAttribute('class',"Contentitem");
		item.set('text',newItems[i].textContent);
		if(newItems[i].getAttribute("title"))
		{
			tagName = 'span';
			styleText = '';
			//allow for some items to "float" to better use space
			if(newItems.length > 7)
			{
				styleText = 'float:left;padding-right:15px;';
			}
			else
			{
				new Element('br').inject(item,"top");
			}
			//allow for items to be links
			if(newItems[i].getAttribute("href"))
			{
				tagName = 'a';
			}
			new Element(tagName, {'href':newItems[i].getAttribute("href"),'text': newItems[i].getAttribute("title"),'style':styleText,'class':"Itemtitle"}).inject(item,"top");
		}
		if(newItems[i].getAttribute("icon"))
		{
			new Element('img', {'src': newItems[i].getAttribute("icon"),'class':"Itemicon"}).inject(item,"top");
		}
		item.inject(newContent);
	}
}

displayContent = function() {
	if(newContent == 0)
	{
		return setTimeout(displayContent,250);
	}
	$("Actualcontent").erase('html');
	$("Actualcontent").set('html',newContent.get('html'));
	fxActualcontent.start(1);
	newContent = 0;
}

cycleRandomQuote = function() {
	setTimeout(setRandomQuote,actionDuration);
	fxRandomquote.start(0);
	fxRandomquote.start(1);
	setTimeout(cycleRandomQuote,$random(10000,15000));
}

initDims = function() {
	windowHeight = window.innerHeight;
	if(currentMode == "home")
	{
		fxUpperhalf.set(windowHeight/2);
		fxLowerhalf.set(windowHeight/2);
	}
	else if(currentMode == "navigation")
	{
		fxUpperhalf.set(windowHeight-100);
		fxLowerhalf.set(100);
	}
	else if(currentMode == "about")
	{
		fxUpperhalf.set(windowHeight-75);
		fxLowerhalf.set(75);
	}
}

changeMode = function(mode) {
	if(mode == "navigation")
	{
		currentMode = mode;
		fxUpperhalf.start(windowHeight-100);
		fxLowerhalf.start(100);
		fxSitename.start(2);
		fxSitelinks.start(15);
	}
	else if(mode == "home")
	{
		currentMode = mode;
		fxUpperhalf.start(windowHeight/2);
		fxLowerhalf.start(windowHeight/2);
		fxSitename.start(30);
		fxSitelinks.start(40);
	}
	else if(mode == "about")
	{
		currentMode = mode;
		fxUpperhalf.start(75);
		fxLowerhalf.start(windowHeight-75);
		fxSitename.start(15);
		fxSitelinks.start(30);
		fxActualcontent.start(0);
	}
}

switchSite = function(site) {
	location.href = siteLookupTable[site];
}