Element.implement({

	getData: function(type){
		return this.get('data-'+type);
	},

	setData: function(type, value){
		return this.set('data-'+type, value);
	}
});

var Counter = {
	
	init: function(element){
		this.element = $(element);
		
		if (!this.element) return;
		
		this.digits = new Array();

		this.element.getElements('span.digit').each(function(digit){
			digit.set('html', '<span class="mask"></span>');
			this.digits.push(new Counter.Digit(digit));
		}, this);

		this.element.addClass('ready');
		
		this.targetTime = new Date(this.element.getData('time')*1000);//Date.parse('2011-04-29 16:15:00');
		
		//this.reset();
		this.render.periodical(1000, this);
		
	},
	
	reset: function(){
		var diff = this.getDiff();
		for (var i = 0, l = diff.length; i<l; i++){
			this.digits[i].setDigit(diff[i]);
		}
	},
	
	render: function(){
		var diff = this.getDiff();
		if (diff == null) return;
		diff = diff.split('');
		for (var i = 0, l = diff.length; i<l; i++){
			this.digits[i].show(diff[i].toInt());
		}
	},
	
	getDiff: function(){
		var time = new Date().diff(this.targetTime, 'second');
		if (time < 0) return null;
		var seconds = (time%60).toString().pad(2, '0', 'left');
		time = (time/60).toInt();
		var minutes = (time%60).toString().pad(2, '0', 'left');
		time = (time/60).toInt();
		var hours = (time%24).toString().pad(2, '0', 'left');
		time = (time/24).toInt();
		var days = time.toString().pad(2, '0', 'left');
		return days+hours+minutes+seconds;
	}
	
	
 };
 Counter.Digit = new Class({
	
	Implements: Options,
	
	options: {
		height: 30,
		duration: 500,
		transition: 'bounce:out',
		direction: 'up'
	},
	
	initialize: function(element, options){
		this.element = $(element);
		
		this.fx = new Fx.Tween(this.element, {property: 'background-position', link: 'cancel', transition: this.options.transition, duration: this.options.duration});
		this.fx.set('0px 0px');
		
		this.value = 0;
		this.counter = 0;
		this.direction = (this.options.direction == 'down' ? 1 : -1);
	},
	
	setDigit: function(value){
		if (value != this.value){
			this.counter = value;
			this.value = value;
      this.fx.set(this.getPosition(value));
		}
		return this;
	},
	
	show: function(value){
		var self = this;
		if (value != self.value){
			self.fx.start('0px '+self.getPosition(value)+'px').chain(function(){
				if (value == 0){
					self.fx.set('0px '+(-10*self.options.height).toString()+'px');
				}
			});
		}
		self.value = value;
  	return this;
	},
	
	getPosition: function(value){
		return this.direction*value*this.options.height;
	}
	
});

// JavaScript Document
window.addEvent('domready', function(){
	if($('counter')) Counter.init('counter');
	
	if(!($('homepage'))) {
	var accordion = new Accordion($$('h3.atStart'), $$('div.atStart'), {
		opacity: true,
		alwaysHide: true
	});
	}
	
	if ($('homepage-flash')){
		 var homepageFlash = new Swiff('flow.swf', {
		     container : 'homepage-flash',
				 width: 830,
				 height: 350,
				 params : {
					   wmode: 'transparent'
				 }
		 });
	}

	$$('.clickable').each(function(element){ 
		var link = element.getElement('a'); 
			if (link){ 
				element.setStyle('cursor', 'pointer');
				element.addEvent('click', function(event){ 
					if (event.target != link) window.location = link.href;      
			}); 
		} 
	});

}); 
