// JavaScript Document
// interface: OpenAddTask
// 5-23-2006
// for portal.xunlei.com
// need Thunder Server Version 1.0.1.22 at least

/* Call WebThunder Add Task Panel
** Parameters
** szParam [IN]  string specifies the download url , resource title and refrence url, split by {\r*\r} 
** Return Values
** int
** If call panel successfully  return  0
** If call panel failed        return  1    
** If ThunderServer uninstall  return  2
*/

var thunder_server = null;
var thunder_isOpenNewWindow = 1;
var thunder_type = "";
function OpenAddTask(szParam, sPid)
{
	try
	{
		thunder_server = new ActiveXObject("ThunderServer.webThunder.1");
	}
	catch(e)
	{
		if(thunder_type == "07") sPid = "g" + sPid;
	    if( thunder_isOpenNewWindow == 1)
	    {
			if( sPid != null) {
				//如果合作商是网游官方
				if(thunder_type == "07") {
					window.open("http://my.xunlei.com/setup.htm?gid=" + sPid,"WEBTHUNDER_SET_UP");
				}
				else {
					window.open("http://my.xunlei.com/setup.htm?pid=" + sPid,"WEBTHUNDER_SET_UP");
				}
			}
			else
				window.open("http://my.xunlei.com/setup.htm?pid=","WEBTHUNDER_SET_UP");
	    }
	    else
	    {
			var ainfo = "";
			if( thunder_isOpenNewWindow == 2)
				ainfo = "此链接为迅雷专用下载通道，必须安装WEB迅雷才能进行下载。强烈建议您安装web迅雷，体验急速下载的乐趣！点击确定即刻安装web迅雷";
			else 
				ainfo = "";
			alert(ainfo);
			top.location = "http://ftp.union.sandai.net:1022/webthunder/union/webxl_" + sPid + ".exe";
	    }			
		return 2;
	}
	
	// split the input parameters
	var aryParam = szParam.split("{\r*\r}");
	
	//parameters' length error 
	// call panel failed
	var nParamLen = aryParam.length;
	
	// old page call with 2 parameters
	// new page call with 3 parameters
	if (nParamLen < 2 ||  nParamLen > 3)
		return 1;
		
	var url = "http://my.xunlei.com/add_task.htm";
// for debug
//   	thunder_server.Debug(aryParam);
	
	nVer = get_server_build_version();
	
	// new js call old thunder server
	if (nVer < 24)
	{
		url += "?PUB_XUNLEI{&*&}" + Decode(aryParam[0]) + "{&*&}" + aryParam[1]; 
		thunder_server.SetBrowserWindowData(url,"New Download");
	}
	else
	{	
		var data = Decode(aryParam[0]) + "{\r*\r}" + aryParam[1] + "{\r*\r}";
		
		if (nParamLen == 3)
			data += Decode(aryParam[2]) + "{\r*\r}";
		else
			data += "{\r*\r}";	

		thunder_server.SetVariable("NewTaskData",data);
		thunder_server.SetBrowserWindowData(url,"New Download");
	}
		
	//call panel successfully
	return 0;
}

/* Decode the refrence URL */
function Decode(str)
{
	str = str.replace(/&lt/g,"<");
	str = str.replace(/&gt/g,">");
	str = str.replace(/&amp/g,"&");
	str = str.replace(/&quot/g,"\"");
	str = str.replace(/&apos/g,"\'");
	str = str.replace(/<br>/g,"\n");
	
	return str;
}

/*get server build version*/
function get_server_build_version()
{
	if (thunder_server == null)
		return 0;
	
	var ver = thunder_server.GetVersion();
	ver = ver.split(".");
	return parseInt(ver[3]);
}

/* isOpenNewWindow: 
	1.open new window 
	2.alert gbk message and direct to download web thunder.
	3.alert UTF-8 message and direct to download web thunder.
*/
function OnDownloadClick(sDownloadURL,sResName, sRefPage, sPid, isOpenNewWindow, sType){
	
	if ( isOpenNewWindow != undefined) {
		if(isOpenNewWindow == false)
			thunder_isOpenNewWindow = 2;
		else 
			thunder_isOpenNewWindow = isOpenNewWindow;
	}
	else thunder_isOpenNewWindow = 1;

	if(sType != undefined) {
		thunder_type = sType;
	}

	if ( (sDownloadURL.indexOf("mms://") != -1) || (sDownloadURL.indexOf("rtsp://") != -1 ))
	{
		return true;
	}
	else
	{		
		//sDownloadURL = webThunderDecode(sDownloadURL);
		//sDownloadURL = webThunderEncode(sDownloadURL);
		if ( sPid != null )
			OpenAddTask(sDownloadURL + "{\r*\r}" + sResName + "{\r*\r}" + sRefPage, sPid);
		else
			OpenAddTask(sDownloadURL + "{\r*\r}" + sResName + "{\r*\r}" + sRefPage);
		return false;
	}

}

function OnDownloadClick_Simple(linkObj, isOpenNewWindow)
{
	var sDownload = linkObj.thunderHref;
	var sResName = linkObj.thunderResTitle;
	var sRefPage = location.href;
	var sPid = linkObj.thunderPid;
	var sType = linkObj.thunderType;

	if ( !sResName)
		sResName = "";
		
	ThunderNetwork_UnsetHref(linkObj);
	return OnDownloadClick(sDownload, sResName, sRefPage, sPid, isOpenNewWindow, sType );
}

function ThunderNetwork_SetHref(linkObj)
{
	var tDownloadURL = linkObj.thunderHref;
	//tDownloadURL = webThunderDecode(tDownloadURL);
	//tDownloadURL = webThunderEncode(tDownloadURL);
	linkObj.href = tDownloadURL;
}

function ThunderNetwork_UnsetHref(linkObj)
{
	linkObj.href = "JavaScript:;";
}


