/*
* 图片预载入
*/
var loader = new Image();
function ImgLoader(path) {
	this.path = path;
	this.loadid = 0;
	loader.onload = Delegate.create(this,this.loaded);
}
ImgLoader.prototype.init = function(o) {
	this.add(0);
};
ImgLoader.prototype.add = function(id) {
	loader.src = this.path[id];
};
ImgLoader.prototype.loaded = function() {
	this.loadid++;
	if(this.loadid>=this.path.length){
		this.onDone();
	}else{
		this.add(this.loadid);
	}
};
ImgLoader.prototype.onDone = function() {};

/**
* 主类
*/
function Rotator(){
	this.rotator = document.getElementById("rotator");
	this.intervalID = 0;
	this.num = 5;
	this.curr = 0;
	this.img = [
		"zhai/images/index/b1.gif",
		"zhai/images/index/b2.gif",
		"zhai/images/index/b3.gif",
		"zhai/images/index/b4.gif",
		"zhai/images/index/b5.gif"
	];
	this.btn = [
		document.getElementById("b1"),
		document.getElementById("b2"),
		document.getElementById("b3"),
		document.getElementById("b4"),
		document.getElementById("b5")
	];
	this.rotator.innerHTML = '<a href="'+this.btn[0].href+'"><img src="'+this.img[0]+'" target="_blank" /></a>';
	var imgloader = new ImgLoader(this.img);
	imgloader.onDone = Delegate.create(this,this.init);
	imgloader.init();
}

Rotator.prototype.init = function(){
	for(var i=0;i<this.num;i++){
		this.btn[i].onmouseover = Delegate.create(this,this.pause,i);
		this.btn[i].onmouseout	= Delegate.create(this,this.play);
	}
	this.btn[0].className = "selected";
	this.play();
};

Rotator.prototype.pause = function(n,m){
	if(!isNaN(m)) n=m;
	if(this.curr!=n){
		this.btn[this.curr].className = "";
		this.btn[n].className = "selected";
		this.curr = n;
	}
	this.rotator.innerHTML = '<a href="'+this.btn[this.curr].href+'" target="_blank"><img src="'+this.img[this.curr]+'" /></a>';
	clearInterval(this.intervalID);
};

Rotator.prototype.play = function(){
	clearInterval(this.intervalID);
	this.intervalID = window.setInterval(Delegate.create(this,this.change),5000);
};

Rotator.prototype.change = function(){
	this.btn[this.curr].className = "";
	this.curr++;
	if(this.curr >= this.num) this.curr = 0;
	this.btn[this.curr].className = "selected";
	this.rotator.innerHTML = '<a href="'+this.btn[this.curr].href+'" target="_blank"><img src="'+this.img[this.curr]+'" /></a>';
};

var r = new Rotator();

