﻿var AwakeningWorld =
{
  CurrentUser: null,
  divModal: null,
  divMessaging: null,

  /****************************************************************************
  * Initialization.
  ****************************************************************************/
  _init: function()
  {
    //Get the integration web service URL
    this.IntegrationURL = $("#WebService").val();

    this.divModal = $("#divModal");
    this.divMessaging = $("#divMessaging");

    AwakeningWorld.updateLoginUI();
    this.wireEvents();
  },
  
  /****************************************************************************
  * Displays an error message to the user and fades it away after 5 seconds.
  ****************************************************************************/
  displayMessage: function(messageText, textColor)
  {
    if (textColor != null)
      AwakeningWorld.divMessaging.css("color", textColor);
    else
      AwakeningWorld.divMessaging.css("color", "#800000");
      
    AwakeningWorld.divMessaging.text(messageText);

    window.setTimeout(function()
    {
      AwakeningWorld.divMessaging.text("");
    }, 5000);
  },
  
  /****************************************************************************
  * Handles user login.
  ****************************************************************************/
  processLogin: function()
  {
    $.ajax(
    {
      type: "POST",
      url: AwakeningWorld.IntegrationURL + "authenticateUser",
      dataType: "json",
      data:
      {
        Username: $("#txtLogin").val(),
        Password: $("#txtPassword").val(),
        format: "json"
      },
      success: function(userCredentials)
      {
        $("#txtLogin").val("");
        $("#txtPassword").val("");
        window.location.reload();
      },
      error: function()
      {
        AwakeningWorld.displayMessage("Invalid username or password");
      }
    });
  },

  /****************************************************************************
  * Handles the user logging out.
  ****************************************************************************/
  processLogout: function()
  {
    $.ajax(
    {
      type: "POST",
      url: AwakeningWorld.IntegrationURL + "logOut",
      dataType: "json",
      data:
      {
        format: "json"
      },
      success: function()
      {
        window.location.reload();
      }
    });
  },
  
  /****************************************************************************
  * Changes the UI based on whether the user is logged in or not.
  ****************************************************************************/
  updateLoginUI: function()
  {
    if (AwakeningWorld.CurrentUser)
    {
      //The customer has logged in
      $("#spanWelcome").text("Welcome " + AwakeningWorld.CurrentUser + "!");
      $("#divGuest").css("display", "none");
      $("#divUser").css("display", "block");

      //Build the menu
      $("#ulMenu").append($("#navigationItems").html());
    }
    else
    {
      $("#divGuest").css("display", "block");
      $("#divUser").css("display", "none");
      $("#ulMenu").find("li:not(:first-child)").remove();
    }
  },

  /****************************************************************************
  * Wire up event-handlers.
  ****************************************************************************/
  wireEvents: function()
  {
    $("#btnLogin").click(this.processLogin);
    $("#linkLogout").click(this.processLogout);

    $("#txtPassword").keypress(function(event)
    {
      if (event.which == 13)
        $("#btnLogin").trigger("click");
    });

    $("#linkForgot").click(function()
    {
      AwakeningWorld.displayMessage("Sucks to be you!");
    });
  }
}

$(document).ready(function()
{
  AwakeningWorld._init();
});
