/** section: modules
 * class ContactForm
**/
ContactForm = Class.create({

	// elms
	elmWrapper: undefined,
	formFields: [],
	MessageBox: undefined,

	// controllers
	isSubmiting: false,

	// init
	initialize: function(wrapper) {

		if(!$(wrapper)) throw 'ContactForm: No wrapper element found at #' + wrapper;

		this.elmWrapper = $(wrapper);
		this.MessageBox = new MessageBoxClass('MessageBox');

		// grab form fields
		this.formFields = this.elmWrapper.select('textarea', 'input[type="text"]');

		// observe submit event
		this.elmWrapper.select('form')[0].observe('submit', this.onSubmit.bindAsEventListener(this));

	},

	// callbacks
	onSubmit: function(e) {

		e.stop();

		if(!this.isSubmiting) {

			this.isSubmiting = true;

			// gather params
			var params = '?site=' + encodeURIComponent($F('site_addr')) + '&site_id=' + $F('site_id');
			this.formFields.each(function(field, index) {

				//if(index > 0) params += '&';
				params += '&' + field.id + '=' + encodeURIComponent($F(field));
			});

			// send
			new Ajax.Request($F('site_addr') + 'components/ContactForm_AjaxSendMail.php', {

				method: 'post',
				parameters: params,
				onComplete: this.onAjaxSuccess.bind(this),
				onFailure: this.onAjaxFail.bind(this)

			});

			this.MessageBox.update('loading', 'Poczekaj...', 'wysyłanie wiadomości');
		}
	},
	onAjaxSuccess: function(request) {

		this.isSubmiting = false;

		if(request.responseText.substring(0,5) == 'error') {

			switch(request.responseText) {

				case 'error_badfields': var reason = 'wypełnij wszystkie wymagane pola'; break;
				case "error_bademail": var reason = 'nieprawidłowy adres e-mail'; break;
				case "error_nouser": var reason = 'SYSTEM<br />nieistniejący użytkownik'; break;
				case "error_nosite": var reason = 'SYSTEM<br />nieistniejąca podstrona'; break;

				default: var reason = null;
			}

			this.onAjaxFail(reason);
		}

		else this.MessageBox.update('ok', 'Ok!', 'wiadomość wysłana!', 1450);
	},
	onAjaxFail: function(reason) {

		this.isSubmiting = false;

		if(!reason) reason = 'nie udało się wysłać wiadomości<br />spróbuj ponownie';
		this.MessageBox.update('error', 'Error!', reason, 2150);
	}
});

MessageBoxClass = Class.create({

	elmWrapper: undefined,
	images: [],
	directory: 'images/layout/MessageBox/',

	initialize: function(wrapper) {

		this.elmWrapper = $(wrapper);

		this.images = ['loading', 'error', 'ok'];

		this._preload();
	},

	_preload: function() {

		var preloaded = new Array();
		this.images.each(function(image) {

			preloaded[image] = new Image;
			preloaded[image].src = this.directory + image + '.gif';

		}.bind(this));
	},

	update: function(image,alt,text,delay) {

		this.elmWrapper.innerHTML = '<p><img src="' + this.directory + image + '.gif" alt="' + alt + '" /><br />' + text + '</p>';

		// Check if we need to show the message box or is it already visible
		if(this.elmWrapper.style.display == 'none') new Effect.Appear('MessageBox');

		// Hide the messagebox if any delay is set
		if(delay) setTimeout('Effect.DropOut("' + this.elmWrapper.identify() + '");', delay);
	}


});

// Runtime
Event.observe(window, "load", function() {

	// load contact form
	new ContactForm('contact_form');
	var MessageBox = new MessageBoxClass('MessageBox');
});

