$(function(){
	//全局变量定义
    var turnI=liI=0,adI=4;
	var playTimer,adshowTimer,imgTimer;//计时器
	var winShow=true;
    var smallpiclist=document.getElementById("smallpiclist");
	var listlength=smallpiclist.getElementsByTagName("li").length;//图片列表的个数
	var listUnit=Math.ceil(listlength/5)<!--每个单元有7张小图标，listUnit记录多少个单元-->
	$("#smallpiclist li bdo").text(listlength);//小图标右下角1/lilength lilength=li的总数
	smallpiclist.style.width=listlength*128+6+"px";//定义smallpiclist宽度,6像素是li border宽度
	var Lfocus=document.getElementById("Lfocus"),//左侧热区
	Rfocus=document.getElementById("Rfocus"),//右侧热区
	Lturn=document.getElementById("Lturn"),//左侧图片切换按钮
	Rturn=document.getElementById("Rturn"),//右侧图片切换按钮
	maximg=document.getElementById("maximg")
	Lturn.style.display="block";
	var turnH=Lturn.offsetHeight/2;
	Lturn.style.display="none";//显示Lturn获取高度给turnH后再隐藏
	//初始化大图地址
	$("#maximg").attr("src",$("#smallpiclist li img").attr("href"));
	$("#picture").attr("href",$("#smallpiclist li img").attr("href"));
	function buttonH(){
		var maximgW=maximg.offsetWidth,//当前大图的宽度
	    maximgH=maximg.offsetHeight;//当前大图的高度
		Lfocus.style.height=Rfocus.style.height=maximgH+"px";
		Lturn.style.top=Rturn.style.top=maximgH/2-turnH+"px";
		var extI=liI+1;
		$(".explain ul li").hide();
        $(".explain"+extI+"").show();
	}
	buttonH();
	$("#Lfocus").mouseover(function(){
		Lturn.style.display="block";
	})
	$("#Lfocus").mouseout(function(){
		Lturn.style.display="none";
	})
	$("#Rfocus").mouseover(function(){
		Rturn.style.display="block";
	})
	$("#Rfocus").mouseout(function(){
		Rturn.style.display="none";
	})
	
	  
	//页面加载完成后图片垂直居中
	function middlepic(){
		$("#smallpiclist li img,#upPage img,#downPage img").each(function(e){
			var picH=parseInt(this.offsetHeight);
			if(picH<86){
				$(this).css("marginTop",(86-picH)/2)
			}
		})
		var picW=parseInt($(".replay img").css("width"));
		var picH=parseInt($(".replay img").css("height"));
		if(picW<85){
			$(".replay img").css("marginLeft",(85-picW)/2);
		}
		if(picH<55){
			$(".replay img").css("marginTop",(55-picH)/2)
		}
		$(".winbotlist img").each(function(e){
			var picH=parseInt($(this).css("height"));
			if(picH<100){
				$(this).css({marginTop:(110-picH)/2,marginBottom:(110-picH)/2})
			}
		})
	}
	window.onload=middlepic
	//替换图片文字说明中的路劲
	$(".explain ul li").each(function(e){
		var str=$(this).text();
		str=str.replace("/template/bw_lqgw/images/",""); 
		$(this).text(str);
	})
	$(".explain ul li").hide();
	$(".explain1").show();
	
	//鼠标经过和离开字体颜色
	$("#fullscreen,#playimg").mouseover(function(){
		$(this).css("color","#ffffff");
	})
	$("#fullscreen,#playimg").mouseout(function(){
		$(this).css("color","#9A9A9A");
	})
	//鼠标经过改变按钮
	$("#left_button").mouseover(function(){
		this.style.backgroundPosition="-21px 0";
	})
	$("#left_button").mouseout(function(){
		this.style.backgroundPosition="0 0";
	})
	$("#right_button").mouseover(function(){
		this.style.backgroundPosition="-63px 0";
	})
	$("#right_button").mouseout(function(){
		this.style.backgroundPosition="-42px 0";
	})
	//鼠标移过效果
	$("#smallpiclist li").mouseover(function(){
		$("#smallpiclist li span,#smallpiclist li img").attr("class","")
		$(this).find("span").attr("class","spancurrent");
		$(this).find("img").attr("class","currentimg");
		$("#smallpiclist li span").eq(liI).attr("class","spancurrent");
	})
	$("#smallpiclist li").mouseout(function(){
		$(this).find("img").attr("class","");
	})
	//点击图片
	$("#smallpiclist li").click(function(){
		$("#smallpiclist li").attr("class","");
		$("#smallpiclist li span").attr("class","");
		$(this).attr("class","current");
		$(this).find("span").attr("class","spancurrent");
		liI=$(this).attr("rel")-1;
		$("#maximg").attr({src:imagesPath+"img2/loading.gif"}).css({marginTop:220+"px",marginBottom:220+"px"});
		var imgsrc=$(this).find("img").attr("href");
		$("#maximg").attr({src:imgsrc});
		$("#picture").attr("href",imgsrc);
		if($("#winimg").css("display")=="block"){
	        winImghide();
		}
	})
	var boxWidth=parseInt($(".piclistbox").css("width"));
	$(".scroll_line").css("width",boxWidth+"px");
	//左按钮
	$("#right_button").click(function(){
		if(turnI<(listUnit-1)){turnI++};
		$("#smallpiclist").animate({marginLeft:"-"+turnI*boxWidth+"px"},300);
	})
	//右按钮
	$("#left_button").click(function(){
		//if(turnI>(listlength-7)){turnI=listlength-turnI};
		if(turnI>0){turnI--};
		$("#smallpiclist").animate({marginLeft:"-"+turnI*boxWidth+"px"},300);
	})
	//上一张
	function upimg(){
		if(liI>0){
			liI--;
			$("#maximg").attr({src:imagesPath+"img2/loading.gif"}).css({marginTop:220+"px",marginBottom:220+"px"});
			var imgsrc=$("#smallpiclist li img").eq(liI).attr("href");
			$("#maximg").attr({src:imgsrc});
			$("#smallpiclist li").attr("class","");
			$("#smallpiclist li span").attr("class","");
			$("#smallpiclist li").eq(liI).attr("class","current");
			$("#smallpiclist li").eq(liI).find("span").attr("class","spancurrent");
			$("#picture").attr("href",imgsrc);
		}
		if($("#winimg").css("display")=="block"){
	        winImghide();
		}
	}
	//下一张
	function downimg(){
		liI++;
		if(liI<listlength){
			$("#maximg").attr({src:imagesPath+"img2/loading.gif"}).css({marginTop:220+"px",marginBottom:220+"px"});
			var imgsrc=$("#smallpiclist li img").eq(liI).attr("href");
			$("#maximg").attr({src:imgsrc});
			$("#smallpiclist li").attr("class","");
			$("#smallpiclist li span").attr("class","");
			$("#smallpiclist li").eq(liI).attr("class","current");
			$("#smallpiclist li").eq(liI).find("span").attr("class","spancurrent");
			$("#picture").attr("href",imgsrc);
		}else{
			liI=listlength-1;
			if($("#winimg").css("display")=="none"){
				winImgshow();
			}
		}
	}
	
	//左区域上一张
	$("#Lfocus").click(function(){
		upimg();
	})
	//右区域下一张
	$("#Rfocus").click(function(){
		downimg();
	})
	//获取键盘按键的响应事件
	 document.onkeydown=nextpage;
	 function nextpage(event) 
	{
		event = event ? event : (window.event ? window.event : null); 
	   if (event.keyCode==39) //right功能；
	   {
		   downimg();
		   return false;
		}
	   if (event.keyCode==37) //left功能；
	  {
		   upimg();
		   return false;
	   }
	   if (event.keyCode==13)//Enter功能；
	   {
	       var imgsrc=$("#smallpiclist li img").eq(liI).attr("href");
		   window.open (imgsrc, 'newwindow', 'fullscreen') //全屏
		   return false;
	   }
	 }

	//幻灯片播放
	$("#playimg").toggle(function(){
		$("#playimg bdo").text("停止播放");
		$("#playimg span").css("backgroundPosition","-34px 0");
		playTimer=setInterval(downimg,5000);
	},function(){
		$("#playimg bdo").text("幻灯播放");
		$("#playimg span").css("backgroundPosition","0 0");
		clearInterval(playTimer);
	})
	
	
	//模拟滚动条
	var moveable=false;//初始化是否可拖动
	var X0,eX0;//声明变量
	var moveObj=document.getElementById('scroll');//获取到拖动对象
	var scrollW=document.getElementById("scroll_line").offsetWidth-moveObj.offsetWidth;//可滚动的宽度
	//列表可滚动的宽度数值；注：smallpiclist取自上方
	var scrollPar=0;
	if(listlength>6){scrollPar=(listlength-6)*7};
	var listW=smallpiclist.offsetWidth-(scrollW+scrollPar+192);
	//鼠标按下
	moveObj.onmousedown=function(e){//e只是一个参数，表示一个事件，名称可随便起
		e=e||event;//鼠标事件在火狐里存在内存中可供函数直接调用；在IE里存放在window对象中,即window.event
		eX0= e.clientX;//记录下拖动对象的坐标X0,Y0
		X0=parseInt(moveObj.style.left+0);//记录下mousedown事件发生时，鼠标的坐标eX0,eY0
		moveable=true;//拖动状态改为可拖动
		moveObj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=70,finishOpacity=70)";
		moveObj.style.opacity = "0.7";
	}
	//鼠标移动
	document.onmousemove=function(e){
		if (!moveable){
			return false;
		}else{
			e=e||event;
			var eX1= e.clientX;
			var eY1= e.clientY;            
			moveObj.style.left=X0+eX1-eX0+"px";//由鼠标刚按下时记录的各个初始值，计算当前拖动对象的坐标
			moveObj.style.pixelLeft =X0+eX1-eX0;
			if(moveObj.style.pixelLeft>scrollW){
				moveObj.style.left=scrollW+"px";
				moveObj.style.pixelLeft=scrollW;
			}else if(moveObj.style.pixelLeft<0){
				moveObj.style.left=0;
				moveObj.style.pixelLeft=0;
			}
			//模拟滚动条鼠标拖动时列表的marginLeft设置
			smallpiclist.style.marginLeft="-"+listW*moveObj.style.pixelLeft/scrollW+"px";
			//禁止元素被选取
			var element=document.body;
			if (typeof(element.onselectstart) != "undefined") {        
				// IE下禁止元素被选取        
				element.onselectstart = new Function("return false");        
			} else {
				// firefox下禁止元素被选取的变通办法        
				element.onmousedown = new Function("return false");        
				element.onmouseup = new Function("return true");        
			} 
		}
	}
	//鼠标弹起
	document.onmouseup=function(e){        
		if(moveable) moveable=false;//状态改为不可拖动
		moveObj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=100,finishOpacity=100)";
		moveObj.style.opacity = "1";
		//允许元素被选取
		var element=document.body;
		if (typeof(element.onselectstart) != "undefined") {        
			// IE下禁止元素被选取        
			element.onselectstart = new Function("return true");        
		} else {
			// firefox下禁止元素被选取的变通办法        
			element.onmousedown = new Function("return true");        
			element.onmouseup = new Function("return false");        
		} 
	}
	
	//关闭广告窗口
	$(".adwinclose").click(function(){
		$("#adwinimg").fadeOut(500);
		clearInterval(adshowTimer);
	})
	//广告窗口样式设置
	function adIfun(){
		if(adI>0){
			adI--;
			$(".adwintext span").text(adI);
		}else{
			clearInterval(adshowTimer);
			$("#adwinimg").fadeOut(500);
		}
	}
	function adwinShow(){
		$(".adwinbg").fadeTo(0,0.2);
		setTimeout('$(".adwinbg").hide(500)',1000);
		setTimeout('$(".adwintextbg").fadeTo(0,0.2)',1500);
		adI=4;
		$(".adwintext span").text(adI);
		adshowTimer=setInterval(adIfun,1000)
	}
	$(".replay").mouseover(function(){
		$(this).find("img").attr("class","currentimg");
	})
	$(".replay").mouseout(function(){
		$(this).find("img").attr("class","");
	})
	
	//上下集弹窗显示
	function winImgshow(){
		var winimg=document.getElementById("winimg");
		var adwinimg=document.getElementById("adwinimg");
		winimg.style.display="block";
		$("#adwinimg").fadeIn(500);
		var winLeft=(document.body.offsetWidth-winimg.offsetWidth)/2;
		var winTop=($(window).height()-winimg.offsetHeight)/2+$(document).scrollTop();
		winimg.style.left=adwinimg.style.left=winLeft+"px";
		adwinimg.style.top=winTop+"px";
		adwinShow();//广告窗口
		$("#winimg").animate({top:winTop+"px"},500);
	}
	//上下集弹窗隐藏
	function winImghide(){
		var winimg=document.getElementById("winimg");
		$("#winimg").animate({top:0+"px"},500).fadeOut(500);
		$("#adwinimg").fadeOut(500);
		clearInterval(adshowTimer);
		$(".adwintextbg").hide();
	}
	//关闭广告窗口
	document.getElementById("winclose").onclick=function(){
		winImghide();
	}
	//重新播放
	$(".replay img,#reshow").click(function(){
		winImghide();
		$("#adwinimg").fadeOut(500);
		clearInterval(adshowTimer);
		$("#smallpiclist li").attr("class","");
		$("#smallpiclist li span").attr("class","");
		$("#smallpiclist li:first").attr("class","current");
		$("#smallpiclist li:first").find("span").attr("class","spancurrent");
		liI=0;
		var imgsrc=$("#smallpiclist li:first").find("img").attr("href");
		$("#maximg").attr({src:imgsrc});
		$("#picture").attr("href",imgsrc);
		smallpiclist.style.marginLeft=0+"px";
	})
	
	//判断图片是否加载完成
	document.getElementById("maximg").onreadystatechange = stateChange;
	function stateChange() {
	    if(document.getElementById("maximg").readyState == 'complete'){
			$("#maximg").css({marginTop:0+"px",marginBottom:0+"px"});
			buttonH();
	    }
	}
	//火狐
	document.getElementById("maximg").onload=function(){
		if (document.getElementById("maximg").complete == true){
			$("#maximg").css({marginTop:0+"px",marginBottom:0+"px"});
			buttonH();
		}
	}
})