s.login = function(){
	this.holder 		 = $$('div.login')[0];

	this.logout_form 	 = $$('form.logout_form', this.holder)[0];

	this.login_form 	 = $$('form.login_form', this.holder)[0];
	this.login_holder 	 = $$('strong', this.holder)[0];
	this.login_input 	 = $$('input#input-login', this.holder)[0];

	this.populated_field = $('id_sender');
	this.populated_field_errorlist = (this.populated_field) ? $$('li',this.populated_field.parentNode)[0] : null;

}

s.login.prototype.append = function(){
	y.util.Event.addListener(this.login_form, "submit", function(event){
		y.util.Event.preventDefault(event);
		y.util.Connect.setForm(this.login_form);
		y.util.Connect.asyncRequest('POST', this.login_form.action, this.login_callback(this));
	},this,true);

	y.util.Event.addListener(this.logout_form, "submit", function(event){
		y.util.Event.preventDefault(event);
		y.util.Connect.asyncRequest('POST', this.logout_form.action, this.logout_callback(this));
	},this,true);
}

s.login.prototype.login_callback = function(obj){
	return {
		'customevents':{
			'onStart': function(eventType, args) {
				s.notifications.show(dict['login_progress'] ,'wait');
			},
			'onSuccess': function(eventType, args) {
				try {
					response = y.lang.JSON.parse(args[0].responseText);
					if (response.status == 1) {
						obj.display_logout_form();
						s.notifications.hide();
					} else {
						s.notifications.show(response.message,'info');
					}
				}
				catch (error) {
					s.notifications.show(dict['ajax_error'] + ': '+error,'error');
				}
			},
			'onFailure': function(eventType, args) {
				s.notifications.show(dict['ajax_error'] + ': '+error,'error');
			}
		}
	}
}

s.login.prototype.logout_callback = function(obj){
	return {
		'customevents':{
			'onStart': function(eventType, args) {
				s.notifications.show(dict['logout_progress'],'wait');
			},
			'onSuccess': function(eventType, args) {
				try {
					response = y.lang.JSON.parse(args[0].responseText);
					if (response.status == 1) {
						obj.display_login_form();
						s.notifications.hide();
					} else {
						s.notifications.show(response.message,'info');
					}
				}
				catch (error) {
					s.notifications.show(dict['ajax_error'] + ': '+error,'error');
				}
			},
			'onFailure': function(eventType, args) {
				s.notifications.show(dict['ajax_error'] + ': '+error,'error');
			}
		}
	}
}

s.login.prototype.display_logout_form = function(){
	y.util.Dom.addClass(this.login_form, 'hidden');
	y.util.Dom.removeClass(this.logout_form, 'hidden');
	this.login_holder.innerHTML = this.login_input.value;
	if (this.populated_field){
		this.populated_field.value = this.login_holder.innerHTML;
		if (this.populated_field_errorlist){
			y.util.Dom.setStyle(this.populated_field_errorlist,'display','none');
		}
	}
	this.login_input.value = '';
}

s.login.prototype.display_login_form = function(){
	y.util.Dom.addClass(this.logout_form, 'hidden');
	y.util.Dom.removeClass(this.login_form, 'hidden');
	if (this.populated_field && this.populated_field.value == this.login_holder.innerHTML){
		this.populated_field.value = '';
		if (this.populated_field_errorlist){
			y.util.Dom.setStyle(this.populated_field_errorlist,'display','');
		}
	}
	this.login_holder.innerHTML = '';
}