var imgRot = {
	acceptchange:true,
	activeImage:"",
	timer:3000,
	initialize:function(){
		//$("image_layer").style.display = 'block';
		//$("image_layer2").style.display = 'block';
		var container = $("image_layer");
		if (container){
			var rotators = container.select("tr")[0].select("td")[0].select(".imgRot");

			
			rotators.each(function(n){
				var images = n.select("img");
				images.without(images.first()).invoke("hide");
				imgRot.start(n);
				/*
				var images = n.select("a");
				var without = images.without(images.first());
				without.each(function(p) {
					p.select("img").invoke("hide");
				});
				imgRot.start(n);*/
			});
		}
	},
	start:function(n){
		var images = n.select("img");
		if (images.size()>1){
			images.each(function(m){
				if (m.getStyle("display") != "none"){
					imgRot.activeImage = m;
					throw $break;
				}
			});
			var length = parseInt(imgRot.activeImage.getAttribute("imglength"),10);
			this.activeImageHandle = window.setTimeout("imgRot.nextImage()",length);
			//this.nextImage();
		}
	},
	nextImage: function(){
		var nextImage = "";
		var stopRotation = false;
		if(arguments.length > 0) {
			nextImage = arguments[0];
			stopRotation = true;
		} else {
		
			nextImage = this.activeImage.next();
		}

		if (this.activeImage == this.activeImage.up().childElements().last()){
			nextImage = this.activeImage.siblings().first();
		}
		
		Effect.Fade(this.activeImage,{delay:stopRotation ? 0.5 : 2});
		Effect.Appear(nextImage,{delay:stopRotation ? 0.5 : 2});
		this.activeImage = nextImage;
		
		var length = parseInt(nextImage.getAttribute("imglength"),10);
		if(!stopRotation)
			this.activeImageHandle = window.setTimeout("imgRot.nextImage()",length);
		else {
			window.setTimeout("imgRot.acceptchange = true;", 1500);
		}
	},
	jump: function(n) {
		$('image_layer').style.display = 'block';
		$('image_layer2').style.display = 'block';
		$('flash_layer').style.display = 'none';	
		if(!imgRot.acceptchange || this.activeImage == n)
			return;
		imgRot.acceptchange = false;
		window.clearTimeout(this.activeImageHandle);
		this.nextImage(n);
	}
}

document.observe("dom:loaded", function() {imgRot.initialize();});