/*
Crawler.js
This "Crawler" creates an automatic crawling effect on any HTML inside two containing DIVs.

*/


// Required Global Variables
var n_containerDiv = "n_container"; // the id of the Containing DIV Block
var n_contentDiv = "n_content"; // the id of the Content DIV Block

var e_containerDiv = "e_container"; // the id of the Containing DIV Block
var e_contentDiv = "e_content"; // the id of the Content DIV Block

var crawlIncr = 1; // Must be greater than 0. Higher numbers increase scroll rate but scrolling is smoother with lower numbers.
var crawlTimeoutDelay = 100; // The Delay (in ms) of the scrolling timeout. Use with scrollIncr to control scrolling speed. Limited by processing time.
var positionUnit = "px"; // Unit of measure used when scrolling the contentDiv. 

// Private global variables, do not change these, they are used by the methods below.
var n_direction = null; // DO NOT SET MANUALLY! direction can be "up" or "down" and is set by the methods below.
var n_timeoutID = null; // DO NOT SET MANUALLY! holds the timeoutID while the timeout is set.

var e_direction = null; // DO NOT SET MANUALLY! direction can be "up" or "down" and is set by the methods below.
var e_timeoutID = null; // DO NOT SET MANUALLY! holds the timeoutID while the timeout is set.

// Cross-Browser microAPI Function

function getObj(name)
{
  if (document.getElementById)
  {
        this.obj = document.getElementById(name);
        this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
        this.obj = document.all[name];
        this.style = document.all[name].style;
  }
  else if (document.layers)
  {
        this.obj = document.layers[name];
        this.style = document.layers[name];
  }
}

function newsCrawl()
{
	// check that user is not Trying to Scroll
	if(containerDiv == "n_container") {
		clearTimeout(n_timeoutID);
		n_timeoutID = null;
		return;	
	} 
	
	// get DOM objects
	var container = new getObj(n_containerDiv);
	var content = new getObj(n_contentDiv);
	
/*	
	The idea here is to:
	1) Find out where the top of the content is.
	2) Find out the heights of the content and the container.
	3) When the content has been moved a neg number of px such that its top >= its height, set its top to the height of the container.
	4) Repeat from step 1.
	
	*/
	if(content.obj.offsetHeight >= container.obj.offsetHeight) {
		if(content.obj.offsetTop <= (container.obj.offsetHeight - content.obj.offsetHeight))
		{
			content.style.top = container.obj.offsetHeight.toString() + positionUnit;
		}
		else
		{
			content.style.top = eval(content.obj.offsetTop - crawlIncr).toString() + positionUnit;
		}
	} else {
		return;
	}
	
	n_timeoutID = setTimeout("newsCrawl()",crawlTimeoutDelay);
	
	
}

function eventsCrawl()
{
	// check that user is not Trying to Scroll
	if(containerDiv == "e_container"){
		clearTimeout(e_timeoutID);
		e_timeoutID = null;
		return;
	}
	
	// get DOM objects
	var container = new getObj(e_containerDiv);
	var content = new getObj(e_contentDiv);
	
/*	
	The idea here is to:
	1) Find out where the top of the content is.
	2) Find out the heights of the content and the container.
	3) When the content has been moved a neg number of px such that its top >= its height, set its top to the height of the container.
	4) Repeat from step 1.
	
	*/
	if(content.obj.offsetHeight >= container.obj.offsetHeight) {
		if(content.obj.offsetTop <= (container.obj.offsetHeight - content.obj.offsetHeight))
		{
			content.style.top = container.obj.offsetHeight.toString() + positionUnit;
		}
		else
		{
			content.style.top = eval(content.obj.offsetTop - crawlIncr).toString() + positionUnit;
		}
	} else {
		return;
	}
	
	e_timeoutID = setTimeout("eventsCrawl()",crawlTimeoutDelay);
	
	
}

function initCrawl()
{
	newsCrawl();
	eventsCrawl();
}

function startCrawl(crawlArea)
{
	// reset containerDiv to bypass shutdown command.
	containerDiv = null;
	
	// alert(eval(crawlArea.charAt(0) + "_timeoutID"));
	
	if(eval(crawlArea.charAt(0) + "_timeoutID") != null)
		return;
	
	// eval crawlArea to call the proper function.
	eval(crawlArea + "Crawl();");
}




