﻿// itse matikka
function calcage(secs, num1, num2) {
  s = ((Math.floor(secs/num1))%num2).toString();
  if (s<10){return "<b>0" + s + "</b>";}
  else {return "<b>" + s + "</b>";}
}
function calcageday(secs, num1, num2) {
  s = ((Math.floor(secs/num1))%num2).toString();
  if (s<10){return "<b>" + s + "</b>";}
  else {return "<b>" + s + "</b>";}
}

//mita tehdaan jos aika mennyt ohitse
function CountBack(secs) {
  if (secs < 0) {
    document.getElementById("cntdwn").innerHTML = FinishMessage;
    return;
  }
//paatetaan mitä naytetaan...

if (calcage(secs,86400,100000)=="<b>01</b>"){
  DisplayStr = DisplayFormat.replace(/%%D%%/g,"<p>Tapahtumaa jäljellä:</p><p><font size=\"50px\">"+ calcageday(secs,86400,100000) + " päivä</font></p>");}
else{DisplayStr = DisplayFormat.replace(/%%D%%/g,"<p>Tapahtumaa jäljellä:</p><p><font size=\"50px\">"+ calcageday(secs,86400,100000)+ " päivää</font></p>");}


if (calcage(secs,3600,24)=="<b>01</b>"){
  DisplayStr = DisplayStr.replace(/%%H%%/g, calcage(secs,3600,24)+" tunti");}
else{DisplayStr = DisplayStr.replace(/%%H%%/g, calcage(secs,3600,24)+" tuntia");}


if (calcage(secs,60,60)=="<b>01</b>"){
  DisplayStr = DisplayStr.replace(/%%M%%/g, calcage(secs,60,60)+" minuutti");}
else{DisplayStr = DisplayStr.replace(/%%M%%/g, calcage(secs,60,60)+" minuuttia");}

if (calcage(secs,1,60)=="<b>01</b>"){
  DisplayStr = DisplayStr.replace(/%%S%%/g, calcage(secs,1,60)+" sekunti!</br>");}
else{DisplayStr = DisplayStr.replace(/%%S%%/g, calcage(secs,1,60)+" sekuntia!</br>");}

//tulostus ja cufon paeivitys
  document.getElementById("cntdwn").innerHTML = DisplayStr;
  if (CountActive)
    setTimeout("CountBack(" + (secs+CountStepper) + ")", SetTimeOutPeriod);
Cufon.refresh();
}
//harvelin alustus
function putspan(background, foreground) {
 document.write("<span id='cntdwn' ></span>");
}

if (typeof(BackColor)=="undefined")
  BackColor = "white";
if (typeof(ForeColor)=="undefined")
  ForeColor= "black";
if (typeof(TargetDate)=="undefined")
  TargetDate = "10/30/2011 11:00 AM";
if (typeof(DisplayFormat)=="undefined")
  DisplayFormat = "%%D%% %%H%%, %%M%% ja %%S%%";
if (typeof(CountActive)=="undefined")
  CountActive = true;
if (typeof(FinishMessage)=="undefined")
  FinishMessage = "<p></p><p>Tapahtuma on päättynyt!</p>";
if (typeof(CountStepper)!="number")
  CountStepper = -1;
if (typeof(LeadingZero)=="undefined")
  LeadingZero = true;

//tarkastetaan aika ja tehaan timeout
CountStepper = Math.ceil(CountStepper);
if (CountStepper == 0)
  CountActive = false;
var SetTimeOutPeriod = (Math.abs(CountStepper)-1)*1000 + 990;
putspan(BackColor, ForeColor);
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);

