var getPage = function(url){
	new Ajax(url,{method:'get',onSuccess:function(text,xml){
			location.href=location.href.split("#")[0]+"#"+url.split("/")[1];
			var right = text;
			var title = text;
			//Get Title
			var title, regexp = /<title[^>]*>([\s\S]*?)<\/title>/gi;
			while ((title = regexp.exec(title))){
				$('title').setHTML(title[1]);
				document.title = "The Branching Story | "+title[1];
			}
			//Get Left Side
			var text, regexp = /id="left" [^>]*>([\s\S]*?)<\/div>/gi;
			while ((text = regexp.exec(text))) book.setLeft(text[1]);
			//Get Right Side
			var right, regexp = /id="right" [^>]*>([\s\S]*?)<\/div>/gi;
			while ((right = regexp.exec(right))) book.setRight(right[1]);	
		}.bind(this)
	}).request();
}

var speaking = {
	el: null,
	timer: null,
	say: function(words){
		if(speaking.timer)$clear(speaking.timer);
		speaking.el.setHTML(words);
		speaking.el.effect('opacity').start(1);
		speaking.timer = (function(){speaking.el.effect('opacity').start(0);}).delay(2000);
	}
};
	
window.onDomReady(function(){
	book = new book('leftPage','rightPage');
	if(location.href.split("#")[1]){
		getPage('pages/'+location.href.split("#")[1]);
	}else{
		$ES('a',$('nav'))[0].addEvent('click',function(e){
			//e =  new Event(e).stop();
			getPage('pages/start.html');
		});
	}
		
});