
//-----------------------------------------------
// config

// use of web start ?
var is_webStart = 0;

// use of web installer?
var is_installing = 0;

// ActiveX launcher version
var launcher_version = '1,0,0,12';

// firefox plugin
var _pluginspage = 'http://210.155.155.250/download/firefoxplugin/csweblauncher_1.0.0.10.xpi';

// firefox plugin install page
var firefoxplugin_installurl = base_url('include') + "start/install_firefox.html";

// activex install page
var activex_installurl = base_url('include') + "start/install_activex.html";

// plugin parameter
if (typeof crypted_param != "string") {
	var crypted_param = "";
}

// install dummy url
var csgame_installurl = base_url('include') + 'nolayout/download/install.html';

// window.open
var childWin = "";

//-----------------------------------------------
//Set Webpage to WebStart ActiveX Control (IE)
function setWebStartModuleIE(stateChangeCallback) {

	if (is_webStart) {
		return is_webStart;
	}

	var tagStr = makeWebStartTag(700, 100, true);
	
	$('div#Game_Install_object').prepend(tagStr);
	if (stateChangeCallback) {
		$(tagStr).bind(
			'readystatechange',
			function() { stateChangeCallback(this); }
		);
	}
	is_webStart = $(tagStr).get(0);
	return is_webStart;
}

//Make to ActiveX Control Object tag
function makeWebStartTag(width, height) {

	var param = {
		id: 'CsWebLauncherController',
		codebase: _base_urls['base'] + 'start/csweblauncher.cab#version=' + launcher_version,
//		classid: 'CLSID:8F60EE6F-DC53-4F9C-9E66-84BD2A545805',		// US?
		classid: 'CLSID:634D53E5-03DC-4DDE-B82E-A93B8DBC7167',
//		style: 'display:none',
//		width: '200',
//		height: '90'
		width: '0',
		height: '0'
	};

	if (width) {
		param['width'] = width;
	}

	if (height) {
		param['height'] = height;
	}

	var tagStr = '<object';
	$.each(param, function(k, v) {
		tagStr += ' ' + k + '="' + v + '"';
	});
	tagStr += '></object>';

	return tagStr;

}



//Set Webpage to WebStart Control(Firefox)
function setWebStartModuleFireFox() {

	if (is_webStart) {
		return is_webStart;
	}

	var tagStr = makeWebStartTagEmbed();

	$('div#Game_Install_object').prepend(tagStr);
	is_webStart = document.getElementById('CsWebLauncherController');

	return is_webStart;
}

//Make to Firefox embed tag
function makeWebStartTagEmbed(width, height) {

	var param = {
		id: 'CsWebLauncherController',
		type: 'application/csweblauncher-plugin',
		pluginspage: _pluginspage,
		width: '0',
		height: '0'
	};

	if (width) {
		param['width'] = width;
	}

	if (height) {
		param['height'] = height;
	}

	var tagStr = '<embed';
	$.each(param, function(k, v) {
		tagStr += ' ' + k + '="' + v + '"';
	});
	tagStr += '>';
	return tagStr;

}


//-----------------------------------------------
function doInstallIE()
{
	result = CsWebLauncherController.DoInstall();
	if (parseInt(result) == 8) {
		timerID = setTimeout("doInstallIE()", 10);
	} else if (parseInt(result) != 0) {
		clearTimeout(timerID);
		alert(result);
	} else {
		clearTimeout(timerID);

		result = CsWebLauncherController.Exec(crypted_param);
		alert('Game installation completed.');
		result = CsWebLauncherController.Exec(crypted_param);
		$('#install_msg').html('');
		$('#CsWebLauncherController').css('display', 'none');
		$('#Game_Install_object').css('top', '-1000px');

		is_installing = 0;

		_m_close();
	}
}

function doInstallFireFox()
{
	result = CsWebLauncherController.DoInstall();
	if (parseInt(result) != 0) {
		timerID = setTimeout("doInstallFireFox()", 10);
	} else {
		clearTimeout(timerID);
		result = CsWebLauncherController.Exec(crypted_param);
		alert('Game installation completed.');
		result = CsWebLauncherController.Exec(crypted_param);

		$('#install_msg').html('');
		$('#CsWebLauncherController').css('display', 'none');
		$('#Game_Install_object').css('top', '-1000px');

		is_installing = 0;

		_m_close();
	}
}

// close install window
function _m_close() {
	if (childWin != null && childWin.closed == false) {
		childWin.close();
	}

	if ($('#install_body').length) {
		window.close();
		$('#wrap_ga2 #Game_Install_object').css('top', '-1000px');
	}

	return;
}

