jQuery.fn.bannerTurnPage = function(userOptions)
{
    // Default options
    var options = {
	  allpage:0,
	  container:"",
	  imgs:"",
	  tabs:"",
	  leftBtn:"",
	  rightBtn:"",
	  allPos:{}
	}
	
    var pageIndex=1,preIndex,isAuto=true,isTurn=true,inteval;
	
	function init(where){
	    var length = $(where).find("li").length;
		options.allpage=length;
		preIndex=length;
		var allUl=$(where).find("li");
		var allImgs=$(options.imgs).find("div");
		for(var i=0 ;i<length-1;i++ ){
			options.allPos[i+1]=i;
		}
		options.allPos[0]=length-1;
		for(var i=0 ;i <length;i++){
			$(allImgs[options.allPos[i]]).css('position','absolute');
			$(allImgs[options.allPos[i]]).css('left','0px');
			$(allImgs[options.allPos[i]]).css('z-index','-2');
			$(allImgs[options.allPos[i]]).css('opacity',0.00);
		}
		$(allImgs[0]).css('z-index','-1');
		$(allImgs[0]).css('opacity',1.00);
		
		$(allUl[0]).bind("mousemove",function(){jumpTo(where,0)});
		$(allUl[1]).bind("mousemove",function(){jumpTo(where,1)});
		$(allUl[2]).bind("mousemove",function(){jumpTo(where,2)});
		$(allUl[3]).bind("mousemove",function(){jumpTo(where,3)});
		$(allUl[4]).bind("mousemove",function(){jumpTo(where,4)});
		$(allUl[5]).bind("mousemove",function(){jumpTo(where,5)});
		$(allUl[6]).bind("mousemove",function(){jumpTo(where,6)});

		$(allUl[0]).find("a:first-child").attr("href",$("#con_tophome_1 a:first-child").attr("href"));
		$(allUl[1]).find("a:first-child").attr("href",$("#con_tophome_2 a:first-child").attr("href"));
		$(allUl[2]).find("a:first-child").attr("href",$("#con_tophome_3 a:first-child").attr("href"));
		$(allUl[3]).find("a:first-child").attr("href",$("#con_tophome_4 a:first-child").attr("href"));
		$(allUl[4]).find("a:first-child").attr("href",$("#con_tophome_5 a:first-child").attr("href"));
		$(allUl[5]).find("a:first-child").attr("href",$("#con_tophome_6 a:first-child").attr("href"));
		$(allUl[6]).find("a:first-child").attr("href",$("#con_tophome_7 a:first-child").attr("href"));
	}
	
    function turnAuto(where){
        return function _move(){
		    if(isAuto){
			    var allUl=$(where).find("li");	
                var allImgs=$(options.imgs).find("div");
				var returnPos=options.allPos[0];
				for(var i=1;i<options.allpage;i++){
				    options.allPos[i-1]=options.allPos[i];
				}
				options.allPos[options.allpage-1]=returnPos;				
				for(var i=0 ;i <options.allpage;i++){
					if (i==1) {
						$(allImgs[options.allPos[i]]).css('z-index','-1');
						$(allImgs[options.allPos[i]]).fadeTo("slow",1.00);
						$(allUl[options.allPos[i]]).attr('class','on');
					}else if(i==0){
                        $(allImgs[options.allPos[i]]).css('z-index','-2');
                        $(allImgs[options.allPos[i]]).fadeTo("slow",0.00);
                        $(allUl[options.allPos[i]]).attr('class','out');
                    }else{
					    $(allImgs[options.allPos[i]]).css('z-index','-2');
						$(allImgs[options.allPos[i]]).css("opacity",0.00);
						$(allUl[options.allPos[i]]).attr('class','out');
						
					}						
				}
			}
		};
    }
	
	function jumpTo(where,index){
		
		var allUl = $(where).find("li");
		var allImgs = $(options.imgs).find("div");
		
		if (!isTurn){
		    return;
		}
		else {
		    isTurn = false;
		}
		
		if (index == options.allPos[1]) {
			isTurn = true;
			return;
		}
		else if (index < options.allPos[1]) { // 要显示的图在左边
			
			$(allUl[options.allPos[1]]).attr('class','out');
			$(allUl[index]).attr('class','on');
			
			$(allImgs[options.allPos[1]]).css('z-index','-2');
			$(allImgs[options.allPos[1]]).fadeTo("slow",0.00);
			
			$(allImgs[index]).css('z-index','-1');
			$(allImgs[index]).css('left','0px');
			$(allImgs[index]).fadeTo("slow",1.00,function() {
				
					for (var i=0;i<options.allpage;i++) {
						options.allPos[i] = i + index - 1;
						if (options.allPos[i] < 0) {
							options.allPos[i] += options.allpage;
						}
						else if (options.allpage <= options.allPos[i]) {
							options.allPos[i] -= options.allpage;
						}
						
						if (i != 1) {
							$(allImgs[options.allPos[i]]).css('left','0px');
						}
					}
					
					isTurn = true;
				}
			);
			
		}
		else { // 要显示的图在右边

			$(allUl[options.allPos[1]]).attr('class','out');
			$(allUl[index]).attr('class','on');

			$(allImgs[options.allPos[1]]).css('z-index','-2');
			$(allImgs[options.allPos[1]]).fadeTo("slow",0.00);
			
			$(allImgs[index]).css('z-index','-1');
			$(allImgs[index]).css('left','0px');
			$(allImgs[index]).fadeTo("slow",1.00,function() {

					for (var i=0;i<options.allpage;i++) {
						options.allPos[i] = i + index - 1;
						if (options.allPos[i] < 0) {
							options.allPos[i] += options.allpage;
						}
						else if (options.allpage <= options.allPos[i]) {
							options.allPos[i] -= options.allpage;
						}
						
						if (i != 1) {
							$(allImgs[options.allPos[i]]).css('left','0px');
						}
					}


					isTurn = true;
				}
			);

		}
		
	}
	
	function turnClick(where,para){
	    if(!isTurn){
		    return;
		}else{
		    isTurn=false;
		}
        var allUl=$(where).find("li");
		var allImgs=$(options.imgs).find("div");
		if(para==1){			       	    
				var returnPos=options.allPos[0];
				for(var i=1;i<options.allpage;i++){
				    options.allPos[i-1]=options.allPos[i];
				}
				options.allPos[options.allpage-1]=returnPos;				
				for(var i=0 ;i <options.allpage;i++){
					if(i==1){    
						$(allImgs[options.allPos[i]]).css('z-index','-1');
						$(allImgs[options.allPos[i]]).fadeTo("slow",1.00,function(){
						isTurn=true;});
						$(allUl[options.allPos[i]]).attr('class','on');
					}else if(i==0){
                        $(allImgs[options.allPos[i]]).css('z-index','-2');
						$(allImgs[options.allPos[i]]).fadeTo("slow",0.00);
                        $(allUl[options.allPos[i]]).attr('class','out');
                    }else{
                        $(allImgs[options.allPos[i]]).css('z-index','-2');
						$(allImgs[options.allPos[i]]).css({'left':'0px'});
						$(allUl[options.allPos[i]]).attr('class','out');
                    }						
				}	   
		}else{
				var returnPos=options.allPos[options.allpage-1];
				for(var i=options.allpage-2;i>=0;i--){
				    options.allPos[i+1]=options.allPos[i];
				}
				options.allPos[0]=returnPos;				
				for(var i=0 ;i <options.allpage;i++){
					if(i==1){    
						$(allImgs[options.allPos[i]]).css('z-index','-1');
						$(allImgs[options.allPos[i]]).fadeTo("slow",1.00,function(){
						isTurn=true;});
						$(allUl[options.allPos[i]]).attr('class','on');
					}else if(i==2){
                        $(allImgs[options.allPos[i]]).css('z-index','-2');
						$(allImgs[options.allPos[i]]).fadeTo("slow",0.00);
						$(allUl[options.allPos[i]]).attr('class','out');
                    }else{
                        $(allImgs[options.allPos[i]]).css('z-index','-2');
						$(allImgs[options.allPos[i]]).css({'left':'0px'});
						$(allUl[options.allPos[i]]).attr('class','out');
                    }						
				}		    
		}
    }
	
	function checkMouse(onOrNot){
	    if(onOrNot){
		    isAuto=false;
		}else{
		    isAuto=true;
		}
	}
	
	$.extend(options,userOptions);
	var here = "."+this.attr('class');
	$(options.leftBtn).bind("click",function(){turnClick(here,1)});
	$(options.rightBtn).bind("click",function(){turnClick(here,0)});
	this.on('mouseover',function(evt){checkMouse(true)});
	this.on('mouseout',function(evt){checkMouse(false)});
	init(here,this);
	interval=setInterval(turnAuto(here),5000);
}

$(function(){
    $("#banner").bannerTurnPage({
	    imgs:"#banner_img",
	    tabs:"#index_tab",
	    leftBtn:"#banner .lbpre",
	    rightBtn:"#banner .lbnext",
		left:710
	});


	// Jeffrey 添加 2014-05-22
	$("#banner").mouseover( 
		function () {
			$(".lbpre").css("display","block"); 
			$(".lbnext").css("display","block"); 
		} 
	); 
	
	$("#banner").mouseout( 
		function () { 
			$(".lbpre").css("display","none"); 
			$(".lbnext").css("display","none"); 
		} 
	);

})
