;(function($) {

  $(document).ready(function() {
    
    // Global Params
    var parameters = null; 
    var tagId = 1;
    var timer = null;
    
    function setupParams() {
      var params = parameters ? parameters : $.query;
      return params.REMOVE('_');
    }
  
    function rotateTags() {
      var old_span = $("#top-topics span.preview");
      var text = $("#tag-id-" + tagId).html();
      var new_span = $("<span class='preview' style='display:none;'>" + text + "</span>");
      old_span.after(new_span);
      old_span.fadeOut('slow');
      new_span.fadeIn('slow');
      old_span.remove();
      tagId++;
      if(tagId == 21) { tagId = 1; }
      timer = setTimeout(function(){ rotateTags(); }, 5000);
    }
  
    rotateTags();
    
    // Filter Bar Functions
    $("#top-topics .control").css('cursor', 'pointer').click(function() {
      $(this).css('cursor', 'default');
      $("#top-topics .list-display").slideDown();
      $("#top-topics .preview").fadeOut();
      clearTimeout(timer);
      return false;
    });

    $("#top-topics .close-link").click(function() {
      $("#top-topics .control").css('cursor', 'pointer');
      $("#top-topics .list-display").slideUp();
      $("#top-topics .preview").show();
      rotateTags();
      return false;
    });

    $("#filters .close-link").live('click', function(){
      // Close filter display and remove all active links
      $(this).parent().slideUp();
      $("#filters .control a").removeClass('active');
      return false;
    });
  
    $('.filter-link').click(function() {
      // remove .active from all links then activate this one
      $('.filter-link').removeClass('active');
      $(this).addClass('active');
      
      // if no filters are visible, Ajax load them
      if(!$("#filters .filter-display").is(":visible")) {
        $.ajax({
          type: 'GET',
          url: $(this).attr('href'),
          dataType: 'script',
          data: "div=" + $(this).attr('id')
        });
        
      // if filter is not visible, switch to it
      } else if(!($("#" + $(this).attr('id') + "-filter").is(":visible"))) {
        $(".filter-box").slideUp();
        $("#" + $(this).attr('id') + "-filter").slideDown();
      }
      return false;
    });
    
    // Clicking anywhere else in the filter control opens the date filter
    $("#filters div.control").live('click', function() {
			if(!($(".filter-display").is(':visible'))) {
				$("#filters .control #date.filter-link").click();
					return false;
				}
			});

    // Toggle the filters between on/off states
    $("#filters div.control a.toggle-filters").live('click', function() {
      $.get($(this).attr('href'), $.params, null, "script");
      return false;
    });
    
    $('.update-filter').live('change', function() {
      var form = $(this).closest('form');
      $.ajax({
        type: 'PUT',
        url: form.attr('action'),
        dataType: 'script',
        data: form.serialize()
      });
    });

    // Deprecated but do not remove

    $("#category-filter :checkbox").live("click", function () {
      var attach = $(this).is(':checked') ? "&attach=true" : "";
      var value = $(this).val();
      $.ajax({
        type: 'PUT',
        dataType: 'script',
        url: '/filters/update_category_ids',
        data: "id=" + value + attach
      });
    });

    $("#channel-filter .channel_check_box").live("click", function () {
      var attach = $(this).is(':checked') ? "&attach=true" : "";
      var value = $(this).val();
      $.ajax({
        type: 'PUT',
        url: '/filters/update_channel_ids',
        dataType: 'script',
        data: "id=" + value + attach
      });
    });

    $("#select_all_channels").live('click',function() {
      var select_all = $(this);
      var ids = "";
      $("#channel-filter :checkbox").each(function() {
        if(!($(this).is(':checked'))) {
          ids = ids + "&ids[]=" + $(this).val();
          $(this).attr("checked", true);
        }
      });
      $.ajax({
        type: 'PUT',
        dataType: 'script',
        url: '/filters/toggle_all_channels',
        data: "add=true" + ids
      });
      $(this).attr('id', 'deselect_all_channels').prev().html("Deselect All");
    });

    $("#deselect_all_channels").live('click',function() {
      var select_all = $(this);
      var ids = "";
      $("#channel-filter :checkbox").each(function() {
        if($(this).is(':checked')) {
          ids = ids + "&ids[]=" + $(this).val();
          $(this).attr("checked", false);
        }
      });
      $.ajax({
        type: 'PUT',
        dataType: 'script',
        url: '/filters/toggle_all_channels',
        data: "add=false" + ids
      });
      $(this).attr('id', 'select_all_channels').prev().html("Select All");
    });
    
  });
})(jQuery);
