/*
	作者：戴铭
	功能：游戏剧本
*/

$(document).ready(function() {
	var $mod_gamscript = $(".mod_gamscript");
	$mod_gamscript.css("background", 'url("img/plugin/mine/loading.gif") center no-repeat');
	$mod_gamscript.css("background-color", "black");
	$mod_gamscript.html('<div class="mod_gamscript_ctrl"></div><div class="mod_gamscript_dg"></div>');
	
	/*----------------控制影片区域样式------------*/
	var $mod_gamscript_ctrl = $(".mod_gamscript_ctrl");
	$mod_gamscript_ctrl.html('<div class="mod_gamscript_ctrl_head"><span class="mp3"></span></div><div class="mod_gamscript_ctrl_center"><div class="mod_gamscript_ctrl_center_playandpause"><img src="img/plugin/mine/pause.gif"/></div></div><div class="mod_gamscript_ctrl_bottom"><div class="mod_gamscript_ctrl_bottom_up"></div><div class="mod_gamscript_ctrl_bottom_slider"></div></div>');
	
	//样式的处理
	$('.mod_gamscript_ctrl_center_playandpause').css({"width":"65px", "margin":"auto"});
	$(".mod_gamscript_ctrl_head").css({"width":"100%", "height":"65px"});
	$(".mod_gamscript_ctrl_center").css({"width":"100%", "height":"65px"});
	$(".mod_gamscript_ctrl_bottom").css({"widht":"100%", "height":"65px"});
	
	
	/*----------------ctrl层事件的处理----------*/
		
	$mod_gamscript_ctrl.fadeTo(0, 0).mouseover(function(){$(this).fadeTo(0, 0.7);}).mouseout(function(){$(this).fadeTo(0, 0);});	
	
	/*----------------过程------------*/
		showgamescript({filename:$mod_gamscript.attr('name')})
	/*----------------对话主函数----------------*/
	function showgamescript(allo) {
		if(allo['filename'] == 'frompost') {
			var getjsonurl = "index.php?action=plugin&v=wave&ajax=juststr&str=get_gs_json&threadid=" + $mod_gamscript.attr('id');
		} else if(allo['filename'] == 'tvplay' || allo['filename'] == 'tvplaybytid'){
			if(allo['filename'] == 'tvplaybytid') {
				var getjsonurl = "index.php?action=plugin&v=wave&tpl=union&ac=module&m=tv&channel=" + $mod_gamscript.attr('id') + "&tidtv=" + $mod_gamscript.attr('tidtv') + "&play=yes";
				allo['filename'] = 'tvplay';
			} else {
				var getjsonurl = "index.php?action=plugin&v=wave&tpl=union&ac=module&m=tv&channel=" + $mod_gamscript.attr('id') + "&play=yes";
			}
			$mod_gamscript_ctrl.html('');
			$mod_gamscript_ctrl.fadeTo(0, 0).mouseover(function(){$(this).fadeTo(0, 0);}).mouseout(function(){$(this).fadeTo(0, 0);});	
		} else {
			var getjsonurl = "index.php?action=plugin&v=mine&ajax=mod_gamscript&ajaxgamfilename=" + allo['filename'];
		}
		$.getJSON(getjsonurl, function(json) {
			//显示原文内容
			//$("#gamscript_post .main").html(json['post']);
			//播放暂停
			var playandpauseswitch = 0;
			$(".mod_gamscript_ctrl_center_playandpause").click(function() {
				if(playandpauseswitch == 0) {
					stoptimelistening();
				}else {
					$(".mod_gamscript_ctrl_center_playandpause").html('<img src="img/plugin/mine/pause.gif"/>');
					timelistening();
					playandpauseswitch = 0;
				}
			});
			var num = 0; //作为数组里key值
			var timelister;
			
			timelistening();
			
			//监听
			function timelistening() {
				var sec = 1500;
				if(allo['filename'] == 'tvplay') {
					sec = 1500;
				}
				timelister = setInterval(function(){show_sayhtml();}, sec);
			}
			function stoptimelistening() {
				clearInterval(timelister);
				timelister = 0;
				playandpauseswitch = 1;
				$(".mod_gamscript_ctrl_center_playandpause").html('<img src="img/plugin/mine/play.gif"/>');
			}
			
			//拖动条的处理
			$(".mod_gamscript_ctrl_bottom_slider").slider({
				animate:true,
				orientation: 'horizontal',
				value:num,
				max:json['script'].length,
				slide:function(event, ui) {
					num = ui.value;
					show_sayhtml();
				}
			});
			$mod_gamscript_ctrl_bottom_slider_a = $(".mod_gamscript_ctrl_bottom_slider a");
			$mod_gamscript_ctrl_bottom_slider_a.attr("style", $mod_gamscript_ctrl_bottom_slider_a.attr("style") + "border:0px;");
			$(".mod_gamscript_ctrl_bottom_slider").attr("class", "mod_gamscript_ctrl_bottom_slider ui-slider ui-slider-horizontal ui-widget mod_gamscript_slider_con ui-corner-all");
			$mod_gamscript_ctrl_bottom_slider_a.attr("class", "ui-slider-handle ui-corner-all mod_gamscript_slider_focus");
			
			//定时显示脚本事件
			function show_sayhtml(o) {
				//预判
				if(o == undefined) {
					o = 0;
				}
				if(num < 0) {
					num = 0;
				}			
				if(num == json['script'].length) {
					num = 0;
					$mod_gamscript_ctrl.fadeTo("slow", 0.7);
					location.reload();
					stoptimelistening();
				}
				
				if(o['playstep'] == 'pre') {
					if(json['script'][num - 1] == 'delay') {
						num -= 1;
					}
					num -= 1;
				}
				
				if(json['script'][num] == 'delay') {
					num += 1;
					return;
				}
				
				var starnameo = json['script'][num].split("：");
				
				var starphotopath;
				var starname;
				var starsaywords;
				if(starnameo[1] == undefined) {
					starphotopath = '';
					starname = '';
					starsaywords = json['script'][num];
				} else {
					starname = starnameo[0];
					starsaywords = starnameo[1];
					if(json['star'][starnameo[0]] == 0) {
						starphotopath = '<img src="img/elm/default_avatar.gif"/>';
					} else {
						starphotopath = '<a href="index.php?action=search_star&star_id=' + json['star'][starnameo[0]] + '" target="_blank"><img src="xml/star/' + getpathbyid(json['star'][starnameo[0]]) + 'img/' + json['star'][starnameo[0]] + '.gif"></a>';
					}
				}
				
				//切换背景
				var bgimg = starsaywords.split("#bg:");
				if(bgimg[1]) {
					if('url("' + $mod_gamscript.css("background-image").substr(-(bgimg[1].length) - 2) == 'url("' + bgimg[1] + '")') {
					} else if(allo['filename'] == 'frompost'){
						$mod_gamscript.css({"background":'url("' + bgimg[1] + '")', "background-color":"black", "background-position":"center top", "background-repeat":"no-repeat"});
					} else if(allo['filename'] == 'tvplay') {
						$mod_gamscript.css({"background":'url("' + bgimg[1] + '")', "background-color":"black", "background-position":"center", "background-repeat":"no-repeat"});
					} else {
						$mod_gamscript.css({"background":"url(img/plugin/mine/mod/gamscript/" + bgimg[1] + ")", "background-color":"black", "background-position":"center top", "background-repeat":"no-repeat"});
					}
					starsaywords = bgimg[0];
				}
				
				//切换精灵
				var spimg = starsaywords.split("#sp:");
				if(spimg[1]) {
					$mod_gamscript_ctrl.html('<img src="' + spimg[1] + '"/>');
					$mod_gamscript_ctrl.css("background","transparent none repeat scroll 0% 0%");
					$mod_gamscript_ctrl.fadeTo(0, 1).mouseover(function(){$(this).fadeTo(0, 1);}).mouseout(function(){$(this).fadeTo(0, 1);});
					starsaywords = spimg[0];
				}
				
				//切换音乐
				var bgsound = starsaywords.split("#mu:");
				if(bgsound[1]) {
					$(".mp3").text(bgsound[1]);
					$(".mp3").jmp3();
					starsaywords = bgsound[0];
				}
				//对话框
				$(".mod_gamscript_dg").html('<div class="mod_gamscript_avatar"><div>' + starphotopath + '</div><div><span>' + starname + '</span></div></div><div class="mod_gamscript_saywords">' + starsaywords + '</div>');
				$(".mod_gamscript_saywords").fadeTo(0, 0).fadeTo("slow", 1);
				if((json['script'][num + 1] == 'delay') && (o['playstep'] == 'yes')) {
					//alert('here');
					num += 1;
				}
				num += 1;
				if(o['playstep'] == 'pre') {
					num -= 1;
				}
			}
			
			/*----手动逐帧点击播放-----*/

			function mod_gamscript_playstep() {
				if(timelister == 0) {
					var sayhtmlo = {playstep:'yes'}
					show_sayhtml(sayhtmlo);
				} else {
					stoptimelistening();
				}
			}
			
			if(allo['filename'] == 'tvplay' || allo['filename'] == 'tvplaybytid') {
				
				$(window).keydown(function(event) {
					//键盘p暂停和播放
					if(event.keyCode == '80') {
						if(playandpauseswitch == 0) {
							stoptimelistening();
							$(".mod_gamscript_dg").html('<span style="font-size:12px;font-weight:bold;float:left">已暂停！按p键开始播放</span>' + $(".mod_gamscript_dg").html());
						} else {
							$(".mod_gamscript_dg").html('<span style="color:#6699CC;font-weight:bold;font-size:12px;float:left;">准备播放...</span>' + $(".mod_gamscript_dg").html());
							timelistening();
							playandpauseswitch = 0;
						}
					}
					//r键刷新
					if(event.keyCode == '82') {
						location.reload();
					}
				});
				
			} else {
				//鼠标点击播放
				$(".mod_gamscript_dg").click(function(){
					mod_gamscript_playstep();
				});
				
				/*------键盘的一些控制------*/
				$(window).keydown(function(event){
					//alert(event.keyCode);
					//下一帧播放
					if(event.keyCode == '78' || event.keyCode == '32' || event.keyCode == '39') {
						mod_gamscript_playstep();
						return false;
					}
					//上一帧播放，使用右箭头和p键均可
					if(event.keyCode == '37' || event.keyCode == '80') {
						if(timelister == 0) {
							show_sayhtml({playstep:'pre'});
						} else {
							stoptimelistening();
						}
					}
				});
			}
		});
	}
});
