/* Functions to get popular and latest articles from JSON */
function getPosts (func, type, id, month, year, limit, page) {
  jQuery(id).html('<img src="/wp-content/themes/lifehack-v2/images/waiting-icon.gif"/>');
  today = new Date();
  month = month || today.getMonth() + 1;
  year = year || today.getFullYear();
  limit = limit || 10;
  page = page || 1;
  
  jQuery.getJSON("/wp-content/plugins/json-interface/json-interface.php?function="+func+"&type=" + type + "&limit=" +limit+ "&page="+page+ "&month="+month+"&year="+year,
    function(data){
      jQuery(id).empty().hide();
      if(!data[0]) jQuery(id).html("No Posts found.");
      jQuery.each(data, function(i,item){
        jQuery('<li><a href="' + item.permalink + '">' + item.post_title + '</a></li>').appendTo(id)
      });
      jQuery("ol"+ id + " > li:odd").addClass('art odd');
      jQuery("ol"+ id +" > li:even").addClass('art even');
      jQuery("ol" + id).attr({'start': 1+(limit*(page-1))})
      jQuery(id).fadeIn('slow');
    });
}

function moveArrow(parent, id) {
  jQuery(parent + " > a:not(" + id + ")").removeClass('active')
  jQuery(parent + " > a" + id).addClass('active')            
}


/* All */
/* --------------------------------------------------------------------------------- */
jQuery(function() {

  /* Front page More Article Menu */
  function zoomTopic(topic) {
    jQuery("div#posts > div." + topic).fadeIn(); 
    jQuery("div#posts > div:not(." + topic + ")").fadeOut(); 
  }

  jQuery("div#latesttopics > a").click(function() {
    var id = jQuery(this).attr('id').match(/[a-zA-Z]+$/);
    zoomTopic(id);
    moveArrow('div#latesttopics', '#post-menu-' + id);        
    return false;    
  });

  jQuery("div#latesttopics > a#post-menu-all").click(function() {  
    jQuery("div#posts > div").fadeIn(); 
    moveArrow('div#latesttopics', '#post-menu-all');
  });

  /* Lazy load on article images and author image on the sidebar */
  //jQuery("div.articleimage > img").lazyload();
  //jQuery("ul > li.writer > img").lazyload();
  /* ... and on author list as well */
  //jQuery("ul#editors-list > li > img").lazyload();
  
  jQuery("#container-1 > ul").tabs();




/* Sidebar */
/* --------------------------------------------------------------------------------- */
  /* Sidebar article archive  */
  /*jQuery("div#articles_archive > ol#sidebar_archive_monthly").hide();*/
  jQuery("div#articles_archive > ol > li:odd").addClass('art odd');
  jQuery("div#articles_archive > ol > li:even").addClass('art even');

  //jQuery("#articles_archive > ul").tabs();
  
  /* This is for cloud popup */
  jQuery('#clound-links').each(function () {
      // options
      var distance = 10;
      var time = 250;
      var hideDelay = 500;

      var hideDelayTimer = null;

      // tracker
      var beingShown = false;
      var shown = false;

      var trigger = jQuery('.clicknow', this);
      var popup = jQuery('.clound', this).css('opacity', 0);

      // set the mouseover and mouseout on both element
      jQuery([trigger.get(0), popup.get(0)]).mouseover(function () {
        // stops the hide event if we move from the trigger to the popup element
        if (hideDelayTimer) clearTimeout(hideDelayTimer);

        // don't trigger the animation again if we're being shown, or already visible
        if (beingShown || shown) {
          return;
        } else {
          beingShown = true;

          // reset position of popup box
          popup.css({
            top: -330,
            left: -200,
            display: 'block' // brings the popup back in to view
          })

          // (we're using chaining on the popup) now animate it's opacity and position
          .animate({
            top: '-=' + distance + 'px',
            opacity: 1
          }, time, 'swing', function() {
            // once the animation is complete, set the tracker variables
            beingShown = false;
            shown = true;
          });
        }
      }).mouseout(function () {
        // reset the timer if we get fired again - avoids double animations
        if (hideDelayTimer) clearTimeout(hideDelayTimer);

        // store the timer so that it can be cleared in the mouseover if required
        hideDelayTimer = setTimeout(function () {
          hideDelayTimer = null;
          popup.animate({
            top: '-=' + distance + 'px',
            opacity: 0
          }, time, 'swing', function () {
            // once the animate is complete, set the tracker variables
            shown = false;
            // hide the popup entirely after the effect (opacity alone doesn't do the job)
            popup.css('display', 'none');
          });
        }, hideDelay);
      });
    });



    jQuery("div#article_archive-menu > a.apost").click(function() {
      var id = jQuery(this).attr('id').match(/[a-zA-Z]+$/);
      getPosts("post", id, "#sidebar_archive_latest", null, null, 10);
      moveArrow('div#article_archive-menu', "#archive-post-" + id);
      return false;    
    });
    jQuery("div#article_archive-menu > a.apopular").click(function() {
      var id = jQuery(this).attr('id').match(/[a-zA-Z]+$/);
      getPosts("popular", id, "#sidebar_archive_latest", null, null, 10);
      moveArrow('div#article_archive-menu', "#archive-popular-" + id);
      return false;    
    });

/* --------------------------------------------------------------------------------- */


});                                 
