function unity_send(data,unity_object) {
	var unity_name = unity_object ? unity_object : 'UnityObject';
	var obj = (navigator.appName.indexOf("Microsoft") != -1) ? window[unity_name] : window.document[unity_name];
	data = 'msg:' + data;
	obj.SendMessage("core", "MSG", data);
}

function unity_send_internal(data,unity_object) {
	var unity_name = unity_object ? unity_object : 'UnityObject';
	var obj = (navigator.appName.indexOf("Microsoft") != -1) ? window[unity_name] : window.document[unity_name];
	data = 'msg:' + data;
	obj.SendMessage("core", "InternalMSG", data);
}

function unity_receive(data, swf_name) {
	var swf_name = (swf_name) ? swf_name : 'SwfMain'; 
	var obj = (navigator.appName.indexOf("Microsoft") != -1) ? window[swf_name] : window.document[swf_name];

	if (obj && obj.UnityTransfer) {
		obj.UnityTransfer(data);
	}
}
function unity_hide(object_id) {
	var object_id = (object_id) ? (object_id) : 'UnityObject_div';
	var obj = gebi(object_id);
	if (obj) {
	  obj.style.top = '-1000px';
	  obj.style.left = '-1000px';
	}
}
function unity_show(object_id, swf_name) {
	var object_id = (object_id) ? (object_id) : 'UnityObject_div';
	var swf_name = (swf_name) ? swf_name : 'SwfMain'; 
	var obj = gebi(object_id);
	if (obj && obj.style.top == '-1000px') {
		obj.style.top = obj.moveTop ? obj.moveTop + 'px' : '0px';
	  obj.style.left = obj.moveLeft ? obj.moveLeft + 'px' : '0px';
	}
	document.getElementsByName(swf_name)[0].blur();
}
function unity_remove(object_id) {
	var object_id = (object_id) ? (object_id) : 'UnityObject_div';
	var obj = gebi(object_id);
	if (obj) {
		obj.parentNode.removeChild(obj);
	}
	var obj1 = gebi(object_id);
	if (obj1) {
		alert('unity still alive :(');
	}
}

function unity_create(src, width, height, nomove, object_id, unity_object, swf_name) {
	var unity_name = unity_object ? unity_object : 'UnityObject';
	var object_id = (object_id) ? (object_id) : 'UnityObject_div';
	var swf_name = (swf_name) ? swf_name : 'SwfMain'; 
	var obj = gebi(object_id);
	 if (!obj) {
	  obj = document.createElement('DIV');
	  obj.id = object_id;
	  obj.style.position = 'absolute';
	  obj.style.top = '-1000px';
	  obj.style.left = '-1000px';
	  obj.style.width = width + 'px';
	  obj.style.height = height + 'px';
      obj.style.zIndex = 1000;
	  document.body.appendChild(obj);
	 } else {

		if (!nomove) {
	    obj.style.top = '-1000px';
	    obj.style.left = '-1000px';
		}
		 
	  obj.style.width = width + 'px';
	  obj.style.height = height + 'px';
	  var unity_obj = (navigator.appName.indexOf("Microsoft") != -1) ? window[unity_name] : window.document[unity_name];		 
	  if (unity_obj) return true;
	 }
	 if (!src) return true;
	 
	 var defArgs = {
	  id: unity_name,
	  name: unity_name,
	  width: '100%',
	  height: '100%',
	  src: src,
	  disableContextMenu: 'true', 
	  backgroundcolor: '000000', 
	  bordercolor: '000000', 
	  textcolor: 'FFFFFF', 
	  cancelwrite: true
	};
	var i;
	 for(i = 6; i<arguments.length; i+=2) {
	  defArgs[arguments[i]] = arguments[i+1];
	 }
	 var args = [];
	 for(i in defArgs) {
	  args[args.length] =  i;
	  args[args.length] =  defArgs[i];
	 }
	 obj.innerHTML = AC_UN_RunContent(args);
	 document.getElementsByName(swf_name)[0].blur();

	return true;
}

function unity_move(left, top, relativeObj,object_id) {
	var object_id = (object_id) ? (object_id) : 'UnityObject_div';
	var obj = document.getElementById(object_id);
	if (!obj) {
	  obj = document.createElement('DIV');
	  obj.id = object_id;
	  obj.style.position = 'absolute';
	  obj.style.top = '-1000px';
	  obj.style.left = '-1000px';
      obj.style.zIndex = 1000;
		document.body.appendChild(obj);
	}

	obj.last_top = top;
	obj.last_left = left;

	unity_validate(relativeObj,object_id);
}

