var isPluginReady = false;
var siteName = "EWHA";
var objectId = "MagicPass";
var serverUrl = "http://portal.ewha.ac.kr/sso";
var isSSOLogin = "isSSOLogin";
var LOGON = 150;
var LOGOUT_TARGET_PAGE = "/magicsso/Login/App/logout.html";
var timerId;
var loginCheckInterval = 5000;		// ½ºÅ©¸³Æ® ½ÇÇà °£°Ý (milliseconds)

function autoLogout()
{
	if (!isLogin(siteName))
	{
		// MagicPass(S)°¡ ·Î±×¾Æ¿ô µÇ¾úÀ¸¸é ·Î±×¾Æ¿ô Ã³¸®
		clearInterval(timerId);
		alert("MagicPass(SSO)°¡ ·Î±×¾Æ¿ô µÇ¾ú½À´Ï´Ù.");
		//parent.mainPage.location.href = LOGOUT_TARGET_PAGE;
		
		top.document.location.href = LOGOUT_TARGET_PAGE;
	}
}

function isLogin(siteName)
{	
	try
	{
		if (document.getElementById(objectId).GetStatus(siteName) == LOGON)
			return true;
	}
	catch (e)
	{
		// Browser MonitoringÀ¸·Î MagicPass°¡ Á¾·áµÇ¾úÀ» ¼ö ÀÖÀ¸¹Ç·Î try/catch·Î Àâ´Â´Ù.
	}
	return false;
}

function logoutcheck()
{
 if (isPluginReady)
 {
  if (document.getElementById(objectId).GetStatus(siteName) != LOGON)
  {
   return;
  }
  
  timerId = setInterval(autoLogout, loginCheckInterval);  
 }
 else
 {
  window.setTimeout(logoutcheck, 300);
 }
}

function logincheck()
{
	if(isPluginReady)
	{
		try
		{	// MagicPassX°¡ ¼³Ä¡µÇÁö ¾ÊÀº °æ¿ì¸¦ À§ÇØ¼­
			
			// token Á¸Àç ¿©ºÎ Ã¼Å©			
			//alert("session:"+document.getElementById(isSSOLogin).value);
			if (document.getElementById(isSSOLogin).value == 'False')
			{
				if (document.getElementById(objectId).GetStatus(siteName) == 150)
				{
					// ¿¬°è ¼öÇà				
					document.location.href= "/magicsso/Login/App/SSOAppConnect.asp?targetPage=/index.asp&login_flag=false";
				}
				else 
					;//alert("NOT LOGIN..");
			}
			else
				;//alert("VALID SESSION..");
		} 
		catch (e) { }
	}
	else
	{
		window.setTimeout(logincheck, 300);
		return;
	}
}

function createObject()
{
	var ieAppletId = (jQuery.browser["msie"] || jQuery.browser["opera"]) ? objectId : "";
	var netscapeAppletId = (jQuery.browser["msie"] || jQuery.browser["opera"]) ? "" : objectId;
	var clientType = (navigator.platform.indexOf("Win") >= 0) ? "magicpass" : "passport";

	var result = '<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" id="' + ieAppletId + '" width="1" height="1" '
					+ 'codebase="http://java.sun.com/products/plugin/1.2.2/jinstall-1_2_2-win.cab#Version=1,2,2,0">'
					+ '<param name="java_code" value="com.naru.app.applet.PassPortApplet.class"/>'
					+ '<param name="java_codebase" value="' + serverUrl + '/plugins/applet"/>'
					+ '<param name="java_archive" value="passport-applet.jar, passport-j.jar"/>'
					+ '<param name="type" value="application/x-java-applet;version=1.6"/>'
					+ '<param name="browser.type" value="MSIE"/>'
					+ '<param name="client.type" value="' + clientType + '"/>'
					+ '<param name="passport.debug" value="' + this.APPLET_DEBUG_MODE + '"/>'
					+ '<param name="client.resources" value="passport-resource.jar"/>'
					+ '<param name="client.resources.version" value="1.0.0.1"/>'
					+ '<param name="passport-j.resources" value="passport-j-resource.jar"/>'
					+ '<param name="passport-j.resources.version" value="1.0.0.0"/>'
					+ '<param name="isBrowserMonitoring" value="false"/>'
					+ '<param name="interval.browser.monitoring" value="1000"/>'
					+ '<param name="mayscript" value="true"/>'
					+ '<comment>'
					+ '<embed id="' + netscapeAppletId + '" type="application/x-java-applet;version=1.6" width="1" height="1" '
						+ 'pluginspage="http://java.sun.com/products/plugin/" java_code="com.naru.app.applet.PassPortApplet.class" '
						+ 'java_codebase="' + serverUrl + '/plugins/applet" java_archive="passport-applet.jar, passport-j.jar" browser.type="' + this.browserType
						+ '" client.type="' + clientType + '" passport.debug="true" client.resources="passport-resource.jar" '
						+ 'client.resources.version="1.0.0.1" passport-j.resources="passport-j-resource.jar" '
						+ 'passport-j.resources.version="1.0.0.0" isBrowserMonitoring="true" '
						+ 'interval.browser.monitoring="60000" mayscript="true"'
					+ '/>'
					+ '<noembed>'
					+ '</noembed>'
					+ '</comment>'
				+ '</object>';

	if (jQuery.browser["msie"]) {
		result = "<object id='MagicLoaderX' classid='clsid:3D64E58D-CB55-4344-B809-CFE38F900838' width='1' height='1'></object>\n"
			+ "<object id='" + objectId + "' classid='clsid:AD6870C0-44B7-42FB-A119-C2C6BD9CD005' width='1' height='1'></object>\n";

		isPluginReady = true;
	}

	return result;
}

function beforeInit() { return true }
function afterInit() { return true }
function beforeStart() { return true }

function afterStart()
{
	isPluginReady = true;
	return true;
}

$(document).ready(function(){
	var pluginArea = document.getElementById("pluginArea");
	if (!pluginArea)
	{
		var bodyTag = document.getElementsByTagName("body")[0];
		var cDiv = document.createElement("div");
		cDiv.setAttribute("id", "pluginArea");
		bodyTag.appendChild(cDiv);
		
		pluginArea = cDiv;
	}

	pluginArea.innerHTML = createObject();

	logincheck();

	logoutcheck();
});

