var thunder_down_link = "http://down.sandai.net/thunder7/Thunder_dl_7.9.5.4480.exe";
var thunder_down_page = "http://dl.xunlei.com/xl7.9/intro.html";
UpStat(500);
try{
var len = data.length;
}catch(e){
var len = 0;
}
var url = decodeURI(window.location.href).replace("%23","#");
var baseurl = url.substr(0,url.lastIndexOf("/")+1);
//面包屑
var tmp_url = url.substr(0,url.lastIndexOf("/"));
var basename = tmp_url.substr(tmp_url.lastIndexOf("/")+1);
var tmp_i = 0;
var url_list=new Array();
var name_list=new Array();
while(tmp_url.lastIndexOf("/")>0){
	tmp_url = tmp_url.substr(0,tmp_url.lastIndexOf("/"));
	if(tmp_url=="http:/")break;
	tmp_name = tmp_url.substr(tmp_url.lastIndexOf("/")+1);
	url_list[tmp_i] = tmp_url;
	name_list[tmp_i] = tmp_name;
	tmp_i++;
}
var url_list_size = url_list.length;
var crumb_html = "";
var title_name = window.location.hostname;
var title_host = "";
if(url_list_size>0){
	title_host = url_list[url_list_size-1];
	name_list[url_list_size-1] = "全部文件";
	crumb_html += '<a href="'+URLencode(url_list[0]+"/")+'" class="back">上一级</a><p>';
	for(i=0;i<url_list_size;i++){
		var index = url_list_size-i-1;
		crumb_html += '<a href="'+URLencode(url_list[index]+"/")+'">'+name_list[index]+'</a><span class="space">&gt;</span>';
	}
	crumb_html += '<span>'+basename+'</span></p>';
}else{
	title_host = url.substr(0,url.lastIndexOf("/"));
	crumb_html += '<p><span>全部文件</span></p>';
	$(".crumb").addClass("no_back");
}
$(".crumb").html(crumb_html);
//信息填充
$("#title_name").html(title_name);
$("#title_host").html(title_host);
$(".get_xl").attr("_href",thunder_down_link);
$(".use_xl").attr("_href",thunder_down_page);
$(".to_website").attr("href",thunder_down_page);
$(".to_website").attr("target","_blank");
$(".find_pc").attr("href",title_host+"/res.page/queryconnectedlist");

