window.addEvent("domready",function(){

	var ImageSrc = ["js/bg_body.jpg","js/bg_body2.jpg","js/bg_body3.jpg"];
	var container = $("imgbox");
	var fx = [];
	var next = prev = 0;

	container.setStyle("background","url(js/ajax-loader.gif) no-repeat center center");

	//Class Assets
	new Asset.images(ImageSrc,{
		onProgress:function(counter, index){
			//console.log(counter, index);
		},
		onComplete: function(){

			ImageSrc.each(function(src,index){

				var bg = new Element("div",{"id":"js/bg_body"+index,
					styles:{
						"height":"286px",
						"width":"826px",
						"background":"url("+src+") no-repeat center center",
						"position":"absolute",
						"top":0,
						"left":0,
						"z-index":0
						}
					});

				container.setStyle("background","none");
				
				bg.inject(container.getLast(),"after");

				fx[index] = new Fx.Tween(bg,{duration:2000,property:"opacity"}).set(0);

			});

			fx[0].start(1);

			next++;
			Repeat.delay(5500);

		},
		onError:function(){
			container.setStyle("background","url(js/bg_body.jpg) no-repeat center center");
		}
	});

	var Repeat = (function(){
		//console.log("next:"+next,"prev:"+prev);
		fx[prev].start(0);
		fx[next].start(1);

		next = (ImageSrc.length-1 > next ) ? next+1 : 0;
		prev = (0 > next-1)? ImageSrc.length-1 : next-1;
		Repeat.delay(5500);
	});
});