var g_arrMainTypes = new Array;
var g_arrTypeOrder = new Array;
var g_arrRecommend = new Array;

var g_sIconBase16 = "/pic/16_";
var g_sIconBase32 = "/pic/32_";
var g_sIconBase = g_sIconBase16;
var g_sXmlDir = "../DrData/"

var g_arrSoftClubSummary = new Array;

var g_TimeOut  = 500; //ms
var g_TimeInterval = 50;
var g_MouseStopTime = 0;

var g_DelayID;

var g_LastEventIndex = -1;
var g_LastEventVer   = -1;

var g_MouseX = 0;
var g_MouseY = 0;

var g_CallBackFun;

function  CSoftInfo(index)
{
	this.Index       = index;
	this.DisplayName = "";
	this.Name        = "";
	this.Desc        = "";
	this.MainType    = "";
	this.Recommend   = -1;
	this.Status      = 0;
	this.Selected    = false;	// add 中处理
	this.TypeIndex   = -1;		// add 中处理
	this.OneKey      = -1;
	this.Popularity  = -1;
	this.CurrentVer  = "";
	this.VersionInfo = new Array;
	this.VersionInfo[0] = new CVersionInfo();
	this.VersionInfo[1] = new CVersionInfo();
	
	this.TotalJudgeofMark	 	= 0;
	this.TotalJudgeofTimes 	= 0;
	this.TotalDownloadTimes	= 0;
	this.ClubCount				= 0;
	this.Clubs				= new Array;
}

function CVersionInfo()
{
	this.Value       = "";
	this.DisplayVer  = "";
	this.CID         = "";
	this.Size        = 0;
	this.IsLast      = "";
	this.VerAttrib   = "";
	this.Whatsnew    = "";
	this.URL         = "";
	this.RegKey      = "";
	this.Plugin      = 0;
	this.SetupCmd    = "";
}

function CClubInfo()
{
	this.id				 = "";
	this.title			 = "";
}

function GetTypeIndex(info)
{
	iType = info.TypeIndex;
	if(g_arrSoftTable[iType]==undefined)
		g_arrSoftTable[iType] = new CSoftTypeInfo;
	return info.TypeIndex;
}

function InitClubStringInfo(sXmlText)
{
	var xmlDoc, index;
	xmlDoc = InitXml();
	
	LoadStringXml(xmlDoc, sXmlText);
	GetXmlClubInfo(xmlDoc);
}

function InitClubInfo(sXml)
{
	var xmlDoc, index;
	xmlDoc = InitXml();
	
	LoadXml(xmlDoc, sXml);
	GetXmlClubInfo(xmlDoc);
}

function GetXmlClubInfo(xmlDoc)
{
	var currNode = xmlDoc.documentElement.childNodes[0];
	var node, nodeTmp;

	node = xmlDoc.documentElement.selectSingleNode("//softbaike");
	for(var i = 0; i < node.childNodes.length; ++i)
	{
		currNode = node.childNodes[i];
		if (currNode.nodeName.substr(0, "club".length) == "club")
		{
			var softwarename;
			var clubs = new Array;
		　for(var j = 0; j < currNode.childNodes.length; ++j)
			{
				switch(currNode.childNodes[j].nodeName)
				{
				case "club_name":
					softwarename = currNode.childNodes[j].text;
					break;
				case "post":
					nodeTmp = currNode.childNodes[j];
					var info  = new CClubInfo();
					info.id      = nodeTmp.getAttribute("post_id");
					for(var k = 0; k < nodeTmp.childNodes.length; ++k)
					{
						switch(nodeTmp.childNodes[k].nodeName)
						{
						case "post_title":
							info.title = nodeTmp.childNodes[k].text;
							break;
						}
					}
					clubs.push(info);
					break;
				}
			}
			g_arrSoftClubSummary [softwarename] = clubs;
		}
	}
}

function InitXml()
{
	xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	xmlDoc.async = false;
	return xmlDoc;
}

function LoadStringXml(xmlDoc, sXmlText)
{
	xmlDoc.loadXML(sXmlText);
	if (xmlDoc.parseError.errorCode != 0)
	{
		// 如果解析 XML 文件出错
		var err = xmlDoc.parseError;
		//alert("You have error " + err.reason);
	}
	xmlDoc.setProperty("SelectionLanguage", "XPath");
}

