/*
 *  Slideshow - Based on Prototype JavaScript framework
 *  (c) 2007 McLennan Community College, Network Services
 *
 *	Feel free to use this at your own risk, however leave this info intact.
 *	The code is in no way or nowhere near to be optimized.
 *	Future versions expected.
 *
 *	required libs:
 *
 *	prototype.js
 *	builder.js
 *	scriptaculous.js
 *	xmldom.js
 *
/*--------------------------------------------------------------------------*/
var xmlSlideShow = Class.create();

xmlSlideShow.prototype =
{
	initialize: function(obj)
	{
	/* NAME FOR THE SLIDESHOW */
	this.Instance = 'd41d8cd98f00b204e9800998ecf8427e';
	/* XML OBJECT PASSED */
	this.XML = obj;
	object = this;
	/* BASE URL FOR IMAGES */
	this.base = this.XML.getElementsByTagName("slideshow")[0].attributes[0].nodeValue;
	/* FADE SPEED */
	this.fadespeed = (this.XML.getElementsByTagName("slideshow")[0].attributes[1].nodeValue < 0.5) ? 0.5 : this.XML.getElementsByTagName("slideshow")[0].attributes[1].nodeValue;
	this.fadespeed = (this.XML.getElementsByTagName("slideshow")[0].attributes[1].nodeValue > 3.0) ? 3.0 : this.XML.getElementsByTagName("slideshow")[0].attributes[1].nodeValue;
	/* TOTAL GALLERIES IN OUR XML FILE DEFINED BY THE  <GALLERY> NODE */
	this.total = this.XML.getElementsByTagName("img").length;
	/* INITIALIZE CURRENT IMAGE INDEX */
	this.index = (!this.index) ? this.index = 0 : this.index;
	/* ARRAY TO HOLD IMAGE OBJECTS */
	this.images = new Array();
	/* ARRAY TO HOLD IMAGE TITLES */
	this.titles = new Array();
	/* CAPTION ON | OFF */
	this.captions = this.XML.getElementsByTagName("slideshow")[0].attributes[3].nodeValue;
	this.captions = (this.captions == 'on') ? true : false;
	/* VARIABLE TO HOLD DELAY IN SECONDS, DEFAULT 3 */
	this.delay = this.XML.getElementsByTagName("slideshow")[0].attributes[2].nodeValue;
	this.delay = (!this.delay || this.delay < '3') ? 3 : this.delay
	/* LOADING IMG */
	this.height = this.XML.getElementsByTagName("slideshow")[0].attributes[4].nodeValue;
	this.width = this.XML.getElementsByTagName("slideshow")[0].attributes[5].nodeValue;
	/* GET STUFF FROM XML */
	this.getdata();
	/* START THE SHOW */
	this.start();
	},

	getdata: function()
	{
	for(i=0;i<this.total;i++)
		{
		/* CREATE IMAGE OBJECTS */
		this.images[i] = Builder.node('img',{id:'XML_Slide_Show_Slide_'+i,src:this.base+this.XML.getElementsByTagName("img")[i].attributes[0].nodeValue});
		this.titles[i] = this.XML.getElementsByTagName("img")[i].attributes[1].nodeValue;
		}
	},



	start: function()
	{
		document.write('<div id="XML_Slide_Show" style="width:'+this.width+'px;"></div>');

		imageFrame = Builder.node('div',{id:'XML_Slide_Show_Image', style:'padding:0;font:normal 0px Tahoma,serif;width:'+this.width+'px;height:'+this.height+'px;'});
		$('XML_Slide_Show').appendChild(imageFrame);
		// IF WE HAVE CAPTIONS ENABLED
		if(this.captions)
		{
		captionFrame = Builder.node('div',{id:'XML_Slide_Show_Caption', className:'Ctrl_Caption'},this.titles[this.index]);
		$('XML_Slide_Show').appendChild(captionFrame);
		}
		// CONTROLS
		ctrlFrame = Builder.node('div',{id:'XML_Slide_Show_Ctrl',className:'XML_Slide_Show_Ctrl',style:'width:auto;height:30px;'});
		countFrame = Builder.node('div',{id:'XML_Slide_Show_Ctrl_Count',className:'Ctrl_Count'},'Photo: '+(this.index+1)+'/'+this.total);
		linkNext = Builder.node('a',{id:'XML_Slide_Show_Ctrl_Next',className:'Ctrl_Next',href:'#'},'Next');
		linkPrev = Builder.node('a',{id:'XML_Slide_Show_Ctrl_Prev',className:'Ctrl_Prev',href:'#'},'Prev');

		$('XML_Slide_Show').appendChild(ctrlFrame);
		$('XML_Slide_Show_Ctrl').appendChild(countFrame);
		$('XML_Slide_Show_Ctrl').appendChild(linkNext);
		$('XML_Slide_Show_Ctrl').appendChild(linkPrev);

		// PUT FIRST IMAGE INTO ROTATION
		$('XML_Slide_Show_Image').appendChild(this.images[this.index]);
		$(this.images[this.index]).style.position = 'relative';
		Element.setOpacity(this.images[this.index], 0);
		Effect.Appear('XML_Slide_Show_Slide_'+this.index, {duration: this.fadespeed, fps: 90, from: 0.0, to: 1.0});

		this.onClickNext   = this.cycleForward.bindAsEventListener(this);
		this.onClickPrev = this.cycleBackward.bindAsEventListener(this);

		/* LISTENERS */
		Event.observe('XML_Slide_Show_Ctrl_Prev', 'click', this.onClickPrev);
		Event.observe('XML_Slide_Show_Ctrl_Next', 'click', this.onClickNext);
	},



 	cycleForward: function()
 	{
 		//DISABLE THE LISTENER WHILE THE EFFECTS ARE RUNNING - TO PREVENT SPAM CLICKING
 		Event.stopObserving('XML_Slide_Show_Ctrl_Next', 'click', this.onClickNext);

 		$(object.images[object.index]).style.top = $(object.images[object.index]).offsetTop+'px';
 		$(object.images[object.index]).style.left = $(object.images[object.index]).offsetLeft+'px';
 		$(object.images[object.index]).style.position = 'absolute';

		nindex = ((object.index + 1) < object.total) ? object.index + 1 : 0;

		Element.setOpacity(object.images[nindex], 0);

 		$(object.images[nindex]).style.top = $(object.images[object.index]).offsetTop+'px';
 		$(object.images[nindex]).style.left = $(object.images[object.index]).offsetLeft+'px';
 		$(object.images[nindex]).style.position = 'absolute';

 		$('XML_Slide_Show_Image').appendChild(object.images[nindex]);
 		Effect.Fade('XML_Slide_Show_Slide_'+object.index, {afterFinish: function()
 		{
 		$(object.images[nindex]).style.position = 'relative';
 		$(object.images[nindex]).style.top = 0;
 		$(object.images[nindex]).style.left = 0;
 		},
 		duration: object.fadespeed, fps: 90, from: 1.0, to: 0.0});

 		Effect.Appear('XML_Slide_Show_Slide_'+nindex, {afterFinish: function()
 		{
 			//ENABLE THE LISTENER BACK
			Event.observe('XML_Slide_Show_Ctrl_Next', 'click', object.onClickNext);
 		},
 		duration: object.fadespeed, fps: 90, from: 0.0, to: 1.0});

 		object.index++;
 		object.index = (object.index < object.total) ? object.index : 0;
 		// IF THERE IS A CAPTION
 		if(object.captions)
 		{
 			if(object.titles[object.index])
 			{
	 		$('XML_Slide_Show_Caption').innerHTML = object.titles[object.index];
	 		Element.show($('XML_Slide_Show_Caption'));
	 		}
	 		else
	 		{
	 		$('XML_Slide_Show_Caption').innerHTML = "";
	 		Element.hide($('XML_Slide_Show_Caption'));
	 		}
 		}
 		// COUNT
 		$('XML_Slide_Show_Ctrl_Count').innerHTML = 'Photo: '+(object.index+1)+'/'+object.total;
 	},


 	cycleBackward: function()
 	{
 		//DISABLE THE LISTENER WHILE THE EFFECTS ARE RUNNING - TO PREVENT SPAM CLICKING
 		Event.stopObserving('XML_Slide_Show_Ctrl_Prev', 'click', this.onClickPrev);

 		$(object.images[object.index]).style.top = $(object.images[object.index]).offsetTop+'px';
 		$(object.images[object.index]).style.left = $(object.images[object.index]).offsetLeft+'px';
 		$(object.images[object.index]).style.position = 'absolute';

		pindex = ((object.index - 1) < 0) ?  object.total - 1 : object.index-1;

 		Element.setOpacity(object.images[pindex], 0);

 		$(object.images[pindex]).style.top = $(object.images[object.index]).offsetTop+'px';
 		$(object.images[pindex]).style.left = $(object.images[object.index]).offsetLeft+'px';
 		$(object.images[pindex]).style.position = 'absolute';

 		$('XML_Slide_Show_Image').appendChild(object.images[pindex]);
 		Effect.Fade('XML_Slide_Show_Slide_'+object.index, {afterFinish: function()
 		{
 		$(object.images[pindex]).style.position = 'relative';
 		$(object.images[pindex]).style.top = 0;
 		$(object.images[pindex]).style.left = 0;
 		},
 		duration: object.fadespeed, fps: 90, from: 1.0, to: 0.0});

 		Effect.Appear('XML_Slide_Show_Slide_'+pindex, {afterFinish: function()
 		{
 			//ENABLE THE LISTENER BACK
			Event.observe('XML_Slide_Show_Ctrl_Prev', 'click', object.onClickPrev);
 		},
 		duration: object.fadespeed, fps: 90, from: 0.0, to: 1.0});

 		object.index--;
 		object.index = (object.index < 0) ? object.total - 1 : object.index;

 		// IF THERE IS A CAPTION
 		if(object.captions)
 		{
	 		if(object.titles[object.index])
	 		{
	 		Element.show($('XML_Slide_Show_Caption'));
	 		$('XML_Slide_Show_Caption').innerHTML = object.titles[object.index];
	 		}
	 		else
	 		{
	 		Element.hide($('XML_Slide_Show_Caption'));
	 		}
 		}
 		// COUNT
 		$('XML_Slide_Show_Ctrl_Count').innerHTML = 'Photo: '+(object.index+1)+'/'+object.total;
 	}
};


