var Teaser = new Class({
	elements: [],

	currentElement: 0,

	switchingElement: false,

	initialize: function(target) {
		if(!$(target)) return false;

		$(target).getChildren().each(function(elem, i){

			if(i > 0) {
				elem.setStyle('display', 'none');
				elem.removeClass('hide');
			}

			this.elements.include(elem);
		}.bind(this));
		
		$('teaser-next').addEvent('click', function(){ this.next(); }.bind(this));
		$('teaser-prev').addEvent('click', function(){ this.prev(); }.bind(this));
		
		this.timer = window.setInterval(function(){ this.next(); }.bind(this), 9000);
	},

	next: function() {
		if(this.switchingElement) return;
		this.switchingElement = true;
		
		if(this.currentElement == (this.elements.length-1)) {
			var newKey = 0;
		} else {
			var newKey = this.currentElement+1;
		}
		
		var newElement = this.elements[newKey];
		
		newElement.setStyle('z-index', this.elements[this.currentElement].getStyle('z-index')+1);
		var FX = new Fx.Style(newElement, 'opacity', {duration: 1500, onComplete: function() {
			this.elements[this.currentElement].setStyle('display', 'none');
			this.currentElement = newKey;
			this.switchingElement = false;
		}.bind(this)});
		FX.set(0);
		newElement.setStyle('display', 'block');
		FX.start(0, 1);
	},

	prev: function() {
		if(this.switchingElement) return;
		this.switchingElement = true;
		
		if(this.currentElement == 0) {
			var newKey = this.elements.length-1;
		} else {
			var newKey = this.currentElement-1;
		}
		
		var newElement = this.elements[newKey];
		
		newElement.setStyle('z-index', this.elements[this.currentElement].getStyle('z-index')+1);
		var FX = new Fx.Style(newElement, 'opacity', {duration: 1500, onComplete: function() {
			this.elements[this.currentElement].setStyle('display', 'none');
			this.currentElement = newKey;
			this.switchingElement = false;
		}.bind(this)});
		FX.set(0);
		newElement.setStyle('display', 'block');
		FX.start(0, 1);
	}
});


var Countdown = new Class({
	initialize: function(year, month, day, hour, minute, second) {
		this.targetDate  = new Date(year, month-1, day, hour, minute, second);
		this.start();
	},

	start: function() {
		this.years = 0;
		this.months = 0;
		this.days = 0;
		this.hours = 0;
		this.minutes = 0;
		this.seconds = 0;
		this.currentDate = new Date();

		// Days
		while(this.currentDate.getTime()+(24*60*60*1000)<this.targetDate) {
			this.days++;
			this.currentDate.setTime(this.currentDate.getTime()+(24*60*60*1000));
		}

		// Hours
		this.hours = Math.floor((this.targetDate - this.currentDate)/(60*60*1000));
		this.currentDate.setTime(this.currentDate.getTime()+this.hours*60*60*1000);

		// Minutes
		this.minutes = Math.floor((this.targetDate-this.currentDate)/(60*1000));
		this.currentDate.setTime(this.currentDate.getTime()+this.minutes*60*1000);

		// Seconds
		this.seconds = Math.floor((this.targetDate-this.currentDate)/1000);

		if(document.getElementById('countdown-years')) {
			document.getElementById('countdown-years').innerHTML = this.years;
		}

		if(document.getElementById('countdown-days')) {
			document.getElementById('countdown-days').innerHTML = this.days;
		}

		if(document.getElementById('countdown-hours')) {
			document.getElementById('countdown-hours').innerHTML = this.hours;
		}

		if(document.getElementById('countdown-minutes')) {
			document.getElementById('countdown-minutes').innerHTML = this.minutes;
		}

		if(document.getElementById('countdown-seconds')) {
			document.getElementById('countdown-seconds').innerHTML = this.seconds;
		}

		setTimeout(function(){ this.start(); }.bind(this), 200);
	}
});


window.addEvent('domready', function(){
	new Teaser('teaser');
});