/************************************************************************/
/*                                                                      */
/*  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.dreimiller.com/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;
	eventLength = 2.0 * 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.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() + eventLength) < 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)
			{if (this.daysRemain != 1)
				{if (this.hoursRemain != 1)
					{this.timeRemain = this.daysRemain + " days, " +
									   this.hoursRemain + " hours";}
				 else
					{this.timeRemain = this.daysRemain + " days, " +
									   this.hoursRemain + " hour";}
				}
			else
				{if (this.hoursRemain != 1)
						{this.timeRemain = this.daysRemain + " day, " +
									       this.hoursRemain + " hours";}
				 else
					{this.timeRemain = this.daysRemain + " day, " +
									   this.hoursRemain + " hour";}
				}
			}
		else
			{if (this.hoursRemain > 0)
				{if (this.hoursRemain != 1)
					{if (this.minsRemain != 1)
						{this.timeRemain = this.hoursRemain + " hours, " +
										   this.minsRemain + " minutes";}
					 else
						{this.timeRemain = this.hoursRemain + " hours, " +
										   this.minsRemain + " minute";}
					}
				 else
					{if (this.minsRemain != 1)
						{this.timeRemain = this.hoursRemain + " hour, " +
										   this.minsRemain + " minutes";}
					 else
						{this.timeRemain = this.hoursRemain + " hour, " +
										   this.minsRemain + " minute";}
					}
				}
			 else
				{if (this.minsRemain > 0)
					{if (this.minsRemain != 1)
						{if (this.secsRemain != 1)
							{this.timeRemain = this.minsRemain + " minutes, " +
											   this.secsRemain + " seconds";}
						 else
							{this.timeRemain = this.minsRemain + " minutes, " +
											   this.secsRemain + " second";}
						}
					 else
						{if (this.secsRemain != 1)
							{this.timeRemain = this.minsRemain + " minute, " +
											   this.secsRemain + " seconds";}
						 else
							{this.timeRemain = this.minsRemain + " minute, " +
											   this.secsRemain + " second";}
						}
					}
				else
					{if (this.secsRemain != 1)
						{this.timeRemain = this.secsRemain + " seconds";}
					 else
						{this.timeRemain = this.secsRemain + " second";}
					}
				}
			}
	}
	else
		{this.timeRemain = "Gig 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 of The Reducers gigs */

	reducersGigs = new makeArray(10);
		
		reducersGigs[1] = new Date(Date.parse("Sun, 14 Feb 2010 16:00:00 GMT-0500"));
		reducersGigs[2] = new Date(Date.parse("Tue, 16 Feb 2010 19:00:00 GMT-0500"));
		reducersGigs[3] = new Date(Date.parse("Sat, 03 Apr 2010 22:00:00 GMT-0400"));
		reducersGigs[4] = new Date(Date.parse("Sat, 12 Jun 2010 22:00:00 GMT-0400"));
		reducersGigs[5] = new Date(Date.parse("Sat, 26 Jun 2010 22:00:00 GMT-0400"));
		reducersGigs[6] = new Date(Date.parse("Sat, 10 Jul 2010 20:30:00 GMT-0400"));
		reducersGigs[7] = new Date(Date.parse("Sat, 17 Jul 2010 22:00:00 GMT-0400"));
		reducersGigs[8] = new Date(Date.parse("Sat, 24 Jul 2010 22:00:00 GMT-0400"));
		reducersGigs[9] = new Date(Date.parse("Fri, 13 Aug 2010 22:00:00 GMT-0400"));
		reducersGigs[10] = new Date(Date.parse("Sat, 04 Sep 2010 19:00:00 GMT-0400"));
		
		
	/* This creates an array of timers, and then creates a timer
	   for The Reducers */
	
	arrayOfTimers = new makeArray(1);
		arrayOfTimers[1] = new defineTimer("gigTimer", "gigDate", reducersGigs);

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

/* Count Down Script - end */