YAHOO.namespace('syzygy');

/* config is a JSON object with params for this object. Supported params are:
 * 
 * MANDATORY:
 * url - an array of URLs with the images to display
 * title - na array of strings with the alt/title text for each image
 * 
 * OPTIONAL:
 * repeat - true for a looping show, false to stop after 1 loop
 * delay - time in ms each image is on show (default 10000)
 * fadeTime - time in ms for image fade effect (default 1000)
 * onFinish - function to call when the slideshow finishes (only if repeat == false)
 * 
 */
YAHOO.syzygy.ImageRotator = function(parentId, config){
	var parentEl = null
	var self = this;
	
	var els = Array();
	var currentImg = 0;
	var nextImg = 1;
	
	var switchInterval = null;
	var fadeInterval = null;
	
	var switchSpeed = 5000;
	var fadeSpeed = 100;
	
	var opacity = 1.0;
	
	var checkParams = function(){
		// check mandatory params are set
		if ("undefined" == typeof(config) || null == config){
			throw("ImageRotator: config object is NULL");
		} else if ("array" == typeof(config.url) || null == config.url){
			throw("ImageRotator: config.url object must be a non-NULL array");
		} else if ("array" == typeof(config.title) || null == config.title){
			throw("ImageRotator: config.title object must be a non-NULL array");
		}
		
		// parse optional params		
		if ("undefined" != typeof(config.delay) && null != config.delay){
			switchSpeed = config.delay;
		}
		if ("undefined" != typeof(config.fadeTime) && null != config.fadeTime){
			fadeSpeed = config.fadeTime / 10;
		}
		if ("boolean" != typeof(config.repeat)){
			config.repeat = true;
		}
	}
	
	var init = function(){
		checkParams();
		
		parentEl = document.getElementById(parentId);

		els[0] = getFirstImage(parentEl);
		els[0].src = config.url[0];
		els[0].title = config.title[0];
		els[0].style.zIndex = 2;
		
		for (var i=1; i < config.url.length; i++){
			els[i] = els[0].cloneNode(false);
			els[i].src = config.url[i];
			els[i].title = config.title[i];
			els[i].style.zIndex = 1;
			YAHOO.util.Dom.setStyle(els[i], 'visibility', 'hidden');
			parentEl.appendChild(els[i]);
		}
				
		switchInterval = setInterval(nextImage, switchSpeed);
	}
	
	var getFirstImage = function(el){
		for(var child = el.firstChild; child != null; child = child.nextSibling){
			if (child.tagName == "IMG"){
				return child;
			}
		}
		return null;
	}

	var nextImage = function(){
		fadeInterval = setInterval(fadeSwapStep, fadeSpeed);
	}
	
	var fadeSwapStep = function(){
		var from = els[currentImg];
		var to = els[nextImg];

		YAHOO.util.Dom.setStyle(from, 'opacity', opacity);
		YAHOO.util.Dom.setStyle(to, 'opacity', 1.0 - opacity);
		YAHOO.util.Dom.setStyle(to, 'visibility', 'visible');

		opacity -= 0.1;
		if (0.0 > opacity){
			opacity = 1.0;
			clearInterval(fadeInterval);
		
			from.style.zIndex = 1;
			to.style.zIndex = 2;
			YAHOO.util.Dom.setStyle(from, 'opacity', 1.0);
			YAHOO.util.Dom.setStyle(from, 'visibility', 'hidden');
			
			// if we're not repeating the slideshow , stop the image rotation timer
			// and call the callback
			currentImg++;
			nextImg++;
			if (false == config.repeat && (config.url.length-1) == currentImg){
				clearInterval(switchInterval);
				if ("function" == typeof(config.onFinish)){
					return config.onFinish();
				}
				return;
			}

			currentImg %= config.url.length;
			nextImg %= config.url.length;
		}
	}
	
	init();
}

