/*
*
* Copyright (c) 2006 Millstream Web Software http://www.millstream.com.au
* 
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* * 
*/

/*
* Class: Countdown
* Version: 1.1 - 19/10/2006
* Written by: Andrew Tetlaw http://tetlaw.id.au
* Displays a visual count down timer
* Option:default
* year:1970 // target year
* month:01 // target month (01 - 12)
* day:01 // target day (01 - 31)
* hour:00 // target hour
* minute:00 // target minute
* second:00  // target second
* interval:1  // how often to update the display
* format : this.elm.innerHTML || '{d} : {h} : {m} : {s}' // The display template
* autoStart : true // starts the timer automatically
*/
function Countdown(elm, options) {
  this.elm = (typeof elm == 'string') ? document.getElementById(elm) : elm;
	if(!this.elm) return;
	this.options = this.extend({
		year: 2010,
		month:01,
		day:01,
		hour:00,
		minute:00,
		second:00,
		interval : 1,
		format : this.elm.innerHTML || '{d} : {h} : {m} : {s}',
		autoStart : true
	}, options || {});
	this.timer = false;
	if(this.options.autoStart) this.start();
}


Countdown.prototype.update = function() {
	var now = new Date();
	var end = new Date(
		this.options.year,
		this.options.month - 1,
		this.options.day,
		this.options.hour,
		this.options.minute,
		this.options.second
	);
	var diff = end - now;
	var days    = this._gN(parseInt(diff / 86400000));
	var hours   = this._gN(parseInt((diff % 86400000) / 3600000));
	var minutes = this._gN(parseInt((diff % 3600000) / 60000));
	var seconds = this._gN(parseInt((diff % 60000) / 1000));
	var html = '{d} <em>Diwrnodau</em> {h} <em>Oriau</em> {m} <em>Munudau</em> {s} <em>Eiliadau</em> ';
	html = html.replace(/\{d\}/, days);
	html = html.replace(/\{h\}/, hours);
	html = html.replace(/\{m\}/, minutes);
	html = html.replace(/\{s\}/, seconds);
	//console.log(html);
	if(days+minutes+hours+seconds > 0)	this.elm.innerHTML = html;
	else this.elm.innerHTML='This game should now be playing';
	
	if(this.options.interval > 0) this.timer = setTimeout(this.update.bind(this), this.options.interval * 1000);
}
Countdown.prototype.start = function() {
	this.update();
}
Countdown.prototype.stop = function() {
	if(this.timer) clearTimeout(this.timer);
}
Countdown.prototype.extend = function(o, s) {
 	for (var p in s) {
  	o[p] = s[p];
	}
 	return o;
}
Countdown.prototype._gN = function(n) {
	n = Math.max(n,0) + '';
	if (n.length == 1) n = '0' + n;
	return n;
}
//http:// prototype.conio.net
var $A = Array.from = function(iterable) {
	if (!iterable) return [];
	if (iterable.toArray) {
		return iterable.toArray();
	} else {
		var results = [];
		for (var i = 0; i < iterable.length; i++)
			results.push(iterable[i]);
		return results;
	}
}

Function.prototype.bind = function() {
	var __method = this, args = $A(arguments), object = args.shift();
	return function() {
		return __method.apply(object, args.concat($A(arguments)));
	}
}

