var FeaturedRecipes = new Class({
	Implements: [Events, Options],
	options:{
		cookieName: 'recipeIndex'
	},
	initialize: function(container, options){
		
		var that = this;
		
		this.setOptions(options);
		
		this.container = $(container);
		
		// grab structure
		this.structure = {
			navigation: this.container.getElement('.navigation'),
			mask: this.container.getElement('.mask')
		}
		
		// grab controls
		this.controls = {
			previous: this.container.getElement('.previous'),
			next: this.container.getElement('.next')
		}
		
		// grab items
		this.items = this.structure.mask.getElements('li');
		this.items.each(function(item){
			item.fx = new Fx.Tween(item, {
				property: 'opacity',
				link: 'cancel'
			});
			item.fx.set(0);
		});
		
		// grab thumbnails
		this.thumbnails = this.structure.navigation.getElements('li');
		this.thumbnails.each(function(item, index){
			item.addEvent('click', that.moveTo.bind(that, index));
			item.fx = new Fx.Tween(item, {
				property: 'opacity',
				link: 'cancel'
			});
			item.fx.set(0.5);
		});
		
		// look for saved state in session cookie
		var cookieState = Cookie.read(this.options.cookieName);
		if(cookieState == null)
			this.currentIndex = 0;
		else
			this.currentIndex = cookieState;
		
		// set initial previous index
		this.previousIndex = null;
		
		// show navigation
		this.structure.navigation.setStyle('visibility', 'visible');
		
		// first update
		this.update();
		
		// attach events
		this.attach();
	},
	attach: function(){
		// attach click events to previous and next buttons
		this.controls.previous.addEvent('click', this.previous.bind(this));
		this.controls.next.addEvent('click', this.next.bind(this));
	},
	moveTo: function(index){
		this.previousIndex = this.currentIndex;
		this.currentIndex = index;
		this.update();
	},
	next: function(){
		if(this.currentIndex < this.items.length - 1){
			this.previousIndex = this.currentIndex;
			this.currentIndex++;
			this.update();
		}
	},
	previous: function(){
		if(this.currentIndex != 0){
			this.previousIndex = this.currentIndex;
			this.currentIndex--;
			this.update();
		}
	},
	update: function(){
		// save state
		this.saveState();

		if(this.currentIndex == 0){
			this.controls.previous.setStyle('visibility', 'hidden');
			this.controls.next.setStyle('visibility', 'visible');
		}
		else if(this.currentIndex == this.items.length - 1){
			this.controls.previous.setStyle('visibility', 'visible');
			this.controls.next.setStyle('visibility', 'hidden');
		}
		else{
			this.controls.previous.setStyle('visibility', 'visible');
			this.controls.next.setStyle('visibility', 'visible');
		}
		
		try{
			this.items[this.previousIndex].fx.start(0);
			this.thumbnails[this.previousIndex].fx.start(0.5);
		}catch(e){}
		try{
			this.items[this.currentIndex].fx.start(1);
			this.thumbnails[this.currentIndex].fx.start(1);
		}catch(e){}
	},
	saveState: function(){
		// save current index in a session cookie
		Cookie.write(this.options.cookieName, this.currentIndex, { duration: 0 });
	}
});
