/*
Script:
	book.js, <http://dev.justinmaier.com/book>

Author:
	Justin Maier, <http://justinmaier.com>

License:
	MIT-style license.
*/
var book = new Class({
	options: {
		onPageChange: Class.empty(),
		duration: 1000,
		scriptDelay: 400,
	},
	leftPage: null,
	rightPage: null,
	contentLeft: null,
	contentRight: null,
	initialize: function(leftPage,rightPage,options){
		this.setOptions(options);
		this.leftPage = $('leftPage');
		this.rightPage = $('rightPage');
	},
	lTimer: null,
	rTimer: null,
	evaluateJavascript: function(script){
		var script, regexp = /<script[^>]*>([\s\S]*?)<\/script>/gi;
		while ((script = regexp.exec(script))) eval(script[1]);
	},
	setRight: function(html){
		this.contentRight = html;
		this.rightPage.effect('opacity',{duration:this.options.duration}).start(0).chain(function(){
			this.rightPage.setHTML(this.contentRight);
			this.rightPage.effect('opacity').start(1);
			if(this.rTimer != null)$clear(this.rTimer);
			this.rTimer = (function(){this.evaluateJavascript(this.contentRight);}.bind(this)).delay(this.options.scriptDelay);
		}.bind(this));
	},
	setLeft: function(html){
		this.contentLeft = html;
		this.leftPage.effect('opacity',{duration:this.options.duration}).start(0).chain(function(){
			this.leftPage.setHTML(this.contentLeft);
			this.leftPage.effect('opacity').start(1);
			if(this.lTimer != null)$clear(this.lTimer);
			this.lTimer = (function(){this.evaluateJavascript(this.contentLeft);}.bind(this)).delay(this.options.scriptDelay);
		}.bind(this));
	}
});

book.implement(new Events);
book.implement(new Options);
		