var passwd = "";
var client_ip = "127.0.0.1";
$.ajax({
	url: title_host+"/res.dynamic/queryinfo/",
	cache:false,
	async:false,
	success: function(data){
		data = $.parseJSON(data);
		passwd = data.passwd;
		title_name = data.pcname;
		client_ip = data.requireip;
	}
});
$("#title_name").html(title_name);
var pass = false;
if(client_ip == window.location.hostname || passwd=="" || checkPasswd(getCookie(title_host),passwd)){
	pass = true;
}
if(len>0 && pass){
	$(".no_file").hide();
	//文件列表
	var html = "";
	for(i=0;i<len;i++){
		var filelink = "";
		if(data[i].isdir=="true"){
			filelink = URLencode(baseurl+data[i].filename+"/");
		}else{
			filelink = URLencode(baseurl+data[i].filename);
		}
		var cssType = "icon_file";
		if(data[i].isdir!="true"){
			cssType = getFileCssType(data[i].filename);
		}
		html += '<li><span class="png icon_box '+cssType+'"></span><div class="file_list_tt">'+
				'<a class="list_filename" href="javascript:void(0)" _href="'+filelink+'" isdir="'+data[i].isdir+'">'+data[i].filename+'</a>'+
				'<div class="list_btn_box">';
		if(data[i].isdir=="true"){
			html += '<span title="打开" class="list_file">文件夹</span>';
		}else{
			var codeCss = "code_box";
			if(i>4)codeCss = "code_box top_box";
			html += '<div class="list_code_box"><span title="二维码" class="list_code">二维码</span><div class="'+codeCss+'" style="display:none">'+
					'<p>扫描二维码下载(需要在同一网络下)</p><div class="code" id="qrcode'+i+'"></div>'+
					'<a class="btn_close" href="javascript:void(0)">关闭</a></div></div>';
		}
		if(data[i].isdir!="true"){
			html += '<span title="下载" class="list_down">下载</span>';
		}
		html += '</div></div>';
		if(data[i].isdir!="true"){
			html += '<span class="size">'+sizeFormat(data[i].filesize)+'</span>';
		}
		html += '<span class="date">'+data[i].time+'</span></li>';
	}
	$(".file_box ul").html(html);
	var show_box_size = $(".code_box:visible").size();
	$(".file_box li").hover(function(){
		if(show_box_size==0){
			$(this).addClass("on");
		}
	},function(){
		if(show_box_size==0){
			$(this).removeClass("on");
		}
	});
	//点击文件名
	$(".file_box .list_filename").click(function(){
		var link = $(this).attr("_href");
		if($(this).attr("isdir")=="true"){
			xl_OpenFile(link);
			return false;
		}else{
			xl_DownloadFile($(this));
		}
	});
	//打开按钮
	$(".file_box .list_file").click(function(){
		var link = $(this).parent().siblings("a").attr("_href");
		xl_OpenFile(link);
	});
	//下载按钮
	$(".file_box .list_down").click(function(){
		xl_DownloadFile($(this).parent().siblings("a"));
	});
	//二维码按钮
	$(".file_box .list_code_box .list_code").click(function(){
		var filelink = $(this).parent().parent().siblings("a").attr("_href");
		if(CheckPathExist(filelink)=="true"){
			var code_div = $(this).siblings(".code_box").children("div");
			$(this).siblings(".code_box").show();
			if(code_div.html()==""){
				code_div.append('<div class="loading_box"><p>二维码加载中</p></div>');
				setTimeout(function(){
					try{
						code_div.qrcode({width:243,height:243,text:utf16to8(filelink)});
					}catch(e){
						code_div.qrcode({width:243,height:243,render:"table",text:utf16to8(filelink)});
					}
					$(".loading_box").remove();
				},0);
			}
			UpStat(504);
			show_box_size = $(".code_box:visible").size();
		}else{
			alert("该文件不存在或被移除");
		}
	});
	$(".code_box .btn_close").click(function(){
		$(".code_box").hide();
		show_box_size = $(".code_box:visible").size();
	});
}else if(!pass){
	$(".crumb").hide();
	$(".no_file").hide();
	$(".pass_setup").show();
}else{
	$(".no_file").html("暂无文件");
}
$("#btn_passwd").click(function(){
	var in_passwd = $("#input_passwd").val();
	in_passwd = hex_md5(in_passwd).toUpperCase();
	if(checkPasswd(in_passwd,passwd)){
		sentCookie(title_host,in_passwd)
		window.location.reload();
	}else{
		if(in_passwd==""){
			$("#txt_passwd").html("请输入密码");
		}else{
			$("#txt_passwd").html("密码输入错误");
		}
		$("#txt_passwd").show();
	}
});
//下载局域网共享
$(".down_share").click(function(){
	$("#pop_xl7").show();
});
$("#pop_xl7 .btn_close").click(function(){
	$("#pop_xl7").hide();
});
$(".get_xl").click(function(){
	var this_a = $(this);
	LinkUpStat(501,this_a);
});
$(".find_pc").click(function(){
	UpStat(901);
});
//打开文件夹
function xl_OpenFile(url){
	if(CheckPathExist(url)=="true"){
		window.location.href=url;
	}else{
		alert("该文件夹不存在或被移除");
	}
}
//迅雷下载
function xl_DownloadFile(a_item){
	var url = a_item.attr("_href");
	if(CheckPathExist(url)=="true"){
		var title = a_item.html();
		var filesize = a_item.parent().siblings(".size").html();
		var icon_class = a_item.parent().siblings(".icon_box").attr("class");
		$("#pop_file_name").html(title);
		$("#pop_file_size").html(filesize);
		$("#pop_file_link").attr("_href",url);
		$("#pop_file_icon").attr("class",icon_class);
		var refer_url = document.location.href;
		xl_download(url,title,refer_url,'',_g_stat_url);
	}else{
		alert("该文件不存在或被移除");
	}
}
//未安装迅雷提示
function show_pop_file(type){
	if(type==2)$("#pop_file_desc").html("当前迅雷版本不支持多任务下载，请下载安装最新版后重试。");
	$("#pop_file").show();
}
$("#pop_file .btn_close").click(function(){
	$("#pop_file").hide();
});
$("#pop_file_link").click(function(){
	var this_a = $(this);
	LinkUpStat(502,this_a);
});
$(".use_xl").click(function(){
	var this_a = $(this);
	LinkUpStat(503,this_a);
});
function CheckPathExist(url){
	var path = url.replace(title_host,"");
	var re = "false";
	$.ajax({
		url: title_host+"/res.dynamic/pathexist"+path,
		cache:false,
		async:false,
		timeout:2000,
		success: function(data){
			data = $.parseJSON(data);
			re = data.exist;
        } 
	});
	return re;
}
function LinkUpStat(id,this_a){
	UpStat(id);
	setTimeout(function(){
		document.location.href = this_a.attr("_href");
	},100);
}
function UpStat(id){
	var timestamp = Date.parse(new Date());
	src = "http://stat.download.xunlei.com:8080/?aid=1021&id="+id+"&val1=1&t="+timestamp;
	var up = new Image();
	up.src = src;
}
function URLencode(url){
	return encodeURI(url).replace("#","%23");
}