/**
 * Javascript functions
 *
 * @package         eelst
 * @subpackage      login.js
 * @author          =undo= <g.fazioli@saidmade.com>
 * @copyright       Copyright (C) 2010 Saidmade Srl
 * @version         1.0.0
 */

var EELST_Login = {
    /**
     * Version
     */
    version : '1.0.0',

    /**
     * RegExp Filter
     */
	filter		: /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i,

    /**
     * Buildin Constructor/init
     */
    init : function() {

        // Da il fuoco al input text della email
        if(typeof EELST_Registration == 'undefined') {
            $('input#eelst_username').focus();
        } 

        // Hook callback on submit form
		$('form#eelst_form-login').submit(
			function() {
				if( $('form#eelst_form-login input#eelst_username').val() != 'admin' ) {
					if( $('form#eelst_form-login input#eelst_username').val() == '' || !EELST_Login.filter.test( $('form#eelst_form-login input#eelst_username').val() ) ) {
						$('form#eelst_form-login input#eelst_username').css('backgroundColor','red').stop().animate( {backgroundColor: '#fff'}, {queue:false,duration:1000} ).focus();
						return false;
					}
				}
				if( $('form#eelst_form-login input#eelst_password').val() == '' ) {
					$('form#eelst_form-login input#eelst_password').css('backgroundColor','red').stop().animate( {backgroundColor: '#fff'}, {queue:false,duration:1000} ).focus();
					return false;
				}
                $('form#eelst_form-login').animate({opacity: 0});
                $('img#ajax_loader').fadeIn();

                // return every false to block submit form (using ajax)
				return true;
			}
		);
		
        // Hook on lost focus input text
		$('form#eelst_form-login input#eelst_username').blur(
			function() {
				if( $('form#eelst_form-login input#eelst_username').val() != 'admin' ) {
					if (!EELST_Login.filter.test( $(this)[0].value )) {
						$(this)
						.css('backgroundColor','red').stop().animate({backgroundColor: '#fff'}, {queue:false,duration:1000});
					} else {
						$(this)
						.css('backgroundColor','#af0').stop().animate({backgroundColor: '#fff'}, {queue:false,duration:1000});
					}
				}
            }
        );

        $('form#eelst_form-login input#eelst_password').blur(
			function() {
				if( $(this)[0].value == "" ) {
					$(this)
					.css('backgroundColor','red').stop().animate({backgroundColor: '#fff'});
				} else {
					$(this)
					.css('backgroundColor','#af0').stop().animate({backgroundColor: '#fff'});
				}
            }
        );    
    }
};
