/*
Based on the script by:
Author: Robert Hashemian http://www.hashemian.com/
*/

function calcage(secs, num1, num2) {
  return (Math.floor(secs/num1))%num2;
}

function LaunchNow() {
  window.location.href = FinishUrl;
}

function CountBack(secs) {
  if (secs <= 0) {
    document.getElementById("cntdwn").innerHTML = FinishMessage;
//    if (secs == 0) {
//      setTimeout("LaunchNow()", 5*1000); // Jump in 5 seconds.
//    }
    return;
  }
  days = calcage(secs,86400,100000);
  hours = calcage(secs,3600,24);
  minutes = calcage(secs,60,60);
  seconds = calcage(secs,1,60);

  DisplayStr = "Live in: ";
  if (days > 1) {
    DisplayStr += DaysFormat.replace(/%%D%%/g, days);
  } else if (days == 1) {
    DisplayStr += DayFormat.replace(/%%D%%/g, days);
  }

  if (days != 0 || hours > 1) {
    DisplayStr += HoursFormat.replace(/%%H%%/g, hours);
  } else if (days != 0 || hours == 1) {
    DisplayStr += HourFormat.replace(/%%H%%/g, hours);
  }

  if ((days != 0 || hours != 0) || minutes > 1) {
    DisplayStr += MinutesFormat.replace(/%%M%%/g, minutes);
  } else if ((days != 0 || hours != 0) || minutes == 1) {
    DisplayStr += MinuteFormat.replace(/%%M%%/g, minutes);
  }

  if ((days != 0 || hours !=0 || minutes != 0) || seconds > 1) {
    DisplayStr += SecondsFormat.replace(/%%S%%/g, seconds);
  } else if ((days != 0 || hours != 0 || minutes != 0) || seconds == 1) {
    DisplayStr += SecondsFormat.replace(/%%S%%/g, seconds);
  }

  document.getElementById("cntdwn").innerHTML = DisplayStr;
  if (CountActive)
    setTimeout("CountBack(" + (secs+CountStepper) + ")", SetTimeOutPeriod);
}

if (typeof(CountActive)=="undefined")
  CountActive = true;
if (typeof(FinishMessage)=="undefined")
  FinishMessage = "";
if (typeof(CountStepper)!="number")
  CountStepper = -1;


DayFormat = "<span class='number' style='font-weight: bold; font-size: 20px;'>%%D%%</span> Day, ";
DaysFormat = "<span class='number' style='font-weight: bold; font-size: 20px;'>%%D%%</span> Day, ";
HourFormat = "<span class='number' style='font-weight: bold; font-size: 20px;'>%%H%%</span> Hour, ";
HoursFormat = "<span class='number' style='font-weight: bold; font-size: 20px;'>%%H%%</span> Hours, ";
MinuteFormat = "<span class='number' style='font-weight: bold; font-size: 20px;'>%%M%%</span> Minute, ";
MinutesFormat = "<span class='number' style='font-weight: bold; font-size: 20px;'>%%M%%</span> Minutes, ";
SecondFormat = "<span class='number' style='font-weight: bold; font-size: 20px;'>%%S%%</span> Seconds.";
SecondsFormat = "<span class='number' style='font-weight: bold; font-size: 20px;'>%%S%%</span> Seconds.";
FinishMessage = "<span class='finishmessage' style='font-weight: bold; color: #ED2102; font-size: 25px;'><a href='" + FinishUrl + "' title='Live now!' style='font-weight: bold; color: #ED2102; font-size: 25px;'>Live now!</a></span>";

CountStepper = Math.ceil(CountStepper);
if (CountStepper == 0)
  CountActive = false;
var SetTimeOutPeriod = (Math.abs(CountStepper)-1)*1000 + 990;
document.write("<span id='cntdwn'></span>");

var dthen;
if (new Date(new Date(TargetDate1).getTime() + LiveNowLinkTime * 60 * 1000) > (new Date())) {
  dthen = new Date(TargetDate1);
} else if (new Date(new Date(TargetDate2).getTime() + LiveNowLinkTime * 60 * 1000) > (new Date())) {
 dthen = new Date(TargetDate2);
} else if (new Date(new Date(TargetDate3).getTime() + LiveNowLinkTime * 60 * 1000) > (new Date())) {
  dthen = new Date(TargetDate3);
} else if (new Date(new Date(TargetDate4).getTime() + LiveNowLinkTime * 60 * 1000) > (new Date())) {
  dthen = new Date(TargetDate4);
} else {
  dthen = new Date();
  CountActive = false;
}

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);
