	/**
 * Script part for EasyPoll Snippet.
 * Enables the Poll to be updated via AJAX
 * This was written to work with the mootools library that comes with MODx
 *
 * @author banal
 * @version 0.2 (2008-02-08)
 *
 * REVISION 0.21 (2010-01-01) MODIFIED TO WORK WITH MT-1.2.4 BY STUDIO EFX
 */
var EasyPollAjax = new Class({
	initialize: function(identifier, url){
		if($type(identifier) != 'string' || $type(url) != 'string'){
			alert('EasyPoll Constructor: invalid arguments');
			return;
		};

		this.url = url;
		this.identifier = identifier;
		this.handlers = new Array();

		this.pollxhr = new Request({
			'url': url,
			method: 'post',
			onSuccess: this.callbackHandler.bind(this),
			onRequest: this.callbackHandler.bind(this),
			headers: {"Content-type": "application/x-www-form-urlencoded; charset=utf-8"}
		});

		if($(identifier)){
			$(identifier + 'ajx').value = '1';
		};
	},

	/** register a button to fire a ajax request */
	registerButton: function(button){
		var self = this;
		if(button == 'submit' || button == 'result' || button == 'vote') {
			if(!$(this.identifier + button)) return;
			$(this.identifier + button).addEvent('click', function(e) {
				e.stopPropagation();
				e.preventDefault();
				self.pollxhr.send($(self.identifier + 'form').toQueryString() + '&' + button + '=1');
				return false;
			});
		};
	},

	/** register a callback method that will be called upon request and upon success */
	registerCallback: function(callback){
		if($type(callback) == 'function')
			this.handlers.push(callback);
	},

	/** distributes response from XHR object to the registered callbacks */
	callbackHandler: function(response){
		if(response == undefined){
			this.handlers.each(function(func){
				func(false, this.identifier);
			}.bind(this));
		} else {
			this.handlers.each(function(func){
				func(response, this.identifier);
			}.bind(this));
		}
	}
});

var EasyPoll_DefaultCallback = function(response, id){
	if(response == false){
		$(id + "submit").disabled = true;
	} else {
		$(id).set('html', response);
	}
}