var g_ViewType = VT_ALL;
var g_LastType = "";
var g_PreLoad = false;
var g_Version = 0;
var g_OnCategoryClick = false;
var g_bViewLoad = false;

function disp_msg(_msg, _param)
{
	switch(_msg)
    {
        case 22:
			/*
        	setTimeout("XL_login.fn_set_login_banner("+_param+")",100);
			if (_param == LoginStatus_LoginOkay)
			{
				setTimeout("fresh_tasklist(0)", 10);
			}
			*/
			break;
		case 24:
			 setTimeout("set_vip_user_state("+_param+")", 150);
			 break;
		case 25:		
			setTimeout('page_redirect()', 500);
			break;
		case 26:
			show_op_tip();
			break;
		case 28:
			setTimeout("fresh_tasklist(0)", 600);
			break;
		case 29:
			{
				var ret=0;
				try{
					ret=thunder_server.GetVariable('PartnerCall');
				}catch(e){}	
				var nm=document.location.host.replace(/\./gi,'_');
				if(!nm) nm="my_xunlei_com_customer";
				if(ret==1){
					try{
						window.focus();
						//window.name=nm;
					}catch(e){}
				}else{
					var strDate = new Date();
					window.open("http://my.xunlei.com/index.htm?" + strDate.toString(),'my_xunlei_com');
				}
			}
			break;
		case 30:
			setTimeout("on_category_click(_param, true)", 10);
			break;
		case 32:
			setTimeout("set_state_bar('off_maching',0)", 10);
			break;
		case 33:
			OFF_BOOL = false;
    		setTimeout("set_state_bar_empty()", 10);
    		break;
		case 34:
			setTimeout("show_task_search_results()", 10);
			window.focus();
			break;
		case 36:
			setTimeout("on_modify_menu()", 10);
			break;
		case 37:
			setTimeout("check_page_idle()", 10);
			break;
		case 38:
			setTimeout('reload_index()', 2000);
			break;
        default: 
			break;
    }
}
function set_vip_user_state(nState)
//function set_vip_user_state()
{
	switch(parseInt(nState))
	{
		case 1:gVipUserStateHtml = "<a href='http://vip.xunlei.com' target='_blank' ><img border='0' src='../images/vip_no.gif' title='迅雷VIP会员查询中...'/></a>";
				break;
		case 2:gVipUserStateHtml ="<a href='http://vip.xunlei.com' target='_blank' ><img border='0' src='../images/vip_no.gif' title='非迅雷VIP会员'/></a>";
				break;
		case 3: gVipUserStateHtml = "<a href='http://vip.xunlei.com' target='_blank' ><img border='0' src='../images/vip_yes.gif' title='迅雷VIP会员'/></a>";
				break;
		case 4:gVipUserStateHtml = "<a href='http://vip.xunlei.com' target='_blank' ><img border='0' src='../images/vip_no.gif' title='查询失败'/></a>";
				break;
		default:gVipUserStateHtml = "<a href='http://vip.xunlei.com' target='_blank' ><img border='0' src='../images/vip_no.gif' title='会员状态未知'/></a>";
	}
	
	if($("vip_user_state") != null)
	{
		$("vip_user_state").innerHTML = gVipUserStateHtml;
	}
}
function on_modify_menu()
{
	fresh_tasklist(0);
	XL_strongTaskCnt.fn_set_cnt();
	update_recycle_ui();
}
function check_page_idle()
{
	var s = '1';
	if ($('login_win')!=null || $('set2_div')!=null || $('set_tools_id')!=null)
		s = '0';	
	set_variable("PageIdle", s);
}

function reload_index()
{
	var ver = get_server_build_version();
	if (ver < 200)
		top.location.href = "http://my.xunlei.com/empty.htm";
}

function show_task_search_results()
{
	g_ViewType = VT_SEARCH;
	fresh_tasklist(1);
	update_menu_ui(g_ViewType);	
}

function set_custom_skin()
{
	var SkinStyle = thunder_server.GetConfig("Skin", "SkinType", "1");
	if(SkinStyle == "1")
	{
		XL_cutis.fn_set_index_style('style');	
	}
	else if(SkinStyle == "2")
	{
		XL_cutis.fn_set_index_style('style_blue');
	}
	else if(SkinStyle == "3")
	{
		XL_cutis.fn_set_index_style('style_argent');
	}
	else
	{
		XL_cutis.fn_set_index_style('style');
	}
}
function set_iframe_custom_skin()
{
	var SkinStyle = thunder_server.GetConfig("Skin", "SkinType", "1");
	if(SkinStyle == "1")
	{
		XL_cutis.fn_set_iframe_style('style');	
	}
	else if(SkinStyle == "2")
	{
		XL_cutis.fn_set_iframe_style('style_blue');
	}
	else if(SkinStyle == "3")
	{
		XL_cutis.fn_set_iframe_style('style_argent');
	}
	else
	{
		XL_cutis.fn_set_iframe_style('style');
	}	
}

