/*
 * 功能明称
 *
 * 描述
 *
 * @date 2008-1-26
 */
$(document).ready(function(){

//============================== 信息切换展示功能 =================================

	//----------------------- 自定义设置 -----------------------------
	var changeSpeed = "slow";			//定义动画的速度 
	var autoPlayFlag = true;            //是否自动播放
	var autoPlayTime = 10;              //自动播放时，每次切换停留的时间，单位为秒
	var currentBtnColor = "red";		//设置当前播放位置，高亮按钮的文字颜色
	//--------------------------------------------------------------

	var adang;																//定义定时器的指针(It's my name ^0^)
	var currentPhotoNum = 1;												//定义当前显示图片的指针
	var currentBtnNum = 1;
	var newsSpanNum = $(".newsImageContainer img").size();					//展示消息的条数
	var inforSpanWidth = $(".newsInforContainer").width();					//获得信息展示块的宽度
	var clickAble = true;													//覆盖点击标志
	var oZindex = 100;
	
	//如果没有信息
	if(newsSpanNum==0) return;
	//如果信息数为1
	if(newsSpanNum==1){
		$(".playPrevBtn").parent().hide();	
		return;
	}
	//如果信息数大于1
	$(".newsImageContainer img:first").parent().before($(".newsImageContainer img:last").parent().clone());
	$(".newsImageContainer img:last").parent().after($(".newsImageContainer img").eq(1).parent().clone());
	$(".newsInforBox:first").before($(".newsInforBox:last").clone());
	$(".newsInforBox:last").after($(".newsInforBox").eq(1).clone());
	$(".newsInforContainerBig").css({left:-inforSpanWidth,width:inforSpanWidth*(newsSpanNum+2)});
	for(var i=0;i<newsSpanNum;i++){
		$(".playNextBtn").before('<input type="button" class="playGoBtn" value="'+(i+1)+'" /> ');
	}
	$(".newsImageContainer img").css("opacity",0);
	$(".newsImageContainer img").eq(currentPhotoNum).css("opacity",1);
	$(".playPrevBtn").click(showNextNews);
	$(".playNextBtn").click(showPrevNews);
	$(".playGoBtn").click(function(){goNews($(this))});
	function showNextNews(){			//播放下一张
		if(!clickAble)	return;
		clickAble=false;
		$(".newsInforContainerBig").animate({left:parseInt($(".newsInforContainerBig").css("left"))-inforSpanWidth},changeSpeed,function(){
			clickAble=true;
			if(parseInt($(".newsInforContainerBig").css("left"))<=(-inforSpanWidth*(newsSpanNum+1))){ 
				$(".newsInforContainerBig").css("left",-inforSpanWidth);
			}
		});
		currentPhotoNum++;
		$(".newsImageContainer img").eq(currentPhotoNum-1).fadeTo(changeSpeed,0);
		$(".newsImageContainer img").eq(currentPhotoNum).fadeTo(changeSpeed,1,function(){
			if(currentPhotoNum==newsSpanNum+1){
				$(".newsImageContainer img").eq(currentPhotoNum).css("opacity",0);
				currentPhotoNum=1;
				$(".newsImageContainer img").eq(currentPhotoNum).css("opacity",1);				
			}
			currentBtnNum--;
			if(currentBtnNum<=0) currentBtnNum = newsSpanNum;
			setCurrentBtnBg(currentBtnColor,currentBtnNum);
			oZindex++;
			$(this).css("z-index",oZindex);
		});
		clearTimeout(adang);
		autoPlay();
	}
	function showPrevNews(){				//播放上一张
		if(!clickAble)	return;
		clickAble=false;
		$(".newsInforContainerBig").animate({left:parseInt($(".newsInforContainerBig").css("left"))+inforSpanWidth},changeSpeed,function(){
			clickAble=true;
			if(parseInt($(".newsInforContainerBig").css("left"))>=0){
				$(".newsInforContainerBig").css("left",(-inforSpanWidth*newsSpanNum));
			}
		});
		currentPhotoNum--;
		$(".newsImageContainer img").eq(currentPhotoNum+1).fadeTo(changeSpeed,0);
		$(".newsImageContainer img").eq(currentPhotoNum).fadeTo(changeSpeed,1,function(){
			if(currentPhotoNum==0){
				$(".newsImageContainer img").eq(currentPhotoNum).css("opacity",0);
				currentPhotoNum=newsSpanNum;
				$(".newsImageContainer img").eq(currentPhotoNum).css("opacity",1);
			}
			currentBtnNum++;
			if(currentBtnNum>=newsSpanNum+1) currentBtnNum = 1;
			setCurrentBtnBg(currentBtnColor,currentBtnNum);
			oZindex++;
			$(this).css("z-index",oZindex);
		});	
		clearTimeout(adang);
		autoPlay();
	}
	function goNews(jqu){					//播放指定张
		var num = parseInt(jqu.attr("value"));
		if(num==currentPhotoNum) return;
		if(!clickAble)	return;
		clickAble=false;
		$(".newsInforContainerBig").animate({left:-inforSpanWidth*num},changeSpeed,function(){
			clickAble=true;
			if(parseInt($(".newsInforContainerBig").css("left"))>=0){
				$(".newsInforContainerBig").css("left",(-inforSpanWidth*newsSpanNum));
			}
		});
		$(".newsImageContainer img").eq(currentPhotoNum).fadeTo(changeSpeed,0);
		$(".newsImageContainer img").eq(num).fadeTo(changeSpeed,1,function(){
			currentPhotoNum = currentBtnNum = num;
			setCurrentBtnBg(currentBtnColor,currentBtnNum);
			oZindex++;
			$(this).css("z-index",oZindex);
		});	
		clearTimeout(adang);
		autoPlay();
	}
	var btnFontColor = $(".playGoBtn").css("color");					//获取按钮文字颜色
	function setCurrentBtnBg(fontColor,oNum){							//高亮显示当前信息位置
		$(".playGoBtn").css("color",btnFontColor).eq(oNum-1).css("color",fontColor);
	}
	function autoPlay(){						//自动播放
		if(!autoPlayFlag) return;
		adang=setTimeout(showPrevNews,autoPlayTime*1000);
	}
		
	function init(){							//初始化函数
		autoPlay();
		setCurrentBtnBg(currentBtnColor,1);
		$(".newsInforBox").css("width",inforSpanWidth);
		$(".playPrevBtn").parent().css("margin-left",$(".newsImageContainer").width()+15);
	}
	init();
})