var icon_str = new Array();
icon_str["icon_movie"] = ".mp4; .mov; .m4v; .rmvb; .mkv; .avi; .mpeg; .mpg; .3gp; .ts; .tp; .wmv; .rm; .flv; .swf; .xv; .xlmv; .xvx; .vob; .mts; .asf; .wm; .wmp; .ram; .rpm; .scm; .qt; .3g2; 3gp2; 3gpp; .evo; .BHD; .GHD; .amv; .bik; .csf; .d2v; dsm; .ivf; .m1v; .m2p; m2ts; .m2v; .m4b; .m4p; .mpe; .ogm; .pmp; .pss; .pva; .ratDVD; .smk; .tpr; .vg2; .vp6; .vp7; . wv; .asm; .avsts; .divx; .webm; .flic; .fli; .flc; .srt; .ssa; .ass";
icon_str["icon_music"] = ".mp3; .wav; .wma; .ape; .ogg; .acc; .amr; .flac; .fla; .m4a; .mid; .aiff; .aif; .au; .ra; .ac3; .act; .dts; .cda; .mac; .midi; .mp2; .mpa; .mpga; .mod; .ofr; .rmi; .tak; .tta; .aifc; .tta; .tak; .mpc; .lrc";
icon_str["icon_txt"] = ".ppt; .doc; .xls; .pptx; .docx; .xlsx; .txt; .xml; .wps; .wpt; .chm; .pdf; .rtf; .htm; .html; .wpd; .umd; .ceb; .xps; .pdb; .epub; .eb2k; shtml; xhtml; .docm; .dotx; .dot; .mht; .odt; .wtf";
icon_str["icon_bt"] = ".Torrent";
icon_str["icon_zip"] = ".Rar; .zip; .cab; .ace; .uue; .bz2; .7z; .iso; .bin; .tar; .mdf; .mds; .jar; .arj; .hqx; .sit; .lzh; .gz; .tgz";
icon_str["icon_pic"] = ".bmp; .jpg; .gif; .png; .exif; .tiff; .psd; .jpeg; .eps; .ai; .raw; .emf; .ico; .jpe; .jif; .jfif";
var _g_stat_url = "http://stat.daquan.xunlei.com";
var refer_url_t = "ThunderXFS_web_download_1qaz";

function checkPasswd(in_pass,pass){
	if(hex_md5(in_pass).toUpperCase()==pass){
		return true;
	}else{
		return false;
	}
}
//阻止冒泡
function stopBubble(e) {
	if ( e && e.stopPropagation )
		e.stopPropagation();
	else
		window.event.cancelBubble = true;
}
//文件大小格式化
function sizeFormat(filesize){
	if (filesize >= 1073741824) filesize = Math.round(filesize / 1073741824 * 100) / 100 + ' GB';  
    else if (filesize >= 1048576) filesize = Math.round(filesize / 1048576 * 100) / 100 + ' MB';  
    else if (filesize >= 1024) filesize = Math.round(filesize / 1024 * 100) / 100 + ' KB';  
    else filesize = filesize + ' Bytes';  
    return filesize;
}
//获取文件后缀样式
function getFileCssType(filename){
	var cssType = "icon_unknow";
	var type = filename.substr(filename.lastIndexOf("."));
	for(var key in icon_str){
		var tmp_arr = icon_str[key].replace(/\s+/g,"").toLowerCase().split(";");
		if($.inArray(type.toLowerCase(),tmp_arr)!=-1){
			cssType = key;
		}
	}
    return cssType;
}

function xl_download(url,title,refer_url,cid,stat){
	var item = new Array();
	item['cid'] = (cid == null ? '' : cid);
	item['url'] = url;
	item['title'] = (title == null ? '' : title);
	var items = new Array();
	items[0] = item;
	xl_batch_download(items,refer_url,stat);
}

function xl_batch_download(items,refer_url,stat){
		if(items.length == 0){
			alert("请选择需要下载的文件");
			return false;
		}
		for(var i = 0; i < items.length; i ++)
		{
			var check_url = (items[i]['url'].indexOf('thunder://') >= 0) ? base64_decode(items[i]['url'].replace("thunder://","")) : items[i]['url'];
			if(check_url.indexOf('.xv')>=0)
			{
				if(items[i]['title'].indexOf('.xv')<0)items[i]['title'] += '.xv';
				else if(items[i]['title'].substr(items[i]['title'].indexOf('.xv')) != '.xv')items[i]['title'] += '.xv';
			}
			else if(check_url.indexOf('.mp4')>=0)
			{
				if(items[i]['title'].indexOf('.mp4')<0)items[i]['title'] += '.mp4';
				else if(items[i]['title'].substr(items[i]['title'].indexOf('.mp4')) != '.mp4')items[i]['title'] += '.mp4';
			}
			else if(check_url.indexOf('.mp3')>=0)
			{
				if(items[i]['title'].indexOf('.mp3')<0)items[i]['title'] += '.mp3';
				else if(items[i]['title'].substr(items[i]['title'].indexOf('.mp3')) != '.mp3')items[i]['title'] += '.mp3';
			}
		}
		//拉起迅雷下载
		$browser_type = getUserBrowser();
		if($browser_type.indexOf('iosClient') >= 0 || $browser_type.indexOf('androidClient') >= 0)
		{
			wirelessClientDownload($browser_type,items);
			return;
		}
		switch($browser_type)
		{
			case 'IE':
				ieDownload(items,refer_url, stat);
				break;
			case 'Chrome':
				chromeAndFFDownload(items);
				break;
			case 'Firefox':
				chromeAndFFDownload(items);
				break;
			default: //提示
				show_pop_file(1);
				break;
		}
}

