var AjaxSubmit = new Class({
	
	Extends: Options,
	options: {
		url: '',
		button: null,
		onSuccess: null,
		response: /^ok$/
	},
	
	initialize: function(form, options) {
		this.form = typeof(form) == 'string' ? $(form) : form;
		this.setOptions(options);
		
		var button = this.options.button ? $(this.options.button) : null;
		if (button) {
			button.disabled = true;
			button.val = button.value;
			button.value = 'please wait...';
		}
			
		this.form.set('send', { method: 'post', url: this.options.url, onComplete: this.onComplete.bind(this) }).send();
	},
	
	onComplete: function(response) {
		var button = this.options.button ? $(this.options.button) : null;
		if (button) {
			button.disabled = false;
			button.value = button.val;
		}
		
		if (response.trim().match(this.options.response)) {
			if (this.options.onSuccess)
				this.options.onSuccess(response.trim());
		}
		else
			alert(response.trim());
	}
	
});

function preload() {
	if (typeof(document.pImages) == 'undefined') document.pImages = [];
	for (var i = 0; i < arguments.length; i++)
		document.pImages.push(new Element('img', { src: arguments[i] }));
}

function leveledHeight(selector) {
	var maxHeight = 0;
	$$(selector).each(function(elem) {
		if (maxHeight < elem.getSize().y)
			maxHeight = elem.getSize().y;
	});
	$$(selector).setStyle('height', maxHeight);
}

function ajaxLoading(id) {
	if ($(id) != null)
		$(id).set('html', '<img src="images/ajax-loader.gif" alt="loading...">');
}