$.ajaxSetup({
  global: "false",
  type: "POST",
  timeout: 3000 
});
function elipsisCollectionTitles() {
  /* We're only using jquery text-overflow on the first two collections
     at the moment as they're one liners. The JS doesn't handle putting
     an elipsis at the end of multiple lines */
  $(".item1 .collectionName a").ellipsis();
  $(".item2 .collectionName a").ellipsis();
  $(".item1 .artistName a").ellipsis();
  $(".item2 .artistName a").ellipsis();
};
function getList(panel, listType, releaseType) {
  genre = null;
  if (jQuery.url.segment(0) == "genre") {
    genre = $.url.segment(1);
  };
  $.ajax({
    url: "/browse/gettoplist",
    data: { releasetype: releaseType, listtype: listType, genre: genre }, 
    success: function(returnval) {
      $(panel).html(returnval);
      bindLatest(panel, listType);
      elipsisCollectionTitles();
    },
    error: function(returnval) {
       raiseMessage("Error", "Oops. There was a problem updating the display."); 
    } 
  });
};
function bindLatest(panel, listType) {
  buttons = [ '.albumButton', '.singleButton', '.epButton' ];
  for (var button in buttons) {
    if (buttons[button] == '.epButton') { 
      $(panel + " .collectionTypeControl " + buttons[button]).click(function() {
        getList(panel, listType, 'ep');
        return false;
      });
    };
    if (buttons[button] == '.albumButton') { 
      $(panel + " .collectionTypeControl " + buttons[button]).click(function() {
        getList(panel, listType, 'album');
        return false;
      });
    };
    if (buttons[button] == '.singleButton') {
      $(panel + " .collectionTypeControl " + buttons[button]).click(function() {
        getList(panel, listType, 'single');
        return false;
      });
    };
  };
};

$(document).ready(function() {
  var displays = new Array();
  displays = [ '#latestReleasesDisplay', '#topCollectionsDisplay', '#latestAddedDisplay' ]
  for (display in displays) {
    if (displays[display] == '#latestReleasesDisplay') { var type = 'latestReleases'; };
    if (displays[display] == '#latestAddedDisplay') { var type = 'latestAdded'; };
    if (displays[display] == '#topCollectionsDisplay') { var type = 'top'; };
    bindLatest(displays[display], type);
   };
});