function ieDownload(download_items,refer_url,stat){
	var thunder_list = new Array();
	refer_url = refer_url_t;//引用页固定为当前页面
	for(var i = 0; i < download_items.length; i ++){
		var thunder_item = new Array();
		thunder_item['cid'] = download_items[i]['cid'];
		thunder_item['url'] = download_items[i]['url'];
		thunder_item['refer'] = refer_url;
		thunder_item['name'] = download_items[i]['title'];
		thunder_item['stat'] = stat;
		thunder_list.push(thunder_item);								
	}
	try 
	{
		var _thunder = Thunder.getInstance();
		// cid,下载URL,引用页URL,文件名,统计
		_thunder.batchDownload(thunder_list,stat);
	}
	catch(e)
	{
		show_pop_file(1);
	}
}

//利用迅雷下载的操作通过调用这个函数实现
function chromeAndFFDownload(items)
{
	var npXLPlugin2 = navigator.mimeTypes["application/np_xunlei_plugin.2"];
	if (npXLPlugin2) 
	{
		var xlPlugin = document.createElement("embed");
		xlPlugin.style.visibility = "hidden";
		xlPlugin.type = "application/np_xunlei_plugin.2";
		xlPlugin.width = 0;
		xlPlugin.height = 0;
		document.body.appendChild(xlPlugin);
		// 批量任务URL拼接方式：ReferUrl#@$@#LinkCount#@$@#LinkUrlUrl#@$@#Comments#@$@#Cookie#@$@#CID#@$@#StatURL
	
		var g_strSplitter = "#@$@#";
		// ReferUrl（引用页，当前网页）
		var strUrls = refer_url_t;
		strUrls = strUrls.concat(g_strSplitter);
		// LinkCount（任务个数）
		strUrls = strUrls.concat(items.length, g_strSplitter);
		for(var i = 0; i < items.length; i ++){
			// LinkUrl
			strUrls = strUrls.concat(items[i]['url'], g_strSplitter);
			// filename
			strUrls = strUrls.concat(items[i]['title'], g_strSplitter);
			// Comments（任务注释）
			strUrls = strUrls.concat("", g_strSplitter);
			// Cookie
			strUrls = strUrls.concat("null", g_strSplitter);
			// CID
			strUrls = strUrls.concat("", g_strSplitter);
			// StatUrl
			strUrls = strUrls.concat("", g_strSplitter);
		}
		xlPlugin.DownLoadListByThunderPlugin(strUrls);
	}else{
	if(items.length > 1)
	{
		show_pop_file(2);
	}
	var url = items[0]['url'];
	var npXLPlugin = navigator.mimeTypes["application/np_xunlei_plugin"];
	if (npXLPlugin) 
	{
			var xlPlugin = document.createElement("embed");
			xlPlugin.style.visibility = "hidden";
			xlPlugin.type = "application/np_xunlei_plugin";
			xlPlugin.width = 0;
			xlPlugin.height = 0;
			document.body.appendChild(xlPlugin);
			xlPlugin.DownLoadByThunderPlugin(url);
	}
	else
	{
		show_pop_file(1);
	}
	}
}
//无线客户端的下载函数
function wirelessClientDownload(type,items)
{
	switch(type)
	{
		case 'iosClient':
			if(items.length == 1)
			{
				window.location.href=items[0]['url'].replace('_IBOdownload_','_webdaquandownload_');
			}
			else
			{
				var url='http://multitask';
				for(var i = 0; i < items.length; i ++)
				{
					url = url+'|'+items[i]['url'].replace('_IBOdownload_','_webdaquandownload_');
				}
				window.location.href=url;
			}
			break;
		case 'iosClient2':
			var xml = '<?xml version="1.0" encoding="UTF-8" ?><filelist><total>'+items.length+'</total>';
			for(var i = 0; i < items.length; i ++)
			{
				xml += '<file><name><![CDATA['+encodeURIComponent(items[i]['title'])+']]></name><url><![CDATA['+items[i]['url'].replace('_IBOdownload_','_webdaquandownload_')+']]></url></file>';
			}
			xml += '</filelist>';
			window.location.href='http://filelist|'+xml;
			break;
		case 'androidClient':
			try
			{
				for(var i = 0; i < items.length; i ++)
				{
					window.share.addTask(items[i]['title'],items[i]['url'].replace('_IBOdownload_','_webdaquandownload_'));
				}
			}
			catch(e)
			{
				show_pop_file(1);
			}
			break;
	}
	return;
}
//供无线ios客户端回调的函数,用于判断是否ios无线客户端
var iosCalled=0;
function iosClientCallBack()
{
	iosCalled=1;
}
function iosClientVerCallBack(ver)
{
	iosCalled=ver;
}
//获取用户的当前的浏览器类型
function getUserBrowser()
{
	var browserName = navigator.userAgent.toLowerCase();
	var browserType = "unknown";
	if(/msie/i.test(browserName) && !/opera/.test(browserName))
	{
			browserType = "IE";
	}
	else if(/firefox/i.test(browserName))
	{
			browserType = "Firefox";
	}
	else if(/chrome/i.test(browserName) && /webkit/i.test(browserName) && /mozilla/i.test(browserName))
	{
			browserType = "Chrome";
	}
	else if(/opera/i.test(browserName))
	{
			browserType = "Opera";
	}
	else if(/webkit/i.test(browserName) &&!(/chrome/i.test(browserName) && /webkit/i.test(browserName) && /mozilla/i.test(browserName)))
	{
			browserType = "Safari";
	}
	//判断是否ios无线客户端
	if(iosCalled!=0)
	{
		browserType = (iosCalled == 1) ? "iosClient" : "iosClient"+iosCalled;
	}
	//判断是否android无线客户端
	try
	{
		browserType = window.share.getWebInfo();
	}
	catch(e)
	{}
	return browserType;
}

