/*===============================================================================
	AJAXCheckbox.js
	John Larson
	3/03/08
	
	Component for making checkboxes update to the server in realtime.
	
	Example:
	<input type="checkbox" name="loveIt" value="true" id="myCheckbox">Love it!
	
	new AJAXCheckbox($('myCheckbox'), 'AJAXAction.asp?a=updateLoveIt', {method: 'get'});
	
	would cause a AJAX request to 'AJAXAction.asp?a=updateLoveIt' every time
	the checkbox checked value was changed with the data of
	"loveIt=true" if checked and "loveit=" if not.
===============================================================================*/

var AJAXCheckbox = new Class({
	
	// These options are passed directly as AJAX options
	options: {
		method:				'post'
	},

	initialize: function(checkboxElement, saveURL, options){
		this.setOptions(options)
		this.checkboxElement = $(checkboxElement);
		if(!this.checkboxElement)
			throw('Non-existent checkbox passed to AJAXCheckbox constructor.');
		
		this.saveURL = saveURL;
		this.checkboxElement.addEvent('click', function() {
			var thisData = this.checkboxElement.name + '=' +
					(this.checkboxElement.checked ? this.checkboxElement.value : '');
			new Ajax(this.saveURL, 
					 $extend(this.options, {data: thisData})
			).request();
		}.bind(this));
		
	}
});
	
AJAXCheckbox.implement(new Options, new Events);