// JavaScript Document
function imgFocus(container,time){
	var THIS=this;
	this.index = 1;
	var pausePlay;
	var s=1;
	this.imgSize = jQuery(container+" .focusPic img").size();
	//初使化函數
	this.init = function(){
		//複制圖片到略縮圖
		//jQuery(container+" .focusPic a img").clone().appendTo(container+" .focusPreview");
		//加載數字按鈕
		jQuery(container+" .focusBtns").empty()
		for(i=this.imgSize,j=0;i>0;i--,j++){
			jQuery(container+" .focusBtns").append("<a></a>");
		}
		//設置焦點圖層的順序
		jQuery(container+" .focusPic a").eq(0).css("z-index",this.imgSize).css("position","absolute");
		jQuery(container+" .focusPic a").not(jQuery(container+" .focusPic a")[0]).css("z-index",1);
		this.setStatus(1);
		this.bindEvent();
	}
	//綁定動作函數
	this.bindEvent = function(){
		//略縮圖綁定動作
		jQuery(container+" .focusPreview img").hover(function(){
			jQuery(this).addClass("active");
		},function(){
			jQuery(this).removeClass("active");
			jQuery(container+" .focusPreview img").eq(THIS.index-1).addClass("active");
		});
		jQuery(container+" .focusPreview img").each(function(i){
			jQuery(this).click(function(){
				THIS.imgActive(i+1);
			});
		});

		//綁定數字按鈕動作
		jQuery(container+" .focusBtns a").hover(function(){
			jQuery(this).addClass("active");
		},function(){
			jQuery(this).removeClass("active");
			jQuery(container+" .focusBtns a").eq(THIS.imgSize - THIS.index).addClass("active");
		});
		jQuery(container+" .focusBtns a").each(function(i){
			jQuery(this).click(function(){
				THIS.imgActive(THIS.imgSize-i);
				clearTimeout(pausePlay); //當單擊按鈕停止循環
			});
		});

		//prev按鈕動作
		jQuery(container+" #prevLink").hover(function(){
			jQuery(this).addClass("active");
		},function(){
			jQuery(this).removeClass("active");
			jQuery(container+" #prevLink").eq(THIS.imgSize - THIS.index).addClass("active");
		});
		
		jQuery(container+" #prevLink").each(function(){
			jQuery(this).click(function(){
				if(s<=1){
					s=1;
				}else{
					s = s-1;
				}
				THIS.imgActive(s);
				clearTimeout(pausePlay); //當單擊按鈕停止循環
			});
		});


		//next按鈕動作
		jQuery(container+" #nextLink").hover(function(){
			jQuery(this).addClass("active");
		},function(){
			jQuery(this).removeClass("active");
			jQuery(container+" #nextLink").eq(THIS.imgSize - THIS.index).addClass("active");
		});
		
		jQuery(container+" #nextLink").each(function(){
			jQuery(this).click(function(){
				if(s>=THIS.imgSize){
					s=THIS.imgSize;
				}else{
					s = s+1;
				}
				THIS.imgActive(s);
				clearTimeout(pausePlay); //當單擊按鈕停止循環
			});
		});
	}
	//設置狀態函數
	this.setStatus = function(i){
		jQuery(container+" .focusPreview img").removeClass("active").eq(i-1).addClass("active");
		jQuery(container+" .focusBtns a").removeClass("active").eq(this.imgSize-i).addClass("active");
		jQuery(container+" #prevLink").removeClass("active").eq(this.imgSize-i).addClass("active");
		jQuery(container+" #nextLink").removeClass("active").eq(this.imgSize-i).addClass("active");
		jQuery(container+" .focusTitles a").hide().eq(i-1).show();
	}
	//焦點圖激活
	this.imgActive = function(i){
		if(THIS.index != i){
			var tempIndex = THIS.index;
			jQuery(container+" .focusPic a").eq(i-1).css("z-index",THIS.imgSize-1);
			this.setStatus(i);
			THIS.index = i;
			jQuery(container+" .focusPic a").eq(tempIndex-1).fadeOut(500,function(){
				jQuery(container+" .focusPic a").eq(i-1).css("z-index",THIS.imgSize);
				jQuery(container+" .focusPic a").not(jQuery(container+" .focusPic a")[THIS.index-1]).css("z-index",0).show();
			});
		}
	}

  //遞增輪換
	this.next = function(){
		
		var tempIndex = THIS.index;
		if(tempIndex>=THIS.imgSize){
			tempIndex = 1;
		}else{
			tempIndex++;
		}

		THIS.imgActive(tempIndex);
		pausePlay = setTimeout(THIS.next,time);
	}

	//啓動函數
	this.play = function(){
		this.init();
		setTimeout(this.next,time);
	}


}