var start = 0;
var currentStory = -1;

var activeStory = -1;

var thumbsDisplayed = 3;

var fading = false;
var delay = 250;
var inLine = -1;

window.setInterval('loadNextStory()', 5000);

function parsePostElements() {

  var curURL = location.href;
  var temp = curURL.split('?')[1];

  if (temp == null)
    return null;

  var peices = temp.split("&");

  var elements = new Array();

  for (var i=0; i<peices.length; i++) {
    var p = peices[i].split("=");

    elements[p[0]] = p[1];
  }

  return elements;
}

function generateStoryDisplay() {

  var elements = parsePostElements();
  var storyHolder = document.getElementById("StudentCongressStoryHolder");
  var value = "";

  if (elements["story"] == null)
     return;

  var st = elements["story"];

  value += "<H3>"+StoryHeadlines[st]+"</H3>";

  value += "<table textwrap='close' border='0' align='right'>";
  value += "<caption>"+PhotoCaptions[StoryPhotos[st]]+"</caption>";
  value += "<tr><td><img SRC=\"stories/"+StoryPhotos[st]+"\" NOSAVE BORDER=\"0\" alt=\""+PhotoCaptions[StoryPhotos[st]]+"\">";
  value += "</table>";

  var body = StoryBodies[st];
  body = body.replace(/\n\n/g, "<p>");
  body = body.replace(/\n/g, "<BR>");

  var image = body.match(/\[photo:.*\]/);

  while (image != null) {
     var imageFile = String(image);
     imageFile = imageFile.substr(7, imageFile.length - 8);

     var imageHTML = "<table textwrap='close' border='1'>";
     imageHTML    += "<caption>"+PhotoCaptions[imageFile]+"</caption>";
     imageHTML    += "<tr><td><img SRC=\"stories/"+imageFile+"\" NOSAVE BORDER=\"0\" alt=\""+PhotoCaptions[imageFile]+"\">";
     imageHTML    += "</table>";

     body = body.replace(image, imageHTML);
     image = body.match(/\[photo:.*\]/);
  }


  var emails = body.match(/\s\S+@\S+\s/g);

  for (var i = 0; i < emails.length; i++) {
    var email = emails[i].replace(/\s/g, "");

    body = body.replace(emails[i], " <a href=\"mailto:"+email+"\">"+email+"</a> "); 
  }


  value += "<p>"+body;

  storyHolder.innerHTML = value;
}

function generateStoryElements() {

  var storyHolder = document.getElementById("StudentCongressStoryHolder");

  var value = "";

  value  += "<TABLE BORDER='0' class='story' width='100%'>";

  value  += "<TR VALIGN='top'><TD width='50%'><TD class='storyBox'>";
  value  += "  <img id=\"STORY-PHOTO\" ALT=\"LOADING\" align='center'>				";
  value  += "  <DIV id=\"STORY-TEXT\" class=\"storyHolder\" align=\"center\"><TD width='50%'>";

  value  += "<TD>";
  value  += "  <DIV id=\"THUMBNAIL\" class=\"thumbnailHolder\">			";
  value  += "  <DIV id=\"STORY-THUMBS\" class=\"thumbnail\"></div>		";

  value  += "<TR><TD COLSPAN='4'>";
  value  += "    <DIV id=\"STORY-PAGE\" class=\"thumbnailHolder thumbPages\"></DIV>";
  value  += "  </DIV>								";
  value  += "</TABLE>";


  storyHolder.innerHTML = value;

  loadThumbnails(0);
  loadStory(0);
}

