// Document Viewer JavaScript
// 2009.06.03

//
// setting
//
var autoDownloadSupport = true;

//
// initial
//
var LANG = "ja";
var init = false;
var macos = false;
var cookieId = "DocViewer.page";	// ディレクトリ内で共通のCookieとして管理される
var cookieDay = 7;

function initial() {
	if(navigator.userAgent.indexOf("Mac") != -1) {
		macos = true;
	}
	if(autoDownloadSupport == false) {
		if(navigator.javaEnabled() == false || document.applets.length < 1) {
			if(LANG == "ja") {
				alert("Docment Viewer Javaアプレットが実行できません");
			} else {
				alert("Could not start Document Viewer Java applet.");
			}
			return false;
		}
	}

	init = true;
	return true;
}

//
// final
//
function final() {
	if(cookieDay > 0) {
		var page = DocViewer.GetPage();
		if(page != null && page != "") {
			setCookie(cookieId, page, cookieDay);
		}
	}
}

//
// Applet tag
//

function appletTag(width, height, folder, openPage) {

	var appletJar = "DocViewer.jar";
	var appletName = "DocViewer";
	var codeBase = ".";
	var session = "";
	var userAgent = navigator.userAgent;
	var param1 = "";
	var param2 = "";
	var param3 = "";
	var startButton = "-";

	var noembedSupport = false;		// for Firefox

	var tagType = '';
	var _userAgent = navigator.userAgent;
	var idx = 0;

	if(autoDownloadSupport) {
		if(_userAgent.indexOf('Windows') != -1) {
			// Windows
			if((idx = _userAgent.indexOf('MSIE')) != -1) {
				if(_userAgent.charAt(idx+5) == 8) {
					//IE8 暫定対応
					tagType = '';
				} else
				if(_userAgent.charAt(idx+5) > 5) {
					//IE6 & IE7	
					tagType = 'O';
				}
			} else if(_userAgent.indexOf('Firefox') != -1) {
				// FireFox
				tagType = 'E';
			}
		}
		if(tagType == '') {
			autoDownloadSupport = false;
		}
	}

	var altText = '';
	var noJavaText = '';
	if(LANG == 'ja') {
		altText = 'Document Viewer Javaアプレットが実行できません';
		noJavaText =    '<b>Document Viewer Javaアプレットが実行できません。<br>'
						+ 'JavaVMをインストールするには '
						+ '<a href="http://www.java.com/" target="_blank">こちら</a> を、<br>'
						+ 'JavaAppletを有効にするには ' 
						+ '<a href="http://java.com/ja/download/manual.jsp" target="_blank">こちら</a> をクリックしてください。<br>'
						+ '</b>\n';
	} else {
		altText = 'Could not start Document Viewer Java applet.';
		noJavaText =    '<b>Could not start Document Viewer Java applet. Either JVM is absent or Java applets are disabled.<br>\n'
						+ 'For JVM, click '
						+ '<a href="http://www.java.com/" target="_blank">here</a> .<br>'
						+ 'To enable Java applets, click '
						+ '<a href="http://java.com/en/download/manual.jsp" target="_blank">here</a> .<br>'
						+ '</b>\n';
	}

	var tagText = '';
	if(tagType == 'O') {
		tagText +=		'<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" '
					+	'name="' + appletName + '" width="' + width + '" height="' + height + '" '
					+	'codebase="http://java.sun.com/update/1.6.0/jinstall-6u12-windows-i586.cab">\n'
					+	'<param name="name" value="' + appletName + '">\n'
					+	'<param name="codebase" value="' + codeBase + '">\n'
					+	'<param name="archive" value="' + appletJar + '">\n'
					+	'<param name="code" value="PageViewer.class">\n'
					+	'<param name="type" value="application/x-java-applet">\n'
					+	'<param name="scriptable" value="true">\n' ;
	} else if(tagType == 'E') {
		tagText +=		'<embed name="' + appletName + '" codebase="' + codeBase + '" '
					+	'archive="' + appletJar + '" code="PageViewer.class" '
					+	'width="' + width + '" height="' + height + '" '
					+	'type="application/x-java-applet" '
					+	'scriptable="true" '
					+	'pluginspage="http://java.sun.com/products/plugin/index.html#download" \n' ;
	} else {
		tagText +=		'<applet name="' + appletName + '" codebase="' + codeBase + '"'
					+	'archive="' + appletJar + '" code="PageViewer.class" '
					+	'width="' + width + '" height="' + height + '"'
					+	'alt="' + altText + '">\n';
	}

	if(tagType == 'E') {
		tagText +=		'SESSION="' + session + '" '
					+	'FOLDER="' + folder + '" '
					+	'USER_AGENT="' + userAgent + '"\n';
		if (startButton != 'null' &&  startButton != '' ) {
			tagText +=	'STARTBUTTON="' + startButton + '" ';
		}
		if (openPage != 'null' &&  openPage != '') {
			tagText +=	'OPENPAGE="' + openPage + '" ';
		}
		if (param1 != 'null' &&  param1 != '') {
			tagText +=	'PARAM1="' + param1 + '" ';
		}
		if (param2 != 'null' &&  param2 != '') {
			tagText +=	'PARAM2="' + param2 + '" ';
		}
		if (param3 != 'null' &&  param3 != '') {
			tagText +=	'PARAM3="' + param3 + '" ';
		}
		tagText +=		'>\n<noembed>\n';
	} else {
		tagText +=		'<param name="SESSION" value="' + session + '">\n'
					+	'<param name="FOLDER" value="' + folder + '">\n'
					+	'<param name="USER_AGENT" value="' + userAgent + '">\n';
		if (startButton != 'null' &&  startButton != '' ) {
			tagText +=	'<param name="STARTBUTTON" value="' + startButton + '">\n';
		}
		if (openPage != 'null' &&  openPage != '') {
			tagText +=	'<param name="OPENPAGE" value="' + openPage + '">\n';
		}
		if (param1 != 'null' &&  param1 != '') {
			tagText +=	'<param name="PARAM1" value="' + param1 + '">\n';
		}
		if (param2 != 'null' &&  param2 != '') {
			tagText +=	'<param name="PARAM2" value="' + param2 + '">\n';
		}
		if (param3 != 'null' &&  param3 != '') {
			tagText +=	'<param name="PARAM3" value="' + param3 + '">\n';
		}
	}

	if(tagType == 'O') {
		tagText += noJavaText;
		tagText +=		'</object>\n';
	} else if(tagType == 'E') {
		if(noembedSupport) {
			tagText += noJavaText;
			tagText +=	'</noembed>\n</embed>\n';
		} else {
			tagText +=	'</noembed>\n</embed>\n';
			if(navigator.javaEnabled() == false) {
				tagText += noJavaText;
			}
		}
	} else {
		tagText += noJavaText;
		tagText += '</applet>\n';
	}

	//alert( tagText );
	document.write( tagText );

}