// webStart
//   web install body
//   call to webStart from other web page
function webStart() {
//Debug
/*
alert('Comming soon!');
window.top.location.href = '/home';
return;
*/
	if (is_installing) {
		_m_close();
		return;
	}

	if (crypted_param == '') {
		_m_close();
		return;
	}

	if (navigator.userAgent.indexOf('Mac') != -1) {
		alert('To play GetAmped2 with Mac,  first you need to download and install a client for Mac and start the game from the client.');

		_m_close();
		return;
	}

	if (csgame_installurl == undefined) {
		csgame_installurl = '../nolayout/download/install.html';
	}

	// from FireFox
	if ($.browser.mozilla) {
		// install pop up
		if (!$('#install_body').length) {
			if (childWin != null && childWin.closed == false) {
				return;
			}
			childWin = window.open(csgame_installurl,'game','scrollbars=1,width=250,height=180,location=0,resizable=0');

			if (childWin != null) {
				childWin.focus();
			} else {
				alert('uncheck Turn on Pop-up Blocker');
			}
			return;
		}

		var isReadyState = false;
		CsWebLauncherController = setWebStartModuleFireFox();
		if ((!CsWebLauncherController) || (!CsWebLauncherController.IsInstall)) {
			alert('Firefox plug-in is not installed, or It\'s old version Firefox plug-in.\nPlease install the new version.');
			location.href=firefoxplugin_installurl;
			return;
		}

		var result = "1";
		result = CsWebLauncherController.IsInstall(crypted_param);

		if (parseInt(result) != 0) {
			is_installing = 1;
			msg = 'Now Installing the game.';
			$('#CsWebLauncherController').attr('width', 700).attr('height', 100);
			$('#install_msg').append(msg);
			result = CsWebLauncherController.Install(crypted_param);
			timerID = setTimeout("doInstallFireFox()", 10);
		} else {
			$('#install_msg').html('');
			//$('#CsWebLauncherController').css('display', 'none');
			$('#Game_Install_object').css('top', '-1000px');
			CsWebLauncherController.Exec(crypted_param);
			_m_close();
		}
	}
	else if ($.browser.msie) {
		var isReadyState = false;
		var objActiveX = setWebStartModuleIE();

		if (! objActiveX || objActiveX.readyState < 4 ||
			! objActiveX.object) {
			$('#CsWebLauncherController').css('display', 'none');
			$('#CsWebLauncherController').bind('readystatechange', function(){
				if (!this || this.readyState < 4) {
					// loading
					return;
				} else if (this.object) {
					alert('ActiveX  installation completed.');
					location.href = '';
				} else {
					$('#CsWebLauncherController').css('display', 'none');
					alert('ActiveX control is not installed, or it\'s old version ActiveX control.\nPlease install the new version. ');
				}
			 });
		} else {
			// install pop up
			if (!$('#install_body').length) {
				if (childWin != null && childWin.closed == false) {
					return;
				}

				childWin = window.open(csgame_installurl,'game','scrollbars=1,width=250,height=180,location=0,resizable=0');

				if (childWin != null) {
					childWin.focus();
				} else {
					alert('uncheck Turn on Pop-up Blocker');
				}
				return;
			}

			setTimeout(function() {
				try {
					version = CsWebLauncherController.Init();
					if (launcher_version.replace(/,/g,'.') != version) { throw "OLD VERSION"; }
					$('#CsWebLauncherController').css('display', 'block');
				} catch(e) {
					$('#CsWebLauncherController').css('display', 'none');
					alert('ActiveX control is not installed, or it\'s old version ActiveX control.\nPlease install the new version. ');
					location.href=activex_installurl;
					return;
				}
				try {
					var result = "1";
					result = CsWebLauncherController.IsInstall(crypted_param);
					if (parseInt(result) != 0) {
						is_installing = 1;
						msg = 'Now Installing the game.';
						$('#CsWebLauncherController').attr('width', 700).attr('height', 100);
						$('#install_msg').append(msg);
						result = CsWebLauncherController.Install(crypted_param);
						timerID = setTimeout("doInstallIE()", 10);
					} else {
						CsWebLauncherController.Exec(crypted_param);
						$('#Game_Install_object').css('top', '-1000px');
						$('#CsWebLauncherController').css('display', 'none');
						_m_close();
					}

				} catch(e) {}

			}, 500);
		}
	} else {
		//
		alert('sorry, only IE or FireFox');
	}
}

