﻿    if(typeof PicFocusManager == "undefined")
	{
		PicFocusManager =[];
	}

	function PicFocus(imageContainerID,textContainerID,buttonContainerID,intervarTime){

		this.$ = function (id){return document.getElementById(id)}
		this.index = PicFocusManager.length;
		PicFocusManager[PicFocusManager.length] = this;
		this.imageContainer = this.$(imageContainerID);
		this.textContainer = this.$(textContainerID);
		this.buttonContainer = this.$(buttonContainerID);
		this.firstShow = 0; 
		this.interval = (isNaN(intervarTime)?0:intervarTime) || 5000;
		this.canAutoPlay = true; 
		this.currentPosition = this.firstShow;
		this.timer;
		this.images = [];
		this.texts = [];
		this.buttons = [];
		this.bindEvent = function(){
			var _self = this;
			for(var i=0;i<this.images.length;i++){
				this.images[i].onmouseover = function(){
					_self.stop();
				}
				this.images[i].onmouseout = function(event){
					var willPlay=true;
					if(!event)event=window.event;
					for(var i=0;i<_self.buttons.length;i++){
						if(typeof event.toElement != 'undefined' && event.toElement == _self.buttons[i]){
							willPlay=false;
						}
						
						if(typeof event.relatedTarget != 'undefined' && event.relatedTarget==_self.buttons[i]){
							willPlay=false;
						}
					}
					if(willPlay)
						_self.play();
				}
			}
			for(var i=0;i<this.buttons.length;i++){
				this.buttons[i].onclick = function(){
					_self.focus(this);
				}
			}
		}
		this.play = function(){
			if(this.canAutoPlay){
				this.setFocus(this.currentPosition ++ )
				if(this.currentPosition >= this.images.length)this.currentPosition =0 ;
				this.timer = setTimeout('PicFocusManager[' + this.index + '].play();' , this.interval )
			}
		}
		this.stop = function(){
			clearTimeout( this.timer );
		}
		this.focus = function(button){
			for(var i=0;i<this.buttons.length;i++){
				if(this.buttons[i] == button){
					this.currentPosition = i;
					this.setFocus(this.currentPosition);
					break;
				}
			}
		}
		this.setFocus = function(i){
       		try{
       		    var vNum=0;
                vNum = Math.random()
                vNum = Math.round(vNum*this.imageContainer.filters.length)
				this.imageContainer.filters[vNum].apply();
				this.imageContainer.filters[vNum].play();
			}catch(e){}
			for(var j=0;j<this.images.length;j++){
				this.images[j].style.display = (i==j)?"":"none";
			}
			for(var j=0;j<this.texts.length;j++){
				this.texts[j].style.display = (i==j)?"":"none";
			}
			for(var j=0;j<this.buttons.length;j++){
				this.buttons[j].className = (i==j)? this.buttons[j].getAttribute("focusClass") :this.buttons[j].getAttribute("normalClass");
			}

		}
		this.init = function(){
			if(this.imageContainer && this.textContainer && this.buttonContainer){
				//init
				this.images=this.imageContainer.getElementsByTagName("img");
				if(this.textContainer) this.texts=this.textContainer.getElementsByTagName("label");
				this.buttons=this.buttonContainer.getElementsByTagName("a");
				this.bindEvent();
				for(var i=0;i<this.images.length;i++){
					this.images[i].style.display = "none";
					if(i<this.texts.length) this.texts[i].style.display = "none";
					this.buttons[i].className = this.buttons[i].getAttribute("normalClass");
				}
				this.images[this.firstShow].style.display = "";
				if(this.firstShow<this.texts.length) this.texts[this.firstShow].style.display = "";
				this.buttons[this.firstShow].className = this.buttons[this.firstShow].getAttribute("focusClass");
			}else{
				
			}
       }
}