/*
	Author:		Robert Hashemian (http://www.hashemian.com/)
  	Modified by:	Munsifali Rashid (http://www.munit.co.uk/)
  	Modified by:	Tilesh Khatri
  	Modified by:	Karel Mokry - additional parametrisation, refresh function added
*/
/*
Use: all time parameters are in minutes!
<script src="./countdown_2.js" language="JavaScript" type="text/javascript"></script>
<script language="JavaScript" type="text/javascript">
  StartRefresh("clock1","1","04/08/2009 13:20", "100", "Next", "reload", "in", "loading...");
  StartCountDown("clock2","04/08/2009 13:53", "2", "Activity will start in", "", "Activity in progress.", "Activity finished.");
</script>
*/

function StartRefresh(myElement,ReloadFreq,myTargetDate,myTargetDuration, myReloadPre, myReload, myReloadIn, myReloadInProgress)
{
  var dthen	 = new Date(myTargetDate);
  var dnow	 = new Date();
  ddiff		 = new Date(dthen-dnow);
  gsecs		 = Math.floor(ddiff.valueOf()/1000);
  gsecsT     = gsecs + myTargetDuration*60;
  ReloadFreq = ReloadFreq*60;

  if (typeof(myReloadPre)=="undefined")  myReloadPre = "Pristi";
  if (typeof(myReload)=="undefined") myReload = "reload";
  if (typeof(myReloadIn)=="undefined") myReloadIn = "za";
  if (typeof(myReloadInProgress)=="undefined") myReloadInProgress = "nacitam...";

  if (!document.getElementById(myElement)) {
    document.write("<span id='" + myElement + "'>" + myElement + "</span>");
  }
  CountBackReload(myElement, ReloadFreq, Math.min(gsecsT, ReloadFreq), gsecs, gsecsT, myReloadPre, myReload, myReloadIn, myReloadInProgress);
}

function StartCountDown(myElement,myTargetDate,myTargetDuration,myBefPre,myBefPost,myInProgress,myFinished)
{
  var dthen	= new Date(myTargetDate);
  var dnow	= new Date();
  ddiff		= new Date(dthen-dnow);
  gsecs		= Math.floor(ddiff.valueOf()/1000);
  gsecsT    = gsecs + myTargetDuration*60;

  if (typeof(myBefPre)=="undefined")  myBefPre = "";
  if (typeof(myBefPost)=="undefined") myBefPost = "";
  if (typeof(myInProgress)=="undefined") myInProgress = "defProg";
  if (typeof(myFinished)=="undefined") myFinished= "defFin";

  if (!document.getElementById(myElement)) {
    document.write("<span id='" + myElement + "'>" + myElement + "</span>");
  }
  CountBack(myElement, gsecs, gsecsT, myBefPre, myBefPost, myInProgress, myFinished);
}

function CountBackReload(myElement, ReloadFreq, ReloadT, secs, secsT, myReloadPre, myReload, myReloadIn, myReloadInProgress)
{
  // refresh text + conditions
  if (ReloadFreq <= 0) {
    myReloadText = "";
  } else {	
	if (ReloadFreq < 60) {
	  strTimer = formatTimeout(ReloadT, 0, 0);
	} else if (ReloadFreq < 3600) {
	  strTimer = formatTimeout(ReloadT, 0, 1);
    } else {
	  strTimer = formatTimeout(ReloadT, 1, 1);
    }	

    myReloadText = myReloadPre + ' <a href="javascript:window.location.reload()">' + myReload + '</a> ' + myReloadIn + ' ' + strTimer;
    if (ReloadT == 0) {
      myReloadText = myReloadInProgress;
	  window.location.reload();
    }
  }	

  // display conditions
  if (secs > 0) {	
    document.getElementById(myElement).innerHTML = "Before";
    setTimeout("CountBackReload('" + myElement + "'," + ReloadFreq + "," + (ReloadT-1) + "," + (secs-1) + "," + (secsT-1) + ",'" + myReloadPre + "','" + myReload + "','" + myReloadIn + "','" + myReloadInProgress + "');", 990);
  } else if (secsT > 0) {
    document.getElementById(myElement).innerHTML = myReloadText;
    setTimeout("CountBackReload('" + myElement + "'," + ReloadFreq + "," + (ReloadT-1) + "," + (secs-1) + "," + (secsT-1) + ",'" + myReloadPre + "','" + myReload + "','" + myReloadIn + "','" + myReloadInProgress + "');", 990);
  } else {
    document.getElementById(myElement).innerHTML = "Finished";
  }  
}

function CountBack(myElement, secs, secsT, myBefPre, myBefPost, myInProgress, myFinished)
{
  // CountDown text 
  DisplayStr = formatTimeout(secs, 1, 1);
  
  // display conditions
  if (secs > 0) {	
    document.getElementById(myElement).innerHTML = myBefPre + " " + DisplayStr + " " + myBefPost;
    setTimeout("CountBack('" + myElement + "'," + (secs-1) + "," + (secsT-1) + ",'" + myBefPre + "','" + myBefPost + "','" + myInProgress + "','" + myFinished + "');", 990);
  } else if (secsT > 0) {
    document.getElementById(myElement).innerHTML = myInProgress;
    setTimeout("CountBack('" + myElement + "'," + (secs-1) + "," + (secsT-1) + ",'" + myBefPre + "','" + myBefPost + "','" + myInProgress + "','" + myFinished + "');", 990);
  } else {
    document.getElementById(myElement).innerHTML = myFinished;
  }
}

function Calcage(secs, num1, num2)
{
  s = ((Math.floor(secs/num1))%num2).toString();
  if (s.length < 2) {	
    s = "0" + s;
  }
  return (s);
}

function formatTimeout(secs, showH, showM)
{
  var DisplayStr;
  var DisplayFormat = "%%D%% Days %%H%%:%%M%%:%%S%%";
  nD = Calcage(secs,86400,100000);
  nH = Calcage(secs,3600,24);
  nM = Calcage(secs,60,60);
  nS = Calcage(secs,1,60);
  
  if (nD==0) {
    if (showH == 1) {
      DisplayFormat = "%%H%%:%%M%%:%%S%%"; 
	} else if (showM == 1) {
      DisplayFormat = "%%M%%:%%S%%"; 
    } else {
      DisplayFormat = "%%S%%"; 
    }	
  } else if (nD==1) { 
    DisplayFormat = "%%D%% den %%H%%:%%M%%:%%S%%";
  } else if (nD<=1) { 
    DisplayFormat = "%%D%% dny %%H%%:%%M%%:%%S%%";
  } else {
    DisplayFormat = "%%D%% dn&iacute; %%H%%:%%M%%:%%S%%";
  }
  DisplayStr = DisplayFormat.replace(/%%D%%/g,	nD);
  DisplayStr = DisplayStr.replace(/%%H%%/g,	nH);
  DisplayStr = DisplayStr.replace(/%%M%%/g,	nM);
  DisplayStr = DisplayStr.replace(/%%S%%/g,	nS);

  return DisplayStr;  
}
