/* note: skip #3 */
var PARotator = Class.create({
	initialize: function(args) {
		this.max_z = 5;
		this.image_path = '';
		this.image_pool_src = [
			'/images/icons/icon1.png',
			'/images/icons/icon2.png',
			'/images/icons/icon4.png',
			'/images/icons/icon5.png',
			'/images/icons/icon6.png',
			'/images/icons/icon7.png',
			'/images/icons/icon8.png',
			'/images/icons/icon9.png',
			'/images/icons/icon7612.png',
			'/images/icons/icon7638.png',
			'/images/icons/icon7659.png',
			'/images/icons/icon7676.png',
			'/images/icons/icon7746.png',
			'/images/icons/icon7755.png',
			'/images/icons/icon7790.png'
		];	
		this.image_target_pool = [];
		this.image_shown_src_pool = [];
		this.image_pool = [];
		this.image_target_idx = 0;
		if (args && args.image_path) {
			this.image_path = args.image_path;
			this.initImages();
			this.initImageTargets();
			this.timer = setInterval(function(){this.interval()}.bind(this), 8000);
		}
		
	},
	initImages: function() {
		this.image_pool_src.each(function(img_src){
			var img = new Image();
			img.src = this.image_path + img_src;
			this.image_pool.push(img);
		}.bind(this));
	},
	initImageTargets: function() {
		for (var i = 1; i < 5; i++) {
			var img_target = $('headline').select('.icon'+i)[0];
			this.image_target_pool.push(img_target);
			this.image_shown_src_pool.push(img_target.src);
		};
	},
	interval: function() {
		var found_unused = true;
		$A(this.image_pool).each(function(img) {
			found_unused = true; // reset for each image
			//window.console.log('checking: ' + img.src);
			
			this.image_shown_src_pool.each(function(image_shown_src) {
				// window.console.log('matching: ' + img.src + ';' + image_shown_src);
				if (img.src == image_shown_src) {
					// window.console.log('already used ');
					found_unused = false;
					throw $break;
				}
			}.bind(this));

			if (found_unused) {
				// found the unused image! rotate it in
				// window.console.log('rotating: ' + img.src);
				// record image as shown
				this.image_shown_src_pool.push(img.src);
				var target_img = this.image_target_pool[this.image_target_idx];
				$(target_img).hide();
				target_img.src = img.src;
				// move up the z-index 
				this.max_z++;
				target_img.style.zIndex = this.max_z;
				$(target_img).appear();
				this.image_target_idx++;
				if (this.image_target_idx > this.image_target_pool.length - 1) {
					this.image_target_idx = 0;
				}
				throw $break;
			}
		}.bind(this));
		
		if (!found_unused) {
			this.image_shown_pool = [];
		}
	}
});




