/* Programmes.js */
/* Include this wherever there's a list of programmes */

function calc_top(item, top)
{
  // Work out where to place the top of the item
  var max_height = $(window).scrollTop() + $(window).height() - item.outerHeight();
  if (top > max_height)
  {
    return max_height;
  }
  return top
}

function anonymize_controls() {
	$controls = $(".favourite, .notfav, .seen, .unseen, .blocked, .unblocked, .prog_blocked, .prog_unblocked, .watchlisted, .unwatchlisted");
	$controls.attr("onclick", "account_needed(event);return false;");
}

function add_prog_cat_controls() {

	function cat_remove_onclick(event)
  {
    // Called when the "remove" icon is clicked next to a category
    event.preventDefault();
    var that=$(this);
                  
    $.ajax({url: that.attr("href") + "&amp;json=true",
            cache: true,
            success: function(msg)
            {
              that.closest("span").remove();
            }});
  }

  // For admins we have a little cross next to each category so we can
  // remove them.  This code makes those links ajaxy so the page doesn't need
  // to refresh
  $(".cat_remove").click(cat_remove_onclick);

  // For admins we have a tick at the end of the categories.  If clicked we
  // display a pop-up with a form for categories to add
  $(".cat_add").click(function(event)
    {
      event.preventDefault();
      var that = $(this);
      var td = that.closest("td");
      var offset = td.offset();

      var edit = $("#category_edit");
      var form = edit.children("form");
      form.each(function()
        {
          this.pg_id.value = that.attr("pg_id");
        })
        .one("submit", function(event)
        {
          event.preventDefault();
          var form2 = $(this);
          
          $.ajax({url: $(this).attr("action") + "?" + $(this).serialize() + "&amp;json=true",
                  cache: true,
                  success: function(msg)
                  {
                    // Add the category to the list
                    form2.each(function()
                    {
                      var cat_name = this.id.options[this.id.selectedIndex].text
                      var cat = build_category(that.attr("pg_id"),
                                               this.id.value,
                                               cat_name);

                      cat.children("a:last").click(cat_remove_onclick);
                      var spans = td.children("span");
                      if (spans.length == 0)
                      {
                        td.prepend(cat);
                      }
                      else
                      {
                        cat.insertAfter(td.children("span:last"));
                      }
                    });

                    // Make the edit thingy disappear
                    edit.css("display", "none");
                  }});
        });

      edit.css({"top": calc_top($("#category_edit"), offset.top) + "px",
                "left": (offset.left + td.width()) + "px",
                "display": "block"});
    });    
}

