﻿function getRealSize(w, h, m){
	var t;
	if(m == 1){
		t = parseInt(flash_width * h/flash_height);
		if(t > w){
			return w;	
		}else{
			return t;
		}
	}else{
		t = parseInt(w * flash_height/flash_width);
		if(t > h){
			return h;
		}else{
			return t;
		}
	}
}

function resizeFlash(m) {
	if (m == 1)
	{
		rw = getRealSize(945,610,1);
		rh = getRealSize(rw,610,2);

	} else if (m == 2)
	{
		rw = getRealSize(780,453,1);
		rh = getRealSize(rw,453,2);

	} else if (m == 3)
	{
		rw = flash_width;
		rh = flash_height;
	} else {
		rw = flash_width;
		rh = flash_height;
	}
	if ($('flasharea'))
	{
		$('flasharea').style.width = rw+'px';
		$('flasharea').style.height=rh+'px';
	}
	$('flashgame').width  = rw+'px';
	$('flashgame').height = rh+'px';
}

function fullScreen() {
	$("flash_fullscreen").target="_blank";
	$("flash_fullscreen").href=""+flash_url;
}

function normalScreen() {
	resizeFlash(3);
	location.href="#playpostion";
}

function mediumScreen(){
	resizeFlash(1);
	location.href="#mediumScreenPostion";
}


function rand(min,max){
	return Math.round(min+(Math.random()*(max-min)));
};

function setStarVote(vote) {
	var my_voted = GetCookie('my_voted');
	voted_arr = my_voted.split(',');
	var len = voted_arr.length;
	for (i=0;i<len;i++) {
		if (voted_arr[i] == vote) {
			return;
		}
	}
	if (len>=50) voted_arr.shift(voted_arr);
	voted_arr.push(vote);
	my_voted = voted_arr.join(',');
	SetCookie('my_voted', my_voted);

}

function checkStarVote() {
	var my_voted = GetCookie('my_voted');
	voted_arr = my_voted.split(',');
	var len = voted_arr.length;
	for (i=0;i<len;i++) {
		if (voted_arr[i] == vote) {
			return;
		}
	}
}

function addToBrowerFavorite() {
	window.external.AddFavorite(location.href,document.title);
}

function downFlash(){
	window.open(flash_downpath);
}


function sendFriend(){
	window.clipboardData.setData("text",location.href+'?clip');alert("网址已复制好，粘贴发给好朋友一起玩吧^_^");
}

function replayFlash() {
	if (disp_gg) return false;
	var type = getTheType();

	if (type == 'swf') playSwfFlash(true);
	else playIframeFlash(true);
}

function playTheFlash() {
	var type = getTheType();

	if (type == 'swf') playSwfFlash();
	else playIframeFlash();
	
}

function playSwfFlash(replay) {
	var params = {
		allowNetworking: "internal"
	};
	flash_url="http://www.willinggames.com"+flash_url;
	swfobject.embedSWF(flash_url, "flashgame", flash_width, flash_height, "9.0.0", "expressInstall.swf",null,params);
	if (replay) {
		$('flashgame').style.display='block';
	}
}

function playIframeFlash(replay) {
	var html = "<iframe id='flash_frame' frameborder='0' width='"+flash_width+"' height='"+flash_height+"'  src='"+flash_url+"'></iframe>";
	$('flashgame').style.width=flash_width;
	$('flashgame').style.height=flash_height;
	$('flashgame').innerHTML=html;
	if (replay) {
		$('flashgame').style.display='block';
	}
}

function flashPercent() {
	var str = '<div id="percentdiv"><div class="loading_bar"><div id="bar" style="background:#009CE8;float:left;height:8px;"></div></div><div>Games downloaded: <span id="progress"></span></div></div>';
	$('percent').innerHTML=str;
	setTimeout("percent()", 200);
}

function percent() {
	var flashgame = document.getElementById('flashgame');
	if (document.all && typeof(flashgame) == 'object') {
		var bg = document.getElementById('bar');
		var loaded = flashgame.PercentLoaded();
		if (typeof(loaded) != 'undefined')
		{
			var timeout = setTimeout('percent()',300);

			bg.style.width=4.5 * loaded;
			var bb = document.getElementById('progress');
			bb.innerHTML=loaded + '%';
			if (loaded == 100) {
				document.getElementById('percentdiv').style.display="none";
				clearTimeout(timeout);
				hidepercent();
			}
		} else {
			hidepercent();
		}
	} else {
		hidepercent();
	}
}
function hidepercent() {
	document.getElementById('percentdiv').style.display="none";
}

function getTheType() {
	var a=/(.*)\.(.*)$/gi;var b=flash_url.split('?');var c=a.exec(b[0]);return RegExp.$2;
}

playTheFlash();

var flash_type = getTheType();
if (flash_type == 'swf') {
	flashPercent();
}

disp_gg = false;
if (document.getElementById('flashgame'))
{
	play_befoce_gg();
} else {
	disp_gg =false;
}

function play_befoce_gg() {
	var play_before=document.createElement("div");
	play_before.setAttribute('id', 'flashgame_play_before');

	play_before_iframe = document.createElement('iframe');
	play_before_iframe.setAttribute('src', '/img/gg.html');
	play_before_iframe.setAttribute('width', flash_width+'px');
	play_before_iframe.setAttribute('height', flash_height+'px');
	play_before_iframe.setAttribute('frameBorder', '0');
	play_before_iframe.setAttribute('scrolling', 'no');

	play_before.appendChild(play_before_iframe);
	$('flashgame').parentNode.appendChild(play_before);
	document.getElementById('flashgame_play_before').style.width=flash_width+'px';
	document.getElementById('flashgame_play_before').style.height=flash_height+'px';
	
	document.getElementById('flasharea').style.width = flash_width+'px';
	document.getElementById('flasharea').style.height=flash_height+'px';
	document.getElementById('flasharea').className = 'flasharea_border';
	setTimeout("play_before_hide()", 4900);
	
}

function play_before_hide() {
	disp_gg = false;
	var p = document.getElementById('flashgame_play_before');
	p.style.display='none';
	$('flashgame').style.display='block';
	$('flashgame').style.float='center';
}

savePlayd(flash_title, flash_path);
