/*  Object AuthForm
/*--------------------------------------------------------------------------*/
var AuthForm = {
	init: function (container, form, message, preloaderImage) {
		container = $(container);
		form = $(form);
		message = $(message);
		
		if (form) form.onsubmit = this.onSubmit.bind(this);
		
		this.url = '/ajax/login/';
		this.container = container;
		this.form = form;
		this.message = message;
		this.preloaderImage = preloaderImage;		
		this.lightBox = null;
	},
	
	show: function () {
		if (this.lightBox) {
			if (this.container) this.container.show();
		}else {
			var lightBox = this.createLightBox();
			
			lightBox.options.onClose = function () { 
				this.close(); this.lightBox = null;				
			}.bind(this);
			
			[lightBox, this.container].invoke('show');
			
			this.lightBox = lightBox;
		}
	},
		
	close: function () {
		if (this.container) this.container.hide();
		this.reset();
	},
	
	cancel: function () {
		if (this.lightBox) this.lightBox.close();
		this.lightBox = null;
	},
	
	onSubmit: function () {
		var preloader = (this.preloaderImage || '').load(
			this.container
		).addClassName('form_preloader');
		
		new Ajax.Updater(this.message, this.url, {					
			evalScripts: true,
			method: 'post',
			parameters: this.form.serialize(),
			onComplete: function () { preloader.remove(); }
		});		
		return false;
	},
	
	createLightBox: function () {
		return new HightLight({
			isShowEffect: false,
			isCloseEffect: false
		});
	},
	
	reset: function () {
		if (this.message) this.message.update('');
		if (this.form) this.form.reset();
	}
}
/*  Object RememberForm
/*--------------------------------------------------------------------------*/
var RememberForm = {
	init: function (container, form, message, preloaderImage) {
		container = $(container);
		form = $(form);
		message = $(message);
		
		if (form) form.onsubmit = this.onSubmit.bind(this);
				
		this.url = '/ajax/remember/';
		this.container = container;
		this.form = form;
		this.message = message;
		this.preloaderImage = preloaderImage;		
		this.lightBox = null;
		this.oldMessageText = this.message ? this.message.innerHTML : '';
	},
	
	show: function () {
		if (this.container) this.container.show();
		
		var lightBox = AuthForm.lightBox;
		if (lightBox) {
			var oldClose = lightBox.options.onClose;
			
			lightBox.options.onClose = function () { 
				if (oldClose) oldClose();
				this.close(); this.lightBox = null;				
			}.bind(this);
			
			this.lightBox = lightBox;
		}
	},
	
	close: function () {
		if (this.container) this.container.hide();
		this.reset();
	},
	
	cancel: function () {
		if (this.lightBox) this.lightBox.close();
		this.lightBox = null;
	},
	
	onSubmit: function () {
		var preloader = (this.preloaderImage || '').load(
			this.container
		).addClassName('form_preloader');
		
		new Ajax.Updater(this.message, this.url, {					
			evalScripts: true,
			method: 'post',
			parameters: this.form.serialize(),
			onComplete: function () { preloader.remove(); }
		});		
		return false;
	},
	
	reset: function () {
		if (this.message) this.message.update(this.oldMessageText);
		if (this.form) this.form.reset();
	}
}
/*  Object FormLogin
/*--------------------------------------------------------------------------*/
var FormLogin = {
	LOGIN_ERROR_VALUE: 		1,
	LOGIN_EXISTS: 			2,
	LOGIN_SUCCESS: 	   		3,
	LOGIN_CHECK:	   	   	4,
	LOGIN_REQUEST_FAILURE: 	5,
		
	element: null,
	url:	 '/ajax/login_check/',	
	_timer:  null,
	
	init: function (element) {
		this.element = $(element);
		
		this.setEvent();				
		this.check();
	},
				
	check: function (event) {
		if (this.element.present()) {
			this.message(this.LOGIN_CHECK);
			
			if (this._timer) {
				this._timer.stop();
			}
			this._timer = new PeriodicalExecuter(function (timer) {
				this.request();
				timer.stop();
				this._timer = null;
			}.bind(this), 1);
		}
	},
				
	request: function () {		
		(new Ajax.Request(this.url, {
			postBody: 'name='+ this.element.value,
			onSuccess: function (transport) {				
				if (/^[0-9]+$/.test(transport.responseText)) {
					this.message(parseInt(transport.responseText));					
				}else {
					this.message(null);
				}
			}.bind(this),
			onFailure: function () {
				this.message(this.LOGIN_REQUEST_FAILURE);
			}.bind(this)
		}));
	},
				
	setEvent: function () {
		Event.observe(this.element, 'keypress', this.check.bindAsEventListener(this));
	},
				
	message: function (type) {
		var message = this.createMessageBox();
		switch(type) {
			case this.LOGIN_ERROR_VALUE:
				message.update('Некорректное имя логина.').className = 'login_message_error';
				break;							
			case this.LOGIN_EXISTS:
				message.update('Логин с таким именем уже существует.').className = 'login_message_error';
				break;
			case this.LOGIN_CHECK:
				message.update('Идет проверка&hellip;').className = 'login_message';
				break;
			case this.LOGIN_SUCCESS:
				message.update('Логин с таким именем свободен.').className = 'login_message_success';
				break;
			case this.LOGIN_REQUEST_FAILURE:
				message.update('Ошибка проверки логина.').className = 'login_message_error';
				break;
			default:
				message.update('Ошибка проверки логина.').className = 'login_message_error';
				break;
		}
	},
				
	createMessageBox: function () {
		var element = $('login_message');
		if (!element) {
			var div = $(document.createElement('div'));
			div.setId('login_message').addClassName('login_message');
			this.element.parentNode.appendChild(div);
			element = div;
		}
		return element;
	}
}
/*  Object UserEmailListForm
/*--------------------------------------------------------------------------*/
var UserEmailListForm = {
	preloaderImage: '/f/form_loader.gif',
	formUrl: '/ajax/user/email_list_form/',	
	loading: false,
	loaded: false,
	
	load: function (onComplete) {
		this.loading = true;
		
		var preloader = this.preloaderImage.load(
			$('user_email_list').down('span.preloader')
		);
		
		return new Ajax.Updater($('user_email_list_frame'), this.formUrl, {					
			evalScripts: true,
			method: 'post',
			onComplete: function () {
				this.loading = false;
				this.loaded = true;
				
				preloader.remove();
				
				if (onComplete) {
					onComplete();
				}
			}.bind(this)
		});
	},
	
	show: function () {
		if (!this.loaded) {
			if (!this.loading) {
				this.load(function () {
					$('user_email_list_frame')
					.setTop($('user_email_list')
					.getTop()).setLeft($('user_email_list').getLeft())
					.show();					
				}.bind(this));
			}
		}else {
			$('user_email_list_frame').show();
		}
		
		return false;
	},
	
	hide: function () {
		if (!this.loading) {
			$('user_email_list_frame').hide();
		}
		
		return false;
	},
	
	toggle: function () {
		this[$('user_email_list_frame').visible()?'hide':'show']();
		
		return false;
	},	
	
	disable: function () {
		$('user_email_list_form').disable().getInputs('submit').invoke('addClassName', 'disabled');
	},
	
	enable: function () {
		$('user_email_list_form').enable().getInputs('submit').invoke('removeClassName', 'disabled');
	},
	
	onSubmit: function () {
		var preloader = this.preloaderImage.load(
			$('user_email_list_frame')
		).addClassName('form_preloader');
		
		var request_options = {
			evalScripts: true,
			method: 'post',
			parameters: $('user_email_list_form').serialize(),
			onComplete: function () { preloader.remove(); }			
		};
		
		this.disable();
		
		(new Ajax.Updater($('user_email_list_frame'), this.formUrl, request_options));
		
		return false;
	},
	
	editEmail: function (element, id) {
		if (element.checked) {
			element.form.email_address.value = element.up().next().down().firstChild.nodeValue; 
			element.form.email_id.value = id;
		}else if (id == element.form.email_id.value) {
			element.form.email_address.value = '';
			element.form.email_id.value = '';
		}
	}
}