//-------------------------------------------------------------------------------------
//	JibberBook v1.0
//	(c) 2007 Chris Jaure
//	JibberBook is freely distributable under the terms of an MIT-style license.
//	website: http://www.chromasynthetic.com/
//
//	jbscript.js
//-------------------------------------------------------------------------------------

var guestbook = {
	effectHeight : '',
	effectScroll : '',
	effectError : '',
	objects : {},
	ajaxReq : '',
	submitForm : function (event) {
		this.loading();
		this.objects.form.elements['_ajax'].value = true;
		this.ajaxReq.send(this.objects.form.getProperty('action'), this.objects.form.toQueryString());
		event.stop();
	},
	processAddResponse : function () {
		var response = Json.evaluate(this.ajaxReq.response.text);
		if (response.value != '1') {
			this.objects.errorText.setStyle('display', 'block')
				.setText(response.content);
			this.effectError.start(0).chain(
				function(){
					this.start(1);
				}).chain(
				function(){
					this.start(0);
				}).chain(
				function(){
					this.start(1);
				})
		}
		else {
			this.objects.commentContainer.innerHTML += response.content;
			this.objects.errorText.setStyle('display', 'none');
			this.objects.form.reset();
			this.effectScroll.toBottom();
		}
		this.loaded();
	},
	loading : function () {
		this.objects.submitButton.setProperties({'disabled': true, 'value': 'Loading...'});
	},
	loaded : function () {
		this.objects.submitButton.setProperties({'disabled': false, 'value': this.objects.submitButton.orgVal});
	},
	scrollUp : function () {
	},
	initialize : function (form, container, error) { // accepts form id, comment wrapper id, and error wrapper id
		this.objects.form = $(form).addEvent('submit', this.submitForm.bindWithEvent(this));
		this.objects.commentContainer = $(container);
		this.objects.errorText = $(error);
		this.objects.submitButton = this.objects.form.getElement('input[type=submit]');
		this.objects.submitButton.orgVal = this.objects.submitButton.value;
		this.effectError = new Fx.Style(this.objects.errorText, 'opacity', {duration:200});
		this.effectScroll = new Fx.Scroll(this.objects.commentContainer, {'duration': 1000, 'transition': Fx.Transitions.Circ.easeInOut}); 
		this.ajaxReq = new XHR({method: 'post', 'onSuccess': this.processAddResponse.bind(this), 'onFailure': function () {
			this.objects.errorText.setText('Your comment could not be added. Please try again later.');
			this.loaded();
		}.bind(this)});
	}
	
};
