/*
Script: Countdown.js
		Contains <Countdown>
License:
		MIT-style license.
Class: Countdown
		Creates a countdown. Returns the time left.
Arguments:
		element - the countdown container
		options - see options below
Options:
		days - display days left
		hours - display hours left
		minutes - display minutes left
		seconds - display seconds left
		formatDays, formatHours, formatMinutes, formatSeconds - how to display your countdown, %days%, %hours%, %minutes%, %seconds% will be replaced by numbers
		message - message to display when time is up
Events:
		onTick - a function to fire every second
		onComplete - a function to fire when time is up
*/
var Countdown = new Class({
	options: {
    countplus:true,
    	weeks: true,
		days: true,
		hours: true, 
		target: false,
		formatWeeks: '%weeks%',
		formatDays: '%days%',
		formatHours: '%hours%',
		formatWeeksSing: '%weeks%',
		formatDaysSing: '%days%',
		formatHoursSing: '%hours%',
		message: 'Expired',
		onTick: Class.empty,
		onComplete: Class.empty
	},
	initialize: function(el, options){
		this.setOptions(options);
		this.el = el;
		
		var srvrDate = document.getElementById('srvrtime').value;
		
		var t = new Date(el.innerHTML);
		var targetDate = t.getTime();

		this.now = Math.round(srvrDate / 1000);
		this.target = targetDate / 1000;
		if (this.target == 'NaN') return;
		// can't proceed without a target time
		if (!this.target) return;
		this.remaining = this.target - this.now;
		// target has already passed

		if (this.remaining < 0 && this.options.countplus==false) {
			this.done();
			return;
		}
		//this.tick();
		this.start();
	},
	start: function() {
		var remaining = this.remaining;
		this.getTime();
		this.display(this.time);
	},
	tick: function() {
		var remaining = this.remaining;
		this.getTime();
		this.display(this.time);
		if (this.remaining <= 0 && this.options.countplus==false) {
		
			this.done();
			return;
		}
		if (remaining != this.remaining) {
			this.fireEvent('onTick', this.time);
		}
		(function(){this.tick()}.bind(this)).delay(500);
	},
	getTime: function() {
		var week = 604800;
		var day = 86400;
		var hour = 3600;
		 this.remaining = this.target - Math.round($time() / 1000);
				if (this.remaining <= 0) {
				  this.remaining = this.target - Math.round($time() / 1000);
				  this.remaining = this.remaining * (-1);
		var weeks   = this.options.weeks   ? Math.floor(this.remaining/week) : 0 ;
		var days    = this.options.days    ? Math.floor(this.remaining/day) : 0 ;
		var hours   = this.options.hours   ? Math.floor((this.remaining - ((weeks*week)+(days*day)))/hour) : 0 ;
//		alert(this.el);

				} else {
		
		var weeks   = this.options.weeks   ? Math.floor(this.remaining/week) : 0 ;
		var days    = this.options.days    ? Math.floor((this.remaining - (weeks*week))/day) : 0 ;
		var hours   = this.options.hours   ? Math.floor((this.remaining - ((weeks*week)+(days*day)))/hour) : 0 ;
        }

		this.time = {
			weeks:   weeks,
			days:    days,
			hours:   hours,
		};
	},
	display: function(){
		var myarr = this.format();
		this.el.innerHTML = '<div id=cd_weeks>'+myarr[0]+'</div><div id=cd_days>'+myarr[1]+'</div><div id=cd_hours>'+myarr[2]+'</div>';
		//this.el.setHTML('<div id=cd_weeks>'+myarr[0]+'</div><div id=cd_days>'+myarr[1]+'</div><div id=cd_hours>'+myarr[2]+'</div>');
		//this.el.setHTML(this.format());
	},
	format: function() {
		var str = '';
		var arr = new Array();
		//if (this.time.weeks > 0) {
		if (1) {
			if (this.time.weeks == 1) {
				str += this.options.formatWeeksSing.replace('%weeks%', this.time.weeks);
				arr[0] = this.options.formatWeeksSing.replace('%weeks%', this.time.weeks);
			} 
			else {
				str += this.options.formatWeeks.replace('%weeks%', this.time.weeks);
				arr[0] = this.options.formatWeeks.replace('%weeks%', this.time.weeks);
			}
		}
		//if (this.time.days > 0) {
		if (1) {
			if (this.time.days == 1) {
				str += this.options.formatDaysSing.replace('%days%', this.time.days);
				arr[1] = this.options.formatDaysSing.replace('%days%', this.time.days);
			}
			else {
				str += this.options.formatDays.replace('%days%', this.time.days);
				arr[1] = this.options.formatDays.replace('%days%', this.time.days);
			}
		}
		//if (this.time.days > 0 || this.time.hours > 0) {
		if (1) {
			if (this.time.hours == 1) {
				str += this.options.formatHoursSing.replace('%hours%', this.time.hours);
				arr[2] = this.options.formatHoursSing.replace('%hours%', this.time.hours);
			} 
			else {
				str += this.options.formatHours.replace('%hours%', this.time.hours);
				arr[2] = this.options.formatHours.replace('%hours%', this.time.hours);
			}
		}
		//return str;
		return arr;
	},
	done: function() {
		this.el.setHTML(this.options.message);
		this.fireEvent('onComplete', this.options.message);
	}
});
Countdown.implement(new Options);
Countdown.implement(new Events);