function LoadXml(xmlDoc, sXml)
{
	xmlDoc.load(sXml);
	if (xmlDoc.parseError.errorCode != 0)
	{
		// 如果解析 XML 文件出错
		var err = xmlDoc.parseError;
		//alert("You have error " + err.reason);
	}
	xmlDoc.setProperty("SelectionLanguage", "XPath");
}

// 需要兼容的 AddSoft(info) 函数
function GetXmlSoftInfo(xmlDoc, funcAddSoft, index0)
{
	var currNode = xmlDoc.documentElement.childNodes[0];
	var index = index0;
	var iType;
	var node, nodeTmp;

	node = xmlDoc.documentElement.selectSingleNode("//bibei/main_type_list");
	for(var k = 0; k < node.childNodes.length; ++k)
	{
		if(node.childNodes[k].nodeName == "type")
		{
			nodeTmp = node.childNodes[k];
			iType = nodeTmp.getAttribute("id");
			g_arrTypeOrder[k] = iType;
			g_arrRecommend[iType] = "";
			for(var l = 0; l < nodeTmp.childNodes.length; ++l)
			{
				if(nodeTmp.childNodes[l].nodeName == "name")
					g_arrMainTypes[iType] = nodeTmp.childNodes[l].text;
				else if(nodeTmp.childNodes[l].nodeName == "recommend")
					g_arrRecommend[iType] = nodeTmp.childNodes[l].text;
			}
		}
	}


	node = xmlDoc.documentElement.selectSingleNode("//bibei/software_list");
	for(var i = 0; i < node.childNodes.length; ++i)
	{
		currNode = node.childNodes[i];
		if (currNode.nodeName.substr(0, "software".length) == "software")
		{
			// 自增
			++index;

			var  info = new CSoftInfo(index);
			info.ID         = currNode.getAttribute("id");
			info.TypeIndex  = currNode.getAttribute("main_type");
			info.MainType   = g_arrMainTypes[info.TypeIndex];
			info.Recommend  = currNode.getAttribute("is_main_recommend");
			info.OneKey     = currNode.getAttribute("is_onekey_install");
			info.Popularity = currNode.getAttribute("is_popularity");
			info.TotalJudgeofMark = currNode.getAttribute("totaljudgeofmark");
			info.TotalJudgeofTimes = currNode.getAttribute("totaljudgeoftimes");
			info.TotalDownloadTimes = currNode.getAttribute("totaldownloadtimes");
		　	for(var j = 0; j < currNode.childNodes.length; ++j)
			{
				switch(currNode.childNodes[j].nodeName)
				{
				case "name":
					info.Name = currNode.childNodes[j].text;
					break;
				case "displayname":
					info.DisplayName = currNode.childNodes[j].text;
					break;
				case "pic":
					info.Pic = currNode.childNodes[j].text;
					break;
				case "description":
					info.Desc = currNode.childNodes[j].text;
					break;
				case "version":
					nodeTmp = currNode.childNodes[j];
//					if(1 * nodeTmp.getAttribute("is_last") == 0)
//						break;
					var att = nodeTmp.getAttribute("ver_attribute");
					if(info.VersionInfo[att].Value != "" && VersionCompare(info.VersionInfo[att].Value,  nodeTmp.getAttribute("version")) == 1)
						break;
					info.VersionInfo[att].Value      = nodeTmp.getAttribute("version");
					info.VersionInfo[att].DisplayVer = nodeTmp.getAttribute("displayver");
					info.VersionInfo[att].CID        = nodeTmp.getAttribute("contentid");
					info.VersionInfo[att].Size       = nodeTmp.getAttribute("filesize");
					info.VersionInfo[att].IsLast     = 1;
					info.VersionInfo[att].VerAttrib  = att;
					for(var k = 0; k < nodeTmp.childNodes.length; ++k)
					{
						switch(nodeTmp.childNodes[k].nodeName)
						{
						case "whatsnew":
							info.VersionInfo[att].Whatsnew = nodeTmp.childNodes[k].text;
							break;
						case "fileurl":
							info.VersionInfo[att].URL = nodeTmp.childNodes[k].text;
							break;
						case "regkey":
							info.VersionInfo[att].RegKey = nodeTmp.childNodes[k].text;
							break;
						case "plugin":
							info.VersionInfo[att].Plugin = nodeTmp.childNodes[k].text;
							break;
						case "setupcmd":
							info.VersionInfo[att].SetupCmd = nodeTmp.childNodes[k].text;
							break;
						}
					}
					break;
				}
			}
			//加入论坛信息
			if(g_arrSoftClubSummary)
			{
				var clubinfo = g_arrSoftClubSummary[info.Name];
				if(clubinfo != undefined)
				{
					for(var m = 0; m < clubinfo.length;m ++)
					{
						if(clubinfo[m])
						{
							info.Clubs.push(clubinfo[m]);
						}
					}
				}
			}
			// 暂时只固定过滤非流行软件的
			//if(info.Popularity == 0)
			//	continue;
			try
			{
				var s = external.GetSoftInstallStatus(info.Name);

				var ii = parseInt(s,10);
				if(ii == 1)
					ii = 2;
				info.Status = ii;
				if(ii == 2)
				{
					s = s.replace(/^[0-9]+ +/, "");
					ii = parseInt(s,10);
					if(ii == 0)
						info.CurrentVer = "_";
					else
					{
						s = s.replace(/^[0-9]+ +/, "");
						info.CurrentVer = s.substr(0, ii);
					}
				}
				else
					info.CurrentVer = "_";
			}
			catch(err)
			{
				info.Status = (index % 3);
				if(index % 2 == 0)
					info.CurrentVer = "_";
				else
					info.CurrentVer = "";
			}
			funcAddSoft(info);
		}
	}
	return index;
}

