/*--dbankClientDownloader--*/
/**
 插件实例名：dbankClientDownloader
 */
var dbankClientDownloader;
(function() {
	jDbank.clientDownloader = function() {
		//支持异步调用
		var This = this;
		this.clientDownloaderPlugIn = null;
		//是否是IE浏览器
		this.isIE = function() {
			//屏蔽客户端下载功能
			return false;
			return /MSIE/ig.test(navigator.appVersion);
		};

		//是否安装了插件
		this.isInstalledIEPlugin = function() {
			var isInstalledIEPluginFlag = "false";
			try {
				this.clientDownloaderPlugIn = document.getElementById("clientDownloaderPlugIn");
				isInstalledIEPluginFlag = this.clientDownloaderPlugIn.IsCustomerProcInstall();
				//isInstalledIEPluginFlag = true;
			} catch (ex) {
				//alert("未下载插件。");
				return '0';
				//alert(">>>>ex1:" + ex.number + "\n" + ex.description);
			}
			return isInstalledIEPluginFlag;
		};

		//是否安装了客户端下载软件
		this.isInstalledClientDownloadSoft = function() {
			var isInstalled="false";
			try{
				isInstalled=this.clientDownloaderPlugIn.IsCustomerProcInstall();
				//isInstalled = true;
			}catch(ex){
				//alert("未下载插件。");
				return '0';
				//alert(">>>>ex2:" + ex.number + "\n" + ex.description);
			}
			return isInstalled;
		};

		//客户端下载初始化参数
		this.init = function() {
			if (this.isIE()) {
				//1. user_header.jsp页面，显示"下载客户端"链接
				jQuery("#clientDownloadsoftLink").show();

				//2. 文件混排页面，显示"客户端下载"菜单项
				jQuery("#clientDownloadFileLink").show();

				//3. 下载页面，现实"使用客户端下载"按钮
				jQuery("#clientDownloadButton").show();
			}
		};
		
		//加载客户端插件
		this.loadPlugin = function (){
			//屏蔽客户端下载功能
			return;
			var objectTemplate="<object ID=\"clientDownloaderPlugIn\" classid=\"clsid:#classid\" codebase=\"#codebase\"></object>";
			//获取参数
			var plugInContainer = jQuery("#clientDownloaderPlugInContainer");
			var classid = plugInContainer.attr("classid");
			var codebase = plugInContainer.attr("codebase");
			//生成插件对象
			var clientDiv = jQuery("#clientDownloaderPlugInDiv");
			clientDiv.append(objectTemplate.replace(/#classid/i,classid).replace(/#codebase/i,codebase));
			
			//alert(objectTemplate.replace(/#classid/i,classid).replace(/#codebase/i,codebase));
		};

		//文件列表页面回调函数
		this.netDiskListPageCallback = function (){
			if (typeof (diskFileList) != 'undefined') {
				var fileArray = diskFileList.getSelectedItems();
				var paramStr = "files=";
				for ( var i = 0; i < fileArray.length; i++) {
					//fileId_creatorId
					paramStr += fileArray[i].data.id + "_"
							+ fileArray[i].data.dUid+"$";
				}
				return paramStr;
			}
			return null;
		};
		
		this.reloadClientPlugin = function (){
		    var form = document.getElementById("download_client_form");
		    form.action = jDbank.data.getGlobal("systemContextPath()")+"/view/client/reloadClientDownPlugIn.jsp";
		    form.submit();
		}
		
		//下载页面回调函数
		this.downloadPageCallback = function (){
			var paramStr = "files=";
			paramStr+=jQuery("#clientDownloadParam").val();
			return paramStr;
		};
		
		this.clientDownloadFiles = function(callback) {
			//1.校验
			if (this.isInstalledIEPlugin() == '0' || this.isInstalledClientDownloadSoft() == "0") {
				//alert("这个功能正在开发中。。。"); 
				this.reloadClientPlugin();
				jDbank.ui.showMsg("未安装控件或控件已被浏览器禁止！", true);
				return;
			}
			if (this.isInstalledClientDownloadSoft() == "false") {
//				if (confirm("未下载客户端，点击确定按钮，开始下载")) {
//					window.location.href="${imageRootPath}/client/DBankMiNiClientSetup.exe";
//					alert("正在下载客户端。。。");
//				}
				jDbank.ui.showMsg("未安装客户端！", true);
				return;
			}
			
			//2.获取参数并执行
			var paramStr = callback();
			if(paramStr!=null && paramStr.length>0){
				this.findSelectedFilesInfo(paramStr);
			}
		};
 
		//查询文件信息
		this.findSelectedFilesInfo = function(paramStr) {
			var url = jDbank.data.getGlobal("systemContextPath()")
					+ "/ajax/findSelectedFilesInfo.action";
			var data = paramStr + "&a=" + Math.random();
			$.ajax( {
				type : "POST",
				url : url,
				dataType : 'json',
				data : data,
				success : function(data) {
					if (!data.isSuccess) {
						jDbank.ui.showMsg(data.message, true);
					} else {
						try{
							this.clientDownloaderPlugIn = document.getElementById("clientDownloaderPlugIn");
						this.clientDownloaderPlugIn.AddAllURL(data.clientDownloadModelJSONString);
						} catch (ex) {
							alert(">>>>ex1:" + ex.number + "\n" + ex.description);
						}
						//alert(data.clientDownloadModel.location);
				//alert(data.clientDownloadModel.cookie);
				//alert(data.clientDownloadModel.infos[0].url);
			}
		}
			});
		};
	}
	
	//创建实例
	dbankClientDownloader = new jDbank.clientDownloader();
	dbankClientDownloader.init();
})();
