// COS Slideshow
// By Daniel Walker
// March 07, 2007
// requires
//	yahoo-min.js	
//	dom-min.js
//	event-min.js
//	animation-min.js
//	CosUtil.js

// MODIFIED for Global Perspectives, on March 12, 2007

function CosSlideShow(eid, imgArray, iName)
	// eid: element id of image to be slideshow
	// imgArray: array of .src's
	// iName: name of instance of CosSlideShow
	{
		// set hold time to *ms for slideshow image duration
		var holdTime = 7777;
		var images = imgArray;
		var image = eid;
		var current = 0;
		var instanceName = (iName == undefined) ? 'cosSlideShow' : iName;
		this.length = imgArray.length;
		this.loader = Preloader;
		this.ready = 0;
		this.fadeOut = new YAHOO.util.Anim(
			image,
			{
				opacity: { to: 0 }
			},
			1,
			YAHOO.util.Easing.easeOut);
		
		var fadeIn = new YAHOO.util.Anim(
			image,
			{
				opacity: { to: 1 }
			},
			1,
			YAHOO.util.Easing.easeOut);
		
		this.construct = function()
		{
			// Mods for G_P
			C.setWidth(this.image, 472);
			C.setHeight(this.image, 104);
			
			// set up a temp image before all images load up
			this.renderCurrent();
			
			// if only one image, do not bother with a slideshow
			if(this.length <= 1)
				return;
			
			// start a preloader to preload an image
			this.loader.add(images);
			this.loader.onFinish(this.start);
			this.loader.load();
			
			this.fadeOut.onComplete.subscribe(this.renderCurrent);
			this.next = function()
			{
				this.putNext();
			}
		}
		
		this.start = function()
		{
			this.putNext;
			setInterval(instanceName+'.next()', holdTime);
		}
		
		this.putNext = function()
		{
			next = current;
			while(this.length > 1 && next == current)
				next = Math.floor(Math.random() * this.length);
			current = next;
			this.ready = 1;
			this.fadeOut.animate();
		}
		
		this.renderCurrent = function()
		{
			C._E(image).src = images[current];
			if(this.ready != 0)
				fadeIn.animate();
		}
	return this.construct();
}

// example instance:
// cosSlideShow = new CosSlideShow(	'imgForSlideShow',
//									arrayOfImages);
