// JavaScript Document

/*
	To use this script 
	
	REQUIRED
	
	1) Include it in the header of the page.
	2) Add a div to your html and giv it an ID 
	3) Under this close tag for this div copy and paste the below code from "Start Required" to "End Required"
	4) Uncomment and edit the variables within "Start Required" and set them
		
		Set timerDiv to equil the ID of the div created in step 2
		To set the end date Change endDate to the desired time in this format ("DD Month, YYYY HH:MM:SS");
	
	5) The last line has to be "endGame();" with out quotes.  This should be placed below the Required or Optional if used
	
	
	OPTIONAL	
	
	Copy in desired items and edit as needed
	
	
	kaboom - is the variable set what wil be displayed when timer reaches 0
	num - is an array defining the images for numbers
	numsec - is an array defining the images for seconds if they are different then dasy, hours or minutes
	bullet - defines the image to use for the bullet seporator

*/

/* ************************************************************ 
	
				START REQUIRED 
	
************************************************************ */	

/*var timerDiv = "timerCountdown";
var endDate = new Date("16 April, 2010 10:35:00");*/


/* ************************************************************ 
	
				END REQUIRED 
	
************************************************************ */	

/* ************************************************************ 
	
				START OPTIONAL 
	
************************************************************ */	
/*	
	<script>
	



var kaboom ="The timer has run out";

var num = ["<img src='/images/timer/clk_w_0.gif'/>",
			   "<img src='/images/timer/clk_w_1.gif'/>",
			   "<img src='/images/timer/clk_w_2.gif'/>",
			   "<img src='/images/timer/clk_w_3.gif'/>",
			   "<img src='/images/timer/clk_w_4.gif'/>",
			   "<img src='/images/timer/clk_w_5.gif'/>",
			   "<img src='/images/timer/clk_w_6.gif'/>",
			   "<img src='/images/timer/clk_w_7.gif'/>",
			   "<img src='/images/timer/clk_w_8.gif'/>",
			   "<img src='/images/timer/clk_w_9.gif'/>"];

var numsec = ["<img src='/images/timer/clk_g_0.gif'/>",
			   "<img src='/images/timer/clk_g_1.gif'/>",
			   "<img src='/images/timer/clk_g_2.gif'/>",
			   "<img src='/images/timer/clk_g_3.gif'/>",
			   "<img src='/images/timer/clk_g_4.gif'/>",
			   "<img src='/images/timer/clk_g_5.gif'/>",
			   "<img src='/images/timer/clk_g_6.gif'/>",
			   "<img src='/images/timer/clk_g_7.gif'/>",
			   "<img src='/images/timer/clk_g_8.gif'/>",
			   "<img src='/images/timer/clk_g_9.gif'/>"];

var bullet = "<img src='/images/timer/clk_w_sep.gif'>";
*/

/* ************************************************************ 
	
				END OPTIONAL 
	
************************************************************ */








var todaysDate = 0;                 
var timeLeft = 0;
var days = 0;
var hoursLeft = 0;
var endString = "";

var endDate;
var num;
var numsec;
var bullet;
var kaboom;
var timerDiv;

function endGame(){
	if(isDefined(endDate)){
		// Get system time
		var today = new Date();	
		todaysDate = today.getTime();
		
			//Find their difference, and convert that into seconds.                  
			timeLeft = Math.round((endDate - todaysDate)/1000);
		
			// If less then 0 force to 0
			if(timeLeft < 0){
				timeLeft = 0;
			}
		if(timeLeft != 0){
			//convert to days
			days = Math.floor((timeLeft / (60 * 60 * 24)));
			
			//convert to hours
			timeLeft %= (60 * 60 * 24);
			hoursLeft = Math.floor(timeLeft / (60 * 60));
			
			//convert to minutes
			timeLeft %= (60 * 60);	
			minutesLeft = Math.floor(timeLeft / 60);
			
			//convert to seconds
			timeLeft %= 60;
			secondsLeft = timeLeft;
			
			
			days = days.toString();
			dayArray = days.split("");
			
			
			hoursLeft = hoursLeft.toString();
			hoursArray = hoursLeft.split("");
			
			minutesLeft = minutesLeft.toString();
			minutesArray = minutesLeft.split("");
			
			secondsLeft = secondsLeft.toString();
			secondsArray = secondsLeft.split("");
			
			buildString(dayArray, "d", " ");
			buildString(hoursArray, "h", " ");
			buildString(minutesArray, "m", " ");
			buildString(secondsArray, "s", "s");
			if(document.getElementById(timerDiv)){ 
				document.getElementById(timerDiv).innerHTML = endString.toString();
				//document.write(endString.toString());
			}
			else{
				//alert("Testing Please ignore this");
			}
			endString = "";
			delete today;
			
			var t=setTimeout("endGame();",1000);
		}
		else{
			
			//endString = "KABOOM";
			if(isDefined(kaboom)){
				endString = kaboom;		
			}
		}
	}
	else{
		document.getElementById(timerDiv).innerHTML = "Please define an END DATE";	
	}
}

