﻿/*
Modified version of Robert Hashemian's (http://www.hashemian.com/) text countdown clock
Written for www.aggienetwork.com as image clock
*/

function calcage(secs, num1, num2) {
  var s = ((Math.floor(secs/num1))%num2).toString();
  if (s.length < 2) {
	 s = "0" + s;
  }
  return s;
}

function getWidth(img) {
	 var newWidth = "21";
	 
	 if (img == "1") {
		  newWidth = "15";
	 }
	 else if (img == "2" || img == "6" || img == "8" || img == "9") {
		  newWidth = "20";
	 }
	 else if (img == "3" || img == "5") {
		  newWidth = "19";
	 }

	 return newWidth;
}

function CountBack(secs) {
  if (secs < 0) 
  {
    return;
  }
  
  var widthToSub = 135;
  var firstW = 135;
  var curWidth = 0;
  
  var d0 = calcage(secs,86400,100000);
  if (d0.length > 2)
  {
    d0 = calcage(secs,86400,100000).substring(0,1);
	 var d1 = calcage(secs,86400,100000).substring(1,2);
	 var d2 = calcage(secs,86400,100000).substring(2,3);
  }
  else
  {
	 d0 = "0";
	 var d1 = calcage(secs,86400,100000).substring(0,1);
	 var d2 = calcage(secs,86400,100000).substring(1,2);
  }
  
  var h1 = calcage(secs,3600,24).substring(0,1);
  var h2 = calcage(secs,3600,24).substring(1,2);
  
  var m1 = calcage(secs,60,60).substring(0,1);
  var m2 = calcage(secs,60,60).substring(1,2);
  
  var s1 = calcage(secs,1,60).substring(0,1);
  var s2 = calcage(secs,1,60).substring(1,2);
  
  document.getElementById("d0").src = "images/" + d0 + ".png";
  curWidth = getWidth(d0);
  document.getElementById("d0t").setAttribute("width", curWidth);
  widthToSub = parseInt(widthToSub) + parseInt(curWidth);
  firstW = parseInt(firstW) + parseInt(curWidth);
  
  document.getElementById("d1").src = "images/" + d1 + ".png";
  curWidth = getWidth(d1);
  document.getElementById("d1t").setAttribute("width", curWidth);
  widthToSub = parseInt(widthToSub) + parseInt(curWidth);
  
  document.getElementById("d2").src = "images/" + d2 + ".png";
  curWidth = getWidth(d2);
  document.getElementById("d2t").setAttribute("width",curWidth);
  widthToSub = parseInt(widthToSub) + parseInt(curWidth);
  
  document.getElementById("h1").src = "images/" + h1 + ".png";
  curWidth = getWidth(h1);
  document.getElementById("h1t").setAttribute("width",curWidth);
  widthToSub = parseInt(widthToSub) + parseInt(curWidth);
  
  document.getElementById("h2").src = "images/" + h2 + ".png";
  curWidth = getWidth(h2);
  document.getElementById("h2t").setAttribute("width",curWidth);
  widthToSub = parseInt(widthToSub) + parseInt(curWidth);
  
  document.getElementById("m1").src = "images/" + m1 + ".png";
  curWidth = getWidth(m1);
  document.getElementById("m1t").setAttribute("width", curWidth);
  widthToSub = parseInt(widthToSub) + parseInt(curWidth);
  
  document.getElementById("m2").src = "images/" + m2 + ".png";
  curWidth = getWidth(m2);
  document.getElementById("m2t").setAttribute("width", curWidth);
  widthToSub = parseInt(widthToSub) + parseInt(curWidth);
  
  document.getElementById("s1").src = "images/" + s1 + ".png";
  curWidth = getWidth(s1);
  document.getElementById("s1t").setAttribute("width", curWidth);
  widthToSub = parseInt(widthToSub) + parseInt(curWidth);
  
  document.getElementById("s2").src = "images/" + s2 + ".png";
  curWidth = getWidth(s2);
  document.getElementById("s2t").setAttribute("width", curWidth);
  widthToSub = parseInt(widthToSub) + parseInt(curWidth);
  
  widthToSub = 332 - parseInt(widthToSub);
  var td = document.getElementById("spacer");
  td.setAttribute("width", widthToSub);
  
  if (d1=="0" && d2=="1") { 
	 document.getElementById("day").src = "images/day.png";
  }
  else { 
	 document.getElementById("day").src = "images/days.png"; 
  }
  
  setTimeout("CountBack(" + (secs+CountStepper) + ")", SetTimeOutPeriod);  
}

TargetDate = "4/15/2011 01:00 PM UTC-0600";
CountStepper = -1;

var SetTimeOutPeriod = (Math.abs(CountStepper)-1)*1000 + 990;
var dthen = new Date(TargetDate);
var dnow = new Date();
if(CountStepper>0) {
  ddiff = new Date(dnow-dthen);
}
else {
  ddiff = new Date(dthen-dnow);
}
gsecs = Math.floor(ddiff.valueOf()/1000);
CountBack(gsecs);