function loadStory(st) {

  if (st == currentStory) {
    return;
  }
  if (fading) {
    inLine = st;
    return;
  }

  fading = true;
  inLine = -1;

  var storyHolder = document.getElementById("STORY-TEXT");

  storyHolder.innerHTML = "<H3><a href='story.html?story="+st+"'>"+StoryHeadlines[st]+"</a></h3><p>"+StoryIntros[st]+"</p>";

  opacity("STORY-PHOTO", 100, 0, delay);

  setTimeout("changeImage("+st+")", delay+10);

  if (document.getElementById("Thumbnail"+currentStory) != null) {
    document.getElementById("Thumbnail"+currentStory ).setAttribute("class", "thumbnail");
  }
  document.getElementById("Thumbnail"+st).setAttribute("class", "activethumbnail");

  setPageDisplay(st);
  currentStory = st;
}

function loadThumbnails(st){

  start = st;
  
  var end = (start + 3 < StoryHeadlines.length) ? start + 3 : StoryHeadlines.length;

  var value = "<TABLE width='1' height='1'><TR>";

  for (var i = start; i < end; i++) { 
      value += "<TR id=\"Thumbnail"+i+"\"><TD><a href='story.html?story="+i+"' onmouseover='loadStory("+i+"); setActiveStory("+i+")' onmouseout='clearActiveStory()' ><img src='stories/"+StoryPhotos[i]+"' border='0'><div class='textHolder'>"+StoryCaptions[i]+"</div></a></TD>";
  }
   
  value += "</TABLE>";

  document.getElementById("STORY-THUMBS").innerHTML = value;
  setPageDisplay(start);

  if (document.getElementById("Thumbnail"+currentStory) != null) {
    document.getElementById("Thumbnail"+currentStory).setAttribute("class", "activethumbnail");
  }

}

function setPageDisplay(st) {
  var last, next;

  if (start >= 4)
    last = start-3;
  else if (start == 0)
    last = StoryHeadlines.length - 3;
  else
    last = 0;
  
  if (last < 0)
    last=0;

  if (start + 6 < StoryHeadlines.length)
    next = start+4;
  else if (start + 3 >= StoryHeadlines.length)
    next = 0;
  else
    next = StoryHeadlines.length - 3;
    

  var value = "<TABLE><TR>";

  value += "<TD width='100%'>"+(st + 1) + " of " + (StoryHeadlines.length);

  value += "<TD><a href='javascript:loadThumbnails("+last+")'>&lt;</a>";
  value += "<TD><a href='javascript:loadThumbnails("+next+")'>&gt;</a>";

  value += "</TABLE>";

  document.getElementById("STORY-PAGE").innerHTML = value;

}

function loadNextStory() {
  if (activeStory >= 0)
    return;

  var nextStory = ((currentStory+1) % StoryHeadlines.length);

  var nextThumb = parseInt(nextStory / thumbsDisplayed) * thumbsDisplayed;

  if (nextThumb + thumbsDisplayed >= StoryHeadlines.length)
	nextThumb = StoryHeadlines.length - thumbsDisplayed;

  loadThumbnails(nextThumb );
  loadStory(nextStory);
}

function setActiveStory(st) {
  activeStory = st;
}
function clearActiveStory() {
  activeStory = -1;
}

function opacity(id, opacStart, opacEnd, millisec) { 
    //speed for each frame 
    var speed = Math.round(millisec / 100); 
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens 
    if(opacStart > opacEnd) { 
        for(i = opacStart; i >= opacEnd; i--) { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } else if(opacStart < opacEnd) { 
        for(i = opacStart; i <= opacEnd; i++) 
            { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    }
} 


//change the opacity for different browsers 
function changeOpac(opacity, id) { 
    var object = document.getElementById(id).style; 
    object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")"; 
} 

function changeImage(st) {
  document.getElementById("STORY-PHOTO").src = "stories/" + StoryPhotos[st];
  document.getElementById("STORY-PHOTO").alt = PhotoCaptions[StoryPhotos[st]];

  opacity("STORY-PHOTO", 0, 100, delay);
  setTimeout("fading = false;", delay+10);
  
  if (inLine >= 0) {
    setTimeout("setPageDisplay("+inLine+")", delay);
  }
}