var ajax_in_progress = false;
function AmourAjax(url, data, callback_fnc, callback_error)
{
	if (typeof callback_error != 'function') callback_error = null;
	
    if(!ajax_in_progress) // only one request per time
    {
        ajax_in_progress = true;
        jQuery.ajax( {
            type: "POST",
            url: url,
            data: data,
            success: function(msg)
            {
                ajax_in_progress = false;
                var res = msg.split('|', 2);
    
                if( res[0] == 'ER' )
                {
                    switch(res[1])
                    {
                        case 'AUTH':
                            alert(LANG['AUTH']);
                            if (callback_error) callback_error('AUTH');
                            break;
    
                        case 'INTERNAL_ERROR':
                            alert(LANG['INTERNAL_ERROR']);
                            if (callback_error) callback_error('INTERNAL_ERROR');
                            break;
    
                        default:
                            callback_fnc('ER', res[1]);
                    }
                }
                else if( res[0] == 'WL' )
                {
                    window.location = res[1];                
                }
                else
                {
                    callback_fnc(res[0], res[1]);
                }
            }
        });
    }
}

//
// maxlength plugin
//
(function($) {
	//
	//plugin definition
	//
	$.fn.showlength = function (options) {
		var opts = $.extend({}, $.fn.showlength.defaults, options);

		if (opts.element) opts.element = $(opts.element);
		if (opts.filter && typeof opts.filter == 'string') {
			opts.filter = new RegExp("^[" + opts.filter + "]*$");
		}

		return this.each(function (k,val){
			$this = $(this);
			$this.keyup(function () {
				$input = $(this);
				if (opts.filter && typeof opts.filter.test == 'function') {
					var val = $input.val();
					while(val && !opts.filter.test(val)) {
						val = val.substr(0,val.length-1);
						$input.val(val);
					}
				}
				if ($input.val().length > opts.maxLength) {
					$input.val($input.val().substr(0,opts.maxLength));
				}
				if (opts.element) {
					$(opts.element).text(opts.maxLength - $input.val().length);
				}
			}).keyup();
		});
	}
	function log(txt) {
		if (window.console && window.console.log)
			window.console.log(txt);
	}
	//
	//plugin defaults
	//
	$.fn.showlength.defaults = {
		maxLength		: 50,		// макс длинна для input'a
		element			: null,		// елемент в котором будет отображатся текущая длинна
		filter			: null		// регулярное выражение
	};
})(jQuery);

// a - as popup
(function($) {
	//
	//plugin definition
	//
	$.fn.asPopup = function (options) {
		var opts = $.extend({}, $.fn.asPopup.defaults, options);

		return this.each(function (k,val){
			$this = $(this);
			$a = $this.parent('a');
			if ($a.length) {
				$a.click(function () {return false;});
				$this.click(function () {
					$this = $(this);
					win = window.open(
						$this.parent('a').attr('href'),
						opts.name,
						'height=' + opts.height + ',width=' + opts.width +',status=' + (opts.status ? 'yes' : 'no') + ',toolbar=' + (opts.toolbar ? 'yes' : 'no') + ' ,resizable=' + (opts.resizeble ? 'yes' : 'no')
					);
				});
			}
		});
	}
	function log(txt) {
		if (window.console && window.console.log)
			window.console.log(txt);
	}
	//
	//plugin defaults
	//
	$.fn.asPopup.defaults = {
		name		: 'NO_NAME',
		width		: 650,
		height		: 500,
		status		: false,
		toolbar		: false,
		resizeble	: false
	};
})(jQuery);

// a - select set value by text
(function($) {
	//
	//plugin definition
	//
	$.fn.txtVal = function (value) {
		if (typeof value == 'undefined') {
			var elm = this[0];
			if ( jQuery.nodeName( elm, "select" ) ) {				
				return $(elm).find('option:selected').text();
			} else {
				return $(elm).val();
			}
		} else {			
			return this.each(function (k,val){				
				if ( jQuery.nodeName( this, "select" ) ) {
					var option = $(this).find('option[text="' + value + '"]').get(0);
					if (option) { option.selected = true; }
				}
			});
		}
	}
	function log(txt) {
		if (window.console && window.console.log)
			window.console.log(txt);
	}
	//
	//plugin defaults
	//
})(jQuery);


var top_login_cancel = function (e) {
	if(e.keyCode == 27) { $(document).trigger('top-login-cancel'); }
}
$(document).ready(function(){
	$('textarea.showlength').each(function () {
		$this = $(this);
		$next = $this.next('div');
		$(this).showlength({
			maxLength	: $next.text(),
			element		: $next
		});	
	});
});

function Try2Login()
{
    var is_ok = true;
    var email    = jQuery.trim( $('#login_email').val());
    var password = jQuery.trim( $('#login_password').val());
    
    var re_email = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

    if( !re_email.test(email) )
    {
        is_ok = false;
        $('div.login-field').addClass('error-form');
    }
    else
    {
        $('div.login-field').removeClass('error-form');
    }		
    
    if(is_ok)
    {
        $('.pswd-field').removeClass('error-form');
        
        var data = {
            'email'       : email,
            'password'    : password,
            'remember_me' : $("#login_remember[checked]").length > 0 ? 1 : 0
        }
        
        AmourAjax(
            'ajax/login.phtml',
            data,
            function(type, msg)
            {
                if(type == 'OK')
                {
                	window.location.href = '/';
                }
                else if(type == 'ER')
                {
                	$('div.LoginForm').addClass('ErroredLogin');
                    $('.pswd-field').addClass('error-form');
                }
            }
        );
    }
    else
    {
        $('div.LoginForm').addClass('ErroredLogin');
    }
}