ONEHAT.widget.imageCycler = {
	init: function() {
		var oThis = this;
		
		this.pic1 = $('imageCycler1'); // for preloading images only
		this.pic2 = $('imageCycler2');
		this.pic3 = $('imageCycler3');
		
		this.images = [
			'images/home/building1.jpg',
			'images/home/aerobicsLady.jpg',
			'images/home/waterguy.jpg',
			'images/home/waterboy.jpg',
			'images/home/bloodPressure.jpg',
			'images/home/proShop.jpg'
		];
		
		this.cachedImages = [];
		
		if (YAHOO.env.ua.gecko) { // Don't cycle images in firefox. Sadly it doesn't support JS animation with overlapping Flash content
			this.pic3.src = this.images[0];
		} else {
			this.anim1 = new YAHOO.util.Anim('imageCycler2');
				this.anim1.duration = 2;
				this.anim1.method = YAHOO.util.Easing.easeOut;
				this.anim1.onTop = false;
			this.anim2 = new YAHOO.util.Anim('imageCycler3');
				this.anim2.duration = 2;
				this.anim2.method = YAHOO.util.Easing.easeIn;
	
			// set initial states		
			YAHOO.util.Dom.setStyle(this.pic2, 'opacity', 0); 
			YAHOO.util.Dom.setStyle(this.pic3, 'opacity', 0); 
			this.currentImage = -1;
			this.initialized = false;
			this.paused = false;
			this.cachedImages[0] = this.pic1.src = this.images[0]; // preload first image into cache
			
			setTimeout(function() {
				oThis.next();
			}, 250);
		}
	},
	
	next: function() {
		var oThis = this;

		if (!this.paused || !this.initialized) {
			var anim, pic, fadeIn;
	
			if (!this.initialized) { // first time through, fade in anim1
	
				this.initialized = true;
				this.anim1.onTop = true;
				anim = this.anim1;
				fadeIn = true;
				pic = this.pic2;
	
			} else { // all other times, fade in/out anim2
	
				anim = this.anim2;
				if (!this.anim1.onTop) {
					this.anim1.onTop = true;
					fadeIn = false;
					pic = this.pic2;
				} else {
					this.anim1.onTop = false;
					fadeIn = true;
					pic = this.pic3;
				}			
	
			}
			
			this.currentImage = this.currentImage == this.images.length -1 ? 0 : this.currentImage +1; // shift the currentImage value
			pic.src = this.images[this.currentImage];
			YAHOO.util.Dom.removeClass(pic, 'hide');
			anim.attributes = fadeIn ? { opacity: { from: 0, to: 1 }  } : { opacity: { from: 1, to: 0 }  };
			anim.animate();			
	
			// prefetch next image
			if (!this.cachedImages[this.currentImage +1]) { // not in cache
				if (this.currentImage < this.images.length -1) { // not out of range
					this.cachedImages[this.currentImage +1] = this.pic1.src = this.images[this.currentImage +1];
				}
			}
		}
				
		setTimeout(function() {
			oThis.next();
		}, 5500);
	},
	
	pause: function() {
		if (!YAHOO.env.ua.webkit) {
			this.paused = !this.paused;
		}
	}
};

ONEHAT.widget.imageCycler.init();