// This script and images which this script used
// are released under CC-BY license (http://creativecommons.org/licenses/by/2.5/tw/).
// By using, editing and/or distributing this script
// you agree to the terms and conditions of this license. 

// Countdowner (C) Mosky <mosky9180@gmail.com> 2009
// Version: 2.1 (091014)

function Countdowner(name)
{
	this.name = name
	this.marks = [];
	this.texts = [];
	this.now = new Date();
	this.offset = 0;
	this.clearOffset = 0;
	this.addMark = function(text, date)
	{
		this.texts.push(text);
		this.marks.push(date);
	}
	this.update = function()
	{
		this.offset = 0;
		while(this.offset < this.marks.length && this.marks[this.offset].getTime() < this.now.getTime())
			this.offset++;
		if(this.offset == this.marks.length) this.offset = this.marks.length-1
	}
	this.get = function(idx)
	{
		idx = ((idx + this.offset) + this.marks.length) % this.marks.length
		return [this.texts[idx], this.marks[idx]]
	}
}

function stdizeReleaseCountdowner(cd)
{
	cd.idx = 0
	cd.mouseover = false
	tmp = document.getElementById(cd.name)
	tmp.pointer = cd
	tmp.onmouseover = function()
	{
		tmp = document.getElementById(cd.name).pointer
		tmp.output(true)
	}
	tmp.onmouseout = function()
	{
		tmp = document.getElementById(cd.name).pointer
		tmp.output(false)
	}

	cd.output = function(mouseover)
	{
		function ft(num) //format
		{
			num = ('00'+String(num)).split('')
			return num[num.length-2]+num[num.length-1]
		}
		
		var data, diff, tail;
		data = this.get(this.idx);
	
		diff = Math.ceil((data[1].getTime() - this.now.getTime()) / (1000*60*60*24));
		if(mouseover)		tail = 'on ' + ft(data[1].getYear()) + '/' + ft(data[1].getMonth()+1) + '/' + ft(data[1].getDate()) + ' !'
		else if(diff == 0)	tail = 'Today!';
		else if(diff > 0)	tail = 'days to go...';
		else if(diff < 0)	tail = 'days ago.';
		td = document.getElementById(this.name).getElementsByTagName('td');
		td[1].innerHTML = data[0];
		td[3].innerHTML = Math.abs(diff);
		td[5].innerHTML = tail;
	}
}
