// Programed by Jose Alberto Martinez
// Remember that on Date, january is month 0 !!!

function countdown(date,id) {
	var countdownObj = new getObj(id);
  var html = ""
	
	var digits = getDigits(date);
	html += "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class='countdown'><tr>"
	for( i=0 ; i<9 ; i++) {
		switch (i) {
		case 3:
		case 5:
		case 7:
			html += "<td><div class=\"countdown_sep\"></div></td>";
		default:
			html += "<td id='" + id + "_" + i + "' class='countdown_digit'><div class=countdown_" + digits[i] + "></div></td>"
			break;
		}
	}
	html += "</tr><tr><td colspan=4 class='countdown_letter'>D</td><td colspan=3 class='countdown_letter'>H</td><td colspan=3 class='countdown_letter'>M</td><td colspan=2 class='countdown_letter'>S</td></table>";
  countdownObj.obj.innerHTML = html;
	
	loop(date,id);
}

function loop(date,id) {
	var digits = getDigits(date);
  showCountdown(date,id,digits);
  setTimeout("loop( " + date + " , '" + id + "' );",1000);
}

function showCountdown(date,id,digits) {
  var countdownDigitObj;
	var html;
	for( i=0 ; i<9 ; i++) {
		countdownDigitObj =  new getObj(id + "_" + i);
		html = countdownDigitObj.obj.innerHTML;
		if( html.charAt( html.indexOf("_")+1 )!= digits[i] ) {
			countdownDigitObj.obj.innerHTML = "<div class=countdown_" + digits[i] + "></div>";
		}
	}
}

function getDigits(date) {

	var digits = new Array();
  var today = parseInt((new Date()).getTime() / 1000);
	var time = parseInt(date/1000) - today;

	// Segundos
	var resto = time % 10;
	digits[8] = resto;
	time = parseInt( time / 10 );
	resto = time % 6;
	digits[7] = resto;
	time = parseInt( time / 6 );
	
	// Minutos
	resto = time % 10
	digits[6] = resto;
	time = parseInt( time / 10 );
	resto = time % 6;
	digits[5] = resto;
	time = parseInt( time / 6 );

  // Horas
	var hours = time % 24;
	resto = hours % 10;
	digits[4] = resto;
	hours = parseInt( hours / 10 );
	digits[3] = hours;
	time = parseInt( time / 24 );
	
	// Dias
	resto = time % 10;
	digits[2] = resto;
	time = parseInt( time / 10 );
	resto = time % 10;
	digits[1] = resto;
	time = parseInt( time / 10 );
	digits[0] = time;  // Solo 3 cifras 

	return digits;
}
