// JavaScript Document
var Page = new Class({
	Implements: [Events, Chain],
	
	currentPage: 'notNULL',
	pageState: null,
	
	requests: [],

	setTopNavigation: null,
	setBottomNavigation: null,
	setSearchForm: null,

	initialize: function(currentPage, pageState) {
		this.addEvent('onDomReady', this.onDomReady);
		this.addEvent('beforeContentUpdate', this.beforeContentUpdate);
		this.addEvent('afterContentUpdate', this.afterContentUpdate);
		
		this.setTopNavigation = this.setBlock.pass(['abNavWrapper', 'topNavigation'], this);
		this.setBottomNavigation = this.setBlock.pass(['abFooterNavWrapper', 'bottomNavigation'], this);
		this.setSearchForm = this.setBlock.pass(['divSearchForm', 'searchForm'], this);
		
		this.currentPage = currentPage;
		this.pageState = pageState;
	},
	
	setBlock: function(div, responseClass, cufonReplace) {
		cufonReplace = $defined(cufonReplace) ? cufonReplace : true;
		
		var divElement = $(div);
		
		//if (cufonReplace) Cufon.replace('.' + responseClass);
		
		this.fireEvent('beforeContentUpdate', divElement.retrieve('responseClass'));
		
		var responseContent = $$('#responseContent .' + responseClass)[0];
		responseContent.dispose();
		responseContent.setStyle('display', 'block');
		
		divElement.empty();
		divElement.adopt(responseContent);
		divElement.store('responseClass', responseClass);
		
		
		this.callChain();
		this.fireEvent('afterContentUpdate', responseClass);
	},
	
	addRequest: function(request) {
		this.requests.push(request);
	},
	sendRequests: function(empty) {
		empty = $defined(empty) ? empty : false;
		
		var fn = this.onCompleteHandler.bind(this);
		
		if (empty) {
			this.requests = [];
			fn = $empty;
		}
		
		request = {
			currentPage: this.currentPage,
			pageState: JSON.encode(this.pageState),
			requestChain: JSON.encode(this.requests)
		};
		new Request.JSON({url: 'lib/server/page.php', onComplete: fn}).get(request);
		this.requests = [];
	},
	onCompleteHandler: function(response) {
		this.currentPage = response.currentPage;
		this.pageState = response.pageState;
		
		var responses = response.responseChain;
		
		var updates = {
			content: new Element('div', {
				id: 'responseContent',
				styles: {'display': 'none'}
			}),
			scripts: []
		}
		
		responses.each (
			function(response) {
				if ($type(response.html) == 'object') new Hash(response.html).each (
					function(val, key) {
						var child = new Element('div', {
							'class' : key,
							styles: {
								display: 'none'
							}
						});
						child.set('html', val);
						this.adopt(child);
					},
					updates.content
				);
				
				if ($type(response.script) == 'array') response.script.each (
					function(val) {
//						this.push(eval.pass(val));
						this.push($exec.pass(val));
					},
					updates.scripts
				);
			},
			updates
		);
		
		if (updates.content.childNodes.length > 0) updates.content.replaces($('responseContent'));
		
		this.chain(updates.scripts);
		this.callChain();
	},
	
	onDomReady: $empty,
	beforeContentUpdate: $empty,
	afterContentUpdate: $empty
});