/*** STARTUP ***/
$.ajaxSetup({ cache: false });

$(document).ready(function() {
  $("#j_password").keyup(function(e) {
    if (e.keyCode == $.ui.keyCode.ENTER) {
      $("#signin-button").click();
    }
  });

  $("#mb-header li:not(.active)").hover(function() {
    $(this).addClass("nav-hover").append($("<img src=\"/images/nav-active-left.png\" class=\"nav-hover-left\" width=\"6\" height=\"24\"/><img src=\"/images/nav-active-right.png\" class=\"nav-hover-right\" width=\"6\" height=\"24\"/>"));
  }, function() {
    $(this).removeClass("nav-hover");
    $("#mb-header li img.nav-hover-left").remove();
    $("#mb-header li img.nav-hover-right").remove();
  });
});

$(function(){
  //all hover and click logic for buttons
  $(".fg-button:not(.ui-state-disabled)")
  .hover(
    function(){
      $(this).addClass("ui-state-hover");
    },
    function(){
      $(this).removeClass("ui-state-hover");
    }
  )
  .mousedown(function(){
      $(this).parents('.fg-buttonset-single:first').find(".fg-button.ui-state-active").removeClass("ui-state-active");
      if( $(this).is('.ui-state-active.fg-button-toggleable, .fg-buttonset-multi .ui-state-active') ){ $(this).removeClass("ui-state-active"); }
      else { $(this).addClass("ui-state-active"); }
  })
  .mouseup(function(){
    if(! $(this).is('.fg-button-toggleable, .fg-buttonset-single .fg-button,  .fg-buttonset-multi .fg-button') ){
      $(this).removeClass("ui-state-active");
    }
  });
});

function escapeIdForJquery(id) {
  return id.replace(/\./g, "\\.").replace(/\[/g, "\\[").replace(/\]/g, "\\]").replace(/_/g,"\\_");
}

function arrayIndexOf(array, val) {
  for (var i=0; i<array.length; i++) {
    if (val == array[i]) return i;
  }
  return -1;
}

function favorites(contractorId, contractorName, isFav, isAdd) {
  $("<div id=\"favorite-dialog\">"+(isAdd ? "Add" : "Remove") + " the service provider '" + contractorName + "' " + (isAdd ? " to " : " from ") + " your " + (isFav ? "favorite" : "blocked") + " providers?" + (isAdd ? (isFav ? "  Providers on your favorite service providers list may be directly chosen for new projects." : "  Service providers that are on your block list will never be matched to new projects you create.  Additionally, adding a service provider to your block list will remove them from any current projects you have.") : "") + "</div>")
      .dialog({
    modal: true,
    title: "Service Provider Preferences",
    buttons: {
      "Yes": function() {
        $("#favorite-dialog").html("<div style=\"text-align: center; padding-top: 30px;\"><span class=\"mb-loading\"><img src=\"/images/loading.gif\"/> Saving...</span></div>");
        $(this).dialog("option", "buttons", { });
        var dialog = $(this);
        $.ajax({
          url: "/propertymanager/favoriteAction.do?contractorId="+contractorId+"&command="+(isAdd ? (isFav ? "addfav" : "addbl") : (isFav ? "remfav" : "rembl")),
          dataType: "json",
          success: function(data) {
            if (data && data.result == "success") {
              $("#favorite-dialog").html("<div>Service provider successfully " + (isAdd ? "added to " : "removed from") + " " + (isFav ? "favorite list" : "block list") + "." + "</div>");
              dialog.dialog("option", "buttons", {
                "OK" : function() {
                  dialog.dialog("close");
                  if (isAdd && !isFav) {
                    location.href = "/common/homeRedirector.do";
                  }
                  else {
                    location.reload();
                  }
                }
              });
            }
            else {
              $("#favorite-dialog").html("<div>Failed to " + (isAdd ? "add to" : "remove from") + " " + (isFav ? "favorites" : "block list") + ".  Please try again later.</div>");
              dialog.dialog("option", "buttons", {
                "OK" : function() {
                  dialog.dialog("close");
                }
              });
            }
          },
          error: function() {
            $("#favorite-dialog").html("<div>Failed to " + (isAdd ? "add to" : "remove from") + " " + (isFav ? "favorites" : "block list") + ".  Please try again later.</div>");
            dialog.dialog("option", "buttons", {
              "OK" : function() {
                dialog.dialog("close");
              }
            });
          }
        });
      },
      "No": function() {
        $(this).dialog("close");
      }
    }
  });
}
