/******************************************************************************
 * University of Wisconsin Platteville
 *
 * University Centers Network Support - ResNet
 *
 * slideshow.js
 *
 * This file contains a class called Image that takes links to images and changes
 * the displayed picture after a set interval of time occurs.
 *
 * @author Adam Nowicki
 * @copyright 2007 ResNet Development
 */

/**
 * @param myID	is the id of the tag where the picture will be placed.
 * @param dir	is the directory where the slide pictures are located.
 * @param fdir	is the directory where the full sized pictures are located.
 */
function Slideshow(myID, dir, fdir)
{
	Slideshow.instance = this;	
	
	/**
	 * Default extension for images
	 *
	 * @var string
	 * @access private
	 */
	Slideshow.DEFAULT_EXT = ".jpg";
	
	/**
	 * ID of the random picture
	 *
	 * @var integer
	 * @access private
	 */
	var id = myID;
	
	/**
	 * Delay in miliseconds
	 *
	 * @var integer
	 * @access private
	 */
	var delay = 4000;
	
	/**
	 * Create a link on each picture for full screen
	 */
	var fullimage = false;
	
	/**
	 * The directory the images are located
	 *
	 * @var string
	 * @access private
	 */
	var directory_thumbs = "./";
	
	var directory_full = "./";
	
	/**
	 * Image file extension
	 *
	 * @var string;
	 * @access private;
	 */
	var extension = Slideshow.DEFAULT_EXT;
	
	/**
	 * Vector containing image file names
	 *
	 * @var Vector
	 * @access private
	 */
	var images = new Vector();
	
	/**
	 * Vector containing image file names in the random order
	 *
	 * @var Vector
	 * @access private
	 */
	var order = new Vector();
	
	/* Constructor Stuff */
	
	if ( dir != null )
		directory_thumbs = dir;
	
	if ( directory_thumbs.substring(directory_thumbs.length - 1, directory_thumbs.length) != "/" )
		directory_thumbs += "/";
		
	if ( fdir != null )
		directory_full = fdir;
	
	if ( directory_full.substring(directory_full.length - 1, directory_full.length) != "/" )
		directory_full += "/";
	
	/* Methods */
	
	this.add = function(file)
	{
		images.push(file);
		this.randomize();
	}
	
	this.setFullSize = function(fullsize)
	{
		if ( fullsize == true )
			fullimage = true;
		else
			fullimage = false;
	}
	
	this.addSequence = function(start, end, ext)
	{
		if ( !isInteger(start) || !isInteger(end) )
			throw Exception("Sequence start and end must be integers");
		
		if ( ext == null )
			ext = extension;
		
		if ( ext.substring(0,1) != "." )
			ext = "." + ext;
		
		for ( var i = start; i <= end; i++ )
		{
			this.add(i + ext);
		}
		
		this.randomize();
	}
	
	this.randomize = function()
	{
		order.clear();
		var oldImages = images.clone();
		
		while ( !oldImages.isEmpty() )
		{
			var rand = getRandom( 0, oldImages.getSize() - 1 );
			order.push( oldImages.get(rand) );
			oldImages.remove(rand);
		}
	}
	
	this.getImages = function()
	{
		return images;
	}
	
	this.getOrder = function()
	{
		return order;
	}
	
	this.start = function(myDelay)
	{
		if ( myDelay != null )
			delay = myDelay;

		order.reset();
		this.nextImage();
	}
	
	this.nextImage = function()
	{
		if ( order.next() == false )
		{
			order.first();
		}
		
		
		var element = document.getElementById(id);
		var pic = order.get();
		var slidepic = directory_thumbs + pic;
		var fullpic = directory_full + pic;
		
		var link = "<a href='" + fullpic + "' target='_blank'>";
		var img = "<img src='" + slidepic + "' />";
		
		if ( fullimage == true ) 
		{
			element.innerHTML = link + img + "</a>";
		}
		else
		{
			element.innerHTML = img;
		}
			
		setTimeout("Slideshow.instance.nextImage()", delay);
	}
	
	this.getId = function()
	{
		return id;
	}
	
	this.getDelay = function()
	{
		return delay;
	}
	
	function getRandom(low, high)
	{
		return Math.round( (Math.random() * high) + low );
	}
}