/**
 * Class for building animated feature sets.
 * Set class="active" on the menu li you wish to use as the starting item, defaults to top.
 * 
 * @author Matt Pike
 * @date 2010-11-09
 */
var FeatureSet = new Class({
	
	Implements : [Options],
	
	container 	: null,
	menu		: null,
	pointer		: null,
	currFeature : null,
	cycleTimer	: null,
	options		: {
		'autoCycle' : true, // auto cycles through features until one is selected
		'cycleInterval' : 2500 // milliseconds between auto cycling
	},
	
	/**
	 * Constructor
	 * @param Element fsContainer
	 * @param Object options
	 */
	initialize : function(fsContainer, options){
		var obj = this;
		this.setOptions(options);
		this.container 	= fsContainer;
		this.menu		= fsContainer.getElement('.featureMenu');
		this.pointer	= fsContainer.getElement('.pointer');
		this.feature	= fsContainer.getElement('.featureInfo');
		this.attachMenuEvents();
		this.feature.getElements('.featureContent').each(function(featureContent){
			if(obj.currFeature.get('href')!='#'+featureContent.get('id')){
				featureContent.set('opacity',0);
			}
		});
		if(this.options.autoCycle) this.startAutoCycle();
	},
	
	/**
	 * Attach events to feature set menu
	 */
	attachMenuEvents : function(){
		var obj = this;
		var menuItems = this.menu.getElements('li a');
		menuItems.each(function(mnuLnk){
			mnuLnk.addEvent('click',function(e){
				e.stop();
				obj.stopAutoCycle();
				obj.selectFeature(mnuLnk);
			});
			if(mnuLnk.getParent('li').hasClass('active')) obj.currFeature = mnuLnk.get('href');
		});
		// If no default selected, use first...
		if(!this.currFeature){
			menuItems[0].getParent('li').addClass('active');
			this.currFeature = menuItems[0];
		}
	},
	
	/**
	 * Switch to a feature in feature set
	 * @param Element menuItem
	 */
	selectFeature : function(menuItem){
		if(!menuItem){ // If no menu item provided try and find next menu item, if none found default back to beginning
			if(this.currFeature){
				var nextLi = this.currFeature.getParent('li').getNext('li');
				if(nextLi && nextLi.getElement('a')!=this.currFeature){
					menuItem = nextLi.getElement('a');
				}else{
					menuItem = this.currFeature.getParent('ul').getElement('li a');
				}
			}
		}
		if(menuItem && menuItem.href){
			this.movePointerTo(menuItem);
			this.fadeSwapFeatureContent(menuItem.get('href'));
			this.currFeature = menuItem;
		}
	},
	
	/**
	 * Stop auto-cycling through features
	 */
	stopAutoCycle : function(){
		this.options.autoCycle = false;
		clearInterval(this.cycleTimer);
	},
	
	/**
	 * Start auto-cycling through features
	 */
	startAutoCycle : function(){
		this.options.autoCycle = true;
		this.cycleTimer = this.selectFeature.periodical(this.options.cycleInterval,this);
	},
	
	/**
	 * Fade out the old feature content whilst fading in the new feature content
	 */
	fadeSwapFeatureContent : function(newFeature){
		this.feature.getElement(this.currFeature.get("href")).fade('out');
		this.feature.getElement(newFeature).fade('in');
	},
	
	/**
	 * Tweens the menu pointer to a new menu item
	 * @param Element menuItem
	 */
	movePointerTo : function(menuItem){
		var dest = menuItem.getPosition(menuItem.getParent('.featureMenu'));
		this.pointer.tween('top',dest.y);
	}
});
