//获取浏览器窗口的宽度（不包含滚动条宽度）
function getWindowWidth(){
	var _width = 0;
	if (window.innerWidth) _width = window.innerWidth;
	if (document.body.clientWidth) _width = document.body.clientWidth;
	if (document.documentElement && document.documentElement.clientWidth)
		_width = document.documentElement.clientWidth;
	return _width;
}

//获取浏览器窗口的高度
function getWindowHeight(){
	var _height = 0;
	if (window.innerHeight) _height = window.innerHeight;
	if (document.body.clientHeight) _height = document.body.clientHeight;
	if (document.documentElement && document.documentElement.clientHeight)
		_height = document.documentElement.clientHeight;
	return _height;
}

//图片播放器
function AdPlayer(id){
	var $AdPlayer = $("#"+id);
	if(!$AdPlayer.is("div")) return;
	
	var $AdBox = $AdPlayer.find(".box");
	var $AdText = $AdPlayer.find(".text");
	var $thumbnail = $AdPlayer.find(".thumbnail");
	
	if($thumbnail.find("img").length == 0){
		$AdBox.html('<div style="text-align:center; color:Red; font-size:20px; padding:130px 10px 10px;">找不到相关图片</div>');
		return;
	}	
	
	if(!$AdPlayer.find(".prev").is("img"))
		$AdPlayer.find(".control").append('<a class="prev" alt="上一张"></a>');
	if(!$AdPlayer.find(".next").is("img"))	
		$AdPlayer.find(".control").append('<a class="next" alt="下一张"></a>');
	var $prev = $AdPlayer.find(".prev");
	var $next = $AdPlayer.find(".next");
	
	var isPlayFinish = true;
	
	var moveSpeed = 300;//缩略图移动速度
	var moveHeight = 55;//每个缩略图的高度
	var _height = $thumbnail.height(); //缩略图的高度总和
	var showAreaHeight = moveHeight * 5;//展示区域的高度
	
	$thumbnail.children("img:first").addClass("now");
	playerShowImg($AdBox,$thumbnail.children(".now"));
	
	//向上点击事件
	$prev.click(function(){
		if(isPlayFinish){
			var $now = $thumbnail.children(".now");
			if(!$now.is("img")){
				$thumbnail.children("img").eq(4).addClass("now");
				//更换图片
				playerShowImg($AdBox,$thumbnail.children(".now"));
			}else if($now.prev("img").is("img")){
				$now.removeClass("now");
				$now.prev("img").addClass("now");
				//更换图片
				playerShowImg($AdBox,$thumbnail.children(".now"));
				//判断是否需要移动
				var n =$thumbnail.children("img:last").index()- $now.index();
				if (n > 1) {
					var top = parseInt($thumbnail.css("top"));
					if (top < 0) {
						$thumbnail.animate({
							top: '+=' + moveHeight
						}, moveSpeed,"",function(){
						});
					}
				}
			}
		}
		return false;
	});
	
	//向下点击事件
	$next.click(function(){
		if(isPlayFinish){
			var $now = $thumbnail.children(".now");
			if(!$now.is("img")){
				$thumbnail.children("li").eq(0).addClass("now");
				//更换图片
				playerShowImg($AdBox,$thumbnail.children(".now"));
			}else if($now.next("img").is("img")){
				$now.removeClass("now");
				$now.next("img").addClass("now");
				//更换图片
				playerShowImg($AdBox,$thumbnail.children(".now"));
				//判断是否需要移动
				var n = $now.index() - $thumbnail.children("img:first").index();
				if (n > 1) {
					var top = parseInt($thumbnail.css("top"));
					if ((_height + top) > showAreaHeight) {
						$thumbnail.animate({
							top:"-="+moveHeight
						}, moveSpeed,"",function(){
						});
					}
				}
			}
		}
		return false;
	});
	
	//每个图片的点击事件
	$thumbnail.children("img").click(function(){
		if(!isPlayFinish) return false;
		var $now = $thumbnail.children(".now");
		var index = $now.index();
		var fristIndex = $thumbnail.children("img:first").index();
		var lastIndex = $thumbnail.children("img:last").index();
		if (index == $(this).index()) return false;
		if (index == -1) index = 0;
		
		$now.removeClass("now");
		$(this).addClass("now");
		//更换图片
		playerShowImg($AdBox,$thumbnail.children(".now"));
		
		if (index == 0) {
			index = 2;
		} else if (index == lastIndex) {
			index = lastIndex-2;
		}
		
		var clickMoveHieght = ($(this).index() - index) * moveHeight;  //点击的缩略图相对当前状态的缩略图所需移动的高度
		
		var subtract;
		var top = -parseInt($thumbnail.css("top"));
		if (clickMoveHieght > 0) {
			subtract = $(this).index() - fristIndex;
			if(subtract>=-2 && subtract<=2) return false;
			//向下滚动
			if ((_height - (showAreaHeight + top)) < clickMoveHieght)
				clickMoveHieght = _height - (showAreaHeight + top);
			$thumbnail.animate({
				top: '-=' + clickMoveHieght
			}, moveSpeed);
		} else {
			//向上滚动
			subtract = $(this).index() - lastIndex;
			if(subtract>=-2 && subtract<=2) return false;
			var temp = -clickMoveHieght;
			if (top < temp) temp = top;
			$thumbnail.animate({
				top: '+=' + temp
			}, moveSpeed);
		}
		return false;
	});
	
	function playerShowImg(AdBoxObj,eachImgObj) {
		isPlayFinish = false;
		var $img = AdBoxObj.find("img");
		$img.animate({
			opacity:0
		},300,"",function(){
			$img.attr("src",eachImgObj.attr("bigSrc"));
			$img.load(function(){
				$AdText.text(eachImgObj.attr("alt"));
				$img.animate({
					opacity:1
				},300,"",function(){
					$("#scroll").jreset();
					isPlayFinish = true;
				});
			});
			
			
		});
		
		/*$AdText.text(eachImgObj.attr("alt"));
		var ImgObj = $("<img />").attr("src",eachImgObj.attr("bigSrc"))
								 .attr("alt",eachImgObj.attr("alt"))
								 .attr("opacity",0.1);
		
		AdBoxObj.html(ImgObj);
		ImgObj.animate({
			opacity:1
		},600,"",function(){isPlayFinish = true;});*/
	}
}