function unity_validate(relativeObj,object_id, swf_name) {
	var swf_name = (swf_name) ? swf_name : 'SwfMain'; 
	var object_id = (object_id) ? (object_id) : 'UnityObject_div';
	if (typeof relativeObj == 'undefined') {
		relativeObj = (navigator.appName.indexOf("Microsoft") != -1) ? window[swf_name] : window.document[swf_name];
	}

	swf_coords = getCoords(relativeObj);

	var obj = document.getElementById(object_id);
	var top = obj.last_top;
	var left = obj.last_left;

	obj.moveTop = Number(swf_coords.t) + Number(top);
 	obj.moveLeft = Number(swf_coords.l) + Number(left);
	if (obj.style.top != '-1000px') {
		obj.style.top = obj.moveTop + 'px';
		obj.style.left = obj.moveLeft + 'px';
	}
}

function unity_plugin_version() {
	
	
	
	var pluginVersion = 0;
	if (navigator.appVersion.indexOf("MSIE") != -1 && navigator.appVersion.toLowerCase().indexOf("win") != -1) {
		pluginVersion = GetUnityWebPlayerVersion();
		if(pluginVersion == 0) {
			return 0;
		}
		var pluginVersionMatch = pluginVersion.match(/([0-9a-z]+)\.([0-9a-z]+)(\.([0-9a-z]+))?/);
	} else {
		if (navigator.mimeTypes && navigator.mimeTypes["application/vnd.unity"]) {
			if (navigator.mimeTypes["application/vnd.unity"].enabledPlugin && navigator.plugins && navigator.plugins["Unity Player"]) {
				if(navigator.plugins["Unity Player"].version) {
					pluginVersion = navigator.plugins["Unity Player"].version.match(/[0-9a-z]+\.[0-9a-z]+(\.[0-9a-z]+)?/);
				} else {
					pluginVersion = navigator.plugins["Unity Player"].description.match(/[0-9a-z]+\.[0-9a-z]+(\.[0-9a-z]+)?/);
				}
			}
			var pluginVersionMatch = pluginVersion[0].match(/([0-9a-z]+)\.([0-9a-z]+)(\.([0-9a-z]+))?/);
		}
	}

	if (pluginVersionMatch) {
		pluginVersion = {major: pluginVersionMatch[1], minor: pluginVersionMatch[2], build: pluginVersionMatch[3]};
	}
	
	return pluginVersion;
}

function unity_get_installer_path() {
	var tDownloadURL = ""

	if (navigator.platform == "MacIntel")
		tDownloadURL = "http://webplayer.unity3d.com/download_webplayer-2.x/webplayer-i386.dmg"
	else if (navigator.platform == "MacPPC")
		tDownloadURL = "http://webplayer.unity3d.com/download_webplayer-2.x/webplayer-ppc.dmg"
	else if (navigator.platform.toLowerCase().indexOf("win") != -1)
		tDownloadURL = "http://webplayer.unity3d.com/download_webplayer-2.x/UnityWebPlayer.exe"

	return tDownloadURL
}

function unity_version_check() {

	try { console.log('check_unity_version') } catch (e) {}

	if (navigator.appVersion.toLowerCase().indexOf("mac") != -1)
		{
		if(navigator.mimeTypes["application/vnd.unity"] && navigator.plugins["Unity Player"] && navigator.plugins){
			return 1;
		} else {
			return 0;
		}
	}

	var version = unity_plugin_version();
	
	try { console.log('version=' + version) } catch (e) {}
	try { console.log('version.major=' + version.major + ' version.minor=' + version.minor + ' version.build=' + version.build) } catch (e) {}
	if (version == 0) {
		return 0;
	} else {
		if(version.major < chkUnityVersionMajor && chkUnityVersionMajor != '') {
			return -1;
		}
		if(version.major == chkUnityVersionMajor && chkUnityVersionMajor != '') {
			if(version.minor < chkUnityVersionMinor && chkUnityVersionMinor != '') {
				return -1;
			}
			if(version.minor == chkUnityVersionMinor && chkUnityVersionMinor != '') {
				if(version.build < chkUnityVersionBuild && chkUnityVersionBuild != '') {
					return -1;
				}
			}
		}
	}
	return 1
}

