var isAllSrceen=false;
var frame_h=800;
var box_w=1000;
var box_h=590;
var box_t=0;
var box_l=0;
var tableBox;
var mainFlash;
var game_Layer;
var gameSWF;
var CloseBtn;
var gameID;
var userID;
var seq;
var version;
var isIE = document.all ? true : false; 
var htmlStr;

mainFlash=getHtml_obj('myFlash');
if(mainFlash){
	mainFlash.onblur=blurFocus;
	document.onfocus=blurFocus;
}

function blurFocus()
{
	var act = document.activeElement;
  	if(String(act)!="[object]"){
  		act.onblur=blurFocus;
  	}else{
  		gameSWF=getHtml_obj('gameSwf');
  		var tempMsgDiv=get_obj('msgDiv');
  		if(!gameSWF&&!tempMsgDiv){
  		//	gameSWF.focus();
  		//}else {
  			mainFlash.focus();
  		}
  	}
} 

function getGameID()
{
	return gameID;	
}
function resizeGame_box()
{
	if(game_Layer && tableBox)
	{		
		if(isAllSrceen){
			var f_w=document.body.offsetWidth;
			game_Layer.style.width		= f_w+ "px"; 
			game_Layer.style.left 		= box_l+ "px";
			game_Layer.style.top 		= box_t+ "px";	
			if(CloseBtn){
				CloseBtn.style.left=f_w-50 + "px";
				CloseBtn.style.top=box_t + "px";
			}
		}else{
			var l = (document.body.offsetWidth - box_w) / 2;
			game_Layer.style.left		= l + "px";
			game_Layer.style.top 		= box_t + "px";	
			if(CloseBtn){
				CloseBtn.style.left=(l+box_w) + "px";
				CloseBtn.style.top=box_t + "px";
			}
		}		
	}
}

function changeGameObjSize(w,h,isFullScreen,limit)
{	 
	frame_h=limit?limit:frame_h;
	if(!gameSWF){
		gameSWF=getHtml_obj('gameSwf');
	}
	gameSWF.width 					= w + "px"; 
	gameSWF.height 					= h + "px"; 	
	
	if(isFullScreen){
		var f_h=document.body.offsetHeight;
		game_Layer.style.height		= f_h>frame_h?frame_h:f_h+ "px"; 
		tableBox.height 			= 10+"px"; 
	}else{
		tableBox.height 			= (h-30)+"px"; 
		game_Layer.style.width		=w + "px"; 
		game_Layer.style.height		=h + "px"; 
	}
	setFullScreen(isFullScreen);
}

function setBgImage(image) 
{
	game_Layer.style.background=image;
}

function setBgColor(color) 
{	
	gameSWF.bgcolor=color;
	game_Layer.style.background=color;
}

function setFullScreen(b)
{
	isAllSrceen=b;
	resizeGame_box();
}

function setSwfObject(name,w,h,align)
{
	align=align?'valign="middle"':'valign="top"';
	name=name?name:"module/game/gameSimulator.swf?"+version;
	w=w?w:1000;
	h=h?h:590;
	
	htmlStr='<table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0"><tr><td align="center" '+align+'>';
	htmlStr=htmlStr+'<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,45,0" name="gameSwf" width="'+w+'" height="'+h+'" id="gameSwf" align="middle">';
	htmlStr=htmlStr+'<param name="movie" value="'+name+'" />';
	htmlStr=htmlStr+'<param name="FlashVars" value="gameID='+gameID+'&userID='+userID+'&seq='+seq+'" />';
	htmlStr=htmlStr+'<param name="salign" value="t" />';
	htmlStr=htmlStr+'<param name="bgcolor" value="#EEEEEE" />';
	htmlStr=htmlStr+'<param name="scale" value="noscale" />';
	htmlStr=htmlStr+'<embed src="'+name+'" FlashVars="gameID='+gameID+'&userID='+userID+'&seq='+seq+'" width="'+w+'" height="'+h+'" scale="noscale" salign="t" align="middle" swliveconnect="true" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"  bgcolor="#EEEEEE" id="gameSwf" name="gameSwf"></embed>';
	htmlStr=htmlStr+'</object></td></tr></table>'; 
}

function swapGameBox(name,w,h,t,limit)
{	 
	frame_h=limit;
	setSwfObject(name,w,h,1);
	box_t=t?t:0;
	if(gameSWF)
	{
		if(isIE) gameSWF.removeNode(true);
		else gameSWF.parentNode.removeChild(gameSWF);
		gameSWF=null;
	}
	game_Layer.innerHTML=htmlStr;
	gameSWF=getHtml_obj('gameSwf');
	
	game_Layer.style.width		=w + "px"; 
	
	createCloseBtn();
	
	box_w=w;
	box_h=h;	
	
	if(isAllSrceen){
		var f_h						=document.body.offsetHeight;
		game_Layer.style.height		=f_h>frame_h?frame_h:f_h+ "px"; 
		tableBox.height 			=10+"px"; ;//game_Layer.style.height；
	}else{
		tableBox.height 			=(h-30)+"px"; 
		game_Layer.style.height		=h + "px"; 
	}
	resizeGame_box();
}

