
// rich recipe teaser...requires prototype & scriptaculous
var protoaculous_included = (typeof Prototype != 'undefined' && typeof Scriptaculous != 'undefined');
if (protoaculous_included) {
	
	function richRecipeTeaserInit() {
		var contentDiv = $('pageBody');
		if (!contentDiv) return;
		
		var richRecipeTeasers = contentDiv.select('div.richRecipeTeaser');
		if (richRecipeTeasers) richRecipeTeasers.each(function(rrt) {
			new richRecipeTeaser(rrt);
		});
	}
	
	
	richRecipeTeaser = Class.create({
		initialize: function(ele) {
			this.ele = ele;
			this.activeItem = 1;
			
			// register click event on buttons
			if (this.ele.down('#buttonPreviousRecipe')) {
				this.ele.down('#buttonPreviousRecipe').setStyle({cursor: 'pointer'});
				Event.observe(this.ele.down('#buttonPreviousRecipe'), 'click', this.btnBackClick.bindAsEventListener(this));
				if (this.ele.down('#buttonPreviousRecipe').down('a'))
					Event.observe(this.ele.down('#buttonPreviousRecipe').down('a'), 'click', this.btnBackClick.bindAsEventListener(this));
			}
			if (this.ele.down('#buttonNextRecipe')) {
				this.ele.down('#buttonNextRecipe').setStyle({cursor: 'pointer'});
				Event.observe(this.ele.down('#buttonNextRecipe'), 'click', this.btnNextClick.bindAsEventListener(this));
				if (this.ele.down('#buttonNextRecipe').down('a'))
					Event.observe(this.ele.down('#buttonNextRecipe').down('a'), 'click', this.btnNextClick.bindAsEventListener(this));
			}
			
			// remove "special" workaround for initial display:none styling
			var items = ele.select('.dynItem');
			for (var i = 0; i < items.length; ++i) {
				if (!items[i].hasClassName('first') && items.length > 1) items[i].hide();
	        	items[i].removeClassName('dynItemSpecial');
			}
			this.itemCount = items.length;
		},
		
		btnBackClick: function(evt) {
			evt.stop();
			this.changeItems(this.activeItem > 1 ? this.activeItem - 1 : this.itemCount);
		},
		btnNextClick: function(evt) {
			evt.stop();
			this.changeItems(this.activeItem < this.itemCount ? this.activeItem + 1 : 1);
		},
		
		changeItems: function(newItem) {
			if (!newItem) return;
				
        	var items = this.ele.select('.dynItem');
        	var activeItem = this.activeItem;
        	if (items) items.each(function(item) {
        		var classNames = item.className.split(' ');
        		for (var i = 0; i < classNames.length; ++i) {
        			if (classNames[i].match(new RegExp('dynItem' + newItem + '$')))
	        			new Effect.Appear(item, {duration: 0.7, queue: { position: 'end', scope: 'dynItem' + newItem}});
	        		if (classNames[i].match(new RegExp('dynItem' + activeItem + '$')))
	        			new Effect.Fade(item, {duration: 0.7, queue: { position: 'end', scope: 'dynItem' + activeItem}});
        		}
        	});
	        this.activeItem = newItem;
		}
	});
	
	
	Event.observe(window, 'load', richRecipeTeaserInit);
}