$(document).ready(function() {
  var prev = $("div#homenews div.pg a.prv");
  var next = $("div#homenews div.pg a.nxt");
  var newsDiv = $("div#homenews div.bdy");
  
  // Add Handlers
  var clickHandler = function() {
    // Fetch data
    var lastIdx = newsDiv.data("totalItems") - 1;
    var currIdx = newsDiv.data("currIdx");
    
    // Fade out current
    $("div.item:eq(" + currIdx.toString() + ")",newsDiv).fadeOut('slow');
    
    // Find newIdx
    if ($(this).hasClass("prv")) {
      var newIdx = (currIdx == 0) ? lastIdx : currIdx - 1;
    }
    else {
      var newIdx = (currIdx == lastIdx) ? 0 : currIdx  + 1;
    }
    
    // Update currIdx
    newsDiv.data("currIdx", newIdx);
    
    // Display new item
    $("div.item:eq(" + newIdx.toString() + ")",newsDiv).fadeIn('slow');
    return false
  };
  
  prev.click(clickHandler);
  next.click(clickHandler);
  
  // Initial Config
  newsDiv.data("totalItems", newsDiv.children().length);
  newsDiv.data("currIdx", newsDiv.children().length - 1);
  // Initial effects  
  newsDiv.children("div.item").hide();  
  next.click();
  
});

