// mooShow 1.04
// (c)2006 Stuart Eaton - http://www.eatpixels.com
//
// Credit where credit is due: Inspiration from Lightbox (http://www.huddletogether.com/projects/lightbox2/)
// and Couloir (http://www.couloir.org/js_slideshow/) and of course moo.fx (http://moofx.mad4milk.net/
// moo.fx and prototype are covered by their own respective license terms.

// Revised by William Albritton to remove unused functions and make debugging IE easier... sigh... IE


// -------------------------------------------
// add autoShow() by tvlgiao
// this.autoshow="yes"; this.interval=5000;
// -------------------------------------------

// --------------------------------------------------------------------------------------------------------------

var loadingImage = "mooshow/loading.gif";		// loading image

// --------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------

var mooShows = null;







	// -----------------------------------------------------------------------------------
	
	function start() {
		// find any mooshows on the page
		find_mooshows();
	}
	
	// -----------------------------------------------------------------------------------
	
	function find_mooshows() {
		shows = document.getElementsByClassName("mooshow"); // find divs using the 'mooshow' class
		if(shows.length == 0) shows = showsIE; // If can't find shows list get it from html page (hack for IE5.5)
		mooShows = new Object();
		for ( var i = 0; i < shows.length; i++ ) {
			showName = shows[i].id; // get shownames
			if(showName==null) showName = shows[i]; // if we are getting show names from the html page (hack for IE5.5)
			mooShows[ showName ] = new mooshow(showName); // create new mooshow objects
			create_mooshow(showName,i); // create the shows htmls
		}
	}
	
	// -----------------------------------------------------------------------------------
	
	function create_mooshow( showName,shownumber ) {
		eval($(showName).innerHTML); //get slideshow settings
		photoArray = eval(showName); // set up image array based on id
		shownumber++; // add 1 to shownumber so it starts on 1 not 0
		
		if(this.dropShadow=='yes'){
			this.outerContainerClass = 'mooshow_outerContainer dropShadowBorder';
		} else {
			this.outerContainerClass = 'mooshow_outerContainer';
		}

		var mooShow_html = ''+
		'<div id=\''+showName+'_outerContainer\' class=\''+this.outerContainerClass+'\' style=\'padding:'+this.border+'px;\'> \n'+
		'	<div id=\''+showName+'_contentContainer\' class=\'mooshow_contentContainer\' > \n'+
		'		<img src=\''+photoArray[0][0]+'\' class=\'mooshow_image\' width=\''+photoArray[0][1]+'\' height=\''+photoArray[0][2]+'\' id=\''+showName+'_image\' /> \n'+
		'		<img src=\''+loadingImage+'\' id=\''+showName+'_loading\' class=\'mooshow_loading\' /> \n'+
		'		<pre id=\''+showName+'_imagename\' class=\'mooshow_imagename\'><br>'+photoArray[0][8]+'</pre> \n'+
		'	</div> \n'+
		'</div> \n'+
		'<script></script>';

		Element.setInnerHTML(showName, mooShow_html);
		
		// display or hide various options
		if(this.imagename == 'yes') Element.setInnerHTML(showName+'_imagename', photoArray[0][8]);
		Element.show(showName);
		
		// autoshow (tvlgiao)
		if (this.autoshow == 'yes') mooShows[showName].autoShow();
	}
	

// -----------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------