function unity_check_init()
{
	var unityCheckedVersion = unity_version_check();
	switch (unityCheckedVersion) {
		case 0:
			gebi('overlay').style.display = 'block'
			gebi('noUnity').style.display = 'block'
			gebi('flash').className += ' trick'
			unity_autoreload()
			return 0
		case -1:
			// TODO: Invalid version of Unity plugin processing
			unity_autoreload()
			return 0
		case 1:
			gebi('noUnity').style.display = 'none'
			gebi('overlay').style.display = 'none'
			gebi('flash').className = gebi('flash').className.replace(/(\s|^)trick(\s|$)/i, ' ')
			return 1
	}
}

function unity_autoreload() {
	navigator.plugins.refresh()
	if (unity_version_check() == 1) {
		window.location.reload()
	}
	setTimeout('unity_autoreload()', 500)
}

function unity_callback_loaded() {}

function unity_callback_created() {}

function unity_load_click(){
	var swf_name = (swf_name) ? swf_name : 'SwfMain';
	var obj = (navigator.appName.indexOf("Microsoft") != -1) ? window[swf_name] : window.document[swf_name];
	if (obj && obj.UnityInstallClick) {
		obj.UnityInstallClick(unity_get_installer_path());
	}
}
function IsClickOnceAvailable(X_ClickOnceSupport) {
	function IsDotNetVersionMatch(element, matchVersion) {
		if (element == null || element.length != 2) {
			return false;
		}
		var elementVersion = element[1].split(".");
		if (elementVersion.length > 0) {
			var matchNumber = 0;
			var elementNumber = 0;
			var versionPos = 0;
			do {
				matchNumber = versionPos < matchVersion.length ? Number(matchVersion[versionPos]) : 0;
				elementNumber = versionPos < elementVersion.length ? Number(elementVersion[versionPos]) : 0;
				++versionPos;
			} while ((versionPos < matchVersion.length || versionPos < elementVersion.length) && matchNumber == elementNumber);

			if (matchNumber <= elementNumber) {
				return true;
			}
		}
		return false;
	}

	function HasDotNetClrVersion(matchVersion) {
		var userAgent = navigator.userAgent.match(/\.NET CLR [0-9.]+/g);
		if (userAgent == null || userAgent.length == 0) {
			return false;
		}
		matchVersion = matchVersion.split(".");
		if (matchVersion.length == 0) {
			return false;
		}
		for (var userAgentI = 0; userAgentI < userAgent.length; ++userAgentI) {
			var element = userAgent[userAgentI].match(/\.NET CLR ([0-9.]+)/);
			if (IsDotNetVersionMatch(element, matchVersion)) {
				return true;
			}
		}
		return false;
	}

	function HasDotNetVersion(matchVersion) {
		var userAgent = navigator.userAgent.match(/\.NET[0-9.]+/g);
		if (userAgent == null || userAgent.length == 0) {
			return false;
		}
		matchVersion = matchVersion.split(".");
		if (matchVersion.length == 0) {
			return false;
		}
		for (var userAgentI = 0; userAgentI < userAgent.length; ++userAgentI) {
			var element = userAgent[userAgentI].match(/\.NET([0-9.]+)/);
			if (IsDotNetVersionMatch(element, matchVersion)) {
				return true;
			}
		}
		return false;
	}

	if (navigator.appVersion.toLowerCase().indexOf("win") == -1) {
		return false;
	}

	if (X_ClickOnceSupport) {
		return true;
	}

	if (HasDotNetVersion("2.0")) {
		return true;
	}
	if (HasDotNetClrVersion("2.0.0")) {
		return true;
	}

	return false;
}

function downloadUnity() {
	var url = '/redist/instUnity.exe';
	if (navigator.platform == "MacIntel")
		url = "http://webplayer.unity3d.com/download_webplayer-2.x/webplayer-i386.dmg"
	else if (navigator.platform == "MacPPC")
		url = "http://webplayer.unity3d.com/download_webplayer-2.x/webplayer-ppc.dmg"
	else if (navigator.platform.toLowerCase().indexOf("win") != -1) {		
		var clickOnceUrl = '/redist/corUnity.application';
		if (IsClickOnceAvailable(X_ClickOnceSupport) && (typeof(clickOnceUrl) == "string") && (clickOnceUrl.length > 0)) {
			url = clickOnceUrl;
		}
	}
	var popup = window.open(url, "_top", "");
	if (popup) {
		popup.focus();
	}
}
