var Feature = Class.create({
	
	initialize: function(container,options) {
		this._container = $(container);
		this._mover = this._container.childElements()[0];
		this._sections = this.remove_ignore_elements(this._container.childElements()[0].childElements());
		this._width = 620;
		this._current = 1;
		this._hold = 8;
		this._slideDuration = .5;
		this._fadeDuration = 1;
		this._moving = false;
		this.initial_format();
		this.create_navigation();
		this.startPe();
	},
	
	remove_ignore_elements: function(array){
		var tempArray = new Array();
		array.each(function(_item){
			if (_item.tagName != 'ignore')
				tempArray.push(_item);
		});
		return tempArray;
	},	
	
	initial_format: function(){
		this.remove_ignore_elements(this._container.childElements())[0].style.width = (this._width * this._sections.length) + 'px';
		$$('.watchnow, .darkdark').each(function(i){
			i.setOpacity(0);							  
		});
	},
	
	create_navigation: function(){
		var myUl = new Element("ul",{id:"fc_buttons"});
		var mya, myLi;
		for (var i = this._sections.length; i > 0; i--){
			mya = new Element('a',{itemnumber:i});
			myLi = new Element('li').update(mya);
			myLi.observe("click",function(_item){
				this.nav_click(_item);
			}.bind(this));
			myUl.insert(myLi);
		}
		$(this._container.parentNode).insert(myUl);
		this._navigation = $('fc_buttons');
		this.switch_nav_classes();
	},
	
	nav_click: function(_item){
		if (!this._moving){
			this.go_to_number(_item.element().getAttribute('itemnumber'));
		}
	},
	
	switch_nav_classes: function(){
		$$('#fc_buttons a').each(function(i){
			i.removeClassName("current");								  
		});
		try{
			this.remove_ignore_elements(this._navigation.childElements()[this._sections.length-this._current].childElements())[0].addClassName('current');
		}
		catch(ex){}
	},
	
	go_to_number: function(num){
		var tempCurrent = this._current;
		this._current = num;
		var multiplier = tempCurrent - this._current;
		this.stopPe();
		this.move(this._width * multiplier);
		this.startPe();
	},
	
	go_next: function(x){
		this._current++;
		this.move(x);
	},

	go_previous: function(x){
		this._current--;
		this.move(-x);
	},
	
	go_start: function(x){
		this._current = 1;
		this.move(x);
	},
	
	show_watch_now: function(){
		new Effect.Fade(this._sections[this._current-1].firstDescendant().childElements()[2], { 
			from: 0,
			to: .5,
			duration: this._fadeDuration,
			delay: 1
		});
		new Effect.Fade(this._sections[this._current-1].firstDescendant().childElements()[1], { 
			from: 0,
			to: 1,
			duration: this._fadeDuration,
			delay: 1
		});
		this._sections.each(function(i,k){
			if (k != this._current - 1)
				i.firstDescendant().childElements()[1].setOpacity(0);
				i.firstDescendant().childElements()[2].setOpacity(0);
		});
	},
	
	move: function(x){
		this._moving = true;
		new Effect.Move(this._mover, { 
			x: x, 
			y: 0,
			mode:'relative',
			duration: this._slideDuration,
			transition: Effect.Transitions.sinoidal,
			afterFinish: function(){
				this._moving = false;
			}.bind(this)
		});
		this.switch_nav_classes();
		this.show_watch_now();
	},
	
	rotate: function(){
		if (this._current < this._sections.length){
			this.go_next(-this._width);
		}
		else{
			this.go_start(this._width * (this._sections.length - 1));	
		}
	},
	
	startPe: function(){
		this._periodik = new PeriodicalExecuter(this.rotate.bind(this),this._hold);	
		this.show_watch_now();
	},
	
	stopPe: function(){
		this._periodik.stop();
	}
	
});