function buildString(myArray,q,t){
	/*if(myArray.length == 1){
		endString = endString + "<img src='/wp-content/themes/default/images/timer/clk_"+t+"_0.gif'>";
	}*/
	if(t == "s" && isDefined(numsec)){
		who = "numsec";
	}
	else{
		
		who = "num";
	}
	if(isDefined(eval(who))){
		if(q == "d" && myArray.length < 3){
		
			for(b = 0; b <= myArray.length - 2; b++){
				endString = endString + eval(who)[0];
			}
		}
		if(q == "h" && myArray.length < 2){
			
			for(b = 0; b < myArray.length; b++){
				endString = endString + eval(who)[0];
			}
		}
		if(q == "m" && myArray.length < 2){
			
			for(b = 0; b < myArray.length; b++){
				endString = endString + eval(who)[0];
			}
		}
		if(q == "s" && myArray.length < 2){
			
			for(b = 0; b < myArray.length; b++){
				endString = endString + eval(who)[0];
			}
		}
		
		for(a = 0; a < myArray.length; a++){		
			switch(myArray[a]){
				case "0":
					endString = endString + eval(who)[0];
				break;    
				case "1":
					endString = endString + eval(who)[1];
				break;
				case "2":
					endString = endString + eval(who)[2];
				break;
				case "3":
					endString = endString + eval(who)[3];
				break;
				case "4":
					endString = endString + eval(who)[4];
				break;
				case "5":
					endString = endString + eval(who)[5];
				break;
				case "6":
					endString = endString + eval(who)[6];
				break;
				case "7":
					endString = endString + eval(who)[7];
				break;
				case "8":
					endString = endString + eval(who)[8];
				break;
				case "9":
					endString = endString + eval(who)[9];
				break;
				default:
				// ERROR
			}		
		}
	}
	else{
		if(q == "d" && myArray.length < 3){
		
			for(b = 0; b <= myArray.length - 2; b++){
				endString = endString + "0";
			}
		}
		if(q == "h" && myArray.length < 2){
			
			for(b = 0; b < myArray.length; b++){
				endString = endString + "0";
			}
		}
		if(q == "m" && myArray.length < 2){
			
			for(b = 0; b < myArray.length; b++){
				endString = endString + "0";
			}
		}
		if(q == "s" && myArray.length < 2){
			
			for(b = 0; b < myArray.length; b++){
				endString = endString + "0";
			}
		}
		
		for(a = 0; a < myArray.length; a++){		
			switch(myArray[a]){
				case "0":
					endString = endString + "0";
				break;    
				case "1":
					endString = endString + "1";
				break;
				case "2":
					endString = endString + "2";
				break;
				case "3":
					endString = endString + "3";
				break;
				case "4":
					endString = endString + "4";
				break;
				case "5":
					endString = endString + "5";
				break;
				case "6":
					endString = endString + "6";
				break;
				case "7":
					endString = endString + "7";
				break;
				case "8":
					endString = endString + "8";
				break;
				case "9":
					endString = endString + "9";
				break;
				default:
				// ERROR
			}		
		}
		
	}
	if(t != "s"){
		if(isDefined(bullet)){			
			endString = endString + bullet;
			
		}
		else{
			endString = endString + ".";
		}
	}	
	return endString;
}

// Check if a variable has been defined
// Note variable must be created.  Will return true if it has been set a value.  false if not

function isDefined(variable){
	if(typeof(variable) == 'undefined' ){
			return false;
	}
	else{
		return true;
	}
}


