//######################################################################################
// Author: ricocheting.com
// Custom: Framelova (May-2011)
// Version: v2.0
// Date: 2011-03-31
// Description: displays the amount of time until the "dateFuture" entered below.

// NOTE: the month entered must be one less than current month. ie; 0=January, 11=December
// NOTE: the hour is in 24 hour format. 0=12am, 15=3pm etc
// format: dateFuture1 = new Date(year,month-1,day,hour,min,sec)
// example: dateFuture1 = new Date(2003,03,26,14,15,00) = April 26, 2003 - 2:15:00 pm

//We set the future date, actual day to noon night
todayBegin = new Date();
//dateFuture1 = new Date(todayBegin.getFullYear(),todayBegin.getMonth(),todayBegin.getDate(),23,59,59);
dateFuture1 = new Date(2011,9,12,20,00,00);
//dateFuture1 = new Date(2011,9,03,20,00,00);

// TESTING: comment out the line below to print out the "dateFuture" for testing purposes
//document.write("el contador vence el: " + dateFuture1 +"<br />");
//document.write("FALTAN: <br />");


//###################################
//nothing beyond this point
function GetCount(ddate,iid){

	dateNow = new Date();	//grab current date
	amount = ddate.getTime() - dateNow.getTime();	//calc milliseconds between dates
	delete dateNow;

	// if time is already past
	if(amount < 0){
		document.getElementById(iid).innerHTML='<img src="images/graciassorteo.jpg" alt="puntos"/>';
	}
	// else date is still good
	else{
		days=0;hours=0;mins=0;secs=0;out="";

		amount = Math.floor(amount/1000);//kill the "milliseconds" so just secs
		
		days=Math.floor(amount/86400);//days
		amount=amount%86400;

		hours=Math.floor(amount/3600);//hours
		amount=amount%3600;

		mins=Math.floor(amount/60);//minutes
		amount=amount%60;

		secs=Math.floor(amount);//seconds

		//if(hours != 0){out += hours +" "+((hours==1)?"hora":"horas")+", ";}
		//out += mins +" "+((mins==1)?"min":"mins")+", ";
		//out += secs +" "+((secs==1)?"sec":"secs")+", ";
		//out = out.substr(0,out.length-2);
		
		document.getElementById(iid).innerHTML=buildOut(out);

		setTimeout(function(){GetCount(ddate,iid)}, 1000);
	}
}

function buildOut(out){
	var img = '<img src="images/numbers/%.jpg" alt="%"/>';
	var ptos = '<img src="images/numbers/puntos.jpg" alt="puntos"/>';
	var strdays = days + '';
	var strmins = mins + '';
	var strhours = hours + '';
	var strsecs = secs + '';
	
	//inicio
	out += '<div id="count">FALTAN:</div>';
	
	//days
	out += ((days > 9)?img.replace('%',strdays.charAt(0))+img.replace('%',strdays.charAt(1)):img.replace('%',0) + img.replace('%',days)) + ptos; 
	
	//hours
	if(hours != 0){
		out += ((hours > 9)?img.replace('%',strhours.charAt(0))+img.replace('%',strhours.charAt(1)):img.replace('%',0) + img.replace('%',hours)) + ptos; 
	}else{
		out += img.replace('%',0) + img.replace('%',0) + ptos;
	}
	
	//minutes	
	out += ((mins > 9)?img.replace('%',strmins.charAt(0))+img.replace('%',strmins.charAt(1)):img.replace('%',0) + img.replace('%',mins)) + ptos; 
	
	//seconds
	out += ((secs > 9)?img.replace('%',strsecs.charAt(0))+img.replace('%',strsecs.charAt(1)):img.replace('%',0) + img.replace('%',secs)); 
	
	//add legends
	out += '<div id="count">&nbsp; DIAS &nbsp; &nbsp; &nbsp; HORAS &nbsp; &nbsp; &nbsp;  MIN &nbsp; &nbsp; &nbsp; SEG </div>';
	
	return out;
}

window.onload=function(){
	GetCount(dateFuture1, 'countbox');
	//you can add additional countdowns here (just make sure you create dateFuture2 and countbox2 etc for each)
};
