
var uriEncode =encodeURIComponent;

var aveversion = "1.1.0.3";
var app_name = "";

function ave_launcher(url, bWindow){

	if(url.indexOf("wales_") > 0){
		app_name = "wales";
	}else{
		app_name = "ecamp";
	}

	try{

		var launcher = new ActiveXObject("AveCore2.Launcher");
		launcher.launch(app_name, url, bWindow);


	}catch(e){
		try{

			var launcher = new ActiveXObject("AveCore.Launcher");


			if(launcher.version < aveversion){
				if(confirm("업데이트가 되어 새로 설치하셔야합니다. \n새로 설치하시겠습니까?")){
					ave_download();
				}

				return;
			}

			launcher.launch(url, "", "", bWindow);

		}catch(e)
		{
			ave_download();
		}
	}

}

function ave_download(){
	if(app_name == 'ecamp'){
		location.href="http://totschool.co.kr/ave/release/E-Camp-1.1.0.3-Setup.exe";
	}else{
		location.href="http://totschool.co.kr/ave/release/wales e-learning-"+aveversion+"-Setup.exe";
	}
}

function $ext(dest, src){
	for(var x in src){
		dest[x] = src[x];
	}
	return dest;
}

function RunECampPlus(opt){

	var opt = $ext({
		server : '',
		acid : location.hostname.substr(0,location.hostname.indexOf('.')),
		id : null,
		pw : null,
		window : false
	}, opt);

	var ev = document.createEventObject();
	if(ev.ctrlKey && ev.shiftKey){
		opt.window = true;
	}

	var url = [opt.server,"app?acid=",uriEncode(opt.acid),"&userid=",uriEncode(opt.id),"&password=",uriEncode(opt.pw)].join("");

	ave_launcher(url, opt.window);


}

function RunOldApp(opt){
	var opt = $ext({
		acid : location.hostname.substr(0,location.hostname.indexOf('.')),
		id : null,
		pw : null,
		window : false
	}, opt);

	var ev = document.createEventObject();
	if(ev.ctrlKey && ev.shiftKey){
		opt.window = true;
	}


	var url = ["http://engcamp.org/main.php?acid=",uriEncode(opt.acid),"&userid=",uriEncode(opt.id),"&password=",uriEncode(opt.pw)].join("");
	ave_launcher(url, opt.window);
}



window.attachEvent('onload', function(){
	if(location.search == '?run'){
		RunApp();
	}
});

var g_dark_div;
var g_update_div;
var g_update_timer = false;

function RunApp(usinfo){
	if(g_update_timer)
		return;

	if(!usinfo)
		usinfo = UserInfo;

	if(!usinfo.idx){
		return alert("먼저 로그인을 해주세요.");
	}

	g_update_div = document.createElement("div");
	g_update_div.style.cssText = "position:absolute;left:50%;top:50%;margin-left:-199;margin-top:-125;width:398;height:250;overflow:hidden;z-index:1000";
	g_update_div.innerHTML = '<iframe allowtransparency="true" src="../update?'+
			'acid=' + encodeURIComponent(usinfo.acid)+
			'&id=' + encodeURIComponent(usinfo.id)+
			'&pw=' + encodeURIComponent(usinfo.pw)+
			'&server='+encodeURIComponent(usinfo.server)+
			'&window='+encodeURIComponent(usinfo.window)+
			'&nodelay='+usinfo.nodelay+
			'" frameborder="0" style="width:398;height:250;" onload=TestCall(this)>';
	document.body.appendChild(g_update_div);


	if(!usinfo.nodelay){
		g_dark_div = document.createElement("div");
		g_dark_div.style.cssText="position:absolute;top:0;left:0;width:"+document.body.scrollWidth+";height:"+document.body.scrollHeight+";background-color:black;filter:alpha(opacity=50);z-index:999";
		document.body.appendChild(g_dark_div);
	}

	g_update_timer = setTimeout(function(){
		g_update_timer = 0;
	}, 5000);
}

function CloseUpdate(){
	try{
		document.body.removeChild(g_dark_div);
	}catch(e){};
	document.body.removeChild(g_update_div);
	g_update_timer = 0;

	if(window.after_launcher)
		window.after_launcher();

}

function TestCall(obj){
	obj.contentWindow.CloseUpdate = CloseUpdate;
}

var g_launcher_div;

function OpenLauncher(){

	g_launcher_div = document.createElement("div");
	g_launcher_div.style.cssText = "position:absolute;left:50%;top:50%;margin-left:-310;margin-top:-271;width:620;height:542;overflow:hidden;z-index:1010";
	g_launcher_div.innerHTML = '<iframe allowtransparency="true" src="../launcher" style="width:620;height:542;" frameborder=0>';
	document.body.appendChild(g_launcher_div);

}

function CloseLauncher(){
	document.body.removeChild(g_launcher_div);
}
