var GravityContent = new Class({
	
	Implements: [Options, Events],
	
	options: {
		start: 0,
		promoDuration: 12000,
		next: 'next',
		previous: 'previous'
	},
	
	initialize: function(slides, slides_content, control_container, options){
		this.setOptions(options);
		this.slides = $$(slides);
		this.slidesContent = $$(slides_content);
		this.controlsContainer = $(control_container);
		this.buildControls();
		this.shadow = $('promo_shadow');
		this.setup();
		
		this.timer = this.changeSlide.periodical(this.options.promoDuration, this);
	},
	
	setup: function(){
		this.currentSlide = this.slides[this.options.start];
		this.currentContent = this.slidesContent[this.options.start];
		this.slides[this.options.start].setStyle('top', '27px');
		
		this.slidesContent.each(function(slide){
			slide.setStyle('opacity', '0');
		});
		
		this.slidesContent[this.options.start].setStyle('opacity', '1');
		this.activateButton();
		
		this.shadow.set('morph',{
			duration: 2000,
			transition: 'quint:out'
		});
	},
	
	changeSlide: function(slide){
		if(slide !== undefined){
			$clear(this.timer);
			//this.timer = this.changeSlide.periodical(this.options.promoDuration, this);
			this.animateDown();
			this.increment(slide);
			this.animateUp.delay(900, this);
		}else{
			this.animateDown();
			this.increment();
			this.animateUp.delay(900, this);
		}
	},
	
	animateDown: function(){
		this.currentSlide.set('morph',{
			duration: 1000, 
			transition: 'quint:out'
		});
		
		this.currentSlide.morph({
			top: '318px'
		});
		
		this.currentContent.set('morph',{
			duration: 2000, 
			transition: 'quint:out'
		});
		
		this.currentContent.morph({
			'opacity' : '0'
		});
		
		if(!(Browser.Engine.name == 'trident'))
		{		
			this.shadow.morph({
				'opacity' : '0'
			});
		}
		
	},
	
	increment: function(to){
		this.deactivateButton();
		
		if(to !== undefined){
			this.options.start = to;
			this.currentSlide = this.slides[this.options.start];
			this.currentContent = this.slidesContent[this.options.start];
		}
		else{
			
			if(this.options.start == this.slides.length - 1){
				this.options.start = 0;
				this.currentSlide = this.slides[this.options.start];
				this.currentContent = this.slidesContent[this.options.start];
			}
			else{
				this.options.start++;
				this.currentSlide = this.slides[this.options.start];
				this.currentContent = this.slidesContent[this.options.start];
			}
		}
		
		this.activateButton();
	},
	
	animateUp: function(){
		this.currentSlide.set('morph',{
			duration: 2000, 
			transition: 'quint:out'
		});
		
		this.currentSlide.morph({
			top: '27px'
		});
		
		this.currentContent.set('morph',{
			duration: 2000, 
			transition: 'quint:out'
		});
		
		this.currentContent.morph({
			'opacity' : '1'
		});
		
		if(!(Browser.Engine.trident4))
		{
			this.shadow.morph({
				'opacity' : '1'
			});
		}
	},
	
	buildControls: function(){
		this.controls = [];
		this.slides.each(function(item, key){
			var control_li = new Element('li');
			var control_a = new Element('a');

			control_a.set('html', key);
			
			control_a.addEvent('click', function(){
				this.changeSlide(key);
			}.bind(this));
			
			control_a.inject(control_li);
			
			this.controls[key] = control_a;
			
			this.controlsContainer.adopt(control_li);
		}.bind(this));
		
		$(this.options.previous).addEvent('click', function(){
			this.previous();
			return false;
		}.bind(this));
		
		$(this.options.next).addEvent('click', function(){
			this.next();
			return false;
		}.bind(this));
	},
	
	activateButton: function(){
		this.controls[this.options.start].addClass('on');
	},
	
	deactivateButton: function(){
		this.controls[this.options.start].removeClass('on');
	},
	
	next: function(){
		var pos = this.changePosition(this.options.start + 1);
		this.changeSlide(pos);
	},
	
	previous: function(){
		var pos = this.changePosition(this.options.start - 1);
		this.changeSlide(pos);
	},
	
	changePosition: function(where){
		var ret;
		
		if(where >= this.slides.length){
			ret = 0;	
		} else if(where < 0){
			ret = this.slides.length - 1;
		} else{
			ret = where;
		}
		return ret;
	}
});
