/**
 *	This file implements the ability to randomly pick
 *	a swf file to be displayed within the 
 *
 *	@author Russell Francis
 *	@date 2006-01-11
 */

/**
 * These global constants control the switching method
 * used within the pickMovie function.
 */
var SWITCH_ON_DAY_OF_WEEK = 1;	// Max 7 entries.
var SWITCH_ON_DAY_OF_MONTH = 2;	// Max 31 entries.
var SWITCH_ON_YEAR = 3;			// unlimited but not practical for large lists.
var SWITCH_ON_HOUR = 4;			// Max 24 entries.
var SWITCH_ON_MINUTE = 5;		// Max 60 entries.
var SWITCH_ON_SECOND = 6;		// Max 60 entries.
var	SWITCH_RANDOM = 7;			// unlimited.

/**
 *	This method will select on of the movies from the movieList
 *	parameter based on the chooseMethod parameter supplied to
 *	the function.
 *
 *	@param movieList and array of movies which might get shown.
 *	@param chooseMethod The method that the movies should be chosen.
 */
function pickMovie( movieList, chooseMethod )
{
	var date = new Date();

	if( chooseMethod == SWITCH_ON_DAY_OF_WEEK )
	{
		writeMovie( movieList[ date.getDay() % movieList.length ] );
	}
	else if( chooseMethod == SWITCH_ON_DAY_OF_MONTH )
	{
		writeMovie( movieList[ date.getDate() % movieList.length ] );
	}
	else if( chooseMethod == SWITCH_ON_YEAR )
	{
		writeMovie( movieList[ date.getFullYear() % movieList.length ] );
	}
	else if( chooseMethod == SWITCH_ON_HOUR )
	{
		writeMovie( movieList[ date.getHours() % movieList.length ] );
	}
	else if( chooseMethod == SWITCH_ON_MINUTE )
	{
		writeMovie( movieList[ date.getMinutes() % movieList.length ] );
	}
	else if( chooseMethod == SWITCH_ON_SECOND )
	{
		writeMovie( movieList[ date.getSeconds() % movieList.length ] );
	}
	else
	{
		writeMovie( movieList[ rand( movieList.length ) ] );
	}
}

/**
 *	This function physically writes out the movie to
 *	the html document.
 *
 *	@param movie The movie to reference in the generated html
 *		which displays the swf.
 */
function writeMovie( movie )
{
	document.write( 
"<object " +
	"classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" " +
	"codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0\" " +
	"width=\"" + movie.width + "\" height=\"" + movie.height + "\" title=\"" + movie.title + "\"> " +
		"<param " +
			"name=\"movie\" value=\"" + movie.video + "\" /> " +
        "<param " +
			"name=\"quality\" value=\"high\" /> " +
       	"<embed " +
			"src=\"" + movie.video + "\"" +
			"quality=\"high\" " +
			"pluginspage=\"http://www.macromedia.com/go/getflashplayer\" " +
			"type=\"application/x-shockwave-flash\" " +
			"width=\"" + movie.width + "\" height=\"" + movie.height +"\"> " +
		"</embed> " +
"</object>" );
}

/**
 *	A simple method to pick a random integer.  It generates
 *	a number between 0-(n-1)
 *
 * 	@param n The maximum value of the number exclusive.
 *	@return a randomly generated integer from 0 - (n - 1)
 */
function rand( n )
{
	return( Math.floor( Math.abs( Math.random() - 0.00001 ) * n ) );
}
