var SignInInterface = Class.create();

SignInInterface.prototype = ({

	initialize: function (config)
	{		
		this._Container  = config.container;
		this._login		 = config.login_field;
		this._loginValue = config.login_default_value;		
		
		this._password	 	= config.password_field;
		this._passwordValue = config.password_default_value;
		
		this._forgot		= config.forgot_field;
		
		//this._signInContainer = config.container;
		this._signinBlockId 			= config.signin_block;
		this._forgotBlockId 			= config.forgot_block;
		this._signInButtonActiveId 		= 'signin-button-active';
		this._signInButtonInactiveId 	= 'signin-button-inactive';
		
		this.ajaxLoadingEffect		= null;		
		
		Event.observe(document, 'dom:loaded', this.setCatchers.bindAsEventListener(this));
	},

	setCatchers: function()
	{
		var signInButtons = $$('a[rel="signin"]');
		signInButtons.each(function(el) { el.onclick = function() { return false } } );
		signInButtons.invoke('observe','click',this._showSignInForm.bindAsEventListener(this));
		//Event.observe($(this._login), 'focus', this._clearDefaultLoginValue(this) );	
		//Event.observe($(this._password), 'focus', this._clearDefaultPasswordValue(this) );	
		
		
		var forgotPasswordButtons = $$('a[rel="forgot_password"]');
		forgotPasswordButtons.each(function(el) { el.onclick = function() { return false } } );
		forgotPasswordButtons.invoke('observe','click',this._showForgotPasswordInForm.bindAsEventListener(this));
		this.initAjaxForm('signin_form');
		this.initAjaxForm('forgot_password_form');		
		
		if (Prototype.Browser.IE)
		{
			this.bindSubmitByEnter();
		}
		
		this.setValue(this._login, this._loginValue);
		this.setValue(this._password, this._passwordValue);
		this.setValue(this._forgot, this._loginValue);
	},
	
	_showSignInForm: function(objEvent)
	{
		var elA = Event.findElement(objEvent,'a');
		$(this._Container).show();
		$(this._forgotBlockId).hide();
		$(this._signinBlockId).show();
		//$(this._login).focus();
		$(this._signInButtonActiveId).up('a').hide();
		$(this._signInButtonInactiveId).show();
	},
	
	_clearDefaultLoginValue: function(objEvent)
	{		
		if( $(this._login).value == this._loginValue){
			this._setValueToField(this._login, '');
		}
	},
	
	_clearDefaultPasswordValue: function(objEvent)
	{
		if( $(this._password).value == this._passwordValue){
			this._setValueToField(this._password, '');
		}		
	},
	
	_showForgotPasswordInForm: function(objEvent)
	{
		$(this._Container).show();
		$(this._signinBlockId).hide();
		$(this._forgotBlockId).show();
		$(this._signInButtonInactiveId).hide();
		$(this._signInButtonActiveId).up('a').show();		
		
	},
	
	_setValueToField: function(fieldName, fieldValue)
	{
		$(fieldName).value = fieldValue;
	},
	
	initAjaxForm: function(formName)
	{
		var form = ($(formName));
		if(form)
		{			
			Event.observe(form, 'submit', this.sendForm.bindAsEventListener(this));
		}
	},
	
	sendForm: function(e)
	{	
		Event.stop(e);
		var f = e.element();
		f = ('FORM' == f.tagName) ? f : f.up('form');		
		var formParams = f.serialize();
        this.setLoadingEffect('front-top-menu');

        if (f.method == 'post')
        {
            new Ajax.Request(f.action, {
                //onCreate: this.onCreateLoading.bind(this),
                //onFailure: this.onFailureLoading.bind(this),
                'onSuccess':this.successLoading.bind(this),
                'postBody':formParams,
                'evalJS':true
            });
        }
        else
        {
            new Ajax.Request(f.action+'?'+formParams, {
                //onCreate: this.onCreateLoading.bind(this),
                //onFailure: this.onFailureLoading.bind(this),
                'onSuccess':this.successLoading.bind(this)
            });
        }
        return false;
		
	},
	
	successLoading: function (response)
    {
		this.removeLoadingEffect();
		$('front-top-menu').update(response.responseText);
		this.setCatchers();
        return false;
    },	
	
	setLoadingEffect: function(objContainer)
    {
		if(this.ajaxLoadingEffect == null)
		{
			this.ajaxLoadingEffect = new AjaxLoadingEffect({'containerId':objContainer,cssLoadingEffect: 'winLoadingEffectTopMenu'});
		}
		this.ajaxLoadingEffect.setLoadingEffect();
	},
	
	removeLoadingEffect: function()
    {
		if(this.ajaxLoadingEffect != null){
			this.ajaxLoadingEffect.removeLoadingEffect();
		}	
    },
    
	processSubmitByEnter: function(e) {		
		e = e || window.event;		
        if (e.keyCode == 13) {        	
        	this.sendForm(e);
            return false;            
        }
	},
    
    bindSubmitByEnter: function() {
    	$(this._Container).select('input').each(function(el) {    			
    			Event.observe(el, 'keydown', this.processSubmitByEnter.bindAsEventListener(this));
    		}.bind(this)
    	);
    },
    
    setValue: function(id, string) {
    	
    	var el = $(id);
    	
    	if (!el) {
			return;
    	}
    	
		if (el.value.blank())
    	{
    		el.value = string;  		    		
		}
    	
		el.observe('focus', function(el, string) {		
			el.value = (string == el.value) ? '' : el.value;
			}.bind(this, el, string)
		);
    	
		el.observe('blur', function(el, string) {    		
			el.value = ('' == el.value) ? string : el.value;
			}.bind(this, el, string)
		);
	}	 
});