// Namespace declarations
if (mw == null || typeof(mw) != "object") {
  var mw = new Object();
}
if (mw.ui == null || typeof(mw.ui) != "object") {
  mw.ui = new Object();
}


mw.ui = {
  handle_drop_down: function () {
    $("#nav-list li").mouseenter(function(event){
      if ($(event.target).is("a")){
        chosenMenu = $(event.target).parent();
      } else {
        chosenMenu = $(event.target);
      }
      event.stopPropagation();
      thisIndex = chosenMenu.index();
      $("#nav-drop-down").hide();
      $(".drop-down-menu").hide();
      $(".drop-down-menu").eq(thisIndex).show();
      if ($("#nav-drop-down li").eq(thisIndex).length > 0){
        $("#nav-drop-down").show();
        newTop = chosenMenu.offset().top - $("#nav-drop-down").height();
        $("#nav-drop-down").css({
          left: chosenMenu.offset().left + 15,
          top: newTop
        });
        $("#nav-drop-down").animate({
          top: chosenMenu.offset().top + chosenMenu.height()
          }, 125, "swing");

          $("#nav, #nav-drop-down").mouseleave(function(event){
            leftBound = $("#nav-drop-down").offset().left;
            rightBound = $("#nav-drop-down").offset().left + $("#nav-drop-down").width();
            topBound = $("#nav-drop-down").offset().top;
            bottomBound = $("#nav-drop-down").offset().top + $("#nav-drop-down").height();
            if (!(event.pageX > leftBound && event.pageX < rightBound && event.pageY < bottomBound && event.pageY > topBound)){
              $("#nav-drop-down").hide("fade", 125);
            }
          });
        }
      });
    },

  show_login: function () {

    if(document.URL.indexOf("https")!=0){
      var s = document.URL.split("http://");
      var url = "https://"+s[1]+"?login";
      window.location = url;
      return;
    }

    $("#login_dialog").dialog({
      title: "Authentication Required",
      closeOnEscape: false,
      draggable: false,
      resizable: false,
      dialogClass: "noTitle",
      show: "fade",
      hide: "fade",
      modal: true,
      dialogClass: "login_dialog",
      buttons: {
        "Cancel": function () {
          $(this).dialog("destroy");
        },
        "Login": function () {
          mw.rest.do_login($("#login_username").val(), $("#login_password").val(), "#login_failed_response");
        }
      }
    });
    $("#login_password").keypress(mw.ui.login_on_enter);
    $(".ui-widget-overlay").css({
      "background-color": "black",
      "opacity": .65
    });
    var username = mw.utils.parse_get("email");
    if(!username){
      username = mw.rest.get_username();
    }
    
    if (username) {
      $("#login_username").val(username);
      $("#login_password").focus();
    }
  },

  login_on_enter: function (e) {
    if (e.which == 13) {
      mw.rest.do_login($("#login_username").val(), $("#login_password").val(), "#login_failed_response"); 
      return false;
    }
  },

  show_logged_out: function () {
    mw.utils.delete_cookie(mw.rest.session_cookie);
    $("#loggedout").remove();
    $("body").prepend("<div id='loggedout'></div>");
    $("#loggedout").dialog({
      title: "You have been logged out",
      autoOpen: true,
      modal: true,
      closeOnEscape: false,
      draggable: false,
      resizable: false,
      show: "fade",
      hide: "fade",
      height: 62,
      width: 200,
      buttons: {
        "OK": function () {
          $(this).dialog("destroy");
          $("#loggedout").remove();
          location.reload();
        }
      }
    });
  }
} // end namespace



