﻿var DELAY = 3.0;
var SCROLL = 0.03;
var container;
var messages;
var subMessages;
var currentMessageIndex = -1;
var currentSubMessageIndex = -1;

function ScrollMessages() {
  // Get the messages
  container = $('messages');
  messages = container.getElementsByClassName('message');
  
  // Set the height of the container to the greatest height
  // of its submessages
  for (i = 0; i < messages.length; i++) {
    subMessageContainer = messages[i].getElementsByClassName('subMessages')[0];
	  subMessages = subMessageContainer.getElementsByClassName('subMessage');
	  // Display the first subMessage of every message
    Element.show(subMessages[0]);
	  // Find and set the highest hight value
	  for (j = 0; j < subMessages.length; j++) {
	    h1 = Element.getHeight(subMessages[j]);
	    h2 = Element.getHeight(subMessageContainer);
	    if (h1 >= h2)
        subMessageContainer.style.height = h1 + 'px';
	  }
  }
    
  // If there are more than two messages begin scrolling them
  // If there aren't any messages don't display them
  if (messages.length > 2) {
    container.scrollTop = messages[0].offsetTop;
    ScrollNextMessage();
  } else if (messages.length == 0) {
    container.style.display = 'none';
  }
}

function ScrollNextMessage() {
  // Increment to the next subMessage
  currentMessageIndex++;
	ScrollToNextMessage();
}

function ScrollToNextMessage() {
  // If the next message hasn't reached the top of the
  // container continue scrolling it up otherwise attempt
  // to display the submessages
	if (container.scrollTop < messages[currentMessageIndex].offsetTop) {
	  // Addjust the scroll position so the items appear
	  // to be scrolling up
    container.scrollTop++;
    setTimeout("ScrollToNextMessage()", SCROLL * 1000);
	} else {	
    // If the current message is the first message
    // in the second half of the list, reset the list
    if (currentMessageIndex >= messages.length / 2) {
      // Reset the currentMessageIndex to the begining
      currentMessageIndex = 0;
      
      // Reset all the subMessages display order
      for (i = 0; i < messages.length; i++) {
        subMessageContainer = messages[i].getElementsByClassName('subMessages')[0];
        subMessages = subMessageContainer.getElementsByClassName('subMessage');
        // Display the first subMessage of every message
        Element.show(subMessages[0]);
        // Hide the remaining messages
        for (j = 1; j < subMessages.length; j++)
          Element.hide(subMessages[j]);
      }

      // Start the messages over
		  container.scrollTop = messages[currentMessageIndex].offsetTop;
    }
	
	  DisplaySubMessages();
	}
}

function DisplaySubMessages() {
  // Get the subMessages of the current message
	subMessages = messages[currentMessageIndex].getElementsByClassName('subMessage');
	
	if (subMessages.length > 1) {
    // Reset the currentSubMessageIndex
    currentSubMessageIndex = -1;
		//setTimeout("DisplayNextSubMessage()", DELAY * 1000);
		DisplayNextSubMessage();
	} else {
    setTimeout("ScrollNextMessage()", DELAY * 1000);
	}
}

function DisplayNextSubMessage() {
  // Increment to the next subMessage
  currentSubMessageIndex++;
  
  // If there are more subMessages to display, display
  // them otherwise display the next message
  if (currentSubMessageIndex < subMessages.length) {
    // If a message is already displayed hide the old message
    if (currentSubMessageIndex > 0) {
	    Effect.Fade(subMessages[currentSubMessageIndex - 1], {queue: {position:'end', scope: 'myScope'}});
      Effect.Appear(subMessages[currentSubMessageIndex], {queue: {position:'end', scope: 'myScope'}});
    }
		setTimeout("DisplayNextSubMessage()", DELAY * 1000);
  } else {
	  ScrollNextMessage();
  }
}

Event.observe(window, 'load', ScrollMessages);