//视频播放器
function VideoPlayer(id){
	var $VideoPlayer = $("#"+id);
	if(!$VideoPlayer.is("div")) return;
	
	var $VideoBox = $VideoPlayer.find(".box");
	var $VideoText = $VideoPlayer.find(".text");
	var $thumbnail = $VideoPlayer.find(".thumbnail");
	
	if($VideoPlayer.find(".thumbnail>img").length == 0){
		$VideoBox.html('<div style="text-align:center; color:Red; font-size:20px; padding:130px 10px 10px;">找不到相关视频</div>');
		return;
	}
	
	if(!$VideoPlayer.find(".prev").is("img"))
		$VideoPlayer.find(".control").append('<a class="prev" alt="上一张"></a>');
	if(!$VideoPlayer.find(".next").is("img"))	
		$VideoPlayer.find(".control").append('<a class="next" alt="下一张"></a>');
    var $prev = $VideoPlayer.find(".prev");
	var $next = $VideoPlayer.find(".next");                    
	var isPlayFinish = true;
	
	var moveSpeed = 300;//缩略图移动速度
	var moveHeight = 55;//每个缩略图的高度
	var _height = $thumbnail.height(); //缩略图的高度总和
	var showAreaHeight = moveHeight * 5;//展示区域的高度
	
	$thumbnail.children("img:first").addClass("now");
	playerShowImg($VideoBox,$thumbnail.children(".now"));
	
	//向上点击事件
	$prev.click(function(){
		if(isPlayFinish){
			var $now = $thumbnail.children(".now");
			if(!$now.is("img")){
				$thumbnail.children("img").eq(4).addClass("now");
				//更换图片
				playerShowImg($VideoBox,$thumbnail.children(".now"));
			}else if($now.prev("img").is("img")){
				$now.removeClass("now");
				$now.prev("img").addClass("now");
				//更换图片
				playerShowImg($VideoBox,$thumbnail.children(".now"));
				//判断是否需要移动
				var n =$thumbnail.children("img:last").index()- $now.index();
				if (n > 1) {
					var top = parseInt($thumbnail.css("top"));
					if (top < 0) {
						$thumbnail.animate({
							top: '+=' + moveHeight
						}, moveSpeed,"",function(){
						});
					}
				}
			}
		}
		return false;
	});
	
	//向下点击事件
	$next.click(function(){
		if(isPlayFinish){
			var $now = $thumbnail.children(".now");
			if(!$now.is("img")){
				$thumbnail.children("li").eq(0).addClass("now");
				//更换图片
				playerShowImg($VideoBox,$thumbnail.children(".now"));
			}else if($now.next("img").is("img")){
				$now.removeClass("now");
				$now.next("img").addClass("now");
				//更换图片
				playerShowImg($VideoBox,$thumbnail.children(".now"));
				//判断是否需要移动
				var n = $now.index() - $thumbnail.children("img:first").index();
				if (n > 1) {
					var top = parseInt($thumbnail.css("top"));
					if ((_height + top) > showAreaHeight) {
						$thumbnail.animate({
							top:"-="+moveHeight
						}, moveSpeed,"",function(){
						});
					}
				}
			}
		}
		return false;
	});
	
	//每个图片的点击事件
	$thumbnail.children("img").click(function(){
		if(!isPlayFinish) return false;
		var $now = $thumbnail.children(".now");
		var index = $now.index();
		var fristIndex = $thumbnail.children("img:first").index();
		var lastIndex = $thumbnail.children("img:last").index();
		if (index == $(this).index()) return false;
		if (index == -1) index = 0;
		
		$now.removeClass("now");
		$(this).addClass("now");
		//更换图片
		playerShowImg($VideoBox,$thumbnail.children(".now"));
		
		if (index == 0) {
			index = 2;
		} else if (index == lastIndex) {
			index = lastIndex-2;
		}
		
		var clickMoveHieght = ($(this).index() - index) * moveHeight;  //点击的缩略图相对当前状态的缩略图所需移动的高度
		
		var subtract;
		var top = -parseInt($thumbnail.css("top"));
		if (clickMoveHieght > 0) {
			subtract = $(this).index() - fristIndex;
			if(subtract>=-2 && subtract<=2) return false;
			//向下滚动
			if ((_height - (showAreaHeight + top)) < clickMoveHieght)
				clickMoveHieght = _height - (showAreaHeight + top);
			$thumbnail.animate({
				top: '-=' + clickMoveHieght
			}, moveSpeed);
		} else {
			//向上滚动
			subtract = $(this).index() - lastIndex;
			if(subtract>=-2 && subtract<=2) return false;
			var temp = -clickMoveHieght;
			if (top < temp) temp = top;
			$thumbnail.animate({
				top: '+=' + temp
			}, moveSpeed);
		}
		return false;
	});
	
	function playerShowImg(VideoBoxObj,eachImgObj) {
		isPlayFinish = false;
		$VideoText.text(eachImgObj.attr("alt"));
		LoadVideo(eachImgObj.attr("preview_src"),eachImgObj.attr("video_src"));
		isPlayFinish = true;
	}
}