function on_body_load()
{
	window.focus();
	//window.onerror = on_err_occured;
	$('view').src = "tasklist.htm";

	if(SiteInfo.headerType==0){
		$('topHeader').src="header.html";
	}


	build_menu();
	set_callback_entry(INDEX_CALLBACK, disp_msg);
	//XL_partner.fn_set_logo_img();
	//var _login_status = get_login_status();
	//XL_login.fn_set_login_banner(_login_status);
	//var _vip_user_state = get_vip_user_state();
	//setTimeout("set_vip_user_state("+_vip_user_state+")", 50);
	//setTimeout("set_vip_user_state()", 50);
	$("file_info_box").style.left = document.body.clientWidth/2 - 370;
	$("file_info_box").style.top = document.body.clientHeight/3 - 140;
	$("variable_menu").style.left = document.body.clientWidth/2 - 200;
	$("variable_menu").style.top = document.body.clientHeight/3 - 80;
	XL_strongTaskCnt.fn_set_cnt();
	document.onclick = function(){on_body_click()}
		
	//set_custom_skin();
}
function on_err_occured()
{
	try{if (thunder_server.DeleteAllCache() == 1)
		top.location.href = "http://my.xunlei.com/index.htm?delcache";
	}catch(e){}
}

// 重定向必须发生在所有的页面加载完毕后
function on_view_load()
{
	setTimeout('page_redirect()',1000);
	try{delay_load();}catch(e){}
	
	setTimeout("set_text_ad()",1000);
	setTimeout("set_news_bar()",2000);
	setTimeout("set_left_bottom()",3000);
	//setTimeout("XL_tips_dft.fn_set_tips()",5000);
	
	
	var ol = parseInt(thunder_server.GetVariable('BatchOverlap'));
	if (ol > 0)
	{		
		thunder_server.SetVariable('BatchOverlap', '0');
		set_state_bar('批量任务中发现重复任务', 5000);
	}	
	
	set_iframe_custom_skin();
}
function set_text_ad()
{
	try{window.frames['view'].set_txt_news();}catch(e){}
}
function delay_load()
{
	$("MOREFUNC").onclick = function(){open_new_works()};
	$("CONFIG").href = "javascript:open_config()";
	try
	{	$("LOGIN").href = "javascript:on_login_dft()";
		$("REG").href = "http://i.xunlei.com/register/register.htm";}catch(e){}
	$("QA").href = "javascript:goto_help()";
	$("SZNET").href = "http://www.sznet110.gov.cn/index.jsp";
	$("COPYRIGHT").href = "http://pstatic.xunlei.com/about/other/copyright.htm";
	$("HELP").href = "http://help.xunlei.com/00002/0000000290.html";
}
function on_body_click()
{
	try{window.frames.view.outer_on_body_click();}catch(e){};
	clear_all_float_window();

}
function on_body_unload()
{	
	thunder_server.SetVariable("PartnerCall",0);
	window.name=window.top.SiteInfo.domain.replace(/\./gi,'_');
	cancel_all_callback();
}
function set_window_name()
{
	var ret=0;
	try{
		ret=thunder_server.GetVariable('PartnerCall');
	}catch(e){}

	//alert(ret);

	var nm=document.location.host.replace(/\./gi,'_');
	if(!nm) nm="my_xunlei_com_customer";
	if(ret==1){
		try{
			window.focus();
		}catch(e){
			window.name = nm;
			thunder_server.SetVariable("PartnerCall",0);
		}
	}else{
		if(window.location.href.indexOf("preload") != -1)
		{
			//window.name = "preload_my_xunlei_com";
			g_PreLoad = true;
		}
		else
		{
			//window.name = "my_xunlei_com";
			window.name = nm;
			g_PreLoad = false;
		}
		thunder_server.SetVariable("PartnerCall",1);
	}
}
function show_op_tip()
{
	var str = get_variable("OperationTips","");
	if (str.length > 1)
		set_state_bar(str, 5000);
}
function get_viewtype_start()
{
	if (get_variable("TasksSearchState").length > 2)
	{
		set_variable("TasksSearchState", "");
		g_ViewType = VT_SEARCH;
	}
	else
	{
		v = get_variable("ViewType");
		set_variable("ViewType","");
		g_ViewType = (v.length>0 ? v:VT_ALL);
		update_menu_ui(g_ViewType);
	}
	
	if (get_finish_shutdown() == 1)
		set_state_bar("off_maching");
}
function set_view_frm_size(_page)
{
	var HEIGHT = [360,415,496,575,630];
	document.all.view.height = HEIGHT[parseInt((_page+1)/3)-3]+26+50;
}
// 创建菜单 核心代码
function build_menu()
{
	var cat_info = get_sub_category();
	var cat = null;
	var html = "";
	if (cat_info != null)
	{
		for (var i = 0; i < cat_info.length; i=i+2)
		{
			var catname = cat_info[i+1];
			if (catname != null)
			{
				if (catname.length > 24)
					continue;
				var re = /.*<.*script.*>.*/gi;
				if (re.test(catname) == true)
					continue;
			}
			html += '<li id="li_' + cat_info[i] + '" style="cursor:pointer" onmouseover="left_tb_class_change_for_over(this)" onmouseout="left_tb_class_change_for_out(this)" onclick="on_category_click(\'' + cat_info[i] + '\',true,true);"><a onfocus="this.blur()">' + catname + '</a></li>';
		}
	}
	html += '<li id="li_13" onmouseover="left_tb_class_change_for_over(this)" style="cursor:pointer" onmouseout="left_tb_class_change_for_out(this)" onclick="on_category_click(\'13\',true,true);" ><a onfocus="this.blur()">其它</a></li>';
	document.all.navigate.innerHTML = html;
	
	update_recycle_ui();
}
function is_inner_category(_cat)
{
	var id = parseInt(_cat.substr(3));
	if (id == 3 || (id >= 10 && id <= 12))
		return true;
	return false;	
}
function clear_last_menu_ui()
{
	if (g_LastType != "")
	{
		id = "li_" + g_LastType;
		var o;
		try{o = document.getElementById(id);}catch(e){o = null;}
		if (o != null)
			o.className = "";
	}
}
function update_menu_ui(_li)
{
	if (g_ViewType == g_LastType || _li == VT_YOUYOU)
		return;
		
	if (_li == VT_SEARCH)
	{
		clear_last_menu_ui();
		return;
	}
	
	var id = "";
	id = "li_" + _li;
	var p = document.getElementById(id);
	p.className = "selected";
	clear_last_menu_ui();
	XL_strongTaskCnt.fn_set_cnt();
	g_LastType = g_ViewType;
}
function update_recycle_ui()
{
	var count = get_task_count(VT_RECYCLE,0);
	if (count > 0)
		document.all.recycle_clear.innerText = "[清空]";
	else
		document.all.recycle_clear.innerText = "";
}
function click_valid(_time)
{
	if (g_bViewLoad==false)
	{
		setTimeout('set_viewload_true()', _time);
		return false;
	}	
	return true;
}
function set_viewload_true()
{
	g_bViewLoad=true;
}
function on_category_click(_vt,_op,forClick)
{
	//alert('sel_'+_vt);
	
	var capN=null;
	switch(String(_vt)){
		case '10':
			capN='cap_showAll';
			break;
		case '11':
			capN='cap_showNoComplete';
			break;
		case '12':
			capN='cap_showCompleted';
			break;
	}
	if(capN)
		selectCap(capN,null,g_CapIdArr);
	//alert(_vt+'=='+capN);

	try
	{
		if(forClick== true){			
			fn_user_and_status_click("li_"+_vt);
		}
	}
	catch(e){}
	if (g_OnCategoryClick == true || click_valid(4000) == false)
		return;
	g_OnCategoryClick = true;
	g_ViewType = _vt;
	set_viewtype(_vt);
	update_menu_ui(_vt);
	if (document.all.view.src.indexOf("tasklist.htm") < 0)
		reload_tasklist();
	else
		fresh_tasklist(1);
	g_OnCategoryClick = false;
}
function on_clear_recycle()
{
	window.event.cancelBubble = true;
	clear_recycle();
	if (g_ViewType == VT_RECYCLE)
		on_category_click(VT_RECYCLE,false);
	update_recycle_ui();
}
function on_login()
{
	$("user_name").value = "";
	$("pass_word").value = "";
	XL_popUpWindow.fn_open("login_window")
}
function on_login_dft()
{
	$("user_name").value = "";
	$("pass_word").value = "";
	$("login_error_div").innerText = "";
	$("login_error_div_back").style.backgroundColor = "#ffffff";
	XL_popUpWindow.fn_open("login_window")
	$("user_name").focus();
}
function fresh_tasklist(_reset)
{
	try
	{	
		window.frames.view.outer_rebuild_list(true);
		if (_reset == 1)
			window.frames.view.flip_over(0);
	}catch(e){}
}
function reload_tasklist()
{
	document.all.view.src = "tasklist.htm";
}
function search_class()
{
    do_search($("search_text").value);
}
function do_search(_word)
{
	if(arguments.length<1)
	{
		if ($('search').value == "找电影来狗狗") 
			return;	
		_word = $('search').value.replace(/(^[\s\u3000]*|[\s\u3000]*$)/g, ""); 	
		if(_word == "")
		{
		 	$('search').value = "";
		 	return;
		}
		$('search').value = "";
	}
	else if (_word == "找电影来狗狗") 
	{		
		window.open('http://www.gougou.com/#id=3');
		return;
	}
	else
	{
		var t = _word.replace(/(^[\s\u3000]*|[\s\u3000]*$)/g, "");
		if (t == '')
		{
			window.open('http://www.gougou.com/#id=3');
			return;
		}
	}

	var url = "http://www.gougou.com/search?search=" + encodeURI(_word) + "&id=3";
	window.open(url);
}
function search_task_layout(_word,opt1,opt2,opt3)
{
	var t = start_task_search(_word,opt1,opt2,opt3);
	on_category_click(VT_SEARCH,true);
}
function set_zixun()
{
	var win = window.open("http://biz5.sandai.net/bd/thunder5/inmedia_popup/partner1/desktop.htm", "xlzixun", "width=625px,height=462px,status=no,toolbar=no, menubar=no,resizable=no");
	if (win != null)
		document.all.zixun_stat.src = "http://pub6.sandai.net/popup_pv.js?id=8080";
}
function menu_new_bt_work()
{
	fn_toobutton_state('menu_new_bt_work');
	open_new_works();
    thunder_server.ImportBtTorrentFile();
}
function menu_add_batch_task()
{
	fn_toobutton_state('menu_add_batch_task');
	open_new_works();
	add_batch_task();
}

