/*
  Copyright (c) 2010 James Olson, http://www.sixfootsixdesigns.com
  Prototype Cross Fade Gallery
  Constructor: new pt_cycle('id of wrapper', options object);
*/
if(pt_cycle == undefined){
	var pt_cycle = Class.create({
		initialize: function(element,options) {
			this.options = Object.extend({
				prev: null,
				next: null,
				lazy: false,
				lazy_q: 2,
				speed: 1,
				timeout: 5000,
				pause: 1,
				stopAfterClick: false,
				random: false,
				rotate: true,
				play_stop: null,
				stopBtnSrc: null,
				playBtnSrc: null			
			}, options || {});
			this.currSlide = 0;
			this.lastSlide = 0;
			this.nextSlide = 0;
			this.busy = false;
			this.timer = null;
			this.playStopBtn = null;
			this.stopAfterClickCheck = false;
			this.container = $(element);
			this.items = this.container.childElements();
			this.total = this.items.length;
			if(this.options.random == true){
				this.items.sort(function(a,b) {return Math.random() - 0.5;});
			}
			this.set_style();
			try{this.items[0].show();}catch(e){};
			if(this.total > 1){
				if(this.lazyLoad()){
					this.buildEvents();
					this.startup();
				}
			}
			this.paused = false;
		},
		set_style: function(){
			this.items.each(function(el){
				el.setStyle({
					position: 'absolute',
					top: '0px',
					left: '0px'
				});
				el.hide();
			});
			this.container.setStyle({position: 'relative'});
		},
		buildEvents: function(){
			if(this.options.prev != null){
				$(this.options.prev).observe('click',this.prev_click.bind(this));
			}
			if(this.options.next != null){
				$(this.options.next).observe('click',this.next_click.bind(this));
			}
			if(this.options.pause == 1){
				this.container.observe('mouseover', this.pause.bind(this));
				this.container.observe('mouseout', this.unpause.bind(this));
			}
			if(this.options.play_stop != null){
				this.playStopBtn = $(this.options.play_stop);
				this.playStopBtn.observe('click', this.playpause.bind(this));
			}
		},
		pause: function(e){
			this.paused = true;
			this.clearTimer();
		},
		unpause: function(e){
			this.paused = false;
			this.setTimer();
		},
		playpause: function(e){
			if(this.paused){
				if(this.options.stopBtnSrc != null){
					this.playStopBtn.src = this.options.stopBtnSrc;
					this.playStopBtn.title = 'Stop';
				}
				this.paused = false;
				this.nextImage();
			} else {
				if(this.options.playBtnSrc != null){
					this.playStopBtn.src = this.options.playBtnSrc;
					this.playStopBtn.title = 'Play';
				}
				this.paused = true;
				this.clearTimer();
			}
		},
		startup: function(){
			this.setTimer();
		},
		setTimer: function(){
			if(!this.stopAfterClickCheck){
				if(!this.paused && this.options.rotate) {
					this.timer = setTimeout(this.nextImage.bind(this), this.options.timeout);
				}
			}
		},
		clearTimer: function(){
			clearTimeout(this.timer);
		},
		animate: function(){
			if(this.options.lazy){
				this.lazyLoadNext(this.nextSlide); //because IE sucks
				this.lazyLoadNext(this.nextSlide + 1);
			}
			this.busy = true;
			this.clearTimer();
			this.items[this.currSlide].fade({duration: this.options.speed, afterFinish: function(){this.items[this.currSlide].hide();}.bind(this)});
			this.items[this.nextSlide].appear({duration: this.options.speed, afterFinish: function(){this.setTimer(); this.busy = false;}.bind(this)});
		},
		prev_click: function(e){
			Event.stop(e);
			if(this.options.stopAfterClick){
				this.stopAfterClickCheck = true;
			}
			this.clearTimer();
			this.previousImage();
		},
		next_click: function(e){
			Event.stop(e);
			if(this.options.stopAfterClick){
				this.stopAfterClickCheck = true;
			}
			this.clearTimer();
			this.nextImage();
		},
		nextImage: function(){
			if(!this.busy){
				this.clearTimer();
				this.lastSlide = this.currSlide;
				var roll = (this.nextSlide + 1) == this.total;
				this.nextSlide = roll ? 0 : this.nextSlide+1;
				this.currSlide = roll ? this.total-1 : this.nextSlide-1;
				this.animate();
			}
		},
		previousImage: function(){
			if(!this.busy){
				this.clearTimer();
				this.lastSlide = this.currSlide;
				var roll = (this.nextSlide - 1) < 0;
				this.nextSlide = roll ? this.total-1 : this.nextSlide-1;
				this.currSlide = roll ? 0 : this.nextSlide+1;
				this.animate();
			}
		},
		lazyLoadNext: function(i){
			var n = null;
			try{
				n = this.items[i];
			}catch(e){
				n = null;
			}
			if(n!= null && n != undefined){
				n.select('img').each(function(el){
					var att = el.readAttribute('lazy_src');
					if(att != null){
						el.src = att;
					}
				});
			}
			return;
		},
		lazyLoad: function(){
			if(this.options.lazy){
				var n = null;
				for(var i=0; i < this.options.lazy_q; i++){
					try{
						n = this.items[i];
					}catch(e){
						n = null;
					}
					if(n!= null || n != undefined){
						n.select('img').each(function(el){
							var att = el.readAttribute('lazy_src');
							if(att != null)
								el.src = att;
						});
					}
				}
			}
			return true;
		}
	});
}

