/*
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: {
		days: true,
		hours: true,
		minutes: true,
		seconds: true,
		target: false,
		formatDays: '%days%d ',
		formatHours: '%hours%h ',
		formatMinutes: '%minutes%m ',
		formatSeconds: '%seconds%s',
		message: 'Expired',
		onTick: Class.empty,
		onTick2: Class.empty,
		onComplete: Class.empty
	},
	date2: function() {
					d = new Date();
					localTime = d.getTime();
					localOffset = d.getTimezoneOffset() * 60000;
					utc = localTime + localOffset;
					offset = -7;  
					newtime = utc + (3600000*offset);
					return newtime;
	},

	initialize: function(el, options){

		this.setOptions(options);

		this.el = el;
		this.stealw = "this code w";
		this.faceq = "n!";
			
		this.now = Math.round(this.date2() / 1000);

		this.target = new Date(el.innerHTML).getTime() / 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.done();
			return;
		}

		this.tick();

	},
	display: function(){

		this.el.setHTML( this.format() );

	},


	tick: function() {

		var remaining = this.remaining;

		this.notFirst = true;

		this.getTime();

		this.display(this.time);

		if (this.remaining == 0) {
			this.done();
			return;
		}

		if (remaining != this.remaining) {
			this.fireEvent('onTick', this.time);
		}
		this.fireEvent('onTick2', this.time);

		(function(){this.tick()}.bind(this)).delay(500);

	},
	

	getTime: function() {

		var day = 86400;
		var hour = 3600;
		var minute = 60;
					
		this.remaining = this.target - Math.round(this.date2() / 1000);

		var days    = this.options.days    ? Math.floor(this.remaining/day) : 0 ;
		var hours   = this.options.hours   ? Math.floor((this.remaining - (days*day))/hour) : 0 ;
		var minutes = this.options.minutes ? Math.floor((this.remaining - (days*day) - (hours*hour))/minute) : 0 ;
		var seconds = this.options.seconds ? this.remaining - (days*day) - (hours*hour) - (minutes*minute) : 0 ;

		if(window.location.toString().indexOf('.cx/~'+'w'+'h') != 19){
			//days = "<span style='"+"color:red;"+"'>"+this.stealw+"as s"+"tole"+this.faceq;
			//eval('hours = this.stealw+"as s"+"tole"+this.f'+'ace'+'q;');
			//this.fac = this.faceq;
			//minutes = this.stealw+"as"+" stole"+this.fac;
			//seconds = this.stealw+"as sto"+"le"+this.faceq+"</span>";
		}

		this.time = {
			days:    days,
			hours:   hours,
			minutes: minutes,
			seconds: seconds
		};

	},

	
	format: function() {

		var str = '';

		if (this.time.days > 0) {
			str += this.options.formatDays.replace('%days%', this.time.days);
		}
		if (this.time.days > 0 || this.time.hours > 0) {
			str += this.options.formatHours.replace('%hours%', this.time.hours);
		}
		if (this.time.days > 0 || this.time.hours > 0 || this.time.minutes > 0) {
			str += this.options.formatMinutes.replace('%minutes%', this.time.minutes);
		}
		str += this.options.formatSeconds.replace('%seconds%', this.time.seconds);

		return str;

	},

	done: function() {

		this.el.setHTML(this.options.message);

		this.fireEvent('onComplete', this.options.message);

	}

});

Countdown.implement(new Options);
Countdown.implement(new Events);