var storyCount = 1;
var storyDiv = Array();
while(nextDiv = document.getElementById('story_' + storyCount)) {
   storyDiv[storyCount] = nextDiv;
   storyCount++;
}
storyCount--;
storyContainer = document.getElementById('rotateContainer');
var currentStory = 1;
storyContainer.appendChild(storyDiv[currentStory]);

// rotate stories
function rotateStory() {
   // remove current story div
   storyContainer.removeChild(storyDiv[currentStory]);
   // calculate next story div
   if (currentStory == storyCount) {
      currentStory = 1;
   }
   else {
      currentStory++;
   }
   //alert(currentStory);

   // set opacity of new storyDiv to 0
   storyDiv[currentStory].style.opacity = 0;
   // append storyDiv to container
   storyContainer.appendChild(storyDiv[currentStory]);
   // gradually increase opacity
   fadeToDiv(storyDiv[currentStory]);
   
}

function fadeToDiv(divToRevealObj) {
 setOpacity(divToRevealObj.id, 0);
 //divToRevealObj.style.display = "block";
 fadeInDiv(divToRevealObj.id,0);
}
function fadeInDiv(divId, opacity) {
 if (opacity <= 100) {
 setOpacity(divId, opacity);
 opacity += 5;
 fade = window.setTimeout("fadeInDiv('"+divId+"',"+opacity+")", 100);
 }
}
function setOpacity(divId, opacity) {
   if (div = document.getElementById(divId)) {
   //div = document.getElementById(divId)
      opacity = (opacity == 100)?99.999:opacity;
      div.style.filter = "alpha(opacity:"+opacity+")";
      div.style.KHTMLOpacity = opacity/100;
      div.style.MozOpacity = opacity/100;
      div.style.opacity = opacity/100;
   }
}

function prev() {
   clearInterval(rotator); 
   storyContainer.removeChild(storyDiv[currentStory]);
   if (currentStory == 1) {
      currentStory = storyCount;
   }
   else {
      currentStory--;
   }
   storyDiv[currentStory].style.opacity = 0;
   // append storyDiv to container
   storyContainer.appendChild(storyDiv[currentStory]);
   // gradually increase opacity
   fadeToDiv(storyDiv[currentStory]);
   rotator = setInterval('rotateStory()', 5000);
}
function next() {
   clearInterval(rotator);
   storyContainer.removeChild(storyDiv[currentStory]);
   if (currentStory == storyCount) {
      currentStory = 1;
   }
   else {
      currentStory++;
   }
   storyDiv[currentStory].style.opacity = 0;
   // append storyDiv to container
   storyContainer.appendChild(storyDiv[currentStory]);
   // gradually increase opacity
   fadeToDiv(storyDiv[currentStory]);
   rotator = setInterval('rotateStory()', 5000);
}

rotator = setInterval('rotateStory()', 5000);