function menu_show_task_search()
{
	fn_toobutton_state('menu_show_task_search');
	open_new_works();
	strUrl = get_server_path() + "Page/gougou.htm?taskSearchMode=true";	
	thunder_server.SetBrowserWindowData(strUrl, "WEB迅雷任务搜索");
}

function menu_import_task()
{
	fn_toobutton_state('menu_import_task');
	open_new_works();
	setTimeout("import_task()",100);
}
function menu_import_download_list()
{
	fn_toobutton_state('menu_import_download_list');
	open_new_works();
	import_download_list();
}
function menu_export_download_list(_flag)
{
	open_new_works();
	var succ = export_download_list(_flag);
	var str = '成功导出' + succ + '个任务';
	set_state_bar(str, 3000);
	if (_flag==1)
		fn_toobutton_state('menu_export_download_list_selected');
	else
		fn_toobutton_state('menu_export_download_list_all');
}
function menu_set_finish_shutdown()
{
	fn_toobutton_state('menu_set_finish_shutdown');
	open_new_works();
	set_finish_shutdown(1);
	set_state_bar("off_maching");
}
function menu_bar_set_shutdown()
{
	fn_toobutton_state('shutdown_on_finish_click');
	set_finish_shutdown(1);
	set_state_bar("off_maching");	
}
function menu_set_schedule_task()
{
	fn_toobutton_state('menu_set_schedule_task');
	open_new_works();
	window.frames["view"].get_select_pause_taskid_list();
	show_set_schedule_page()
}
function menu_clear_task_list()
{
	fn_toobutton_state('menu_clear_task_list');
	if (g_ViewType != VT_RECYCLE)
	{
		if(confirm("确定删除当前类别下所有任务？"))
			try{thunder_server.OperateAllTask(g_ViewType, OPERATION_DELETE_ALL, 0);}catch(e){}
	}
	else
	{
		document.all.recycle_clear.click();
	}
	
	open_new_works();
}

function page_redirect()
{
	var param = get_variable("ShowTaskInfo");
	if (param && param.length > 4)
	{
		set_variable("ShowTaskInfo", "");
		show_taskinfo(param);
		return;
	}
	
	var taskid = get_variable("ShowVirusTaskId");
	if (taskid != ''&&taskid!=null)
	{
		top.show_variable_menu2(taskid);
		set_variable("ShowVirusTaskId","");
		return;
	}	
}
function show_taskinfo(_param)
{
	close_all_popUpWindow();
	set_variable("Content_TaskId", _param);
	set_variable("Content_ViewType", VT_ALL);
	open_the_set2_for_id();
}
function goto_mxl()
{
	var url = GetJumpUrl(thunder_server, "http://m.xunlei.com");
	window.open(url, "_blank");
}
function goto_help()
{
	var url = GetJumpUrl(thunder_server, "http://main.club.xunlei.com/main.fcg?club=web%D1%B8%C0%D7%B0%EF%D6%FA&t=l");
	window.open(url, "_blank");
	
}