var ddSlider = new Class({
	
	initialize: function(sliderId) {

		
		this.slider = $(sliderId);
		this.sliderId = sliderId;
		this.slideActive = true;
		this.slideInProgress = false;

		/* fetch elements */
		this.elements = $$('#' + this.sliderId + ' div.sliderElement');
		this.links = $$('#' + this.sliderId + ' h2');
		/* first element is active element */
		this.activeElement = this.elements[0];
		this.activeIndex = 0;
		
		/* first hide all except first element */
		for (c=0; c<this.elements.length; c++) {
			if (c) {
				this.elements[c].fade('hide');
			}
		}
		
		/* click actions */
		for (c=0; c<this.links.length; c++) {
			this.links[c].addEvent('click', function() {
				this.slideActive = false;
				
				this.select(arguments[0]);
			}.bind(this, c));
		}
		
		this.slideInterval = this.slide.periodical(8000, this);
		
	},
	
	select: function(index) {
		if (!this.slideInProgress && index != this.activeIndex) {
			this.slideInProgress = true;
			this.activeIndex = index;
			var fadeOut = new Fx.Tween(this.activeElement, 
				{
					duration: 500,
					property: 'opacity'
				}).start(0);
			fadeOut.addEvent('complete',  function() {
				this.slideInProgress = false;
			}.bind(this));	
						
			this.activeIndex = index;
			this.activeElement = this.elements[this.activeIndex];
			var fadeIn = new Fx.Tween(this.activeElement, 
				{
					duration: 500,
					property: 'opacity'
				}).start(1);
			fadeIn.addEvent('complete',  function() {
				this.slideInProgress = false;
			}.bind(this));							
		}
			
	},
	
	slide: function() {
		if (this.slideActive) {
			var fadeOut = new Fx.Tween(this.activeElement, 
				{
					duration: 2000,
					property: 'opacity'
				}).start(0);
			
			this.activeIndex = ((this.activeIndex+1)==this.elements.length)?0:(this.activeIndex+1);
			this.activeElement = this.elements[this.activeIndex];
			var fadeIn = new Fx.Tween(this.activeElement, 
				{
					duration: 2000,
					property: 'opacity'
				}).start(1);			
		}	
	}
	
	
});

window.addEvent('domready', initSlider);
function initSlider() {
	homeSlider = new ddSlider('slider');
	
}
