$(document).ready(function(){

$('.justfoto').hide(); 
 
$('.login').bind('click', function() {
  showLoginForm();
 return false;
});


function showLoginForm()
{
    var $dialog = $('<div></div>')
		.html('<form name="auth" id="auth" action="" method="post">\n\
<table width="250" border="0" cellpadding="0">\n\
  <tr>\n\
    <td>Логин:</td>\n\
    <td><input name="login" type="text" size="15" /></td>\n\
  </tr>\n\
  <tr>\n\
    <td>Пароль:</td>\n\
    <td><input name="pass" type="password" size="15" /></td>\n\
  </tr>\n\
<tr>\n\
<div id="error"></div>\n\
</tr>\n\
</table>\n\
</form>\n\
<a href="http://ipdbx.com/lostPass">Забыли пароль?</a>\n\
')
		.dialog({
			autoOpen: false,
			title: 'Авторизация',
                        modal: true,
                        resizable: false,
                        buttons: {
      "Выход": function() {
      $(this).dialog("close");
      },
      "Авторизация": function() {
          $("[name=auth] [name=auth] div#error").html('');
          if( $("[name=auth]#auth input[name='login']").val().length < 4 || $("[name=auth] input[name='login']").val().length > 15)
             $("[name=auth] div#error").html('Логин не может быть короче 4 или длинее 15 символов !');
          else if( $("[name=auth] input[name='pass']").val().length < 4 || $("[name=auth] input[name='pass']").val().length > 15)
             $("[name=auth] [name=auth] div#error").html('Пароль не может быть короче 4 или длинее 15 символов !');
          else
          {
            $("[name=auth] [name=auth] div#error").html('');
            login();
          }
      }}
		});
    $dialog.dialog('open');
   
}

function login()
{
    $.post("http://ipdbx.com/login", $("#auth").serialize(),
        function(data){
            if($.isEmptyObject(data.messages))
                window.location.replace(document.referrer);
            else
                $("[name=auth] div#error").html(data.messages[0]);
        }, "json"
    );
}

$('#toggle-foto').toggle(function() {
    $('.justfoto').show(400);  
    $('#toggle-foto').text('Спрятать кадры');
    
}, function() {
    $('.justfoto').hide(400);  
    $('#toggle-foto').text('Показать кадры');
});
});
