var urlStr;
var bvDebug,bMonth,bDay,bHour,bHinute;
var tdelay=400;    //  (1000=1 second)- see below
var monthArray = new Array(13);
var nowDate,billionDate;
var backwards,fullDaysToGo, fullMinutesToGo, fullSecondsToGo,totalSecondsToGo,counterNum,ink;

function setCounter()
{
 nowDate = new Date();
 setUpBillionDate();
 sortFromAndToDates();
      if(bvDebug=="1") {
      alert("document.URL=" + document.URL + "\nmonth=" + bMonth + "\nday=" + bDay +"\nhour=" + bHour + "\nbMinute=" + bMinute + "\nfullDaysToGo=" + fullDaysToGo + "\nfullMinutesToGo=" + fullMinutesToGo + "\nfullSecondsToGo=" + fullSecondsToGo + "\ncounterNum=" + counterNum +"\nbvDebug=" + bvDebug);
      }
 primeGraphics(); 
 ink=(27*(tdelay/400));   // equivalent to approx 37 per Second (1000=1 second)- also adjust where this occurs below to reset this number
 inkCt();
}

function setUpBillionDate()
{
 urlStr = new String(document.URL);
 bvDebug=getQueryString("bv_debug=","NONE");
 bMonth=getQueryString("month=","1");
 bDay=getQueryString("day=","20"); 
 bHour=getQueryString("hour=","1"); 
 bMinute=getQueryString("minute=","40"); 
 billionDate = new Date(); 
 setUpMonthArray();
}


function getQueryString(qsName,defaultValue)
{
var dumStr = new String(qsName);
var qsPos,qsStr = new String();

 qsStr=defaultValue;

   if((qsPos=urlStr.indexOf(dumStr))>0) {
   qsStr=urlStr.substring(qsPos+dumStr.length,urlStr.length);
     if(qsStr.indexOf("&")>-1) {
     dumStr=qsStr.substring(0,qsStr.indexOf("&"));
     qsStr=dumStr;
     }

      if(bvDebug=="1") {
      alert("qsPos=" + qsPos + "\n qsStr=" + qsStr);
      }
   }

 return qsStr;
}


function setUpMonthArray()
{
 monthArray[0]=31;
 monthArray[1]=29;
 monthArray[2]=31;
 monthArray[3]=30;
 monthArray[4]=31;
 monthArray[5]=30;
 monthArray[6]=31;
 monthArray[7]=31;
 monthArray[8]=30;
 monthArray[9]=31;
 monthArray[10]=30;
 monthArray[11]=31;
}




function sortFromAndToDates()
{

var frDate= new Date(),toDate = new Date();
var frMins,frSec,toMins,toSec;
 fullDaysToGo=0;
   if(dateBefore(billionDate,nowDate)) { //If We Have PASSED The 'setDate'
   backwards=true; 
   frDate=billionDate;
   frMins= ((billionDate.getHours() * 60 ) + billionDate.getMinutes());
   frSec=billionDate.getSeconds();
   toDate=nowDate;
   toMins= ( (nowDate.getHours() * 60 ) + nowDate.getMinutes());
   toSec=nowDate.getSeconds();
   } else {
   frDate=nowDate;
   frMins= ( (nowDate.getHours() * 60 ) + nowDate.getMinutes() );
   frSec=nowDate.getSeconds();
   toDate=billionDate;
   toMins=( (billionDate.getHours() * 60 ) + billionDate.getMinutes() )
   toSec=billionDate.getSeconds();
   }

     if( frDate.getMonth() < toDate.getMonth()) {
      fullDaysToGo = fullDaysToGo + getDaysTilEndOfMonth(frDate.getMonth(),frDate.getDate());
      fullDaysToGo=fullDaysToGo + getDaysBetweenMonths(frDate.getMonth(),toDate.getMonth());
      fullDaysToGo=fullDaysToGo + toDate.getDate()-1; //The Day Before
         
          if(backwards) {fullDaysToGo = -fullDaysToGo;}

      } else {
          if(backwards) {
          fullDaysToGo=fullDaysToGo + (frDate.getDate()-toDate.getDate()); //Same Month
          } else {
          fullDaysToGo=fullDaysToGo + (toDate.getDate()-frDate.getDate()); //Same Month
          }
      }


     if(backwards) {
     fullMinutesToGo=(frMins-toMins);
     } else {
     fullMinutesToGo=(toMins-frMins);
     }
   
     if(backwards) {
     fullSecondsToGo = (frSec-toSec);
     } else {
     fullSecondsToGo = (toSec-frSec);
     }
 totalSecondsToGo=(((fullDaysToGo*24*60*60) + (fullMinutesToGo*60) + fullSecondsToGo) * 27); //57 per Second
 counterNum= (00000000)-(totalSecondsToGo);
}

function getDaysTilEndOfMonth(theMonth,theDay)
{//Full Days
var dayTEOM;
 dayTEOM=monthArray[theMonth] - theDay;
 return dayTEOM;
}

function getDaysBetweenMonths(startMonth,endMonth)
{//Full Days
var n,fDays=0;
       if(startMonth==endMonth) {return fDays;}
    for(n=startMonth+1;n<endMonth;n++){
    fDays+= monthArray[n];
    }
 return fDays;
}


function dateBefore(chkDate, datumDate) 
{//Returns True IF chkDate<datumDate
   if(chkDate.getFullYear() > datumDate.getFullYear()) {return false;} //Too Late
      if(chkDate.getFullYear() == datumDate.getFullYear() ) {
         if(chkDate.getMonth() > datumDate.getMonth()) {return false;} //Month Too Late
         if(chkDate.getMonth() == datumDate.getMonth()) {
            if(chkDate.getDate() >= datumDate.getDate()) {return false;} //Day Too Late
         }
      }
 return true;
}


function primeGraphics() 
{
var d=document;
 d.num = new Array();
 d.num[0] = new Image(); d.num[0].src = "images/counter/0num.png";
 d.num[1] = new Image(); d.num[1].src = "images/counter/1num.png";
 d.num[2] = new Image(); d.num[2].src = "images/counter/2num.png";
 d.num[3] = new Image(); d.num[3].src = "images/counter/3num.png";
 d.num[4] = new Image(); d.num[4].src = "images/counter/4num.png";
 d.num[5] = new Image(); d.num[5].src = "images/counter/5num.png";
 d.num[6] = new Image(); d.num[6].src = "images/counter/6num.png";
 d.num[7] = new Image(); d.num[7].src = "images/counter/7num.png";
 d.num[8] = new Image(); d.num[8].src = "images/counter/8num.png";
 d.num[9] = new Image(); d.num[9].src = "images/counter/9num.png";
}


function inkCt()
{
 layoutGNumbers((counterNum+=ink));
 window.setTimeout("inkCt()",tdelay);
 return true;
}

function layoutGNumbers(num)
{
var n,charStr,mod,start;
var numStr = new String(num);
 start=numStr.length;
    for(n=start;n>=1;n-=1) {
    charStr=numStr.charAt(n-1);
    document.images['img' + (start-(n-1))].src =  document.num[parseInt(charStr)].src;   
    }
}
