﻿function login()
{       

    if ($('#x-login').length < 1)
    {
        var h;
        h = '<div id="x-login" title="Представиться системе" style="display:none;">';
        h += '   <p>Для входа введите имя и пароль</p>';
        h += '		<table>';
        h += '			<tr>';
        h += '				<td style="text-align: right;">Имя</td>';
        h += '				<td style="width:100%"><input id="username" name="username" type="text" style="width:100%" tabindex="1"  /></td>';
        h += '			</tr>';
        h += '			<tr>';
        h += '				<td style="text-align: right;">Пароль</td>';
        h += '				<td><input id="userpw" type="password" style="width:100%" tabindex="2" /></td>';
        h += '			<tr>';
        h += '		</table>';
        h += '	<p><a href="/tools/registration/default.aspx">Регистрация</a> | <a href="/tools/registration/Restore.aspx">Забыли пароль?</a></p>';
        h += '</div>';
        $('body').prepend(h);
    }

    $('#x-login').dialog({ resizable: false, bgiframe: false, width: 320, modal: true, hide: 'drop',
        buttons: {
            "Войти": function()
            {
                var imin = new Imin.Web.Services.Imin();
                imin.Login(
					$(this).find('#username').val(),
					$(this).find('#userpw').val(),
					function(Response)
					{
					    var pos = window.location.href.indexOf('#');
					    var location = pos > -1 ? window.location.href.substring(0, pos) : window.location.href;
					    if (Response.type == 'login_ok')
					    {
					        $('#x-login').dialog('close');
					        window.setTimeout(function() { window.location.href = (location); }, 800);
					    }
					    else
					    {
					        $('#inbtn').removeAttr('disabled');
					        alert(Response.data);
					    }
					});
            },
            "Отмена": function() { $(this).dialog('close') }
        },

        open: function()
        {
            var $user = $(this).find('#username');
            var $passwd = $(this).find('#userpw');
            var dialog = this;

            $user.keydown(function(event) { if (event.keyCode == 13) { $passwd.focus(); } });
            $passwd.keydown(function(event)
            {
                if (event.keyCode == 13)
                {
                    $(dialog).parent().find('.ui-dialog-buttonpane button:contains("Войти")').click();
                }
            });
            $user.focus();
        }
    });
}

function logout()
{
    var imin = new Imin.Web.Services.Imin();
    imin.LogOut(function(Response)
    {
        if (Response.type == 'logout_ok')
        {            
            window.location.href = (Response.data);
        }
    });
}

Type.registerNamespace('IWCtrls');
IWCtrls.SvcLogin = function(element) { IWCtrls.SvcLogin.initializeBase(this, [element]); }

IWCtrls.SvcLogin.prototype = {

    initialize : function() {        
        IWCtrls.SvcLogin.callBaseMethod(this, 'initialize');                                
        $('.login-click').attr("href", "javascript:login()");
        $('.info_logout').attr("href", "javascript:logout()");
    },
    
    dispose : function() {        
        $clearHandlers(this.get_element());
        IWCtrls.SvcLogin.callBaseMethod(this, 'dispose');
    }
}

IWCtrls.SvcLogin.registerClass('IWCtrls.SvcLogin', Sys.UI.Control);
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();