jQuery(function() {
  var today = new Date();
  var month = today.getMonth() + 1;
  var year =  today.getFullYear();
  var popular_page = 1;
  var post_page = 1;

  moveArrow('p#by-years', "#"+year);
  moveArrow('p#by-months', "#"+month);
  getPosts("post", "monthly", "#listing-months", month, year, 20);
  jQuery("#archive_post_page > span.previous-entries").hide();

  function checkPageButton() {
    if(post_page == 1) jQuery("#archive_post_page > span.previous-entries").hide();
      else jQuery("#archive_post_page > span.previous-entries").show()
    //if(!i) $("#archive_post_page > span").hide();
   // if(i > 0 && i < 20) $("#archive_post_page > span.next-entries").hide();
      //else $("#archive_post_page > span.next-entries").show();
  }

  // if it has the id, get popular thru JSON on the first run
  jQuery("#listing-popular").each(function() {
    getPosts("popular", "monthly", "#listing-popular", null, null, 20);
  });

  jQuery("div#archive-popular > div > a").click(function() {
    var id = jQuery(this).attr('id').match(/[a-zA-Z]+$/);
    getPosts("popular", id, "#listing-popular", null, null, 20);
    moveArrow('div.heading_links', "#archive-popular-" + id);
    return false;    
  });

  
  jQuery("p#by-years > a").click(function() {
    year = jQuery(this).attr('id');
    getPosts("post", "monthly", "#listing-months", month, year, 20);
    moveArrow('p#by-years', "#"+year);
    post_page = 1;
    jQuery("#archive_post_page > span.previous-entries").hide();
    return false;
  });  

  jQuery("p#by-months > a").click(function() {
    month = jQuery(this).attr('id');
    getPosts("post", "monthly", "#listing-months", month, year, 20);
    moveArrow('p#by-months', "#"+month);
    post_page = 1;
    checkPageButton();
    return false;
  });  

  jQuery("#archive_post_page > span.next-entries > a").click(function() {
    post_page++;
    getPosts("post", "monthly", "#listing-months", month, year, 20, post_page);
    checkPageButton();
    return false;
  });  

  jQuery("#archive_post_page > span.previous-entries > a").click(function() {
    post_page--;
    getPosts("post", "monthly", "#listing-months", month, year, 20, post_page);
    checkPageButton();
    return false;
  });  

})