/* 
 * Javascript for the user favourites functionality.
 * 
 */

//add everthing to a fav's js object.
var fav = {};

//has the control contents been loaded.
//only supports on control on a page atm.
fav.isLoaded = false;

fav.groups = [];
//add an item to the users favourites.
//curently on used for pages in java atm.
fav.add = function(res, type) {
  jQuery.ajax({
      url: "/servlet/favourites",
      dataType: 'json',
      data: {fresId: res, frType: type, faction: "add"},
      success: function(data) {
        alert(data.message);
      }
    });
};

fav.remove = function(aarId, type) {
  jQuery.ajax({
      url: "/servlet/favourites",
      dataType: 'json',
      data: {faarId: aarId, faction: "remove"},
      success: function(data) {
        if (data.success) {
          if (type === '2')
          {
          fav.loadPages();
          }
          else if (type === '1')
          {
            fav.loadChats();
          }
          else if(type === '3')
          {
            fav.loadLinks();
          }
        } else {
          alert(data.message);
        }
      }
    });
};
fav.loadPages = function() {
  jQuery("#favourite-pages-list").load("/servlet/favourites", {faction: "render", frType: 2, "rpp" : favourites_rpp });
};
fav.loadChats = function() {
  jQuery("#favourite-chats-list").load("/servlet/favourites", {faction: "render", frType: 1, "rpp" : favourites_rpp });
};
fav.loadLinks = function() {
  jQuery("#favourite-links-list").load("/servlet/favourites", {faction: "render", frType: 3, "rpp" : favourites_rpp });
};
fav.load = function() {
  if (fav.isLoaded) {
    return;
  }
  for (var i = 0; i < fav.groups.length; i++) {
    if (fav.groups[i] === "2") {
      fav.loadPages();
    }
    if (fav.groups[i] === "1") {
      fav.loadChats();
    }
    if (fav.groups[i] === "3") {
      fav.loadLinks();
    }
  }
  fav.isLoaded = true;
};
fav.addLinkRow = function(id, title, url, desc) {
  var row = "<tr>" +
    "<td class='identifier hidden' style='display:none;'>" + id + "</td>" +
    "<td class='title'>" + title + "</td>" +
    "<td class='url'>" + url + "</td>" +
    "<td class='description'>" + desc + "</td>" +
    "</tr>";
  return row;
};
fav.clearLinkInputs = function() {
  jQuery("#edit-fav-links table input[name='linktitle']").val("");
  jQuery("#edit-fav-links table input.[name='linkurl']").val("");
  jQuery("#edit-fav-links table input.[name='linkdesc']").val("");
};
fav.addLink = function(title, url, desc) {
  jQuery('#link-add-button').prop("disabled", true);
  jQuery('#link-add-button').attr('value','Adding');
  if(!url.match("http"))
  {
    url = "http://" + url;
  }
  jQuery.ajax({
      url: "/servlet/favourites",
      dataType: 'json',
      data: {faction: "addLink", flinktitle: title, flinkurl: url, flinkdesc: desc},
      success: function(data) {
        if (data.success) {
          //jQuery("#edit-fav-links table tbody").append(fav.addLinkRow(data.result, title, url, desc));
          //fav.clearLinkInputs();
          top.fav.loadLinks();
          jQuery('#link-add-button').prop("disabled", false);
          fav.clearLinkInputs();
          location.reload();
        }
      }
    });
};
fav.removeLink = function(button) {
   jQuery(button).prop("disabled", true);
   jQuery(button).val('Deleting');
   var id = jQuery(button).attr("id");
   jQuery.ajax({
      url: "/servlet/favourites",
      dataType: 'json',
      data: {faction: "removelink", flinkid: id},
      success: function(data) {
        if (data.success) {
          top.fav.loadLinks();
          location.reload();
        } else {
          alert(data.message);
        }
      }
    });
}

//the favourites link.
jQuery(document).ready(function() {
  jQuery(".favourites-link").click(function(e) {
    e.preventDefault();
    var type = jQuery(this).children("input[name='frType']").val();
    var res = jQuery(this).children("input[name='fresId']").val();
    fav.add(res, type);
  });

});

