$(function(){
    $("div#login_bl a").live('click',function(){
        var i = $("div#login_bl a").index(this);
        $('#login_panel').show();
        $('#bottom a').eq(i).click();
        $('body').bind('mousedown',function(e){
            var login_panel = $('#login_panel');
            var off = login_panel.offset();
            var width = login_panel.width();
            var height = login_panel.height();
            if(!(e.pageX>off.left && e.pageX<(off.left+width) && e.pageY>off.top && e.pageY<(off.top+height))) {
                login_panel.hide();
                $('body').unbind();
            }
        })
        return false;
    });
    $('#login_panel #bottom a').live('click',function(){
        $('#login_panel #bottom a.active').removeClass('active');
        $(this).addClass('active');
        $('#login_panel #top div:not(hidden)').addClass('hidden');
        $('#t'+$(this)[0].id).removeClass('hidden');
        return false;
    });
    function tt1_submit() {
        var arr = {};
        arr['param'] = $('#tt1 form').serialize();
        $.ajax({
          type: "POST",
          url: '/user/login/',
          dataType: 'json',
          data: arr,
          success: function(data) {
            if(data=='success') window.location.href=window.location.href;
            else humanMsg.displayMsg('Неверное имя пользователя или пароль');
          }
        });  
        return false;
    }
    function tt2_submit() {
        var arr = {};
        arr['param'] = $('#tt2 form').serialize();
        $.ajax({
          type: "POST",
          url: '/user/registration/',
          dataType: 'json',
          data: arr,
          success: function(data) {
            if(data['message']=='success') window.location.href='/user/settings/';
            else humanMsg.displayMsg(data['message']);
          }
        });  
        return false;
    }
    function tt3_submit(){
        var arr = {};
        arr['param'] = $('#tt3 form').serialize();
        $.ajax({
          type: "POST",
          url: '/user/reminder/',
          dataType: 'json',
          data: arr,
          success: function(data) {
            if(data['message']=='success') window.location.href='/user/settings/';
            else humanMsg.displayMsg(data['message']);
          }
        });  
        return false;
    }
    $('#tt1 form').live('submit',tt1_submit);
    $('#tt2 form').live('submit',tt2_submit);
    $('#tt3 form').live('submit',tt3_submit);
    if(jQuery.browser.msie)
    {
        $('#tt1 form input[type=submit]').live('click',tt1_submit);
        $('#tt2 form input[type=submit]').live('click',tt2_submit);
        $('#tt3 form input[type=submit]').live('click',tt3_submit);
    }
});
function textual(num, one, two, many) {
    if (num%100==1 || (num%100>20 && num%10==1)) return one;
    if ((num%100>1 && num%100<5) || (num%100>20 && num%10>1 && num%10<5)) return two;
    return many;
}
function numFormat(n, d, s) {
	if (arguments.length == 2) { s = " "; }
	if (arguments.length == 1) { s = " "; d = "."; }
	n = n.toString();
	a = n.split(d);
	x = a[0];
	y = a[1];
	z = "";
	if (typeof(x) != "undefined") {
	for (i=x.length-1;i>=0;i--)
		z += x.charAt(i);
	z = z.replace(/(\d{3})/g, "$1" + s);
	if (z.slice(-s.length) == s)
		z = z.slice(0, -s.length);
	x = "";
	for (i=z.length-1;i>=0;i--)
		x += z.charAt(i);
	if (typeof(y) != "undefined" && y.length > 0)
		x += d + y;
	}
	return x;
}
