var AccountSignin = new Class({

	// extensions & implementations
	Extends: EcobotForm,
	Implements: [Events, Options],

	// instance properties
	formEl: null,
	submitEl: null,
	validationUrl: '/account/signin/validate',
	
	// constructor
	initialize: function () {
		
		// reference the core elements
		this.formEl = $('signin');
		this.submitEl = $('signin-submit');
		
		// add event to the form itself
		this.formEl.addEvent('submit', this.validate.bind(this));
		
		// add events to the submit button
		this.submitEl.addEvent('click', this.validate.bind(this));
		this.submitEl.addEvent('mouseenter', onImageMouseEvent);
		this.submitEl.addEvent('mouseleave', onImageMouseEvent);
			
	},
	
	// methods
	validate: function (e) {
	
		// collect values
		var out = new Hash();
		
		this.formEl.getElements('.use').each(function (el, index) {
			out.set(el.name, el.value);
		}.bind(this));
	
		// send for validation
		new Request.JSON({ url:this.validationUrl, method:'post', onComplete:this.onValidated.bind(this) }).send(out.toQueryString());
	
		// stop the default action
		e.preventDefault();
		
	},
	
	// event handlers
	onValidated: function (json, text) {

		// determine if there was an error
		if (json.error == 0) {
		
			// no error detected; submit the form
			this.formEl.submit();
		
		} else {
		
			// there was an error; define bitwise flags
			var EMAIL_INVALID = 8;
			var EMAIL_NOT_FOUND = 32;
			var PASSWORD_INCORRECT = 256;		
		
			// remove all `error` class assignments
			this.formEl.getElements('input.error').each(function (el, index) {
				el.removeClass('error');
				el.getNext().destroy();
			});
		
			// remove all existing hints/error messages
			this.formEl.getElements('.hint').each(function (el, index) {
				el.destroy();
			});
		
			// reference all the fields
			var inputs = [$('signin-email'),
						  $('signin-passwd')];

			// show error messages where applicable		
			if (json.error & EMAIL_INVALID) {
				inputs[0].addClass('error');
				this.hint(inputs[0], json.hints[EMAIL_INVALID]);
			} else if (json.error & EMAIL_NOT_FOUND) {
				inputs[0].addClass('error');
				this.hint(inputs[0], json.hints[EMAIL_NOT_FOUND]);
			} else if (json.error & PASSWORD_INCORRECT) {
				inputs[1].addClass('error');
				this.hint(inputs[1], json.hints[PASSWORD_INCORRECT]);
			}
		
		}			

	}	
	
});