//
// Cookie
//

function setCookie(cookieID ,data ,day){

	var cookieData
	if (cookieID == null) {
		return false;
	}
	cookieData = cookieID + "=" + encodeURI(data) + ";";
	if (day != '' && day != null) {
		theDay = new Date();
		theDay.setTime(theDay.getTime() + (day * 1000 * 60 * 60 * 24));
		cookieData += "expires=" + theDay.toGMTString() + ";";
	}
    document.cookie = cookieData
	return true;
}

function getCookie(key){
	var cookieArr = document.cookie.split(" ");
	var max       = cookieArr.length;
	var oneCookie;
	for(var i = 0; i < max; i++){
		if (key == cookieArr[i].substr(0 , key.length)) {
			var temp = cookieArr[i].split("=");
			temp[1]  = decodeURI(temp[1]);
			if ((temp[1].length -1) == temp[1].lastIndexOf(";")) {
				temp[1] = temp[1].slice(0 , temp[1].length - 1);
			}
			return temp[1];
		}
	}
	return null;
}

function TopPage() {
	DocViewer.SetPage("TOP");
}

function LastPage() {
	var state = DocViewer.GetState();
	if(state == "-") {
		var page = DocViewer.GetPage();
		if(page != null && page != "") {
			DocViewer.SetPage(page);
			return;
		}
	}
	var page = "TOP";
	var s;
	if(cookieDay > 0) {
		s = getCookie(cookieId);
		if(s != null && s != "") {
			page = s;
		}
	}
	DocViewer.SetPage(page);
}

function SetPage(page) {
	DocViewer.SetPage("#"+page);
}


//
//
//

