  $j(document).ready(function() {
	  
  // initialise superfish jquery plugin
  /* jQuery('ul.sf-menu').superfish({ */
  /*   autoArrows: false */
  /* }); */

  $("#popular_week").hide();
  $("#popular_month").hide();
  $("#popular_year").hide();

  $("#discussed_week").hide();
  $("#discussed_month").hide();
  $("#discussed_year").hide();

//  $('#gallery').galleria();

  });

function hide_all_popular() {
  $j("#popular_day").hide();
  $j("#popular_week").hide();
  $j("#popular_month").hide();
  $j("#popular_year").hide();  
}

function hide_all_discussed() {
  $j("#discussed_day").hide();
  $j("#discussed_week").hide();
  $j("#discussed_month").hide();
  $j("#discussed_year").hide();  
}

function remove_popular_selected() {
  $j('#popular_day_link').removeClass("selected");
  $j('#popular_week_link').removeClass("selected");
  $j('#popular_month_link').removeClass("selected");
  $j('#popular_year_link').removeClass("selected");
}

function popular_day() {
  remove_popular_selected();
  $j('#popular_day_link').addClass("selected");
  hide_all_popular();
  $j("#popular_day").show();
}

function popular_week(cat_id) {
  remove_popular_selected();
  $j('#popular_week_link').addClass("selected");
  hide_all_popular();

  if ($j('#popular_week li').size() == 0) {
    $j('#popular_week').html("<li>Loading...</li>");
      
    var url = '/most-popular-query?num_weeks=1';
      if (cat_id) {
	  url = url+"&cat_id="+cat_id;
      }

    $j.get(url, function(data) {
      $j('#popular_week').html(data);
    });
  }
  
  $j("#popular_week").show();
}

function popular_month(cat_id) {
  remove_popular_selected();
  $j('#popular_month_link').addClass("selected");
  hide_all_popular();

  if ($j('#popular_month li').size() == 0) {
    $j('#popular_month').html("<li>Loading...</li>");
      
    var url = '/most-popular-query?num_weeks=4';
      if (cat_id) {
	  url = url+"&cat_id="+cat_id;
      }

    $j.get(url, function(data) {
      $j('#popular_month').html(data);
    });
  }
  

  $j("#popular_month").show();
}

function popular_year(cat_id) {
  remove_popular_selected();
  $j('#popular_year_link').addClass("selected");
  hide_all_popular();

  if ($j('#popular_year li').size() == 0) {
    $j('#popular_year').html("<li>Loading...</li>");
      
    var url = '/most-popular-query?num_weeks=52';
      if (cat_id) {
	  url = url+"&cat_id="+cat_id;
      }

    $j.get(url, function(data) {
      $j('#popular_year').html(data);
    });
  }

  $j("#popular_year").show();
}


function remove_discussed_selected() {
  $j('#discussed_day_link').removeClass("selected");
  $j('#discussed_week_link').removeClass("selected");
  $j('#discussed_month_link').removeClass("selected");
  $j('#discussed_year_link').removeClass("selected");
}

function discussed_day() {
  remove_discussed_selected();
  $j('#discussed_day_link').addClass("selected");
  hide_all_discussed();

 if ($j('#discussed_day li').size() == 0) {
    $j('#discussed_day').html("<li>Loading...</li>");
     
    $j.get('/most-discussed-query?num_weeks=0', function(data) {
      $j('#discussed_day').html(data);
    });
  }
    
  $j("#discussed_day").show();
}

function discussed_week() {
  remove_discussed_selected();
  $j('#discussed_week_link').addClass("selected");
  hide_all_discussed();

  if ($j('#discussed_week li').size() == 0) {
    $j('#discussed_week').html("<li>Loading...</li>");
      
    $j.get('/most-discussed-query?num_weeks=1', function(data) {
      $j('#discussed_week').html(data);
    });
  }
  
  $j("#discussed_week").show();
}

function discussed_month() {
  remove_discussed_selected();
  $j('#discussed_month_link').addClass("selected");
  hide_all_discussed();

  if ($j('#discussed_month li').size() == 0) {
    $j('#discussed_month').html("<li>Loading...</li>");
      
    $j.get('/most-discussed-query?num_weeks=4', function(data) {
      $j('#discussed_month').html(data);
    });
  }
  
  $j("#discussed_month").show();
}

function discussed_year() {
  remove_discussed_selected();
  $j('#discussed_year_link').addClass("selected");
  hide_all_discussed();

  if ($j('#discussed_year li').size() == 0) {
    $j('#discussed_year').html("<li>Loading...</li>");
      
    $j.get('/most-discussed-query?num_weeks=52', function(data) {
      $j('#discussed_year').html(data);
    });
  }

  
  $j("#discussed_year").show();
}

function most_recent() {

  if ($j('#most_recent li').size() == 0) {
    $j('#most_recent').html("<li>Loading...</li>");
      
    $j.get('/most-recent-query', function(data) {
      $j('#most_recent').html(data);
    });
  }

  
  $j("#most_recent").show();
}

