/************************************************************************/
/*                                                                      */
/*  Multi-Timer Countdown Script v1.1                                   */
/*                                                                      */
/*  Copyright (C) 1997 Blind Canary Consulting.  All Rights Reserved.   */
/*                                                                      */
/*  Feel free to reuse or modify this script as long as you leave this  */
/*  header in the script and you send me an email with the address of   */
/*  the page that you used it on. Thanks.                               */
/*                                                                      */
/*      The latest version of this script is available at:              */
/*                                                                      */
/*      <http://www.toto.com/dreimiller/mcd/coding.html>                */
/*                                                                      */
/*  ==================================================================  */
/*                                                                      */
/*      When I wrote this script I started with a script written        */
/*      by someone else. I rewrote a large part of that script but      */
/*      portions of the section of this script which calculates         */
/*      the time remaining weren't changed siginficantly so...          */
/*                                                                      */
/*  ******************************************************************  */
/*                                                                      */
/*      Copyright (C) 1996  Glub, Un-Inc.  All Rights Reserved.         */
/*      Feel free to reuse or modify this code,                         */
/*      provided this header remains in tact.                           */
/*      [http://www.dotdotcom.com/] [http://sdcc8.ucsd.edu/~gcohen/]    */
/*                                                                      */
/************************************************************************/

/* Count Down Script - start */

/* Initializations */

	isRunning = false;
	timersID = null;
	secTics = 1000;
	minTics = 60 * secTics;
	hourTics = 60 * minTics;
	dayTics = 24 * hourTics;
	avgEventLength = 2 * hourTics;
	

function defineTimer (timerName, timerDate, arrayOfDates)
{
	this.name = timerName;
	this.date = timerDate;
    this.eventsArray = arrayOfDates;

	this.secsLeft = 0;
	this.secsRound = 0;
	this.secsRemain = 0;
	this.minsLeft = 0;
	this.minsRound = 0;
	this.minsRemain = 0;
	this.timeRemain = 0;
	this.timeRemainDays = 0;
	this.eventTime = null;

	this.calculateTimeRemaining = calculateTimeRemaining;
	
} /* End defineTimer */


function calculateTimeRemaining()
{
   /* Figure out what the current event is */

	var rightNow = new Date();	 
	
	var i = 1;
	while ( (this.eventsArray[i].getTime() + avgEventLength) < rightNow.getTime())
		i++;
	this.eventTime = this.eventsArray[i];

	if (rightNow.getTime() < this.eventTime.getTime())
	{
		/*Seconds*/
	
		this.secsLeft = (this.eventTime.getTime() - rightNow.getTime()) / 	minTics;
		this.secsRound = Math.round(this.secsLeft);
		this.secsRemain = this.secsLeft - this.secsRound;
		this.secsRemain = (this.secsRemain < 0) ?
		   this.secsRemain = 60 - ((this.secsRound - this.secsLeft) * 60) :
		   this.secsRemain = (this.secsLeft - this.secsRound) * 60;
		this.secsRemain = Math.round(this.secsRemain);
	
	
		/*Minutes*/
	
		this.minsLeft = ((this.eventTime.getTime() - rightNow.getTime()) / 	hourTics);
		this.minsRound = Math.round(this.minsLeft);
		this.minsRemain = this.minsLeft - this.minsRound;
		this.minsRemain = (this.minsRemain < 0) ?
		   this.minsRemain = 60 - ((this.minsRound - this.minsLeft)  * 60) :
		   this.minsRemain = ((this.minsLeft - this.minsRound) * 60);
		this.minsRemain = Math.round(this.minsRemain - 0.495);
	
	
		/*Hours*/
	
		this.hoursLeft = ((this.eventTime.getTime() - rightNow.getTime()) / 	dayTics);
		this.hoursRound = Math.round(this.hoursLeft);
		this.hoursRemain = this.hoursLeft - this.hoursRound;
		this.hoursRemain = (this.hoursRemain < 0) ?
		   this.hoursRemain = 24 - ((this.hoursRound - this.hoursLeft)  * 24) :
		   this.hoursRemain = ((this.hoursLeft - this.hoursRound) * 24);
		this.hoursRemain = Math.round(this.hoursRemain - 0.5);
	
		/*Days*/
	
		this.daysLeft = ((this.eventTime.getTime() - rightNow.getTime()) / 	dayTics);
		this.daysLeft = (this.daysLeft - 0.5);
		this.daysRound = Math.round(this.daysLeft);
		this.daysRemain = this.daysRound;
	
		/*Time*/
	
		if (this.daysRemain > 0)
          {this.timeRemainDays = this.daysRemain + "d, ";}
		
        this.timeRemain = this.timeRemainDays + this.hoursRemain + "h, "
        								  + this.minsRemain + "m, "
        								  + this.secsRemain + "s";
		  
	}
	else
		{this.timeRemain = "Game in Progress";}


    this.currentEvent = this.eventTime.toLocaleString();
    
   	document.timer.elements[this.date].value = this.currentEvent;
	document.timer.elements[this.name].value = this.timeRemain;
	this.running = true;

} /* End calculateTimeRemaining */


/* updateTimers */

function updateTimers(arrayOfTimers)
{
	this.timersArray = arrayOfTimers;
	for(var i = 1; i<= this.timersArray.length; i++)
		this.timersArray[i].calculateTimeRemaining();
	timerID = setTimeout("updateTimers(arrayOfTimers)",1000);
} /* End updateTimers */


/* This function stops the timers */

function stopTimers()
{
	if(isRunning)
		clearTimeout(timersID);
	isRunning = false;
} /* End stopTimers */


/* Used to create array-like objects */

function makeArray(n)
{
	this.length = n;
	for(var i = 1; i<= n; i++)
		this[i] = 0;
	return this;
} /* End makeArray */


/* This is the initial function. */
/* It is called by an onLoad command on the <BODY> tag. */

function createTimers ()
{
	/* To create a timer you need to create on array object */
	/* which contains the series of events you wish to track */
	/* Then call the createTimer function and pass it the array */
	
	/* This creates the event array for the Men's team */
	
	mensEvents = new makeArray(1);
		
		mensEvents[1] = new Date(Date.parse("Fri, 16 Oct 2009 17:00:00 -0400"));

	/* This creates the event array for the Women's team */
	
	womensEvents = new makeArray(2);
	
		womensEvents[1] = new Date(Date.parse("Sun, 05 Apr 2009 21:00:00 -0400"));
		womensEvents[2] = new Date(Date.parse("Tue, 07 Apr 2009 20:30:00 -0400"));

	/* This creates the event array for the Football's team */
	
	fbEvents = new makeArray(2);
	
        fbEvents[1] = new Date(Date.parse("Sat, 18 Apr 2009 12:00:00 -0400"));
        
        fbEvents[2] = new Date(Date.parse("Sat, 05 Sep 2009 12:00:00 -0400"));

	/* This creates an array of timers, and then create the two timers
	   for the Men's and Women's teams */
	arrayOfTimers = new makeArray(3);

		arrayOfTimers[1] = new defineTimer("mensTimer", "mensDate", mensEvents);

		arrayOfTimers[2] = new defineTimer("womensTimer", "womensDate", womensEvents);

		arrayOfTimers[3] = new defineTimer("fbTimer", "fbDate", fbEvents);

	stopTimers();
	updateTimers(arrayOfTimers);
	
} /* End of createTimers */

/* Count Down Script - end */