function AddInstall(info, ver)
{
	if(!ver)
		ver = 0;
	if(ver == 0 && info.VersionInfo[ver].Value == "")
		ver = 1;
	if(info.VersionInfo[ver].Plugin != 0)
		if(!confirm("“" + info.DisplayName + " " + info.VersionInfo[ver].DisplayVer + "”捆绑有“" + info.VersionInfo[ver].Plugin + "”，请在安装过程中注意选择。"))
			return false;
	var name = /*(info.DisplayName != "")? info.DisplayName:*/ info.Name;
	try
	{
		if(info.VersionInfo[ver].URL != "")
		{
			external.AddDownInfo(info.Index, "DisplayName", name);
			external.AddDownInfo(info.Index, "Version",     info.VersionInfo[ver].Value);
			external.AddDownInfo(info.Index, "size",        t_filesize(info.VersionInfo[ver].Size));	// 大写？
			external.AddDownInfo(info.Index, "URL",         info.VersionInfo[ver].URL);
		}
		else
		{
			alert("信息不足无法安装。");
		}
	}
	catch(err)
	{
		return false;
	}
	return true;
}

function StartInstall()
{
	try
	{
		external.StartDown();
	}
	catch(err)
	{
		alert("只支持在迅雷装机必备客户端中安装！");
	}
}

function InstallSoft(info, ver)
{
	if(!ver)
		ver = 0;
	if(AddInstall(info, ver))
		StartInstall();
}

function t_filesize(i_filesize)
{
    var s_filesize;
    if(parseInt(i_filesize,10) > 1024)
    {
        if(parseInt(i_filesize,10) > 1024*1024)
        {
            s_filesize = Math.round(parseInt(i_filesize/1024/1024),10)+"MB";
        }
        else
        {
            s_filesize = Math.round(parseInt(i_filesize/1024),10)+"KB";
        }
    }
    else
    {
        s_filesize = "未知文件大小";
    }
    return s_filesize;
}

