/*

Usage Sample:

<script language="JavaScript">
SongLength = 200; // Time in secs
CountActive = true;
CountStepper = -1;
LeadingZero = true;
StopAtZero = true; // stop counting at zero
DisplayFormat = "%%M%%:%%S%%"; //default format
</script>
<script language="JavaScript" src="http://www.desimusicmix.com/scripts/countdown.js"></script>
*/

function calcage(secs, num1, num2) {
  s = ((Math.floor(secs/num1))%num2).toString();
  if (LeadingZero && s.length < 2)
    s = "0" + s;
  return s;
}

function CountBack(secs) {
  if (secs <= 0 & StopAtZero) {
    document.getElementById("cntdwn").innerHTML = "00:00";
  }
  else
  {
	  DisplayStr = DisplayFormat.replace(/%%M%%/g, calcage(secs,60,60));
	  DisplayStr = DisplayStr.replace(/%%S%%/g, calcage(secs,1,60));
	  document.getElementById("cntdwn").innerHTML = DisplayStr;
  }
  if (CountActive)
     setTimeout("CountBack(" + (secs+CountStepper) + ")", SetTimeOutPeriod);
}

function putspan() {
 document.write("<span id='cntdwn'></span>");
}

if (typeof(SongLength)!="number")
  SongLength = 0;
if (typeof(DisplayFormat)=="undefined")
  DisplayFormat = "%%M%%:%%S%%";
if (typeof(CountActive)=="undefined")
 	CountActive = true;
if (typeof(CountStepper)!="number")
 	CountStepper = -1;
if (typeof(LeadingZero)=="undefined")
 	LeadingZero = true;
if (typeof(StopAtZero)=="undefined")
 	StopAtZero = true;


CountStepper = Math.ceil(CountStepper);
if (CountStepper == 0)
 	CountActive = false;
var SetTimeOutPeriod = (Math.abs(CountStepper)-1)*1000 + 990;
putspan();
gsecs = Math.floor(SongLength);
CountBack(gsecs);