function sentCookie(name,value)
{
	var Days = 30;
	var exp = new Date();
	exp.setTime(exp.getTime() + Days*24*60*60*1000);
	document.cookie = name+"="+escape(value)+";path=/;expires="+exp.toGMTString();
	return;
}

function getCookie(name)
{
	var arrStr = document.cookie.split("; ");
	for(var i = 0;i < arrStr.length;i ++)
	{
		var temp = arrStr[i].split("=");
		if(temp[0] == name && temp[1] != "") return temp[1];
	}
	return "";
}

function base64_decode(input) {
	var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
	var output = "";
	var chr1, chr2, chr3 = "";
	var enc1, enc2, enc3, enc4 = "";
	var i = 0;
	if (input.length % 4 != 0) {
		return "";
	}
	var base64test = /[^A-Za-z0-9\+\/\=]/g;
	if (base64test.exec(input)) {
		return "";
	}
	do {
		enc1 = keyStr.indexOf(input.charAt(i++));
		enc2 = keyStr.indexOf(input.charAt(i++));
		enc3 = keyStr.indexOf(input.charAt(i++));
		enc4 = keyStr.indexOf(input.charAt(i++));
		chr1 = (enc1 << 2) | (enc2 >> 4);
		chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
		chr3 = ((enc3 & 3) << 6) | enc4;
		output = output + String.fromCharCode(chr1);
		if (enc3 != 64) {
			output += String.fromCharCode(chr2);
		}
		if (enc4 != 64) {
			output += String.fromCharCode(chr3);
		}
		chr1 = chr2 = chr3 = "";
		enc1 = enc2 = enc3 = enc4 = "";
	} while ( i < input . length );
	return output;
}

function utf16to8(str) {  
    var out, i, len, c;  
    out = "";  
    len = str.length;  
    for(i = 0; i < len; i++) {  
    c = str.charCodeAt(i);  
    if ((c >= 0x0001) && (c <= 0x007F)) {  
        out += str.charAt(i);  
    } else if (c > 0x07FF) {  
        out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));  
        out += String.fromCharCode(0x80 | ((c >>  6) & 0x3F));  
        out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));  
    } else {  
        out += String.fromCharCode(0xC0 | ((c >>  6) & 0x1F));  
        out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));  
    }  
    }  
    return out;  
}

