﻿
	if(typeof PicFocusManager == "undefined"){
		PicFocusManager =[];
	}
	function PicFocus(imageContainerID,intervarTime){
		this.$ = function (id){return document.getElementById(id)}
		this.index = PicFocusManager.length;
		PicFocusManager[PicFocusManager.length] = this;
		this.imageContainer = this.$(imageContainerID);
		this.firstShow = 0; //默认显示项
		this.interval = (isNaN(intervarTime)?0:intervarTime) || 5000;
		 //切换时间
		this.canAutoPlay = true; //是否可以自动切换
		this.currentPosition = this.firstShow;
		this.timer;
		this.divs = [];
		this.callback=function(){};
		this.bindEvent = function(){
			var _self = this;
			for(var i=0;i<this.divs.length;i++){
				this.divs[i].onmouseover = function(){
					_self.stop();
				}
				this.divs[i].onmouseout = function(){
					_self.timer = setTimeout('PicFocusManager[' + _self.index + '].play()' , _self.interval )
				}
			}
		}
		this.play = function(){
			this.stop();
			if(this.canAutoPlay){
				this.setFocus(this.currentPosition ++ )
				if(this.currentPosition >= this.divs.length)this.currentPosition =0 ;
			}
			this.timer = setTimeout('PicFocusManager[' + this.index + '].play()' , this.interval )
		}
		this.stop = function(){
			clearTimeout( this.timer );
		}

		this.setFocus = function(i){
			for(var j=0;j<this.divs.length;j++){
				this.divs[j].style.display = (i==j)?"":"none";
			}
			this.callback(i);
		}
		this.init = function(){
			if(this.imageContainer){
				//init
				this.divs=this.imageContainer.getElementsByTagName("div");
				this.bindEvent();
				for(var i=0;i<this.divs.length;i++){
					this.divs[i].style.display = "none";
				}
				this.divs[this.firstShow].style.display = "";
			}else{
				alert("未提供正确的参数")
			}
		}
	}