function VersionCompare(sVer1, sVer2)
{
	if(sVer1 == "_")
		return -2;
	if(sVer1 == "")
		return -1;
	else if(sVer2 == "")
		return 1;

	a1 = sVer1.split(".");
	a2 = sVer2.split(".");

	var len;

	if(a1.length < a2.length)
		len = a2.length;
	else
		len = a1.length;

	for(var i = 0; i < len; ++i)
	{
		if(a1[i] == undefined)
			a1[i] = 0;
		else if(a2[i] == undefined)
			a2[i] = 0;

		if(parseInt(a1[i],10) - parseInt(a2[i],10) < 0)
			return -1;
		else if(parseInt(a1[i],10) - parseInt(a2[i],10) > 0)
			return 1;
	}
	return 0;
}

function CommonLoad()
{
	if(typeof(external) != "undefined" && typeof(external.GetDrThunderVersion) != "undefined")
	{
		document.body.onselectstart = document.body.oncontextmenu = function()
		{
			if(event.srcElement.tagName == "INPUT" && 
				(event.srcElement.type == "text" || event.srcElement.type == "password") || 
			   event.srcElement.tagName == "TEXTAREA")
			{
				return true;
			}
			else
				return false;
		}
		document.body.style.cursor = "default";
	}
}

function TimeOutCheck()
{
	if(g_MouseStopTime > g_TimeOut)
	{
		if(g_LastEventVer != -1 && g_LastEventIndex != -1)
		{
			if(g_CallBackFun)
			{
				g_CallBackFun(g_LastEventIndex,g_LastEventVer,g_MouseX,g_MouseY);
			}
		}
		ResetEvent();
	}
	else
	{
		g_MouseStopTime += g_TimeInterval;
	}
}

function ResetEvent()
{
	g_MouseStopTime = 0;
	g_LastEventIndex = -1;
	g_LastEventVer = -1;
}

function StartCheckTimeOut()
{
	ResetEvent();
	g_DelayID = setInterval("TimeOutCheck()",g_TimeInterval);
}

function StopCheckTimeOut()
{
	clearTimeout(g_DelayID);
}

function Set_TimeOut(time)
{
	if(time > 0)
	{
		g_TimeOut = time;
	}
}

function SetMouse(cx,cy)
{
	g_MouseX = cx;
	g_MouseY = cy;
}

function AddPosForMouse(softindex,ver,callbackfun)
{
	StopCheckTimeOut();
	StartCheckTimeOut();
	g_LastEventIndex = softindex;
	g_LastEventVer   = ver;
	g_CallBackFun = callbackfun;
	
}

function t_avgmark(mark,times)
{
	if(times == 0)
	{
		return 7;
	}
	return Math.round((mark/times)*10)/10;
}


//首页，登陆页面新闻展示
function CNewsInfo()
{
	this.newsposition = 0;
	this.strong = 0;
	this.bignews = 0;
	this.img = "";
	this.title = "";
	this.link  = "";
	this.desc = "";
}

function GetXmlNewsInfo(xmlDoc, funcAddNews)
{
	var currNode = xmlDoc.documentElement.childNodes[0];
	var node, nodeTmp;

	node = xmlDoc.documentElement.selectSingleNode("//bibei");
	for(var i = 0; i < node.childNodes.length; ++i)
	{
		var info = new CNewsInfo();
		currNode = node.childNodes[i];
		if (currNode.nodeName.substr(0, "news".length) == "news")
		{
			info.newsposition = currNode.getAttribute("newsposition");
			info.strong = currNode.getAttribute("strong");
			info.bignews = currNode.getAttribute("bignews");
		　for(var j = 0; j < currNode.childNodes.length; ++j)
			{
				switch(currNode.childNodes[j].nodeName)
				{
				case "img":
					info.img = currNode.childNodes[j].text;
					break;
				case "title":
					info.title = currNode.childNodes[j].text;
					break;
				case "link":
					info.link = currNode.childNodes[j].text;
					break;
				case "desc":
					info.desc = currNode.childNodes[j].text;
					break;
				}
			}
			funcAddNews(info);
		}
	}
}

function getjumpurl(URL)
{
	if(typeof(external) == "undefined" || typeof(external.GetJumpKey) == "undefined")
		return URL;
	var jumpkey = external.GetJumpKey();
	if(jumpkey == "")
		return URL;
	var jumpurl = "http://jump.xunlei.com/jump/?jump_key="+jumpkey+"&u1="+URL;
	return jumpurl;
}