var hexcase = 0;  
var b64pad  = ""; 
var chrsz   = 8;
function hex_md5(s){ return binl2hex(core_md5(str2binl(s), s.length * chrsz));}
function core_md5(x, len)
{

  x[len >> 5] |= 0x80 << ((len) % 32);
  x[(((len + 64) >>> 9) << 4) + 14] = len;
  var a =  1732584193;
  var b = -271733879;
  var c = -1732584194;
  var d =  271733878;
  for(var i = 0; i < x.length; i += 16)
  {
    var olda = a;
    var oldb = b;
    var oldc = c;
    var oldd = d;

    a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);
    d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);
    c = md5_ff(c, d, a, b, x[i+ 2], 17,  606105819);
    b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);
    a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);
    d = md5_ff(d, a, b, c, x[i+ 5], 12,  1200080426);
    c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341);
    b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983);
    a = md5_ff(a, b, c, d, x[i+ 8], 7 ,  1770035416);
    d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417);
    c = md5_ff(c, d, a, b, x[i+10], 17, -42063);
    b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162);
    a = md5_ff(a, b, c, d, x[i+12], 7 ,  1804603682);
    d = md5_ff(d, a, b, c, x[i+13], 12, -40341101);
    c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290);
    b = md5_ff(b, c, d, a, x[i+15], 22,  1236535329);
    a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510);
    d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
    c = md5_gg(c, d, a, b, x[i+11], 14,  643717713);
    b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302);
    a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691);
    d = md5_gg(d, a, b, c, x[i+10], 9 ,  38016083);
    c = md5_gg(c, d, a, b, x[i+15], 14, -660478335);
    b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848);
    a = md5_gg(a, b, c, d, x[i+ 9], 5 ,  568446438);
    d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690);
    c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961);
    b = md5_gg(b, c, d, a, x[i+ 8], 20,  1163531501);
    a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467);
    d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784);
    c = md5_gg(c, d, a, b, x[i+ 7], 14,  1735328473);
    b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734);
    a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558);
    d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463);
    c = md5_hh(c, d, a, b, x[i+11], 16,  1839030562);
    b = md5_hh(b, c, d, a, x[i+14], 23, -35309556);
    a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
    d = md5_hh(d, a, b, c, x[i+ 4], 11,  1272893353);
    c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632);
    b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640);
    a = md5_hh(a, b, c, d, x[i+13], 4 ,  681279174);
    d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222);
    c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979);
    b = md5_hh(b, c, d, a, x[i+ 6], 23,  76029189);
    a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487);
    d = md5_hh(d, a, b, c, x[i+12], 11, -421815835);
    c = md5_hh(c, d, a, b, x[i+15], 16,  530742520);
    b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651);
    a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844);
    d = md5_ii(d, a, b, c, x[i+ 7], 10,  1126891415);
    c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905);
    b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055);
    a = md5_ii(a, b, c, d, x[i+12], 6 ,  1700485571);
    d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606);
    c = md5_ii(c, d, a, b, x[i+10], 15, -1051523);
    b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799);
    a = md5_ii(a, b, c, d, x[i+ 8], 6 ,  1873313359);
    d = md5_ii(d, a, b, c, x[i+15], 10, -30611744);
    c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380);
    b = md5_ii(b, c, d, a, x[i+13], 21,  1309151649);
    a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070);
    d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379);
    c = md5_ii(c, d, a, b, x[i+ 2], 15,  718787259);
    b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551);

    a = safe_add(a, olda);
    b = safe_add(b, oldb);
    c = safe_add(c, oldc);
    d = safe_add(d, oldd);
  }
  return Array(a, b, c, d);
  
}

function md5_cmn(q, a, b, x, s, t)
{
  return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b);
}
function md5_ff(a, b, c, d, x, s, t)
{
  return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
}
function md5_gg(a, b, c, d, x, s, t)
{
  return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
}
function md5_hh(a, b, c, d, x, s, t)
{
  return md5_cmn(b ^ c ^ d, a, b, x, s, t);
}
function md5_ii(a, b, c, d, x, s, t)
{
  return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
}

function core_hmac_md5(key, data)
{
  var bkey = str2binl(key);
  if(bkey.length > 16) bkey = core_md5(bkey, key.length * chrsz);

  var ipad = Array(16), opad = Array(16);
  for(var i = 0; i < 16; i++) 
  {
    ipad[i] = bkey[i] ^ 0x36363636;
    opad[i] = bkey[i] ^ 0x5C5C5C5C;
  }

  var hash = core_md5(ipad.concat(str2binl(data)), 512 + data.length * chrsz);
  return core_md5(opad.concat(hash), 512 + 128);
}

function safe_add(x, y)
{
  var lsw = (x & 0xFFFF) + (y & 0xFFFF);
  var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  return (msw << 16) | (lsw & 0xFFFF);
}

function bit_rol(num, cnt)
{
  return (num << cnt) | (num >>> (32 - cnt));
}

function str2binl(str)
{
  var bin = Array();
  var mask = (1 << chrsz) - 1;
  for(var i = 0; i < str.length * chrsz; i += chrsz)
    bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (i%32);
  return bin;
}

function binl2hex(binarray)
{
  var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
  var str = "";
  for(var i = 0; i < binarray.length * 4; i++)
  {
    str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) +
           hex_tab.charAt((binarray[i>>2] >> ((i%4)*8  )) & 0xF);
  }
  return str;
}