var currentTab;
var currentSection;
var scrollbox;
var position = 0;
var interval;

var tabs = new Object();
tabs['search'] = "search";
tabs['recent-posts'] = "recent-posts";
tabs['recent-comments'] = "recent-comments";
tabs['highlights'] = "openxtra-most-popular-post";
tabs['tags'] = "tag_cloud";
tabs['calendar'] = "calendar";

var tablist = ['recent-posts', 'recent-comments','search',  'highlights', 'tags', 'calendar'];
var tabIndex = 0;
var cyclingInterval;

function startCycling() {
  cycleInterval = setInterval('cycleTabs()', 10000);
  cycleTabs();
}

function stopCycling() {
  clearInterval(cycleInterval);
}

function cycleTabs() {
  showTab(tablist[tabIndex]);

  if(++tabIndex >= tablist.length){
    tabIndex = 0;
  }

}

function click(id){
  stopCycling();
  showTab(id);
}

function showTab(id)
{
  var tab = id + "-tab";
  var section = tabs[id];
  
  var activeTab = document.getElementById(tab);
  var activeSection = document.getElementById(section);

  if (currentTab) {
      currentTab.className = "inactive";
      currentSection.className = "inactive";
  }

  activeTab.className = "active";
  activeSection.className = "active";

  currentTab = activeTab;
  currentSection = activeSection;

  scrollbox = document.getElementById('sidebar');

  if(interval != null){
   clearInterval(interval);
  }

  interval = setInterval("scroll();", 15);
}

function scroll() {
  var destination = -currentSection.offsetLeft;
  var delta = destination - position;

  if(Math.abs(delta) > 20)
    position += delta/5;
  else if(delta != 0)
    position += delta;
  else
    clearInterval(interval);

  scrollbox.style.left = (position) + "px";
}
