﻿//detect plugin
var detectableWithVB = false;
var pluginFound = false;
function detectFlash(redirectURL, redirectIfFound) {
    pluginFound = detectPlugin('Shockwave','Flash'); 
    if(!pluginFound && detectableWithVB) {
	pluginFound = detectActiveXControl('ShockwaveFlash.ShockwaveFlash.1');
    }
    return pluginFound;
}
if ((navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Win') != -1)) {
    document.writeln('<script language="VBscript">');
    document.writeln('detectableWithVB = False');
    document.writeln('If ScriptEngineMajorVersion >= 2 then');
    document.writeln('  detectableWithVB = True');
    document.writeln('End If');
    document.writeln('Function detectActiveXControl(activeXControlName)');
    document.writeln('  on error resume next');
    document.writeln('  detectActiveXControl = False');
    document.writeln('  If detectableWithVB Then');
    document.writeln('     detectActiveXControl = IsObject(CreateObject(activeXControlName))');
    document.writeln('  End If');
    document.writeln('End Function');
	document.writeln('</scr' + 'ipt>');
}
function detectPlugin() {
    var daPlugins = detectPlugin.arguments;
    var pluginFound = false;
    if (navigator.plugins && navigator.plugins.length > 0) {
		var pluginsArrayLength = navigator.plugins.length;
		for (var pluginsArrayCounter=0; pluginsArrayCounter < pluginsArrayLength; pluginsArrayCounter++ ) {
		    var numFound = 0;
		    for(var namesCounter=0; namesCounter < daPlugins.length; namesCounter++) {
				if( (navigator.plugins[pluginsArrayCounter].name.indexOf(daPlugins[namesCounter]) >= 0) || 
				    (navigator.plugins[pluginsArrayCounter].description.indexOf(daPlugins[namesCounter]) >= 0) ) {
				    numFound++;
				}   
		    }
		    if(numFound == daPlugins.length) {pluginFound = true;break;}
		}
    }
    return pluginFound;
}
function RePlay(){location.href = document.URL;}
//createflash
var QP = new Object();
QP.drawIframe = function (p_Src, p_id, p_width, p_height){
	return "<iframe src=\""+p_Src+"\" frameborder=\"0\" name=\""+p_id+"\" id=\""+p_id+"\" height=\""+p_height+"\" width=\""+p_width+"\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" border=\"0\"></iframe>";
};
QP.drawSWF = function (p_Src, p_Var, p_id, p_width, p_height, p_script, p_net){
	var str='<object id="'+p_id+'" width="'+p_width+'" height="'+p_height+'" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0"><param name="movie" value="'+p_Src+'"/><param name="flashvars" value="'+p_Var+'"/><param name="quality" value="high"/><param name="bgcolor" value="#000"/>';
	if(p_script==1){str+='<param name="allowScriptAccess" value="always"/>';}
	if(p_net==0){str+='<param name="allowNetworking" value="internal"/>';}
	str+='<embed id="'+p_id+'_em" name="'+p_id+'" width="'+p_width+'" height="'+p_height+'" src="'+p_Src+'" flashvars="'+p_Var+'"';
	if(p_script==1){str+=' allowScriptAccess="always"';}
	if(p_net==0){str+=' allowNetworking="internal"';}
	str+=' quality="high" bgcolor="#000" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" /></object>';
	return str;
};
QP.drawDCR = function(p_Src, p_id, p_width, p_height){
	var str = "";
	if(detectFlash()){
		str = '<object id="'+p_id+'" width="'+p_width+'" height="'+p_height+'" classid="clsid:166B1BCA-3F9C-11CF-8075-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=10,0,0,0"><param name="background" value="#000"><param name="swStretchStyle" value="fill"><param name="logo" value="false"><param name="menu" value="false"><param name="src" value="'+p_Src+'"><embed id="'+p_id+'_em" name="'+p_id+'" src="'+p_Src+'" background="#000" logo="false" menu="false" width="'+p_width+'" height="'+p_height+'" pluginspage="http://www.macromedia.com/shockwave/download/"></embed></object>';
	}
	else{
		str = '<div><iframe src="http://www.7k7k.com/scripts/shockwaveplayer.html" height="120" width="450" marginheight="0" marginwidth="0" scrolling="no" frameborder="0"></iframe></div>';
	}
	return str;
}
QP.drawXAP = function(p_Src, p_id, p_width, p_height){
	var str='<object id="'+p_id+'" width="'+p_width+'" height="'+p_height+'" data="data:application/x-silverlight," type="application/x-silverlight-2-b2"><param name="source" value="'+p_Src+'"/><param name="onerror" value="onSilverlightError" /><param name="background" value="black" /><a href="http://go.microsoft.com/fwlink/?LinkID=115261" style="text-decoration: none;"><img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/></a></object>';
	return str;
}
//变量调节
var _v,_w,_h,_p,_e;//变量,宽,高,路径,扩展名
var _playmode=_gamemark.split('|')[0];
var _playscript=_gamemark.split('|')[1];
var _playnet=_gamemark.split('|')[2];
var _fw = 640;
var _fh = 480;
if(_gamewidth==0){_gamewidth=550;}
if(_gameheight==0){_gameheight=400;}
_w = _gamewidth;
_h = _gameheight;
_v = _gamevar;
_p = _gamepath;
//server
function readcookie(name){var cookieValue = "";var search = name + "=";if(document.cookie.length > 0){offset = document.cookie.indexOf(search);if (offset != -1){offset += search.length;end = document.cookie.indexOf(";", offset);if (end == -1) end = document.cookie.length;cookieValue = unescape(document.cookie.substring(offset, end));}}return cookieValue;};
function setcookie(name,value,expirehours){var expiration = new Date((new Date()).getTime() +expirehours*3600000);document.cookie = name+ "=" + value + ";expires=" + expiration.toGMTString()+";domain=www.7k7k.com;path=/;";};
if(_gamemark.split('|')[5]==0 && _p.indexOf('http://flash.7k7k.com/')>=0){
	var _serv = readcookie('server');
	if(_serv==""){
		_serv = Math.floor(Math.random()*5)+1;
		setcookie('server',_serv,24*7);
	}
	_p = _p.replace('flash.7k7k.com','flash'+_serv+'.7k7k.com');
}
//
_e = _p.replace(/^(.+?)(\.([^\.\?]+?))(\?.+?)?$/g, "$2");
if(_e == '.swf'){
	_playmode = 1;
}else if(_e == '.dcr'){
	_playmode = 2;
}else if(_e == '.xap'){
	_playmode = 3;
}else if(_e == '.htm' || _e == '.html'){
	_playmode = 4;
}else{
	_playmode = 0;
}
//函数
QP.drawA = function(){
	return '<table height="100%" width="100%" cellspacing="0" cellpadding="0"><tbody><tr><td valign="middle"><iframe src="/scripts/v1/loading.htm" height="400" width="550" marginheight="0" marginwidth="0" scrolling="no" frameborder="0"></iframe><div style="height:17px;color:#0f0;font-size:14px;margin-top:3px;">游戏加载中，请稍后......</div></td></tr></tbody></table>';
}
QP.Player = function(w,h,$){
	var str = new Array();
	switch(_playmode){
		case 1 :
			str.push(QP.drawSWF(_p, _v, "gameobj", "100%", "100%", _playscript, _playnet));
			break;
		case 2 :
			str.push(QP.drawDCR(_p, "gameobj",  "100%", "100%"));break;
		case 3 :
			str.push(QP.drawXAP(_p, "gameobj",  "100%", "100%"));break;
		case 4 :
			str.push(QP.drawIframe(_p + '?w='+w+'&h='+h+'&v='+ _v, "gameobj", w, h));break;
		default :
			str.push('<div style="height:50px;background:color:#000;"><a href="'+ _p +'" target="_blank" style="#fff">下载该文件</a></div>');break;
	}
	if($) document.getElementById("loading").innerHTML = QP.drawA();
	document.getElementById("play").innerHTML =  str.join("");
};
QP.Replay = function(){
	document.getElementById("play").innerHTML = document.getElementById("play").innerHTML;
};
var state = {
	arr:[],
	set:function(o,w,h,d,c,p){this.arr[o.id]={"w":w,"h":h,"d":d,"c":c,"p":p};return null;},
	get:function(o){return this.arr[o.id];},
	view:function(o,w,h,d,c,p){
		if(!o) return null;
		w = w || this.arr[o.id].w, h = h || this.arr[o.id].h, d = d || this.arr[o.id].d, c = c || this.arr[o.id].c, p = p || this.arr[o.id].p;
		o.style.width = w + "px";
		o.style.height = h=="auto" ? "auto" : h + "px";
		o.style.display = d;
		o.style.backgroundColor = c;
		o.style.padding = p;
	}
},
tools = {
	flash:null,
	ad:null,
	getscreensize:function(){
		var w=0,h=0;
		if(self.innerHeight){w=self.innerWidth;h=self.innerHeight}
		else if(document.documentElement&&document.documentElement.clientHeight){
			w=document.documentElement.clientWidth;h=document.documentElement.clientHeight;
		}
		else if(document.body){w=document.body.clientWidth;h=document.body.clientHeight;}
		return {"w":w,"h":h};
	},
	getobjinfo:function(o){
		if(!o)return {"w":0,"h":0,"d":"","c":"","p":""};
		return {"w":o.clientWidth,"h":o.clientHeight,"d":o.style.display,"c":o.style.backgroundColor,"p":o.style.padding}
	},
	getzoomedsize:function(o,add){
		if(!o) return {"w":0,"h":0};
		var obj = this.getobjinfo(o);
		if(obj.w > obj.h){
			var w = obj.w + add;
			return {"w":w,"h":Math.round(w*(this.flash.h/this.flash.w))};
		}
		else{
			var h = obj.h + add;
			return {"w":Math.round(h*(this.flash.w/this.flash.h)),"h":h};
		}
	},
	getobjstyle:function(o,isfullscreen){
		var cw,ch,cp,lw,lh,lp,rw,rh,rp,rd,pw,ph,pp,fw,fh,fp;
		var refer1 = (this.flash.w + 307) > 954 ? this.flash.w + 307 : 954;
		var refer2 = this.flash.w > 647 ? this.flash.w : 647;
		var padding = 496 - o.h > 0 ? Math.round((496 - o.h)/2) : 0;
		var paddingh = padding*2;
		if(o.w >= refer1){
			cw = o.w,ch = "auto",cp = "3px";
			lw = o.w,lh = o.h,lp = padding + "px 0px";
			rw = 304,rh = o.h+paddingh,rp = "0px",rd = "none";
			pw = o.w,ph = o.h,pp = "0px";
			fw = o.w,fh = o.h,fp = "0px";
		}
		else if(o.w > refer2){
			cw = refer1,ch = "auto",cp = "3px";
			lw = refer1,lh = o.h,lp = padding + "px 0px";
			rw = 304,rh = o.h+paddingh,rp = "0px",rd = "none";
			pw = o.w,ph = o.h,pp = "0px";
			fw = o.w,fh = o.h,fp = "0px";
		}
		else if(o.w <= refer2){
			cw = refer1,ch = "auto",cp = "3px";
			lw = refer2,lh = o.h,lp = padding + "px 0px";
			rw = 304,rh = o.h+paddingh,rp = "0px",rd = "block";
			pw = o.w,ph = o.h,pp = "0px";
			fw = o.w,fh = o.h,fp = "0px";
		}
		return {"cw":cw,"ch":ch,"cp":cp,"lw":lw,"lh":lh,"lp":lp,"rw":rw,"rh":rh,"rd":rd,"rp":rp,"pw":pw,"ph":ph,"pp":pp,"fw":fw,"fh":fh,"fp":fp};
	},
	getbestsize:function(isfullscreen){
		var cw = this.flash.w > 647 ? this.flash.w + 307 : 954;
		var ch = "auto";
		var cp = "3px";
		var rw = 304;	//rw have 3px margin-left
		var rh = this.flash.h > 496 ? this.flash.h : 496;
		var rp = "0px";
		var rd = "block";
		var lw = this.flash.w > 647 ? this.flash.w : 647;
		var lh = this.flash.h > this.ad.h ? this.flash.h : this.ad.h;
		var lp = 496 - lh > 0 ? Math.round((496-lh)/2) + "px 0px" : "0px";
		var pw = this.flash.w > this.ad.w ? this.flash.w : this.ad.w;
		var ph = this.flash.h > this.ad.h ? this.flash.h : this.ad.h;
		var pp = "0px";
		var fw = this.flash.w;
		var fh = this.flash.h;
		var fp = ph - fh > 0 ? Math.round((ph-fh)/2) + "px 0px" : "0px";
		return {"cw":cw,"ch":ch,"cp":cp,"lw":lw,"lh":lh,"lp":lp,"rw":rw,"rh":rh,"rd":rd,"rp":rp,"pw":pw,"ph":ph,"pp":pp,"fw":fw,"fh":fh,"fp":fp};
	}
},
player={
	container:null,	//最外层
	playbarbox:null,	//菜单、进度条
	leftbox:null,	//左侧DIV
	rightbox:null,	//右侧DIV
	playbox:null,
	flashbox:null,
	isfullscreen:false,	//是否全屏状态
	isloading:true,	//是否载入中
	loadingtimeout:null,	//进度控制
	loadingtime:0,	//进度时间控制
	screenobj:null,	//屏幕对象
	playmode:0,
	zoom:function(flag){
		flag = flag > 0 ? 1 : -1;
		var add = 150*flag;
		this.adjusted = tools.getzoomedsize(this.flashbox,add);	//最终调整后的宽和高
		if(this.isloading && (flag<0 && this.adjusted.w <= tools.ad.w)){current = null;return;}
		if(flag<0 && this.adjusted.w*this.adjusted.h < 60000){current=null;return;}
		this.setsize(flag);
	},
	adjusttimeout:null,
	setsize:function(flag,isinit){
		var add = 150,delay = 35,flag = flag || 0,style;
		if(flag){
			var temp = tools.getzoomedsize(this.flashbox,add*flag); //过渡的宽和高
			if(flag>0&&temp.w>this.adjusted.w)temp.w = this.adjusted.w;
			if(flag<0&&temp.w<this.adjusted.w)temp.w = this.adjusted.w;
			if(flag>0&&temp.h>this.adjusted.h)temp.h = this.adjusted.h;
			if(flag<0&&temp.h<this.adjusted.h)temp.h = this.adjusted.h;
			style = tools.getobjstyle(temp,this.isfullscreen);
		}
		else{
			style = tools.getbestsize(this.isfullscreen);	//最佳尺寸
		}
		state.set(this.container,style.cw,style.ch,"block","#d9d9d9",style.cp);
		state.set(this.leftbox,style.lw,style.lh,"block","#000",style.lp);
		state.set(this.rightbox,style.rw,style.rh,style.rd,"#606060",style.rp);
		state.set(this.playbox,style.pw,style.ph,"block","#000",style.pp);
		state.set(this.flashbox,style.fw,style.fh,"block","#000",style.fp);
		state.view(this.container);
		state.view(this.leftbox);
		state.view(this.playbox);
		state.view(this.flashbox);
		state.view(this.rightbox);
		if(isinit){
			QP.Player(style.fw,style.fh,true);
		}
		if(this.playmode == 4){
			QP.Player(style.fw,style.fh,false);
		}
		if(flag){
			var that = this;
			if(temp.w==this.adjusted.w && temp.h==this.adjusted.h){clearTimeout(this.adjusttimeout);}
			else this.adjusttimeout=window.setTimeout(function(){that.setsize(flag,isinit)},delay);
		}
		else{
			clearTimeout(this.adjusttimeout);
		}
	},
	setbestsize:function(){
		this.setsize(0);
		if(!this.isfullscreen){
			window.scroll(0,0);
		}
	},
	loading:function(){
		var loaded = 0;
		if(this.playmode != 1){
			loaded = 100;
			document.getElementById("loading").style.display = "block";
			document.getElementById("menu").style.display = "block";
			document.getElementById("progress").style.display = "none";
		}
		else{
			try{
				loaded = window.document.gameobj.PercentLoaded();
				document.getElementById("loading").style.display = "block";
				document.getElementById("menu").style.display = "none";
				document.getElementById("progress").style.display = "block";
				document.getElementById('bar').style.width =loaded + '%';
			}
			catch(e){
				loaded = 100;
				document.getElementById("loading").style.display = "block";
				document.getElementById("menu").style.display = "block";
				document.getElementById("progress").style.display = "none";
			}
		}
		if(loaded == 100){
			document.getElementById('framecount').innerText = '下载完毕';
			if(this.loadingtime >= 4000){
				this.isloading = false;
				clearTimeout(this.loadingtimeout);
				document.getElementById("loading").style.display = "none";
				document.getElementById("menu").style.display = "block";
				document.getElementById("progress").style.display = "none";
			}else{
				this.isloading = true;
				this.loadingtime += 300;
				var that = this;
				document.getElementById("loading").style.display = "block";
				document.getElementById("menu").style.display = "block";
				document.getElementById("progress").style.display = "none";
				this.loadingtimeout = setTimeout(function(){that.loading(that.playmode)},300);
			}
		}
		else{
			document.getElementById("menu").style.display = "none";
			document.getElementById("progress").style.display = "block";
			document.getElementById('framecount').innerText = '载入中<<';
			if (this.loadingtime >= 6000) {
				this.isloading = false;
				document.getElementById("loading").style.display = "none";
			}
			var that = this;
			this.loadingtime += 300;
			this.loadingtimeout = setTimeout(function(){that.loading(that.playmode)},300);
		}
	}
};
//init tools object
tools.flash = {"w":_gamewidth,"h":_gameheight};
tools.ad = {"w":550,"h":420};
//init player object
player.container = document.getElementById("container");
player.playbarbox = document.getElementById("menu_box");
player.leftbox = document.getElementById("left_box");
player.rightbox = document.getElementById("right_box");
player.playbox = document.getElementById("play_box");
player.flashbox = document.getElementById("flash_box");
player.playmode = _playmode;
player.setsize(0,true)
player.loading();