var imgs_left = Array('menu_img_left_3.jpg', 'menu_img_left_1.jpg', 'menu_img_left_2.jpg', 'menu_img_left_4.jpg', 'menu_img_left_5.jpg', 'menu_img_left_6.jpg', 'menu_img_left_7.jpg');
//var imgs_center = Array('menu_img_center_1.jpg', 'menu_img_center_2.jpg', 'menu_img_center_3.jpg', 'menu_img_center_4.jpg');


/**************************************/

var ImgRotator = new Class ({
	
	initialize: function (oWhere, aImgs, iDelay) {
		this.images = aImgs;
		this.container = $(oWhere);
		this.next_loaded = false;
		this.curr_idx = Math.floor(this.images.length*Math.random());
		this.prev_idx = this.curr_idx;
		this.last_switch = (new Date()).getTime() + iDelay;
		this.fx = new Fx.Style(this.container.getParent(), 'opacity', {duration: 1000, transition: Fx.Transitions.linear});
		var showImage = this.showImage.bind(this);
		this.curr_image = new Asset.image(img_base_dir + this.images[this.curr_idx], {onload: showImage});
	},
	
	showImage: function () {
		var time_diff = (new Date()).getTime() - this.last_switch;
		if (time_diff > 5000) {
			var switch_img = (function() {this.container.src = this.curr_image.src; this.container.getParent().setOpacity(1);}).bind(this);
			new Fx.Style(this.container.getParent(), 'opacity', {duration: 500, onComplete: switch_img}).start(1,0.2);
			this.last_switch = (new Date()).getTime();
			//this.curr_idx++;
			//if (this.curr_idx == this.images.length) this.curr_idx = 0;
			
			this.curr_idx = Math.floor(this.images.length*Math.random());
			while (this.curr_idx == this.prev_idx) {
				this.curr_idx = Math.floor(this.images.length*Math.random());
			}
			this.prev_idx = this.curr_idx;
			
			var showImage = this.showImage.bind(this);
			this.curr_image = new Asset.image(img_base_dir + this.images[this.curr_idx], {onload: showImage});
		} else {
			this.showImage.delay(5100 - time_diff, this);
		}
	}
	
});

//window.addEvent('domready', function () { var menu_img_center = new ImgRotator('menu_img_center_img', imgs_center, 0); });
window.addEvent('domready', function () { var menu_img_left = new ImgRotator('menu_img_left_img', imgs_left, 3000); });
