	

	/************************************

	*	Class slideShow   				*

	*	Author: Jerome Guilbot			*

	*************************************/

	

	var slideShow = Class.create();

	

	slideShow.prototype = {

	

		initialize: function(aElems, aOptions)

		{

			/* Configuration */

			

			this.bDebug 	= true;				// if Firebug is installed

			this.bLoop      = true;					// activates loops

			this.bEffects 	= false;				// activates effects during transition

			this.imagePath	= aOptions.imagePath	//'/uploads/photos/';	// path to image folder

			this.container  = aOptions.container;

			this.controls  	= 'controls'; 

			this.width		= aOptions.width;

			this.height		= aOptions.height;

			

			/* if loops activated */

			this.delay 		= 8;			// time between switches in sec

			

			/* if effects activated*/

			this.transition = 0.5;			// animation duration in sec

			

			/* Configuration end */

			this.aElems 	= aElems;

			this.aPicIds 	= new Array();

			this.currentKey;

			

			

			var i = 0;

			

			for (var key in this.aElems) {

	

				$(this.container).innerHTML += '<img src="'+this.imagePath+key+'" id="pic_'+i+'" style="display:none" width="'+this.width+'" height="'+this.height+'" alt="" onclick="window.location.href=\''+this.aElems[key]+'\'" />'; // pre-loads images

				

				this.addToPager(i);			// paginates

				

				this.aPicIds.push('pic_'+i);

				

				i++;

			}

			

			if (this.aPicIds.length < 2) { // do not loop if less than 1 photo

				this.bLoop = false;

			} else {

				$(this.controls).show();

			}

			

			if (this.aPicIds.length > 0) {

				this.currentKey = 0;

				this.show(this.currentKey);

			}

	

		},

		

		next: function()

		{

			if (this.currentKey == this.aPicIds.length-1) { 	// if end of the array...

				var nextPicKey = 0; 							//... back to the beginning

			} else { 

				var nextPicKey = parseInt(this.currentKey)+1;

			}

			

			if (this.aPicIds[nextPicKey]) {

				this.show(nextPicKey);

			}

		},

		

		previous: function()

		{

			if (this.currentKey == 0) {  						// if beginning of the array...

				var previousPicKey = this.aPicIds.length-1; 	//... back to the end

			} else { 

				var previousPicKey = parseInt(this.currentKey)-1;

			}

			

			if (this.aPicIds[previousPicKey]) {

				this.show(previousPicKey);

			}

	

		},

		

		show: function(picKey)

		{

			if ($(this.aPicIds[picKey])) {

			

				if (this.bLoop) this.loop('stop');

				

				this.hide(this.aPicIds[this.currentKey]);

				$('a' + (this.currentKey)).className = '';

				

				this.currentKey = picKey;

				$('a' + (this.currentKey)).className = 'selected';

				

				if (this.bEffects) {

					new Effect.Appear(this.aPicIds[this.currentKey], {

						queue: 'end',

						duration: this.transition

					})

				} else {

					$(this.aPicIds[this.currentKey]).show();	// show() is a built-in function of the prototype library, not the method of this class

				}

				

				if (this.bLoop) this.loop('start');

			}

			this.sendConsole('current pic: ' + this.aPicIds[this.currentKey]);

		},

		

		hide: function(picId)

		{

			if ($(picId)) {

				if (this.bEffects) {

					new Effect.Fade(picId, {

						queue: 'end',

						duration: this.transition

					})

				} else {

					$(picId).hide();

				}

			}

		},

		

		loop: function(action)

		{

			if (action == 'start') {

				this.timeoutId = setTimeout(this.next.bind(this), this.delay*1000);

				this.sendConsole('loop start');

			} else if (action == 'stop') {

				if (this.timeoutId) clearTimeout(this.timeoutId);

				this.sendConsole('loop stop');

			}

		},

		

		addToPager: function(id)

		{

			var element	= '<li><a href="javascript: slideshow.show(\''+ id +'\')" id="a'+ id +'" >'+ (id+1) +'</a></li>';

			Element.insert($(this.controls).lastChild, { before: element });

		},

		

		sendConsole: function(elem)

		{

			if (typeof(console) != "undefined" && this.bDebug) console.debug(elem);

		}

	};

