/*
Copyright (c) 2007, SK Communications. All rights reserved.
File : quickLaunch.js
Abstract : register, download and execute nateon quickLaunch
Author :  dongwoo kim (rer44@nate.com)
 */

function quickLaunch() {
	this.nateon = "";
	this.sid 	= "";
	this.guid 	= "";
	this.sdesc 	= "";
	this.statName 	= "";
	this.start	= false;
	this.error	= false;
}

quickLaunch.prototype = {
	startNateOn : function() {
		if("false" == this.start.toString()) {
			this.nateon.StartNateOn();
			this.start = true;
		}
	},

	setNateon : function(inputObject) {
		this.nateon = inputObject;
	},

	setSID: function(inputSID) {
		this.sid 	= inputSID;
		this.guid 	= this.getGUID(this.sid);
		this.sdesc 	= this.getDESC(this.sid);
	},

	setStatName : function(inputStatName) {
		this.statName = inputStatName;
	},

	errorOccur : function() {
		this.error = true;
	},

	registerQuickLaunch : function(webCMN) {
		
		nateonCMN = this.nateon.GetMyInfo("my:cmn");

		//alert("webCMN : [" + webCMN + "] number : [" + Number(webCMN) + "]");
		//alert("nateonCMN : [" + nateonCMN + "] number : [" + Number(nateonCMN) + "]");

		if(Number(webCMN) != Number(nateonCMN)) return "300"; // ´Ù¸¥ ¾Æµð ·Î±×ÀÎ

		var availableResult;
		var registerResult;
		availableResult = this.nateon.IsServiceAvailable(this.sid);

		if("-1" == availableResult) return "500"; // ¼³Ä¡µµÁß ¿À·ù
		else if ("0" == availableResult) return "301"; // Àç·Î±×ÀÎ
		else if("1" == availableResult) return "201"; // ÀÌ¹Ì ¼³Ä¡
		else if("2" == availableResult) {
			
			registerResult = this.nateon.RegisterServiceToQR(this.sid);

			if("-1" == availableResult) return "500"; // ¼³Ä¡µµÁß ¿À·ù
			else if ("0" == availableResult) return "301"; // Àç·Î±×ÀÎ
			else if("1" == availableResult) return "201"; // ÀÌ¹Ì ¼³Ä¡

			this.channelStat("reg_quick");

			return "200"; // Á¤»ó ¼³Ä¡
		}
	},

	checkNateOn : function() {
		
		if (!this.nateon.CheckInstallStatus()) return "302"; // nateon ¹Ì¼³Ä¡
		if (!this.nateon.CheckValidVersion(600)) return "303"; // ³Ê¹« ³·Àº nateon ¹öÀü

		var ret = this.nateon.CheckLoginStatus();
		if (ret == "0") return "304"; // Off-Line
		else if (ret == "100") return "305"; // Not Running
		return "202";
	},

	installQuickLaunch : function() {
		
		//ÇÃ·¯±×ÀÎ ½ÇÇà¿©ºÎ Ã¼Å©
		var ret = this.nateon.GetPluginStatus(this.guid);
		var chkInstall;
		var chkUpgrade;

		if(!(ret & 2)) return "404"; // plugin not exist
		if(!(ret & 4)) { //not installed
			
			//¼³Ä¡¸¦ À§ÇØ CheckPlugin
			this.nateon.PopupAnyWindow("CheckPlugin", this.guid, "");

			do
			{	//¼³Ä¡°¡ ¿Ï·áµÉ¶§±îÁö Ã¼Å©
				chkInstall = this.nateon.GetPluginStatus( this.guid );
			} while(!(chkInstall & 4));
			
			return "204"; // install done
		}
		else if(ret & 16) {//need upgrade
			
			//Upgrade¸¦ À§ÇØ CheckPlugin
			this.nateon.PopupAnyWindow("CheckPlugin", this.guid, "");

			do
			{	//¼³Ä¡°¡ ¿Ï·áµÉ¶§±îÁö Ã¼Å©
				chkUpgrade = this.nateon.GetPluginStatus( this.guid );
			} while(chkUpgrade & 16);
			
			return "204"; // install done
		}
		
		if(ret & 8) return "203"; // ÀÌ¹Ì ½ÇÇà
		if(ret & 5) return "203"; // ÀÌ¹Ì ¼³Ä¡

		return "500"; // »ó´ã½Ç·Î ¹®ÀÇ
	},

	runPluginQuickLaunchWithoutParam : function() {
		
		this.nateon.PopupAnyWindow("RunPlugin", this.guid, "");
		this.channelStat("exec");
	},

	runPluginQuickLaunchWithSID : function(param) {
		
		this.nateon.PopupAnyWindow("ServiceRun", this.sid, "");
		this.channelStat("exec");
	},

	getGUID : function(inputSID) {
		
		if("2" == inputSID) {
			return "D3D97AF7-1B96-4992-BFF1-BF2D36852DE4";
		}
		else if("3" == inputSID) {
			return "965AC526-B52C-4846-9A22-DAAAAE76E9CA";
		}
		else if("5" == inputSID) {
			return "37A90507-CF63-49D8-8E1E-F32E4A869CB8";
		}
		else if("12" == inputSID) {
			return "54E70798-B9FE-4e74-83F9-B11E1DE1DEF7";
		}
		else if("17" == inputSID) {
			return "E3429F32-D8FA-47F6-A670-68730E4C7A65";
		}
		else if("18" == inputSID) {
			return "08880AE9-EC64-46cf-B1E1-42A035CEA8AE";
		}
		else if("26" == inputSID) {
			return "B8BB4352-B498-451f-9F91-671688637C41";
		}
		else if("32" == inputSID) {
			return "99B558F3-D936-4866-9666-141E0E759023";
		}
		else if("33" == inputSID) {
			return "7143B70F-313F-4DFD-99A7-9AE7DBFE915C";
		}
		else if("35" == inputSID) {
			return "3F0C46E4-3784-4744-8239-B90679E254D4";
		}
		else if("66" == inputSID) {
			return "882E51CC-12C8-4524-8482-CF72D567B472";
		}
		else {
			return "";
		}
	},
	getDESC : function(inputSID) {
		
		if("2" == inputSID) {
			return "¹®ÀÚ´ëÈ­";
		}
		else if("3" == inputSID) {
			return "³×ÀÌÆ®¿Â Æù";
		}
		else if("5" == inputSID) {
			return "ÅëÇÕ¸Þ½ÃÁöÇÔ";
		}
		else if("11" == inputSID) {
			return "ÆÅ½º¿Â";
		}
		else if("12" == inputSID) {
			return "¸Þ¸ðÂ¯";
		}
		else if("16" == inputSID) {
			return "½ÅÇÑ ¸Þ½ÅÀú ¹ðÅ·";
		}
		else if("17" == inputSID) {
			return "³×ÀÌÆ®¿Â ¹Ì´Ï¹ðÅ©";
		}
		else if("18" == inputSID) {
			return "KBS ¶óµð¿À Äá";
		}
		else if("20" == inputSID) {
			return "¸ð³×Å¸ ¹Ì´Ï °¡°èºÎ";
		}
		else if("21" == inputSID) {
			return "¸Þ½ÃÁö ¸Å´ÏÀú";
		}
		else if("22" == inputSID) {
			return "¹®ÀÚ ¸Å´ÏÀú";
		}
		else if("23" == inputSID) {
			return "±âÇÁÆ¼ÄÜ";
		}
		else if("25" == inputSID) {
			return "³×ÀÌÆ®¿Â tossi";
		}
		else if("26" == inputSID) {
			return "CBS ¶óµð¿À ·¹ÀÎº¸¿ì";
		}
		else if("31" == inputSID) {
			return "¿¥ÆÄ½º ¹Ì´Ï»çÀü";
		}
		else if("32" == inputSID) {
			return "SBS¶óµð¿À °í¸±¶ó";
		}
		else if("33" == inputSID) {
			return "½ºÅ¹¿Â";
		}
		else if("35" == inputSID) {
			return "³×ÀÌÆ® Áö½Ä";
		}
		else if("59" == inputSID) {
			return "³×ÀÌÆ® ÇÁ·Î¾ß±¸ Live";
		}
		else if("66" == inputSID) {
			return "UFOÅ¸¿î Fly";
		}		
		else {
			return "";
		}
	},
	getErrorString : function(errorCode) {
		
		if("200" == errorCode) {
			return this.sdesc + " Äü·±Ä¡°¡ ³×ÀÌÆ®¿Â¿¡ µî·ÏµÇ¾ú½À´Ï´Ù.";
		}
		else if("201" == errorCode) {
			return this.sdesc + " ³×ÀÌÆ®¿Â Äü·±Ä¡¿¡ ÀÌ¹Ì µî·ÏµÇ¾îÀÖ½À´Ï´Ù.";
		}
		else if("202" == errorCode) {
			return "³×ÀÌÆ®¿ÂÀÌ µ¿ÀÛÁßÀÔ´Ï´Ù.";
		}
		else if("203" == errorCode) {
			return this.sdesc + " ÇÃ·¯±×ÀÎÀÌ ³×ÀÌÆ®¿Â¿¡ ÀÌ¹Ì ¼³Ä¡µÇ¾î ÀÖ½À´Ï´Ù.";
		}
		else if("20" == errorCode) {
			return this.sdesc + " ÇÃ·¯±×ÀÎÀÌ ³×ÀÌÆ®¿Â¿¡ ¼³Ä¡µÇ¾î ÀÖ½À´Ï´Ù.";
		}
		else if("300" == errorCode) {
			return "³×ÀÌÆ®¿Â¿¡ ·Î±×ÀÎÇÏÁö ¾ÊÀ¸¼Ì°Å³ª ´Ù¸¥ ¾ÆÀÌµð·Î ·Î±×ÀÎ ÁßÀÌ½Ê´Ï´Ù.\n" + this.sdesc + " Äü·±Ä¡¸¦ µî·ÏÇÏ±â À§ÇØ¼­ ³×ÀÌÆ®¿Â¿¡ ´Ù½Ã ·Î±×ÀÎÇØ ÁÖ¼¼¿ä.\n³×ÀÌÆ®¿Â Àç ·Î±×ÀÎ ÈÄ¿¡µµ ÀÌ ¸Þ½ÃÁö°¡ º¸ÀÏ °æ¿ì\nÇöÀç ÆäÀÌÁö¸¦ »õ·Î°íÄ§(F5) ÇØÁÖ¼¼¿ä.";
		}
		else if("306" == errorCode) {
			// Äü·±Ä¡ µî·Ï + ¼³Ä¡ + ½ÇÇàÀÎ °æ¿ìÀÇ ¸Þ½ÃÁö(2008.12.15 happymiya ½ºÅ¹¿Â ¸Þ½ÃÁö º¯°æ ¿äÃ»À¸·Î º¯°æ)
			return "³×ÀÌÆ®¿Â¿¡ ·Î±×ÀÎÇÏÁö ¾ÊÀ¸¼Ì°Å³ª ´Ù¸¥ ¾ÆÀÌµð·Î ·Î±×ÀÎ ÁßÀÌ½Ê´Ï´Ù.\n" + this.sdesc + " ½ÇÇàÀ» À§ÇØ¼­ ³×ÀÌÆ®¿Â¿¡ ´Ù½Ã ·Î±×ÀÎÇØ ÁÖ¼¼¿ä.\n³×ÀÌÆ®¿Â Àç ·Î±×ÀÎ ÈÄ¿¡µµ ÀÌ ¸Þ½ÃÁö°¡ º¸ÀÏ °æ¿ì\nÇöÀç ÆäÀÌÁö¸¦ »õ·Î°íÄ§(F5) ÇØÁÖ¼¼¿ä.";
		}
		else if("301" == errorCode) {
			return this.sdesc + "³o·±Ä¡ µî·Ï¸¦ À§ÇØ¼­ ³×ÀÌÆ®¿ÂÀ» Àç·Î±×ÀÎ ÇØÁÖ¼¼¿ä.";
		}
		else if("302" == errorCode) {
			return "³×ÀÌÆ®¿ÂÀÌ ¼³Ä¡µÇÁö ¾Ê¾Ò½À´Ï´Ù.";
		}
		else if("303" == errorCode) {
			return "³×ÀÌÆ®¿Â ¹öÀüÀ» ¾÷±×·¹ÀÌµå ÇØ ÁÖ¼¼¿ä.";
		}
		else if("304" == errorCode) {
			return "ÇöÀç ¿ÀÇÁ¶óÀÎ »óÅÂÀÔ´Ï´Ù.";
		}
		else if("305" == errorCode) {
			return "³×ÀÌÆ®¿ÂÀÌ ½ÇÇàµÇÁö ¾Ê¾Ò½À´Ï´Ù.";
		}
		else if("404" == errorCode) {
			return "Á¸ÀçÇÏÁö ¾Ê´Â ÇÃ·¯±×ÀÎÀÔ´Ï´Ù.";
		}
		else {
			//500
			return "¼³Ä¡µµÁß ¿À·ù°¡ ¹ß»ýÇÏ¿´½À´Ï´Ù.³×ÀÌÆ®¿Â »ó´ã½Ç·Î ¹®ÀÇÇØÁÖ¼¼¿ä.";
		}
	},

	channelStat : function(actionType) {
		
		statTag = document.getElementById(this.statName);
		statTag.innerHTML =
			"<img src = 'http://nateon.nate.com/external/channel/stat/channel_stat.php?sid=" + this.sid + "&actiontype=" + actionType + "&from=web' width = 0 height = 0>";
	}

};

