/* 
	Copyright (c) 2007-2008 Nesbert Hidalgo (http://nesbert.com)
	
	Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
	documentation files (the "Software"), to deal in the Software without restriction, including without limitation
	the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
	and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
	
	The above copyright notice and this permission notice shall be included in all copies or substantial portions
	of the Software.
	
	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
	TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
	THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
	CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
	DEALINGS IN THE SOFTWARE.
	
	Licensed under The MIT License (http://www.opensource.org/licenses/mit-license.php The MIT License).
	Redistributions of files must retain the above copyright notice.
	
	For details, see the script.aculo.us web site: http://nesbert.com/slideshow
	
	*Required* Prototype (http://www.prototypejs.org)
	Optional Script.aculo.us (http://script.aculo.us)
*/

Event.observe(window, "load", function() {
	$$('.slideshow').each(function(o) {
		new Slideshow(o);
	});
});

var Slideshow = Class.create({
	
	show: '',
	
	imgs: [],
	
	controls: '',
	
	currentIndex: 0,
	
	frequency: 5,
	
	initialize: function(elm)
	{
		this.show = $(elm.id);
		
		var i = 0;
		this.show.descendants().each(function(o) {
			if (o.tagName == 'IMG') {
				//o.style.position = 'absolute'
				o.hide();
				this.imgs[i] = o;
				i++;
			}
		}.bind(this));
		
		if (!this.imgs.length) return;
		
		this.show.insert(''+
			'<div class="slideshow_controls">'+
				'<span class="slideshow_label"></span>'+
			'</div>');
		
		this.controls = this.show.down('.slideshow_controls');
		if (this.imgs.length > 1) {
			this.controls.insert(''+
				'<span class="slideshow_links">'+
					'<a href="javascript:void(0);" class="control_first pngfix"><span>First</span></a>'+
					'<a href="javascript:void(0);" class="control_prev pngfix"><span>Previous</span></a>'+
					'<a href="javascript:void(0);" class="control_play pngfix"><span>Play</span></a>'+
					'<a href="javascript:void(0);" class="control_stop pngfix"><span>Stop</span></a>'+
					'<a href="javascript:void(0);" class="control_next pngfix"><span>Next</span></a>'+
					'<a href="javascript:void(0);" class="control_last pngfix"><span>Last</span></a>'+
				'</span>');
			this.addControlEvents();
		}
		
		this.showCurrent();
		
		if (this.show.readAttribute('frequency')) {
			this.frequency = this.show.readAttribute('frequency');
			this.play();
		}
	},
	
	addControlEvents: function()
	{
		if (this.show.readAttribute('controls')) {
			var controls = $A(this.show.readAttribute('controls').gsub(' ', '').split(','));
		}
		
		if (!controls || controls.indexOf('first') != -1) {
			$(this.controls.down('a.control_first')).onclick = function() {
				this.first();
				return false;
			}.bind(this);
		} else {
			$(this.controls.down('a.control_first')).hide();
		}
		
		if (!controls || controls.indexOf('prev') != -1) {
			$(this.controls.down('a.control_prev')).onclick = function() {
				this.prev();
				return false;
			}.bind(this);
		} else {
			$(this.controls.down('a.control_prev')).hide();
		}
		
		if (!controls || controls.indexOf('play') != -1) {
			$(this.controls.down('a.control_play')).onclick = function() {
				this.play();
				return false;
			}.bind(this);
		} else {
			$(this.controls.down('a.control_play')).hide();
		}
		
		if (!controls || controls.indexOf('stop') != -1) {
			$(this.controls.down('a.control_stop')).onclick = function() {
				this.stop();
				return false;
			}.bind(this);
		} else {
			$(this.controls.down('a.control_stop')).hide();
		}
		
		if (!controls || controls.indexOf('next') != -1) {
			$(this.controls.down('a.control_next')).onclick = function() {
				this.next();
				return false;
			}.bind(this);
		} else {
			$(this.controls.down('a.control_next')).hide();
		}
		
		if (!controls || controls.indexOf('last') != -1) {
			$(this.controls.down('a.control_last')).onclick = function() {
				this.last();
				return false;
			}.bind(this);
		} else {
			$(this.controls.down('a.control_last')).hide();
		}
	},
	
	currentImage: function()
	{
		return this.imgs[this.currentIndex];
	},
	
	hideCurrent: function()
	{
		var img = this.currentImage();
		if (Effect) {
			new Effect.Fade(img, {duration:0.2, queue:'end'});
		} else {
			img.hide();
		}
	},
	
	showCurrent: function(index)
	{
		var img = this.currentImage();
		if (Effect) {
			new Effect.Appear(img, {queue:'end'});
		} else {
			img.show();
		}
		if (img.alt) this.controls.down('span.slideshow_label').innerHTML = img.alt;
	},
	
	next: function(index)
	{
		this.hideCurrent();
		if (index) this.currentIndex = index;
		if ( this.currentIndex < (this.imgs.length - 1) ) {
			this.currentIndex++;
		} else {
			this.currentIndex = 0;
		}
		this.showCurrent();
	},
	
	prev: function(index)
	{
		this.hideCurrent();
		if (index) {
			this.currentIndex = index;
		} else {
			this.currentIndex--;
		}
		if (this.currentIndex < 0) {
			this.currentIndex = this.imgs.length - 1;
		}
		this.showCurrent();
	},
	
	play: function(frequency)
	{
		if (this.imgs.length < 2) return;
		var This = this;
		if (frequency) this.frequency = frequency;
		this.pe = new PeriodicalExecuter(function(){This.next();}, this.frequency);
		this.pe.currentlyExecuting = false;
		this.controls.down('a.control_stop').removeClassName('control_stop_on');
		this.controls.down('a.control_play').addClassName('control_play_on');
	},
	
	stop: function()
	{
		this.pe.currentlyExecuting = true;
		this.controls.down('a.control_play').removeClassName('control_play_on');
		this.controls.down('a.control_stop').addClassName('control_stop_on');
	},
	
	first: function()
	{
		this.next(this.imgs.length - 1);
	},
	
	last: function()
	{
		this.next(this.imgs.length - 2);
	}
	
});