function createCloseBtn()
{	   
	CloseBtn=document.createElement("div");
	CloseBtn.setAttribute("id","CloseGameBtn"); 

	CloseBtn.setAttribute("valign","top");
	CloseBtn.style.position="absolute";

	CloseBtn.style.zIndex = "11112";
	CloseBtn.innerHTML='<img onclick="closeGameLayer();" src="http://www.51mole.com.tw/images/close1.jpg" width="39" height="39" style="cursor:pointer" onmouseover="this.src=\'http://www.51mole.com.tw/images/close2.jpg\'" onmouseout="this.src=\'http://www.51mole.com.tw/images/close1.jpg\'"  onmousedown="this.src=\'hthttp://www.51mole.com.tw/images/close3.jpg\'"/>'; 
	document.body.appendChild(CloseBtn);
}

function openGameLayer(_gameID,_userID,_seq,_version)
{	   

	closeGameLayer();
	
	gameID=_gameID;
	userID=_userID;
	seq=_seq;
	version=_version;
	
	setSwfObject();
	tableBox=getHtml_obj('myFlashBox');
	
	mainFlash=getHtml_obj('myFlash');
	mainFlash.style.width = "0px"; 
	mainFlash.style.height = "0px"; 
	
	hideMainLayer()
	

	var msgObj=document.createElement("div") ;
	msgObj.setAttribute("id","gameLayer"); 
	msgObj.setAttribute("name","gameLayer"); 
	msgObj.setAttribute("align","center");
	msgObj.style.position="absolute";

	var l = (document.body.offsetHeight - box_w) / 2;
	msgObj.style.left = l + "px";
	msgObj.style.top = box_t+ "px"; 

	msgObj.style.width = box_w + "px"; 
	msgObj.style.height = box_h + "px"; 
	msgObj.style.zIndex = "11111";
	document.body.appendChild(msgObj);
	game_Layer = msgObj;

	msgObj.innerHTML=htmlStr;
	gameSWF=getHtml_obj('gameSwf');
	gameSWF.focus(); 
	gameSWF.onblur=blurFocus;
}

function showMainLayer()
{	   
	!isIE?tableBox.style.visibility="visible":"";
}
function hideMainLayer()
{	   
	!isIE?tableBox.style.visibility="hidden":"";
}

function closeGameLayer() 
{	

	if(tableBox){
		mainFlash.style.width = "960px"; 
		mainFlash.style.height = "560px"; 
		showMainLayer();
		tableBox.height = "590px"; 	
		mainFlash.focus(); 
	}
	if(gameSWF)
	{		
		if(isIE) gameSWF.removeNode(true);
		else gameSWF.parentNode.removeChild(gameSWF);
	}
	if(game_Layer)
	{			
		game_Layer.innerHTML="";
		if(isIE) game_Layer.removeNode(true);
		else game_Layer.parentNode.removeChild(game_Layer);
	}	
	if(CloseBtn)
	{			
		CloseBtn.innerHTML="";
		if(isIE) CloseBtn.removeNode(true);
		else CloseBtn.parentNode.removeChild(CloseBtn);
	}	
	isAllSrceen=false;
	frame_h=800;
	box_w=1000;
	box_h=590;
	box_t=0;
	box_l=0;
	gameID=0;
	userID=0;
	seq=0;
	
	//game_Layer.expandoProperty = null;
	
	tableBox=null;
	game_Layer=null;
	gameSWF=null;
	CloseBtn=null;
	htmlStr="";
	releaseForIE();
}

function releaseForIE()
{        
        if(!document.all) return;
　　　　CollectGarbage();
　　　　setTimeout("CollectGarbage();", 1);　
}
function conveyance(o,t,s) 
{
	var gswf=getHtml_obj(o);
	if(s=="complete"){
		resizeGame_box();
	}
	gswf[t](s);	
}


function getHtml_obj(id) 
{
   return  document.getElementById(id);
}

function alertMsg() 
{
	var numargs = arguments.length;  
	var s="|";
	for(var i=0;i<numargs;i++){
		s+=arguments[i]+"|";
	}
	alert( s ); 
}

function shake_xy(n) {
	if (window.moveBy) {
		for (i = 10; i > 0; i--) {
			for (j = n; j > 0; j--) {
				window.moveBy(0,i);
				window.moveBy(i,0);
				window.moveBy(0,-i);
				window.moveBy(-i,0);
			}		
		}
	}
}

function shake_x(n) {
	if (window.moveBy) {
		for (i = 10; i > 0; i--) {
			for (j = n; j > 0; j--) {
				window.moveBy(i,0);
				window.moveBy(-i,0);
			}
		}
	}	
}

function shake_y(n) {
	if (window.moveBy) {
		for (i = 10; i > 0; i--) {
			for (j = n; j > 0; j--) {
				window.moveBy(0,i);
				window.moveBy(0,-i);
			}
		}
	}
}