quickLaunch = new quickLaunch();

//ActiveX error
function onNateOnApiAtxError()
{
	quickLaunch.errorOccur();
}

function nateLogin(callbackURL)
{
	if("" == callbackURL) {
		var thisURL = document.location.href;
		document.location = 'http://nateonweb.nate.com/login/loginfull.php?rurl=' + thisURL;
	}
	else {
		document.location = 'http://nateonweb.nate.com/login/loginfull.php?rurl=' + callbackURL;
	}
	return;
}

// accepted flag case = 3
// registerFlag 	installFlag	executeFlag
// true, 		false, 	do't care (acceptable)
// true, 		true, 		false (acceptable)
// true, 		true, 		true (acceptable)
function runQuickLaunch(inputObject, inputSID, webCMN, registFlag, installFlag, executeFlag, rurl, statName) {

	quickLaunch.setNateon(inputObject);
	quickLaunch.setSID(inputSID);
	quickLaunch.setStatName(statName);
	quickLaunch.startNateOn();

	if("" == webCMN || "0" == webCMN) {
		nateLogin(rurl);
		return;
	}

	if(true == registFlag && false == installFlag && false == executeFlag) {
		resultCode = quickLaunch.registerQuickLaunch(webCMN);
		alert(quickLaunch.getErrorString(resultCode));
		return;
	}

	if(true == registFlag && false == installFlag && true == executeFlag) {
		resultCode = quickLaunch.registerQuickLaunch(webCMN);

		if("200" != resultCode && "201" != resultCode) {
			alert(quickLaunch.getErrorString(resultCode));
			return;
		}

		// ¿¹¿Ü KBS Äá, ¸ð³×Å¸ ¹Ì´Ï °¡°èºÎ, ¸Þ½ÃÁö¸Å´ÏÀú(KTF), ¹®ÀÚ¸Å´ÏÀú(SKT), ±âÇÁÆ¼ÄÜ
		if("11" == quickLaunch.sid || "13" == quickLaunch.sid || "14" == quickLaunch.sid || "18" == quickLaunch.sid || "20" == quickLaunch.sid || "21" == quickLaunch.sid || "22" == quickLaunch.sid || "23" == quickLaunch.sid || "25" == quickLaunch.sid || "26" == quickLaunch.sid || "31" == quickLaunch.sid || "32" == quickLaunch.sid || "33" == quickLaunch.sid || "35" == quickLaunch.sid || "59" == quickLaunch.sid ) {
			quickLaunch.runPluginQuickLaunchWithSID();
		}
		else {
			quickLaunch.runPluginQuickLaunchWithoutParam();
		}
	}

	if(true == registFlag && true == installFlag && false == executeFlag) {
		resultCode = quickLaunch.registerQuickLaunch(webCMN);
		
		if("200" != resultCode && "201" != resultCode) {
			
			alert(quickLaunch.getErrorString(resultCode));
			return;
		}
		
		//2008.07.14 eunjung
		if("201" == resultCode){
			alert(quickLaunch.getErrorString(resultCode));
		}
		
		resultCode = quickLaunch.checkNateOn();
		if("202" != resultCode) {
			alert(quickLaunch.getErrorString(resultCode));
			return;
		}
		resultCode = quickLaunch.installQuickLaunch();
		
		if("203" != resultCode && "204" != resultCode) {
			
			alert(quickLaunch.getErrorString(resultCode));
			return;
		}
		
		return;
	}
	if(true == registFlag && true == installFlag && true == executeFlag) {
				
		resultCode = quickLaunch.registerQuickLaunch(webCMN);
		
		if("200" != resultCode && "201" != resultCode) {
			
			if("300" == resultCode)
				resultCode = "306";
			
			alert(quickLaunch.getErrorString(resultCode));
			return;
		}

		resultCode = quickLaunch.checkNateOn();


		if("202" != resultCode) {
			alert(quickLaunch.getErrorString(resultCode));
			return;
		}

		if("11" == quickLaunch.sid || "13" == quickLaunch.sid || "14" == quickLaunch.sid || "20" == quickLaunch.sid || "23" == quickLaunch.sid || "25" == quickLaunch.sid || "31" == quickLaunch.sid|| "59" == quickLaunch.sid) ;
		else {
			resultCode = quickLaunch.installQuickLaunch();
			if("203" != resultCode && "204" != resultCode) {
				alert(quickLaunch.getErrorString(resultCode));
				return;
			}
		}

		// ¿¹¿Ü KBS Äá, ¸ð³×Å¸ ¹Ì´Ï °¡°èºÎ, ¸Þ½ÃÁö¸Å´ÏÀú(KTF), ¹®ÀÚ¸Å´ÏÀú(SKT), ±âÇÁÆ¼ÄÜ

		if("11" == quickLaunch.sid || "13" == quickLaunch.sid || "14" == quickLaunch.sid || "18" == quickLaunch.sid || "20" == quickLaunch.sid || "21" == quickLaunch.sid || "22" == quickLaunch.sid || "23" == quickLaunch.sid || "25" == quickLaunch.sid || "26" == quickLaunch.sid || "31" == quickLaunch.sid || "32" == quickLaunch.sid || "33" == quickLaunch.sid|| "35" == quickLaunch.sid|| "59" == quickLaunch.sid) {
			quickLaunch.runPluginQuickLaunchWithSID();
		}
		else {
			quickLaunch.runPluginQuickLaunchWithoutParam();
		}
	}
}
