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 front = 0;
	var behind = 1;
	
	var switchInterval = null;
	var fadeInterval = null;
	
	var switchSpeed = 10000;
	var fadeSpeed = 100;
	
	var opacity = 1.0;
	
	var currentIndex = 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[front] = getFirstImage(parentEl);
		els[front].src = config.url[0];
		els[front].title = config.title[0];
		els[front].style.zIndex = 2;
		els[behind] = els[front].cloneNode(false);
		els[behind].style.zIndex = 1;
		YAHOO.util.Dom.setStyle(els[behind], 'visibility', 'hidden');
		
		changeImgSrcAndTitle(els[behind]);
		parentEl.appendChild(els[behind]);
				
		switchInterval = setInterval(imgRotate, switchSpeed);
	}
	
	var getFirstImage = function(el){
		for(var child = el.firstChild; child != null; child = child.nextSibling){
			if (child.tagName == "IMG"){
				return child;
			}
		}
		return null;
	}
			
	var changeImgSrcAndTitle = function(img){
		if (null != config && null != config.url){
			var len = config.url.length;
			if(++currentIndex >= len){
				currentIndex = 0;
				if (false == config.repeat){
					clearInterval(switchInterval);
					if ("function" == typeof(config.onFinish)){
						return config.onFinish();
					}
					return;
				}
			}
			img.src = config.url[currentIndex];
			img.title = config.title[currentIndex];
		}
	}
			
	var imgRotate = function(){
		fadeInterval = setInterval(fadeAndChangeImages, fadeSpeed);
	}
	
	var fadeAndChangeImages = function(){
		YAHOO.util.Dom.setStyle(els[front], 'opacity', opacity);
		YAHOO.util.Dom.setStyle(els[behind], 'opacity', 1.0 - opacity);
		YAHOO.util.Dom.setStyle(els[behind], 'visibility', 'visible');

		opacity -= 0.1;
		if (0.0 > opacity){
			opacity = 1.0;
			clearInterval(fadeInterval);
			
			front = (++front)%2;
			behind = (++behind)%2;
			
			els[behind].style.zIndex = 1;
			els[front].style.zIndex = 2;
			changeImgSrcAndTitle(els[behind]);
			YAHOO.util.Dom.setStyle(els[behind], 'opacity', 1.0);
			YAHOO.util.Dom.setStyle(els[behind], 'visibility', 'hidden');
		}
	}
	
	init();
}

