var countdowns = new Array();

function countdown(obj)
{
	this.obj = obj;
	this.Div = "clock";
	this.TargetDate = "12/31/2020 5:00 AM UTC+0100";
	//this.DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
	this.EventName = "Event";
	this.CountActive = true;
	
	this.ShowZeros = true;
	
	this.NameClass = "rig_countdown-name";
	this.TimeClass = "rig_countdown-time";
	this.NameClassEnded = "rig_countdown-name-ended";
	this.TimeClassEnded = "rig_countdown-time-ended";
	
	this.DisplayStr;

	this.Calcage = _Calcage;
	this.CountBack = _CountBack;
	this.Setup = _Setup;
}

function _Calcage(secs, num1, num2)
{
	s = ((Math.floor(secs / num1)) % num2).toString();
	if(s.length < 2)
	{ 
	  s = "0" + s;
  }
  return s;
}

function _CountBack(totalsecs)
{
	var days = this.Calcage(totalsecs, 86400, 100000);
	var hours = this.Calcage(totalsecs, 3600, 24);
	var mins = this.Calcage(totalsecs, 60, 60);
	var secs = this.Calcage(totalsecs, 1, 60);
	
	// If all 0, shown ended message
	//if(parseInt(days) <= 0 && parseInt(hours) <= 0 && parseInt(mins) <= 0 && parseInt(secs) <= 0)
	//{
	//	this.CountActive = false;
	//	this.DisplayStr = "<span class=\"" + this.NameClassEnded + "\">" + this.EventName + ":</span> <span class=\"" + this.TimeClassEnded + "\">Started</span>";
	//}
	//else
	{
		var added = false;
		this.DisplayStr = "<span class=\"" + this.NameClass + "\">" + this.EventName + ":</span> <span class=\"" + this.TimeClass + "\">";
		
		if(parseInt(days) > 0 || (this.ShowZeros && parseInt(days) == 0))
		{
			this.DisplayStr += days;// + " days";
			added = true;
		}
		
		if(added)
		{
			this.DisplayStr += ":";
		}
		
		if(parseInt(hours) > 0 || ((this.ShowZeros || added) && parseInt(hours) == 0))
    {
      this.DisplayStr += hours;// + " hours";
      added = true;
    }
		
		if(added)
    {
      this.DisplayStr += ":";
    }
    
    if(parseInt(mins) > 0 || ((this.ShowZeros || added) && parseInt(mins) == 0))
    {
      this.DisplayStr += mins;// + " mins";
      added = true;
    }
		
		if(added)
    {
      this.DisplayStr += ":";
    }
    
    if(parseInt(mins) > 0 || ((this.ShowZeros || added) && parseInt(mins) == 0)) // show 0 seconds
    {
      this.DisplayStr += secs;// + " secs";
    }
		
		this.DisplayStr += "</span>";
		
		if(!added && parseInt(secs) <= 0)
		{
			this.CountActive = false;
      this.DisplayStr = "<span class=\"" + this.NameClassEnded + "\">" + this.EventName + ":</span> <span class=\"" + this.TimeClassEnded + "\">Started</span>";
		}
		
		//this.DisplayStr = this.DisplayFormat.replace(/%%D%%/g, days);
	  //this.DisplayStr = this.DisplayStr.replace(/%%H%%/g, hours);
	  //this.DisplayStr = this.DisplayStr.replace(/%%M%%/g, mins);
	  //this.DisplayStr = this.DisplayStr.replace(/%%S%%/g, secs);
	}
	
	document.getElementById(this.Div).innerHTML = this.DisplayStr;
  if(this.CountActive)
  {
    setTimeout(this.obj + ".CountBack(" + (totalsecs - 1) + ")", 990);
  }
}

function _Setup(clock)
{
	var dthen	= new Date(clock.TargetDate);
  var dnow	= new Date();
	ddiff = new Date(dthen - dnow);
	gsecs = Math.floor(ddiff.valueOf() / 1000);
	clock.CountBack(gsecs);
}

function Countdown_Setup()
{
	var html = "";
	for(var i in countdowns)
	{
		html += "<div id=\"" + countdowns[i].Div + "\"></div>\n";
	}
	
	var countdownsDiv = document.getElementById("countdowns");
	if(countdownsDiv)
	{
		countdownsDiv.innerHTML = html;
		for(var i in countdowns)
	  {
	    //countdowns[i].Setup();
            _Setup(countdowns[i]);
	  }
	}
}
