$(document).ready(function() {
  var prev = $("div.fader div.paging a.prev");
  var next = $("div.fader div.paging a.next");
  var paging = $("div.fader div.paging");
  var newsDiv = $("div.fader 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
  };
  
  if(newsDiv.children().length > 1) {
    paging.removeClass("hidefader");    
    paging.addClass("showfader");
    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();    
  }
  
});