//加载视频
function LoadVideo(imgSrc,videoSrc){
	jwplayer("videoBox").setup({
		controlbar:"bottom",//控制条位置bottom,top,over,none
		//fullscreen:true,//全屏
		stretching:"fill",//强制填充全屏uniform(锁定高宽比，以黑色填充空白部分。),fill(锁定高宽比填满屏幕),exactfit(不锁定高宽比填满屏幕),none
		
		//screencolor:"#000000",//屏幕背景
		//backcolor:"#ffffff",//控制条背景
		//frontcolor:"#000000",//按钮和字体颜色
		//lightcolor:"#000000",//进度条和音量颜色
		
		//playlist:"none",//播放列表none,bottom,over,right,left,top
		//playlistfile:"",//XML PlayList file location
		//playlistsize:0,//播放器列表大小
		//dock:true,//是否支持插件
		//skin:"",//The skin location of XML file
		
		//autostart:false,
		//item:0,//默认播放列表第一个
		//repeat:"none",
		//volume:50,//初始音量大小 0-100
		//icons:true,//缓冲图标
		//mute:false,//静音
		//quality:true,//回放品质，默认高品质true
		//shuffle:false,//乱序播放
		
		flashplayer: "/flash/player.swf",//flash播放器路径
		file: videoSrc,//视频路径
		image: imgSrc,//预览图路径
		width:452,
		height:287
	});
}

//修复flash遮挡问题
function OverHidden(){
	var obj = document.getElementById("bottomBox");
	if(obj){
		//alert("OverHidden");
		obj.style.height = 35+"px";
	}
}

function ShowAll(){
	var obj = document.getElementById("bottomBox");
	if(obj){
		//alert("ShowAll");
		obj.style.height = 200+"px";
	}
}
//禁止复制
function DisableCopy(id){
	var content = $("#"+id);
	if(content.is("div")){
		content.hover(function(){
			$(document).bind("contextmenu",function(){return false;});
			$(document).bind("selectstart",function(){return false;});
		},function(){
			$(document).unbind("contextmenu");
			$(document).unbind("selectstart");
		});
		content.bind('keydown','ctrl+a',function(){
			return false;
		});
		content.bind('keydown','ctrl+c',function(){
			return false;
		});
		content.bind('keydown','ctrl+v',function(){
			return false;
		});
	}
}