var mooshow = Class.create();
mooshow.prototype = {
	
	

	// -----------------------------------------------------------------------------------

	initialize: function(showName) {
		this.id = showName;
		this.busy = 0;
		this.counter = 0;
		this.photoArray = eval(this.id);
		this.numberOfImages = this.photoArray.length -2;
		eval($(showName).innerHTML); //get slideshow settings (set inside the mooshow div)
	},
	
	// -----------------------------------------------------------------------------------
	
	// tvlgiao
	autoShow : function () {
		if (this.autoshow == 'yes' && typeof(this.interval) != 'undefined') {
			window.setTimeout(function(){this.nextImage();this.autoShow();}.bind(this), this.interval);
			
		}
	},
	
	// -----------------------------------------------------------------------------------

	nextImage: function() {
		if(this.busy<1){
			this.busy=1;
			if(this.counter < this.numberOfImages) {
				this.counter ++;
			} else {
				this.counter = 0;
			}
			this.loadImage();
		}
	},
	
	// -----------------------------------------------------------------------------------
	
	prevImage: function() {
		if(this.busy<1){
			this.busy=1;
			if(this.counter > 0) {
				this.counter --;
			} else {
				this.counter = this.numberOfImages;
			}
			this.loadImage();			
		}
	},
	
	// -----------------------------------------------------------------------------------
	
	jumptoImage: function(counter) {
		if(this.busy<1){
			this.busy=1;
			this.counter = counter-1;
			this.loadImage();		
		}
	},
	
	// -----------------------------------------------------------------------------------
	
	switchContent: function(newArray) {
		if(this.busy<1){
			this.busy=1;
			this.photoArray = eval(newArray);
			this.numberOfImages = this.photoArray.length -2;
			this.counter = 0;
			this.loadImage();		
		}
	},
	
	// -----------------------------------------------------------------------------------

	loadImage: function() {
		//get showname
		showName = this.id;

		// show laoding animation
		Element.show(showName+'_loading');
		// preload in new image
		newImgPreloader = new Image();
		// if image is preloaded
		newImgPreloader.onload=function(){
			// when loaded
			// hide current photo
			Element.setSrc(showName+'_image','mooshow/blank.gif');
			Element.setOpacity(showName+'_image',0);
			// hide laoding animation
			Element.hide(showName+'_loading');
			// set image name to blank
			Element.setInnerHTML(showName+'_imagename', '');
			// get new sizes
			newHeight = newImgPreloader.height;
			newWidth = newImgPreloader.width;
			// resize containers to new size
			this.resizeOuterContainerHeight = new fx.Height(showName+'_image', {duration: mooShows[showName].speed});
			this.resizeOuterContainerWidth = new fx.Width(showName+'_image', {duration: mooShows[showName].speed, onComplete: function() {
				// set up next image
				Element.setSrc(showName+'_image',newImgPreloader.src);
				// image name
				if(mooShows[showName].imagename == 'yes') Element.setInnerHTML(showName+'_imagename', mooShows[showName].photoArray[mooShows[showName].counter][8]);
				// new moo.fx 'fader'
				this.fader = new fx.Opacity(showName+'_image', {duration: mooShows[showName].fadeSpeed, onComplete:function() { 
					mooShows[showName].busy = 0;
					}});
				// call fader fx
				this.fader.hide(); this.fader.toggle();
			}});
			
			// get current sizes
			oldHeight = Element.getHeight(showName+'_image');
			oldWidth = Element.getWidth(showName+'_image');

			// call moo.fx and when done switchImage()
			this.resizeOuterContainerHeight.custom(oldHeight,newHeight);
			this.resizeOuterContainerWidth.custom(oldWidth,newWidth);
		};
		
		newImgPreloader.src = this.photoArray[this.counter][0]; // preloader src
	}
	
	// -----------------------------------------------------------------------------------

}

// --------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------







// -----------------------------------------------------------------------------------
//	Additional methods for Element by SU, Couloir
//	- further additions by Lokesh Dhakar (huddletogether.com)
//	- and Stuart Eaton (eatpixels.com)

Object.extend(Element, {
	getWidth: function(element) {
	   	element = $(element);
	   	return element.offsetWidth; 
	},
	
	getHeight: function(element) {
	   	element = $(element);
	   	return element.offsetHeight; 
	},
	

	setWidth: function(element,w) {
	   	element = $(element);
    	element.style.width = w +"px";
	},
	setHeight: function(element,h) {
   		element = $(element);
    	element.style.height = h +"px";
	},
	setTop: function(element,t) {
	   	element = $(element);
    	element.style.top = t +"px";
	},
	setSrc: function(element,src) {
    	element = $(element);
    	element.src = src; 
	},
	
	setAlt: function(element,alt) {
    	element = $(element);
    	element.alt = alt; 
	},
	
	setOpacity: function(element,opacity) {
    	element = $(element);
    	element.style.opacity = opacity; 
	},

	setHref: function(element,href) {
    	element = $(element);
    	element.href = href; 
	},
	setInnerHTML: function(element,content) {
		element = $(element);
		element.innerHTML = content;
	},

	hide: function(element) {
      	element = $(element);
      	element.style.display = 'none';
  	},

  	show: function(element) {
      	element = $(element);
      	element.style.display = 'block';
  	}
});

// ---------------------------------------------------
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}

}

// ---------------------------------------------------

function run() {
	start();
}

addLoadEvent(run);	// run initMooshow onLoad

// --------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------

