$(document).ready(function() {
    $("#dialog").hide();
});

function ShowDialog(title, message) {
    $("#dialog").attr('title', title);
    $("#dialog").html(message);
    $("#dialog").dialog();
}

function Clicked(_this) {
    $(_this).attr("onclick", "");
};

function AjaxRequest(_this, _data) {
    Clicked(_this);

    $.ajax(
          {
              url: "/api/AjaxRequest",
              data: { "RequestData": _data },
              type: "POST",
              success: function(data) {
                  eval('resp =' + data);
                  if (resp.status == 200) {
                      $(_this).fadeOut("slow");
                  }
                  else {
                      ShowDialog('An Error Has Occured', resp.message)
                  }
              },
              error: function(XMLHttpRequest, textStatus, errorThrown) {
                  ShowDialog('An Error Has Occured', textStatus + " " + errorThrown)
              }
          });
      }

      function AjaxRequestNew(_this, _data) {
          $(_this).attr("onclick", "");
          $(_this).attr("class", "request_link_clicked");

          $.ajax(
          {
              url: "/api/AjaxRequest",
              data: { "RequestData": _data },
              type: "POST",
              success: function(data) {
                  eval('resp =' + data);
                  if (resp.status == 200) {
                      $(_this).fadeOut("slow");
                  }
                  else {
                      ShowDialog('An Error Has Occured', resp.message)
                  }
              },
              error: function(XMLHttpRequest, textStatus, errorThrown) {
                  ShowDialog('An Error Has Occured', textStatus + " " + errorThrown)
              }
          });
      }

function Rate(_data, _rating, _button, _ratingCtrl) {
    Clicked(_button);
    
    $.ajax(
          {
              url: "/api/AjaxRequest",
              data: { "RequestData": _data, "Rating": _rating },
              type: "POST",
              success: function(data) {
                  eval('resp =' + data);
                  if (resp.status == 200) { }
                  else { ShowDialog('An Error Has Occured', resp.message) }
              },
              error: function(XMLHttpRequest, textStatus, errorThrown) {
                  ShowDialog('An Error Has Occured', textStatus + " " + errorThrown)
              }
          });

          $(_button).hide();
          $(_ratingCtrl).rating('readOnly', true);
}

function formatUserLinkText() {
    var stuff = $(".trim").each(function() {
    ellipseText($(this), 115);
    });
}

function ellipseText(element, width) {
    elem = $(element);
    var text = elem.html();

    if (elem.width() > width) {
        var i = 1;
        elem.html('');
        while (elem.width() < (width) && i < text.length) {
            elem.html(text.substr(0, i) + ' ...');
            i++;
        }
    }
}

function skipCrop() { 
    $("#X").val('');
    $("#Y").val(''); 
    $("#W").val(''); 
    $("#H").val('');
}

