// methods to implement for client
// 1. init4ktj() - called just after kotakji is detected
// 2. powerEditApplet(openURL) - invoke powerEdit in Applet Mode

// Kotaji is detected
var isPowerEdit = false;
var tid;
var gOpenURL;  //global variable 'openURL'
var gAccount;	// global variable 'account'
var gHpswd;	// global variable 'hpswd'

var APPLET_LAUNCHER_ID = "launcher_applet_div";
var APPLET_LAUNCHER_ID_1 = "launcher";

var clearDetector = false;
var webtopLaunched = true;

// should be call from onload of body tag
function installKtjDetector() {
	var ct = new Date();
	var detectorElem = "<img src='http://127.0.0.1:21121/com/tf/common/images/progress.gif#" + ct + "' onLoad='ktjDetected(true)' onerror='ktjDetected(false)'/>";
	document.getElementById("cmdHack").innerHTML = detectorElem;
}

function ktjDetected(detected) {
	if(detected){
		clearInterval(tid);
		if(!clearDetector){
			if(isPowerEdit){
				var cmdReq = "http://127.0.0.1:21121/invoke?open="+encodeURIComponent(gOpenURL)+"&username="+gAccount+"&nos="+gHpswd;
				document.getElementById("cmdHack").innerHTML = '<img src="'+cmdReq+'"/>';
			}else{
				var cmdReq = "http://127.0.0.1:21121/invoke?app=SYNC&username="+gAccount+"&nos="+gHpswd;;
				document.getElementById("cmdHack").innerHTML = '<img src="'+cmdReq+'"/>';
			}
		}
		clearDetector = true;
		
		//close Window
		closeWindow();
				
		// hide applet
		var launcherDiv = document.getElementById(APPLET_LAUNCHER_ID);
		if ( launcherDiv != null ) {
			launcherDiv.style.display = "none";
		}

	}else{
		if ( webtopLaunched == true ) {
			var launcherDiv = document.getElementById(APPLET_LAUNCHER_ID);
			if ( launcherDiv == null || launcherDiv.style.display == "none") {
				runWebtop();
			}
		}
	}
}

function powerEdit(account, hpswd, openURL) {
	gAccount = account;
	gHpswd = hpswd;
	gOpenURL = openURL;
	
	clearDetector = false;
	isPowerEdit = true;
	installKtjDetector();
}

function showSync(account, hpswd){
	gAccount = account;
	gHpswd = hpswd;
	
	clearDetector = false;
	isPowerEdit = false;
	installKtjDetector();
}

function getAppletElement() {
	var result = '<!--[if !IE]> Firefox and others will use outer object -->';
	result += '<OBJECT classid="java:com.tf.invoker.InvokerApplet" ';
	result += ' type="application/x-java-applet" id="InvokerApplet"';
	result += ' archive="invokerApplet.jar" codebase="/"';
	result += ' WIDTH="640px" HEIGHT="303px" mayscript=true>';
	result += '<PARAM NAME = "MAYSCRIPT" VALUE = "true" />';
	result += '<PARAM NAME = "codebase" VALUE = "/" />';
	result += '<PARAM NAME = "CALLBACK_METHOD" VALUE = "webtopStarted" />';
	//result += '<strong>ThinkFree Office requires Java-runtime. Visit <A href="http://www.thinkfree.co.jp/troubleshooting.tfo" target="_new">trouble shooting page</A></strong>';
	//result += '<strong>Java-runtimeが必要となります。詳細は<A href="http://www.thinkfree.co.jp/troubleshooting.tfo">「パワーエディットに関する問題の解決方法について」</A>のページをご参照ください。</strong>';
	result += '<!--<![endif]-->';
	result += '<OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" id="InvokerApplet"';
	result += ' codebase="http://java.sun.com/update/1.5.0/jinstall-1_5-windows-i586.cab#Version=1,5,0,0"';
	result += ' WIDTH="640px" HEIGHT="303px">';
	result += '<PARAM NAME = "codebase" VALUE = "/" />';
	result += '<PARAM NAME = "CODE" VALUE = "com.tf.invoker.InvokerApplet" />';
	result += '<PARAM NAME = "MAYSCRIPT" VALUE = "true" />';
	result += '<PARAM NAME = "archive" VALUE = "invokerApplet.jar" />';
	result += '<PARAM NAME = "CALLBACK_METHOD" VALUE = "webtopStarted" />';
	//result += '<strong>ThinkFree Office requires Java-runtime. Visit <A href="http://www.thinkfree.co.jp/troubleshooting.tfo" target="_new">trouble shooting page</A></strong>';
	result += '<strong>Java-runtimeが必要となります。詳細は<A href="http://www.thinkfree.co.jp/troubleshooting.tfo">「パワーエディットに関する問題の解決方法について」</A>のページをご参照ください。</strong>';
	result += '</OBJECT>';
	result += '<!--[if !IE]> close outer object -->';
	result += '</OBJECT>';
	result += '<!--<![endif]-->';	
	return result;
}

function runWebtop(){
	var appletElem = getAppletElement();
	var launcherDiv = document.getElementById(APPLET_LAUNCHER_ID);

	if ( launcherDiv != null ) {
		document.body.removeChild(launcherDiv);
	}

	var ct = new Date();
	var newLauncherDiv = document.createElement('div');
	newLauncherDiv.setAttribute('id',APPLET_LAUNCHER_ID);
	//newLauncherDiv.setAttribute('date',ct);

	var style = newLauncherDiv.style;
	style.display = "block";
	style.position = "absolute";
	style.top = "100px";
	style.left = "0";
	style.width = "100%";
	style.height = "303px";
	newLauncherDiv.innerHTML = "<center>"+appletElem+"</center>";

	document.body.appendChild(newLauncherDiv);

}

//call-back from InvokerApplet
function webtopStarted(started){
	if(started){
		tid = setInterval('installKtjDetector()', 500);
		clearDetector = false;
	} else {	// premium edition is not installed
		if(isPowerEdit){
			powerEditApplet(gOpenURL);
		}else{
			openDownloadGuide();
		}
		// hide applet
		var launcherDiv = document.getElementById(APPLET_LAUNCHER_ID);
		if ( launcherDiv != null ) {
			launcherDiv.style.display = "none";
		}
	}
}