Ajaxify = Class.create({

	initialize: function(form, success, options, scrollTo) {
		
		this.form = form;
		this.success = success;
		this.scrollTo = scrollTo;
		this.url = $(form).getAttribute("action");
		
		this.options = Object.extend({
									loading: 'image',
									successTransition: 'appear'
									}, options || {});


		$(form).observe('submit', this.request.bind(this));

		},

	startLoading: function() {
		switch (this.options.loading) {
			
			case 'image' :
				$(this.form + '_loading').show();
				break;

			default:
				$(this.form + '_loading').show();
				break;
		
			}
		},

	stopLoading: function() {
		switch (this.options.loading) {
			
			case 'image' :
				$(this.form + '_loading').hide();
				break;

			default:
				$(this.form + '_loading').hide();
				break;
		
			}
		},

	successMessage: function() {
		
		switch (this.options.successTransition)	{
			case 'show':
				$(this.success).show();
				break;

			case 'appear':
				new Effect.toggle(this.success, 'appear');
				break;

			case 'blind':
				new Effect.toggle(this.success, 'blind');
				break;
		
			}
		
		new Effect.ScrollTo('scroll_success');
		$(this.form).disable();
		
		},

	request: function(event) {
		
		var form_ = this.form;
		var success_ = this.success;
		var scrollTo_ = this.scrollTo;

		new Ajax.Request(
				this.url,
				{
						method: "post",
						parameters: $(this.form).serialize() + '&ajax=true', // send ajax parameter to confirm js activated
						onLoading: $(this.form + '_loading').show(),
						onComplete: function(transport) {
								//alert(transport.responseText);
								var response = eval('(' + transport.responseText + ')'); // get JSON response
								var properties = new Array();
								var i = 0;
								for (property in response) { //get object properties
									properties[i] = property;
									i++;
									}
								var state = "ok"; // control variable
								for (i = 0; i < properties.length; i++) {
									if (eval("response." + properties[i]) == 'error') { //if current field has error
										if (state == "ok") { // switch control variable
											state = "error";
											$(properties[i]).focus(); // focus first error input
											}
										$(properties[i] + "_info").addClassName('error');
										} else {
											$(properties[i] + "_info").removeClassName('error');
											}
									}
								$(form_ + '_loading').hide();
								if (state == "ok") { // success
									new Effect.toggle(success_, 'blind');
									new Effect.ScrollTo(scrollTo_);
									$(form_).disable();
									}
							}
				}
				);
				Event.stop(event);

		}
	
	});