/*--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("objectForDetectPlugin");
				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{
				this.clientDownloaderPlugIn = document.getElementById("objectForDetectPlugin");
				isInstalled=this.clientDownloaderPlugIn.IsCustomerProcInstall();
				//isInstalled = true;
			}catch(ex){
				//alert("未下载插件。");
				return 'false';
				//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();
				
				//4. 文件快递页面，显示"客户端下载"菜单项
				jQuery("#clientDownloadFileLinkSend").show();
			}
		};
		
		//加载客户端插件
		this.loadPlugin = function (){
			//屏蔽客户端下载功能
			return;
			
			this.detectPlugin("29188EBF-EF06-4BD4-9FFD-D1D7E12A8E97", "IsCustomerProcInstall");
//			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.detectPlugin = function(CLSID,functionName)
		{
		    var pluginDiv = document.getElementById('clientDownloaderPlugInDiv');
		    pluginDiv.innerHTML = '<object id="objectForDetectPlugin" classid="CLSID:'+ CLSID +'"></object>';
		    try
		    {
		    	this.clientDownloaderPlugIn = document.getElementById("objectForDetectPlugin");
		    	
		    	if(this.clientDownloaderPlugIn.IsCustomerProcInstall())
		        {
		            //pluginDiv.removeNode(true);//删除 pluginDiv及其所有的子元素
		            return true;
		        }
		        else
		        {
		            //pluginDiv.removeNode(true);//删除 pluginDiv及其所有的子元素
		            return false;
		        }
		    }
		    catch(e)
		    {
		        return false;
		    }
		}

		//文件列表页面回调函数
		this.netDiskListPageCallback = function (){
			if (typeof (diskFileList) != 'undefined' || typeof (diskFileListImg) != 'undefined') {
				var fileArray;
				if(jDbank.data.getGlobal('filelistdisplay') == '1'){
			    	fileArray = diskFileListImg.getSelectedItems();
			    }else{
			    	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) {
			this.loadPlugin();
			//1.校验
			if (this.isInstalledIEPlugin() == '0' || this.isInstalledClientDownloadSoft() == "0") {
				//alert("这个功能正在开发中。。。"); 
				//this.reloadClientPlugin();
				//jDbank.ui.showMsg("未安装控件或控件已被浏览器禁止！", true);
				jDbank.ui.showMsg("未安装客户端或控件已被浏览器禁止！", true);
				window.open("/minidbank.htm",'a');
				return;
			}
			if (this.isInstalledClientDownloadSoft() == "false") {
//				if (confirm("使用飞速下载，需要您安装迷你DBank客户端，现在是否安装？")) {
//					//window.location.href="${imageRootPath}/client/DBankMiNiClientSetup.exe";
//					window.open("minidbank.htm");
//				}
				jDbank.ui.showMsg("请安装客户端！", true);
				window.open("/minidbank.htm",'a');
				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("objectForDetectPlugin");
						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();
})();

