// JavaScript Document

/* nmb namespace */
if (!nmb) { var nmb = {}; }

nmb.slideShow = function(el, delay) {
	var animations = {
		fadeSlideOut: { opacity: { from: 1, to: 0 } },
		fadeSlideIn: { opacity: { from: 0, to: 1 } },
		fadeKeyOut: { borderColor: { to: '#f1f2ee' } },
		fadeKeyIn: { borderColor: { to: '#5d5d5d' } }
	};

	var yui_util = YAHOO.util;
	var yui_Event = yui_util.Event;
	var yui_Dom = YAHOO.util.Dom;
	
	var timer;
	var anim;
	
	function queue() { timer = setTimeout(transition, delay * 1000); }
	
	function transition() {
		clearTimeout(timer);
		
		// fade the current slide out
		anim = new yui_util.Anim( slides[currIndex], animations.fadeSlideOut, 2 );
		
		// once anim complete
		anim.onComplete.subscribe(
			function() {
				
				// move current slide to bottom of stack;
				yui_Dom.setStyle( slides[currIndex], 'zIndex', 0 );
				yui_Dom.setStyle( slides[currIndex], 'opacity', 1 );
				
				// we fade out the highlight box on current category
				yui_Dom.removeClass( keys[currIndex], 'fsshow-active-key' );
				
				// update our tracking index
				currIndex = (currIndex + 1) % slides.length;
				
				// highlight the new key
				yui_Dom.addClass( keys[currIndex], 'fsshow-active-key' );
				
				// move the new current slide up to the top
				yui_Dom.setStyle( slides[currIndex], 'zIndex', 2 );

				var nextIndex = (currIndex + 1) % slides.length;
				yui_Dom.setStyle( slides[nextIndex], 'zIndex', 1 );
				yui_Dom.setStyle( slides[nextIndex], 'opacity', 1 );
				
				queue();
			}
		);
		
		// start the animation
		anim.animate();
	}

	function keyClick() {
		anim.stop();

		clearTimeout(timer);
		
		var prevIndex = currIndex;
		var prevNext = (prevIndex + 1) % slides.length;

		// find the index of the clicked keys
		var clickedIndex;
		for (var i = 0; i < keys.length; i++) {
			if (keys[i] == this) {
				clickedIndex = i;
				break;
			}
		}
		
		// activate the selected slide
		if (clickedIndex != prevIndex) {
			// remove highlight from current key
			yui_Dom.removeClass( keys[prevIndex], 'fsshow-active-key' );
			
			// show the selected slide
			currIndex = clickedIndex;
			yui_Dom.setStyle( slides[currIndex], 'zIndex', 2 );
			yui_Dom.setStyle( slides[currIndex], 'opacity', 1 );
	
			// hide the previous active slide
			yui_Dom.setStyle( slides[prevIndex], 'zIndex', 0 );
			yui_Dom.setStyle( slides[prevIndex], 'opacity', 0 );
			
			// hide the previous next slide
			if (prevNext != currIndex) {
				yui_Dom.setStyle( slides[prevNext], 'zIndex', 0 );
				yui_Dom.setStyle( slides[prevNext], 'opacity', 0 );
			}
			
			// prep the new next slide
			var nextIndex = (currIndex + 1) % slides.length;
			yui_Dom.setStyle( slides[nextIndex], 'zIndex', 1 );
			yui_Dom.setStyle( slides[nextIndex], 'opacity', 1 );
			
			// activate the clicked key
			yui_Dom.addClass( keys[currIndex], 'fsshow-active-key' );
		}
		queue();
	}
	



	/* initialize */
	var timer;
	var currIndex = 0;

	// save reference to the containing element
	var container = document.getElementById(el);
	if (!container) { return; }

	// get array of slides
	var slides = yui_Dom.getElementsByClassName( 'fsshow-slides', 'ul', container );
	if (!slides) { return; }
	slides = yui_Dom.getChildren( slides[0] );
	if (!slides || slides.length < 2) { return; }
	
	// set the stacking order of the slides
	// 0 = bottom, 1 = middle, 2 = top
	yui_Dom.setStyle( slides, 'zIndex', 0 );
	yui_Dom.setStyle( slides[0], 'zIndex', 2 );
	yui_Dom.setStyle( slides[1], 'zIndex', 1 );
	
	// create the keys
	var keys = [];
	var ul = document.createElement('ul');
	yui_Dom.addClass( ul, 'fsshow-keys' );
	for (var i=0; i < slides.length; i++) {
		keys[i] = document.createElement('li');
		yui_Dom.addClass( keys[i], 'fsshow-key' );
		yui_Dom.addClass( keys[i], 'iepngfix' );
		keys[i].appendChild(document.createTextNode(i+1));
		ul.appendChild(keys[i]);
	}
	// add keys to the document
	container.appendChild(ul);
	
	// assign the click handler for the keys
	yui_Event.addListener( keys, "click", keyClick );
	
	// activate the first key
	yui_Dom.addClass( keys[0], 'fsshow-active-key' );

	// and start the show
	queue(); 

	return this;
};
