/*  Script 'imageScroller.js'
 *  
 *  PURPOSE: Generates an image scroller within specified div element and image directory.
 *  REQUIRES: MooTools, image directory
 */

var statusType = { PAUSED : 0, RUNNING : 1, RETRIEVING : 2 }

function imageScroller(id, directory, time)
{
	imageScroller7(id,directory,time,0,statusType.RUNNING,new Array(),0);
}

function imageScroller7(id, directory, time, index, status, images, timeout)
{
	// Public variables
	this.id = id;
	this.directory = directory;
	this.time = time;
	this.index = index;
	this.status = status;
	this.images = images;
	this.timeout = timeout;
	
	// Initialize
	new Request({
		method: 'get',
		url: 'http://www.ncenet.com/directory.php',
		onSuccess: function(ret) {
			if(ret == '' || ret.indexOf(',') == -1)
			{
				document.getElementById(id).innerHTML = 'Image Directory Not Found.';
			}
			else
			{
				images = ret.split(',');
				status = statusType.RUNNING;
				// Randomize
				index = Math.floor((images.length - 1) * Math.random());
				maintenance();
			}
		},
		onFailure: function() {
			document.getElementById(id).innerHTML = 'Directory.php Not Found.  Please Check Your Internet Connection';
		}
	}).send('directory='+directory);
	
	// Start
	function start()
	{
		status = statusType.RUNNING;
		maintenance();
	}
	
	// Forward
	function forward()
	{
		if(index == images.length - 2)
			index = 0;
		else
			index++;
		maintenance();
	}
	
	// Back
	function back()
	{
		if(index == 0)
			index = images.length - 2;
		else
			index--;
		maintenance();
	}
	
	// Pause
	function pause()
	{
		clearTimeout(timeout);
		status = statusType.PAUSED;
	}
	
	// Refresh and maintain
	function maintenance()
	{
		// Create Div Object
		var div = document.createElement('div');
			div.style.position = 'absolute';
			div.style.top = document.getElementById(id).style.top;
			div.style.left = document.getElementById(id).style.left;
		/*
			ADDED SECTION
		*/
		var a = document.createElement('a');
		var alternatetext = 'alt text';
		switch(images[index])
		{
			case 'images/headers/header_civil_engineering.jpg':
				a.href = 'civil_engineering.php';
				alternatetext = 'Civil Engineering Services';
				break;
			case 'images/headers/header_environmental_engineering.jpg':
				a.href = 'environmental_engineering.php';
				alternatetext = 'Environmental Engineering Services';
				break;
			case 'images/headers/header_transportation_research.jpg':
				a.href = 'transportation_research.php';
				alternatetext = 'Transportation Research Services';
				break;
			case 'images/headers/header_pavement_engineering.jpg':
				a.href = 'pavement_engineering.php';
				alternatetext = 'Pavement Engineering Services';
				break;
			case 'images/headers/header_watershed_management.jpg':
				a.href = 'watershed_management.php';
				alternatetext = 'Watershed Science and Planning Services';
				break;
			case 'images/headers/header_geotech_engineering.jpg':
				a.href = 'geotechnical_engineering.php';
				alternatetext = 'Geotechnical Engineering Services';
				break;
		}

		// Create Image Object
		var img = document.createElement('img');
			img.style.visibility = 'hidden';
			img.setAttribute('src', images[index]);
			img.setAttribute('id', 'temp'+index);
			img.setAttribute('alt', alternatetext);
			a.title=alternatetext;
		a.appendChild(img);
		div.appendChild(a);
		document.getElementById(id).appendChild(div);
				
		new Fx.Morph('temp'+index, {
			duration: 'long',
			wait: false
		}).start({
			'opacity': [0,1]
		}).chain(function() {
			if(document.getElementById(id).childNodes.length > 1)
				document.getElementById(id).removeChild(document.getElementById(id).childNodes[0]);
		});
		
		
		if(status == statusType.RUNNING)
		{
			if(index == images.length - 2)
				index = 0;
			else
				index++;
			timeout = setTimeout(maintenance, time);
		}
	}
}