<?php

// -------------------------------------------------------------------------------------------------
class UILogin
{ 
  protected $lst = NULL;
  
  public function __construct(Listopia $listopiaInst) 
  {
    $this->lst = $listopiaInst;
  }
  
  public function GenLoginDialog()
  {
echo <<<HTML
  <div id="UILoginDialog" title="Login" style="display:none">
    <form id="SignIn" action="">
      <label for="AuthUser">User</label><input spellcheck="false" autocapitalize="off" autocorrect="off" autocomplete="off" type="text" id="AuthUser" />
      <label for="AuthPwd">Password</label><input type="password" id="AuthPwd" />
      <p class="smaller-centered"><a id="AuthForgot" style="cursor:pointer">... no, I forgot my password</a></p>
      <div id="Reason" style="font-size:0.8em; color:red;"></div>
    </form>
  </div>  
HTML;
  }
  
  public function GenLoginScripting()
  {
echo <<<HTML
  <script type="text/javascript">
  $(function()
  {
    $('#UILogin').click(function(){
      $('#UILoginDialog').dialog('open');
      return false;
    });
    
    $('#UILogout').click(function(){
      $.get("php/ajax_authLogout.php").complete(function() { location.reload(); });
      return false;
    });
    
    $('#UILoginDialog #AuthForgot').click(function(){
      
      var authReason = $("#SignIn #Reason");
      if ( $("#SignIn #AuthUser").val() == "" )
      {
        authReason.html("Enter a username first, dude.");
      }
      else
      {
        authReason.html("<img style='margin:0px auto' src='images/ajax-loader.gif' alt=''/>");
        $(this).slideUp("fast");
        
        $.post(
            "php/ajax_remailForgottenPwd.php", 
            { user: $("#SignIn #AuthUser").val() },
            function(data) 
            {
              authReason.html(data);
            },
            "text");
      }
      
      return false;
    });
    
    $('#UILoginDialog').dialog({
      autoOpen: false,
      modal: true,
      resizable: false,
      width: 250,
      buttons: {
        "Submit": function(event) 
        {
          var authDialog = $(this);
          var authReason = $("#SignIn #Reason");
          
          $(event.target).attr("disabled", true).addClass("ui-state-disabled");
          authReason.html("<img style='margin:0px auto' src='images/ajax-loader.gif' alt=''/>");
          
          $.post(
            "php/ajax_authLogin.php", 
            { user: $("#SignIn #AuthUser").val(), pass: $("#SignIn #AuthPwd").val() },
            function(data) 
            { 
              if (data.success)
              {
                location.reload();
              }
              else
              {
                authReason.html(data["reason"]);
                $(event.target).attr("disabled", false).removeClass("ui-state-disabled");
              }
            },
            "json");
        }
      }
    });
  });
  </script>  
HTML;
  }
}