//SIMPLE AUTOPLAY SLIDESHOW - EXTENDED/INHERITED FROM MAIN CLASS

var xmlSlideShowSimple = Class.create();
Object.extend(xmlSlideShowSimple.prototype, xmlSlideShow.prototype);
Object.extend(xmlSlideShowSimple.prototype,
{

	start: function()
	{
		document.write('<div id="XML_Slide_Show" style="width:'+this.width+'px;"></div>');

		imageFrame = Builder.node('div',{id:'XML_Slide_Show_Image', style:'padding:0;font:normal 0px Tahoma,serif;width:'+this.width+'px;height:'+this.height+'px'});
		$('XML_Slide_Show').appendChild(imageFrame);
		// IF WE HAVE CAPTIONS ENABLED
		if(this.captions)
		{
		captionFrame = Builder.node('div',{id:'XML_Slide_Show_Caption', className:'Ctrl_Caption'},this.titles[this.index]);
		$('XML_Slide_Show').appendChild(captionFrame);
		}

		// PUT FIRST IMAGE INTO ROTATION
		$('XML_Slide_Show_Image').appendChild(this.images[this.index]);
		$(this.images[this.index]).style.position = 'relative';
		Element.setOpacity(this.images[this.index], 0);
		Effect.Appear('XML_Slide_Show_Slide_'+this.index, {duration: this.fadespeed, fps: 90, from: 0.0, to: 1.0});


		/* LISTENERS */
		new PeriodicalExecuter(this.cycleForward, this.delay);
	},


	cycleForward: function()
 	{

 		$(object.images[object.index]).style.top = $(object.images[object.index]).offsetTop+'px';
 		$(object.images[object.index]).style.left = $(object.images[object.index]).offsetLeft+'px';
 		$(object.images[object.index]).style.position = 'absolute';

		nindex = ((object.index + 1) < object.total) ? object.index + 1 : 0;

		Element.setOpacity(object.images[nindex], 0);

 		$(object.images[nindex]).style.top = $(object.images[object.index]).offsetTop+'px';
 		$(object.images[nindex]).style.left = $(object.images[object.index]).offsetLeft+'px';
 		$(object.images[nindex]).style.position = 'absolute';

 		$('XML_Slide_Show_Image').appendChild(object.images[nindex]);
 		Effect.Fade('XML_Slide_Show_Slide_'+object.index, {afterFinish: function()
 		{
 		$(object.images[nindex]).style.position = 'relative';
 		$(object.images[nindex]).style.top = 0;
 		$(object.images[nindex]).style.left = 0;
 		},
 		duration: object.fadespeed, fps: 90, from: 1.0, to: 0.0});

 		Effect.Appear('XML_Slide_Show_Slide_'+nindex, {duration: object.fadespeed, fps: 90, from: 0.0, to: 1.0});

 		object.index++;
 		object.index = (object.index < object.total) ? object.index : 0;
 		// IF THERE IS A CAPTION
 		if(object.captions)
 		{
 			if(object.titles[object.index])
 			{
	 		$('XML_Slide_Show_Caption').innerHTML = object.titles[object.index];
	 		Element.show($('XML_Slide_Show_Caption'));
	 		}
	 		else
	 		{
	 		$('XML_Slide_Show_Caption').innerHTML = "";
	 		Element.hide($('XML_Slide_Show_Caption'));
	 		}
 		}
 	}

});