﻿var SBLAUNCHER = null;
try{ SBLAUNCHER = Class.create(); } catch(re) { SBLAUNCHER = new Class(); }
SBLAUNCHER = {
	debugMode : 0,		// 1이면 Exception 출력 처리, null이면 nextExecJS 실행
    obj     : null,
    target  : "self",       //"self":해당창, "opener":부모창
    objAlert: null,			//런처 설치가 안되어 있을 경우 안내메시지 출력 대상, null이면 레이어 띄우기
    nextExecJS: null,		//결과 이벤트를 받은 후에 수행할 자바스크립트 코드
    layerType:null,			//설치안내레이어 종류
    timer : null,

    Load:function() {
		try{
			if(!$('SBLauncher')) {
				var obj = null;
				if(window.ActiveXObject) {	//navigator.userAgent.indexOf("MSIE") > -1
					obj = document.createElement("OBJECT");
					//obj.setAttribute("classid", "CLSID:4FC67D13-9F30-4DA0-B222-59D653B09114");	//v1.0.0.4(v1.5.7)
					obj.setAttribute("classid", "CLSID:61C149B8-0FA9-4f9a-81EF-BE82845FB104");	//v1.0.0.5(v1.7.0)
					obj.id = "SBLauncher";
					obj.style.width = "0px"; obj.style.height = "0px";
					document.getElementsByTagName("head")[0].appendChild(obj);

					try {
						SBEvent.add($('SBLauncher'), "SBLNotify", this.SBLNotify);
					} catch(ex) {
						if($('SBLauncher').addEventListener) $('SBLauncher').addEventListener("SBLNotify", this.SBLNotify, false);
						else if($('SBLauncher').attachEvent) $('SBLauncher').attachEvent("SBLNotify", this.SBLNotify);
					}

				} else {
					obj = document.createElement("EMBED");
					obj.setAttribute("type", "application/x-seebox-launcher");
					obj.setAttribute("SBLNotify", "SBLAUNCHER.SBLNotify");
					obj.id = "SBLauncher";
					obj.style.width = "0px"; obj.style.height = "0px";
					document.body.appendChild(obj);		//EMBED 태그는 body 내에 있어야 한다.
					
					//EMBED 사용시는 SBLNotify 이벤트 처리 함수를 속성으로 지정함.
				}

			}
		} catch(e){
			this.ExceptionProc("SBLAUNCHER.Load::", e);
		}
    },
    Check:function(objAlert, nextExecJS, layerType) {	//layerType : 설치안내레이어 종류
		//Mozilla에서는 지원안됨. Pass!!!
		if(navigator.userAgent.indexOf("MSIE") < 0) {
			if(nextExecJS) eval(nextExecJS);
			return;
		}
		this.Load();

        this.obj = $('SBLauncher');
        if(objAlert) this.objAlert = objAlert;
        else this.objAlert = null;
        if(nextExecJS) this.nextExecJS = nextExecJS;
        if(layerType) this.layerType = layerType;
//this.Log("getCookie('update')="+getCookie("update"));
	    //check update
	    if(getCookie("update") != "Y") {
		    this.CheckUpdate();
	    } else {
			if(this.nextExecJS) {
//this.Log("nextExecJS="+this.nextExecJS);
				eval(this.nextExecJS);
			}
	    }
    },
    CheckUpdate:function() {
	    try {
//this.Log("this.obj.Launch('SeeBox Updater', '"+activeXInstallURL+"', 4)");
            this.obj.Launch("SeeBox Updater", activeXInstallURL, 4);
            //this.obj.Launch("SeeBox Updater", "Handle=\"%u\", CfgXML=\""+activeXServerURL+"SeeBox_Main.xml\", SharedXML=\""+activeXServerURL+"SharedLib_Main.xml\"", 4);

			//아래 코드는 실제로 대용량 다운로드시 결과이벤트를 받기까지 시간이 걸릴 수 있는
			//내재 오류를 포함하고 있으므로 특이한 경우를 제외하고 사용금지, 2008-06-27, soo@
			//if(this.nextExecJS) this.timer = setTimeout("SBLAUNCHER.CheckReceiveEvent()", 3000);
	    } catch(e) {
			//통합매니저 설치안내 레이어 띄우기
			this.PopInstallInfoLayer();
	    }
    },
    PopInstallInfoLayer:function() {
		try{
			if(this.layerType==3) {	//이벤트페이지용 설치안내 레이어
				var infoL = $("SBLauncherInstallInfoLayer");
				var l = document.createElement("DIV");
				l.id = "SBLauncherInstallInfoLayer";
				l.style.backgroundImage = "url(http://img.seebox.sg/sgp/tmanager/totalm_bg02.gif)";
				l.style.width = "340px";
				l.style.height = "160px";
				//중앙
				var lW = null; var lH = null;
				lW = getElementWidth($(this.objAlert));
				lH = getElementHeight($(this.objAlert));
				$(this.objAlert).style.position = "relative";
				l.style.position = "absolute";
				l.style.top	= (Math.round(lH/2) - 80) + "px";
				l.style.left= (Math.round(lW/2) - 170) + "px";
				l.innerHTML = this.GetInstallLayerHTML(3);
				$(this.objAlert).innerHTML = "";
				$(this.objAlert).appendChild(l);
			
			} else {
				if(this.objAlert && $(this.objAlert)) {
					var l = document.createElement("DIV");
					l.id = "SBLauncherInstallInfoFrame";
					var lW = null; var lH = null;
					if($(this.objAlert).tagName == "BODY") {
						window.onresize = null;
						lW = this.GetWindowSize("WIDTH");
						lH = this.GetWindowSize("HEIGHT");
					} else {
						lW = getElementWidth($(this.objAlert));
						lH = getElementHeight($(this.objAlert));
					}
					//alert("lW="+lW+", lH="+lH);
					//l.setAttribute("width", lW);
					//l.setAttribute("height",lH);
					l.style.width = lW + "px";
					l.style.height= lH + "px";
					var ltype = this.layerType ? this.layerType : 1;
					l.innerHTML = this.GetInstallLayerHTML(ltype);
					$(this.objAlert).innerHTML = "";
					$(this.objAlert).appendChild(l);

				} else {
					var infoL = $("SBLauncherInstallInfoLayer");
					if(!infoL) {
						var l = document.createElement("DIV");
						l.id = "SBLauncherInstallInfoLayer";
						l.style.backgroundImage = "url(http://img.seebox.sg/sgp/tmanager/totalm_bg02.gif)";
						l.style.width = "340px";
						l.style.height = "160px";
						//화면중앙
						l.style.position = "absolute";
						l.style.top	= (Math.round(this.GetWindowSize("HEIGHT")/2) - 80) + "px";
						l.style.left= (Math.round(this.GetWindowSize("WIDTH")/2) - 170) + "px";
						var ltype = this.layerType ? this.layerType : 2;
						l.innerHTML = this.GetInstallLayerHTML(ltype);
						document.body.appendChild(l);
					} else {
						infoL.style.top	= (Math.round(this.GetWindowSize("HEIGHT")/2) - 80) + "px";
						infoL.style.left= (Math.round(this.GetWindowSize("WIDTH")/2) - 170) + "px";
						infoL.style.display = "";
					}
				}
			}
		} catch(e) {
			this.ExceptionProc("SBLAUNCHER.PopInstallInfoLayer::", e);
		}
    },
	GetInstallLayerHTML:function(type){
		var retHtml = "";
		switch(parseInt(type)){
			case 1:
				retHtml = 
					 "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\" height=\"100%\">"
					+"	<tr><td align=\"center\" valign=\"middle\"><table width=\"560px\" height=\"420px\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" background=\"http://img.seebox.sg/sgp/tmanager/totalm_bg01.gif\">"
					+"			<tr><td>"
					+"				<div style=\"margin:145px 0 0 0\"><img src=\"http://img.seebox.sg/sgp/tmanager/bt_foundation.gif\" alt=\"설치\" onclick=\"SBLAUNCHER.GoInstall();\" style=\"cursor:pointer\" width=\"85\" height=\"22\" border=\"0\" /><img src=\"http://img.seebox.sg/sgp/tmanager/bt_cancel.gif\" alt=\"취소\" onclick=\"SBLAUNCHER.Cancel();\" style=\"cursor:pointer\" width=\"85\" height=\"22\" border=\"0\" /></div>"
					+"			</td></tr></table>"
					+"	</td></tr></table>";
				break;
			case 2:
				retHtml = 
					 "<div style=\"margin:53px 0 0 15px;font-family:Dotum,돋움; color:#CCCCCC;font-size:11px;line-height:125%;\">"
					+"	You should install Total Manager provided by SEEBOX <br/>"
					+"	to use VOD and LIVE Service of SEEBOX.<br/>"
					+"	<span style=\"color:#CCFF33\">In case where it is not installed, you will have a limited access in the service.</span><br/>"
					+"	</div><div align=\"center\" style=\"padding:26px 3px 0 0;\">"
					+"		<img src=\"http://img.seebox.sg/sgp/tmanager/bt_foundation.gif\" alt=\"설치\" onclick=\"SBLAUNCHER.GoInstall();\" style=\"cursor:pointer\" border=\"0\" />"
					+"		<img src=\"http://img.seebox.sg/sgp/tmanager/bt_cancel.gif\" alt=\"취소\" onclick=\"if($('SBLauncherInstallInfoLayer')){$('SBLauncherInstallInfoLayer').style.display='none';}\" style=\"cursor:pointer\" border=\"0\" />"
					+"</div>";
				break;
			case 3:
				retHtml = 
					 "<div style=\"margin:53px 0 0 15px;font-family:Dotum,돋움; color:#CCCCCC;font-size:11px;line-height:125%;\">"
					+"	You should install Total Manager provided by SEEBOX <br/>"
					+"	to use VOD and LIVE Service of SEEBOX.<br/>"
					+"	<span style=\"color:#CCFF33\">In case where it is not installed, you will have a limited access in the service.</span><br/>"
					+"	</div><div align=\"center\" style=\"padding:26px 3px 0 0;\">"
					+"		<img src=\"http://img.seebox.sg/sgp/tmanager/bt_foundation.gif\" alt=\"설치\" onclick=\"SBLAUNCHER.GoInstall();\" style=\"cursor:pointer\" border=\"0\" />"
					+"</div>";
				break;
		}
		return retHtml;
	},
    SetCookieChecked:function() {
        setCookie("update", "Y", "seebox."+hostDomain);
    },
    GoInstall:function() {
		var url = "http://"+webRoot+"www.seebox."+hostDomain+"/help/help_manager.sb";
		if(this.target == "opener") {
			if(opener) opener.location.href = url;
			else window.open(url, '', '');
			this.WinClose();
		} else if(this.target == "_blank") {
			window.open(url, '', '');
			this.WinClose();
		} else {
			location.href = url;
		}
    },
    Cancel:function() {
		if(this.target == "opener") {
			this.WinClose();
		} else {
			if(history.length > 0)	history.back();
			else goHome();
		}
    },
    WinClose:function() {
		if(navigator.appVersion.indexOf("MSIE 7.0")>=0) {
			window.open('about:blank','_self').close();
		} else {
			window.opener = self;
			self.close();
		}
    },
    GetWindowSize:function(wh) {
		var lW, lH;
		if(window.innerWidth) {
			lW = window.innerWidth;
			lH = window.innerHeight;
		} else if(document.documentElement && document.documentElement.clientWidth) {
			lW = document.documentElement.clientWidth;
			lH = document.documentElement.clientHeight;
		} else if(document.body.clientWidth) {
			lW = document.body.clientWidth;
			lH = document.body.clientHeight;
		}
		if (wh=="WIDTH") return lW;
		else return lH;
    },
    ExceptionProc:function(str, e) {
		if(this.debugMode) {
			if(e) str += ":" + e.message;
			this.Log(str);
		} else if(this.nextExecJS) {
			eval(this.nextExecJS);
		}
    },
    Log:function(str) {
		if(!this.debugMode) return;
		if(!$('LauncherLogBox')) {
			//document.write("<div id=\"LauncherLogBox\" style=\"position:absolute;top:5px;left:5px; height:100px; background-color:#fff; border:solid 1px #9a9a9a; overflow-y:scroll; z-index:1000;\" ondblclick=\"this.innerHTML=''\" title=\"더블클릭하면 내용이 지워집니다\"></div>");
			var objLC = document.createElement("DIV");
			objLC.id = "LauncherLogBox";
			objLC.style.position = "absolute";
			objLC.style.top	= "5px";
			objLC.style.left = "5px";
			objLC.style.height = "100px";
			objLC.style.border = "solid 1px #9a9a9a";
			objLC.style.backgroundColor = "#fff";
			objLC.style.overflowY = "scroll";
			objLC.style.zIndex = "1000";
			objLC.ondblclick = function(){ this.innerHTML = ""; }
			document.body.appendChild(objLC);
		}
		var s = document.createElement("SPAN");
		s.innerHTML = str + "<br/>";
		$('LauncherLogBox').insertBefore(s, $('LauncherLogBox').firstChild);
    },
    //일정시간까지 SBLNotify 이벤트를 못 받은 경우, 예정 스크립트 실행
    CheckReceiveEvent:function() {
		if(this.timer != null && getCookie("update") != "Y" && this.nextExecJS) {
			eval(this.nextExecJS);
			this.timer = null;
			//this.Log("SBLAUNCHER.CheckReceiveEvent() 실행!!!");
		}
    },
    SBLNotify:function(code, param) {
//SBLAUNCHER.Log("SBLNotify:::code="+code+", param="+param);
		try{

			if(SBLAUNCHER.timer) {
				clearTimeout(SBLAUNCHER.timer);
				SBLAUNCHER.timer = null;
			}
			//SBLAUNCHER.ExceptionProc("SBLNotify::code="+code+", param="+param);
			//code = 0:업데이트 실패, 1:성공, 2:자체 업데이트 수행한 후 다시 실행(?)
			if(code == 1) {
				SBLAUNCHER.SetCookieChecked();
				//window.location.reload();
			}
			/* 업데이터가 자체 수행함.
			else if(code == 2) {
				//SBLAUNCHER.CheckUpdate();
				setTimeout("SBLAUNCHER.CheckUpdate()", 5000);
				return;
			}
			*/
			if(SBLAUNCHER.nextExecJS) {
				eval(SBLAUNCHER.nextExecJS);
				//해결됨(v1.5.0824.2) => window.open 같은 경우 "응용 프로그램이 입력 동기화된 호출을 전달하고 있으므로 나가는 호출을 할 수 없습니다." 오류.
				//setTimeout(SBLAUNCHER.nextExecJS, 1);
				//SBLAUNCHER.Log("SBLNotify:::nextExecJS="+SBLAUNCHER.nextExecJS);
			}
		} catch(ex) {
			//if(SBLAUNCHER.debugMode) SBLAUNCHER.Log("SBLAUNCHER.SBLNotify::"+ex.message);
		}
    }
}


//OCX Load
//SBLAUNCHER.Load();

