window.addEvent('domready', function() {
	
  var nextId = "";
  var newsList = $$('.newsItem');
  var newsNexts = $$('.newsItem .next');
  var pagerList = $$('#pager li');
  var showNext = function(){
	  var nextToShow = $$("#slider div.active a.next");
	  var newsNextBtn = nextToShow[0];
	  nextId = newsNextBtn.href.slice(newsNextBtn.href.indexOf("#")+1);
	  var nextItem = $(nextId);
	  AddClassName(nextItem, "active");
	  var nextP = $("newsP"+newsNextBtn.href.charAt(newsNextBtn.href.length-1));
	  AddClassName(nextP, "active");
	  
      newsList.each(function(other, j){
        if (other != nextItem){
		  RemoveClassName(other, "active");
        }
      });
	  pagerList.each(function(otherP, j){
        if (otherP != nextP){
		  RemoveClassName(otherP, "active");
        }
      });
	  
  };
  var timer = showNext.periodical(5000);
  	
  newsNexts.each(function(newsNextBtn, i){
    newsNextBtn.addEvent('click', function(e){
	  if (e.preventDefault) { e.preventDefault(); } else { event.returnValue = false; }
	  timer = $clear(timer);
	  nextId = newsNextBtn.href.slice(newsNextBtn.href.indexOf("#")+1);
	  var nextItem = $(nextId);
	  AddClassName(nextItem, "active");
	  var nextP = $("newsP"+newsNextBtn.href.charAt(newsNextBtn.href.length-1));
	  AddClassName(nextP, "active");
	  
      newsList.each(function(other, j){
        if (other != nextItem){
		  RemoveClassName(other, "active");
        }
      });
	  pagerList.each(function(otherP, j){
        if (otherP != nextP){
		  RemoveClassName(otherP, "active");
        }
      });
    });
  });
  
  var newsPrevs = $$('.newsItem .prev');
  newsPrevs.each(function(newsPrevBtn, i){
    newsPrevBtn.addEvent('click', function(e){
	  if (e.preventDefault) { e.preventDefault(); } else { event.returnValue = false; }
	  timer = $clear(timer);
	  prevId = newsPrevBtn.href.slice(newsPrevBtn.href.indexOf("#")+1);
	  var newsPrevItem = $(prevId);
	  AddClassName(newsPrevItem, "active");
	  var prevP = $("newsP"+newsPrevBtn.href.charAt(newsPrevBtn.href.length-1));
	  AddClassName(prevP, "active");
	  
      newsList.each(function(other, j){
        if (other != newsPrevItem){
		  RemoveClassName(other, "active");
        }
      });
	  pagerList.each(function(otherP, j){
        if (otherP != prevP){
		  RemoveClassName(otherP, "active");
        }
      });
	  e.stop();
    });
  });
  
   pagerList.each(function(pagerLink, i){
    pagerLink.addEvent('click', function(e){
	  if (e.preventDefault) { e.preventDefault(); } else { event.returnValue = false; }
	  timer = $clear(timer);
	  AddClassName(pagerLink, "active");
	  var pLink = pagerLink.getElement('a');
	  pLink = pLink.href;
  	  var showItem = $(pLink.slice(pLink.indexOf("#")+1));
	  AddClassName(showItem, "active");
	  
	  newsList.each(function(other, j){
        if (other != showItem){
		  RemoveClassName(other, "active");
        }
      });
	  
	  pagerList.each(function(otherP, j){
        if (otherP != pagerLink){
		  RemoveClassName(otherP, "active");
        }
      });
    });
  });
});