
function batonload()
{
	var path = window.dialogArguments;
	$('txtPath').value = path;
	Components.GetObject("@pplive.com/outputdebug;1").DebugString('collection download path'+window.dialogArguments);
	Components.GetObject("@pplive.com/outputdebug;1").DebugString('collection download value'+$('txtPath').value);
	if (!path)
		SetPath($('txtPath').value);
	var downloadMgr = Components.GetObject("@pplive.com/chDownloadMgr;1");
	if (downloadMgr)
	{
		Components.GetObject("@pplive.com/outputdebug;1").DebugString('collection download:get downloadMgr OK');
		var collectionFolder = downloadMgr.GetCollectionFolder();
		if (collectionFolder)
		{
			Components.GetObject("@pplive.com/outputdebug;1").DebugString('collection download get Collection Folder OK');
			var length = collectionFolder.length;
			Components.GetObject("@pplive.com/outputdebug;1").DebugString('collection download items= '+length);
			$('list').BeginAddTask();
			for (var i=0; i<length; i++)
			{
				var task = collectionFolder.item(i);
				if (task)
				{
					$('list').AddHjTaskToWnd(task);
				}
			}
			$('list').EndAddTask();
			Components.GetObject("@pplive.com/outputdebug;1").DebugString('collection download invoke EndAddTask');
		}
	}

	window.selectedItems = 1;	//默认只要>0都可以去表示有选中的就可以了
}

function SelectItemChange(items)
{
	Components.GetObject("@pplive.com/outputdebug;1").DebugString('collection download selected items change= '+items);
	window.selectedItems = items;
	if (items <= 0)
	{
		Components.GetObject("@pplive.com/outputdebug;1").DebugString('collection download selected items change disable button');
		$('btnOK').disabled = true;
	}
	else
	{
		Components.GetObject("@pplive.com/outputdebug;1").DebugString('collection download selected items change enable button');
		$('btnOK').disabled = false;
	}
}

function onok()
{
	var path = $('txtPath').value;
	var iSelectCounts = $('list').GetSelectedItemCounts();
	if (iSelectCounts <=0)
		CloseWindow();
	var iRet = verify(path);
	Components.GetObject("@pplive.com/outputdebug;1").DebugString('collection download verifyPath, iRet= '+iRet);
	if (iRet)
	{
		Components.GetObject("@pplive.com/outputdebug;1").DebugString('collection download verifyPath return value= '+iRet);
		var iCoverCount = $('list').TestCoverFiles(path);
		if (iCoverCount > 0)
		{
			Components.GetObject("@pplive.com/outputdebug;1").DebugString('collection download covered files= '+iCoverCount);
			var iRet = window.showModalDialog('coveredfile.xml', {coveredcount:iCoverCount});
			if (2 != iRet)
			{
				if (1 == iRet)
				{
					$('list').SkipExistFileTask();
				}
				if ($('list').CheckExistingDownloadingTask())	//是否有冲突的任务
				{
					if (!window.showModalDialog("DownloadTaskConflict.xml"))	//if user not click OK, do nothing.
						return;
				}
				if ($('list').CheckExistingFinishTask())
				{
					if (!window.showModalDialog("DownloadTaskConflict2.xml"))
						return;
				}

				if (0==iRet || 1==iRet)
				{
					//Attention:clear foder after insert.
					if (0 == iRet)
					{
						//cover name conflicted files.
						$('list').StartDownloadCollection($('txtPath').value, true);
						if (iSelectCounts > 0)
							SwitchToDownloadWnd();
					}
					else if (1 == iRet)
					{	//skip cover name conflicted files.
						$('list').StartDownloadCollection($('txtPath').value, false);
						if (iSelectCounts > 0)
							SwitchToDownloadWnd();
					}
					CloseWindow();
				}
			}
			return;
		}
		else
		{
			if ($('list').CheckExistingDownloadingTask())	//是否有冲突的任务
			{
				if (!window.showModalDialog("DownloadTaskConflict.xml"))	//if user not click OK, do nothing.
					return;
			}

			if ($('list').CheckExistingFinishTask())
			{
				if (!window.showModalDialog("DownloadTaskConflict2.xml"))
					return;
			}

			Components.GetObject("@pplive.com/outputdebug;1").DebugString('collection download covered files= '+iCoverCount);
			//Attention:clear foder after insert.
			$('list').StartDownloadCollection($('txtPath').value);
			if (iSelectCounts > 0)
				SwitchToDownloadWnd();
			CloseWindow();
		}
	}
	else
	{
		Components.GetObject("@pplive.com/outputdebug;1").DebugString('collection download path invalidate varify return value= '+iRet);
	}
}

function SwitchToDownloadWnd()
{
	Components.GetObject("@pplive.com/outputdebug;1").DebugString('collection download SwitchToDownload');
	var id = 8;
	var uiMainWindow = Components.GetObject("@pplive.com/ui/mainwindow;1"); 
	if(uiMainWindow)
	{
		if(uiMainWindow.runMode==0)
		{
			Components.GetObject("@pplive.com/outputdebug;1").DebugString('collection download current mode is old mode');
			//旧模式
			if(!uiMainWindow.tidyMode && !EwaIsFullScreen() && !uiMainWindow.BossState)
			{
				if (!uiMainWindow.channelIsVisible())
				{
					uiMainWindow.showChannel(true);
				}
				uiMainWindow.tabs.put_selectedId(id); 
			}
			var ppFrame=Components.GetObject("@pplive.com/PPLStat;1"); 
			if(ppFrame) 
			{ 
				ppFrame.AddTabClickCount(id); 
			} 
		}
		else if (uiMainWindow.runMode==1 && !uiMainWindow.BossState)
		{
			Components.GetObject("@pplive.com/outputdebug;1").DebugString('collection download current mode is new mode');
			//新模式
			//uiMainWindow.SetSelectedTabName("Download");
			//创建下载管理窗口组件
			showDownloadMgrWnd();
		}
	}
	else
			Components.GetObject("@pplive.com/outputdebug;1").DebugString('collection download get uiMainWindow fail');
}

function oncancel()
{
	CloseWindow();
}

function CloseWindow()
{
	var downloadMgr = Components.GetObject("@pplive.com/chDownloadMgr;1");
	if (downloadMgr)
	{
		Components.GetObject("@pplive.com/outputdebug;1").DebugString('collection download close window:1.Clear collection. 2.window.close');
		var collectionFolder = downloadMgr.GetCollectionFolder();
		collectionFolder.ClearCollection();
		//关闭所有打开的合集下载框，参数false表示窗口由window.close关闭，downloadMgr只是清除，以免app关闭的时候再次关闭
		downloadMgr.CloseAllCollectionWindow(false);
	}

	Components.GetObject("@pplive.com/outputdebug;1").DebugString('collection download close window:UnRegisterObject @pplive.com/batDownload;1');
	Components.UnregisterObject("@pplive.com/batDownload;1");
	window.close();
}

function SetPath(path)
{
	if(path && path.length!=0)
	{
		$("txtPath").value = path;
	}
	else
	{
		var filepath = Components.GetObject("@pplive.com/filepath;1");
		if(!filepath) return;
		$("txtPath").value = filepath.PathMaxFreeSpaceDisk("PPDownload");
		window.writePath = true;
	}
	$("txtPath").emptyText = $("txtPath").value;
}

function verify(path)
{
//	if(!window.modify) return false;
	var vy = true;
	var result = verifyPathAndFile(path);
	switch(result)
	{
		case 0:
			vy=true;
			$("lblSaveError").value="";
			break;
		case 1:
			break;
		case 2:
			$("lblSaveError").value = "路径不可用";
			vy = false;
			break;
		case 4:
			$("lblSaveError").value = "路径过长";
			vy = false;
			break;
		case 5:
			$("lblSaveError").value = "文件名过长";
			vy = false;
		    break;
	}

	return vy;
}

//0 成功
//1 程序错误
//2 路径不合法
//3 文件不合法 
//4 路径过长
//5 文件名过长
//6 文件已存在
function verifyPathAndFile(path)
{
	var filepath = Components.GetObject("@pplive.com/filepath;1");
	if(!filepath) return 1;
	//文件名合法性
	var maxLengthName = $('list').GetMaxLengthName();
	Components.GetObject("@pplive.com/outputdebug;1").DebugString('collection download max lenght file name= '+maxLengthName);
	if (maxLengthName < 0)
		return false;

	var filename = maxLengthName;
	if(!filepath.IsValidFileName(filename))
	{
		Components.GetObject("@pplive.com/outputdebug;1").DebugString('collection download max lenght file name is not validate filename return 3');
		return 3;
	}

	//全路径是否合法
	var pathname = path;
	Components.GetObject("@pplive.com/outputdebug;1").DebugString('collection download path= '+pathname);
	if (pathname.length > 240)
	{
		Components.GetObject("@pplive.com/outputdebug;1").DebugString('collection download path length >240, return 4');
		return 4;
	}
	var file = filepath.PathAppend(pathname,filename);
	Components.GetObject("@pplive.com/outputdebug;1").DebugString('collection download savePath length= '+file.length);
	var filecfg = filepath.PathAppend(pathname,filename+".ppd.cfg");
	Components.GetObject("@pplive.com/outputdebug;1").DebugString('collection download cfg file savePath length= '+filecfg.length);
	if(file.length==0 || filecfg.length==0)
	{
		Components.GetObject("@pplive.com/outputdebug;1").DebugString('collection download file path or cfg path>240, return 4');
		return 2;
	}

	//路径合法性
	Components.GetObject("@pplive.com/outputdebug;1").DebugString('collection download PathCreate: '+pathname);
	if(!filepath.PathCreate(pathname))
	{
		Components.GetObject("@pplive.com/outputdebug;1").DebugString('collection download create path failed return 2,path= '+pathname);
		return 2;
	}

	if(!filepath.PathAccessHit(pathname,2))
	{
		Components.GetObject("@pplive.com/outputdebug;1").DebugString('collection download PathAccessHit failed return 2, pathname: '+pathname);
		return 2;
	}

	return 0;
}

// SIG // Begin signature block
// SIG // MIIMjQYJKoZIhvcNAQcCoIIMfjCCDHoCAQExCzAJBgUr
// SIG // DgMCGgUAMGcGCisGAQQBgjcCAQSgWTBXMDIGCisGAQQB
// SIG // gjcCAR4wJAIBAQQQEODJBs441BGiowAQS9NQkAIBAAIB
// SIG // AAIBAAIBAAIBADAhMAkGBSsOAwIaBQAEFGiKyRTzwi5O
// SIG // LHddWXE7PLqMN0GToIIKFTCCBPwwggRloAMCAQICEGVS
// SIG // JuGyLhjhWQ8phawi51wwDQYJKoZIhvcNAQEFBQAwXzEL
// SIG // MAkGA1UEBhMCVVMxFzAVBgNVBAoTDlZlcmlTaWduLCBJ
// SIG // bmMuMTcwNQYDVQQLEy5DbGFzcyAzIFB1YmxpYyBQcmlt
// SIG // YXJ5IENlcnRpZmljYXRpb24gQXV0aG9yaXR5MB4XDTA5
// SIG // MDUyMTAwMDAwMFoXDTE5MDUyMDIzNTk1OVowgbYxCzAJ
// SIG // BgNVBAYTAlVTMRcwFQYDVQQKEw5WZXJpU2lnbiwgSW5j
// SIG // LjEfMB0GA1UECxMWVmVyaVNpZ24gVHJ1c3QgTmV0d29y
// SIG // azE7MDkGA1UECxMyVGVybXMgb2YgdXNlIGF0IGh0dHBz
// SIG // Oi8vd3d3LnZlcmlzaWduLmNvbS9ycGEgKGMpMDkxMDAu
// SIG // BgNVBAMTJ1ZlcmlTaWduIENsYXNzIDMgQ29kZSBTaWdu
// SIG // aW5nIDIwMDktMiBDQTCCASIwDQYJKoZIhvcNAQEBBQAD
// SIG // ggEPADCCAQoCggEBAL5nHbRgqhBJb1YXfGbJXoYN1fGs
// SIG // p3GDjouJ+IgEiRUGui2EIZXk0ZxQTPvSIr3a8rI1Ox6P
// SIG // wwn7/BMuWr+JfD07JR7281h7nPQBtcYKuIDOvid0YWcn
// SIG // TWrl7IFhWHmj4BcQEhUnsOFNNH8rRyBEud5mJGaKzU+6
// SIG // H8U4yFSQ4XL2GWZ1arlJaM84eQ2qMKjbLGBInteqFAGp
// SIG // g9c4kTA5E5YDOnxAVLat4C8bg9yoEVI+ArPXK/0htqdc
// SIG // ow8LqaYQUA40Lk2nzsleJdSMvPNufCm8AV38MYda1YyF
// SIG // Z1iIGaC/NfDqK6Mh55D2g+Wo7WB4Xntgg/1XC11BDWNU
// SIG // YNZDIe8CAwEAAaOCAdswggHXMBIGA1UdEwEB/wQIMAYB
// SIG // Af8CAQAwcAYDVR0gBGkwZzBlBgtghkgBhvhFAQcXAzBW
// SIG // MCgGCCsGAQUFBwIBFhxodHRwczovL3d3dy52ZXJpc2ln
// SIG // bi5jb20vY3BzMCoGCCsGAQUFBwICMB4aHGh0dHBzOi8v
// SIG // d3d3LnZlcmlzaWduLmNvbS9ycGEwDgYDVR0PAQH/BAQD
// SIG // AgEGMG0GCCsGAQUFBwEMBGEwX6FdoFswWTBXMFUWCWlt
// SIG // YWdlL2dpZjAhMB8wBwYFKw4DAhoEFI/l0xqGrI2Oa8PP
// SIG // gGrUSBgsexkuMCUWI2h0dHA6Ly9sb2dvLnZlcmlzaWdu
// SIG // LmNvbS92c2xvZ28uZ2lmMB0GA1UdJQQWMBQGCCsGAQUF
// SIG // BwMCBggrBgEFBQcDAzA0BggrBgEFBQcBAQQoMCYwJAYI
// SIG // KwYBBQUHMAGGGGh0dHA6Ly9vY3NwLnZlcmlzaWduLmNv
// SIG // bTAxBgNVHR8EKjAoMCagJKAihiBodHRwOi8vY3JsLnZl
// SIG // cmlzaWduLmNvbS9wY2EzLmNybDApBgNVHREEIjAgpB4w
// SIG // HDEaMBgGA1UEAxMRQ2xhc3MzQ0EyMDQ4LTEtNTUwHQYD
// SIG // VR0OBBYEFJfQa6gmcMihP5QfCC3ENZukoR7yMA0GCSqG
// SIG // SIb3DQEBBQUAA4GBAIsDwN2U2EGiYWmwFah4xzDGkDx+
// SIG // QvcktuSDcxcEfwQQnKHi+oEv68DKROdy4FC2VRAgg26W
// SIG // kuSaUWq0NzHcpS3rjADHHU/nTTK6hfhOvvpnVWXwar56
// SIG // ymQ4GhAQeEV2MfOGegMPYMKzXZ32i2Z2ghtZ4YPlvUml
// SIG // OFbl3kF3DlgPMIIFETCCA/mgAwIBAgIQbVJ/TX1sGiun
// SIG // 74HVDInXZTANBgkqhkiG9w0BAQUFADCBtjELMAkGA1UE
// SIG // BhMCVVMxFzAVBgNVBAoTDlZlcmlTaWduLCBJbmMuMR8w
// SIG // HQYDVQQLExZWZXJpU2lnbiBUcnVzdCBOZXR3b3JrMTsw
// SIG // OQYDVQQLEzJUZXJtcyBvZiB1c2UgYXQgaHR0cHM6Ly93
// SIG // d3cudmVyaXNpZ24uY29tL3JwYSAoYykwOTEwMC4GA1UE
// SIG // AxMnVmVyaVNpZ24gQ2xhc3MgMyBDb2RlIFNpZ25pbmcg
// SIG // MjAwOS0yIENBMB4XDTA5MTExOTAwMDAwMFoXDTEyMTEx
// SIG // ODIzNTk1OVowgc4xCzAJBgNVBAYTAkNOMREwDwYDVQQI
// SIG // EwhTaGFuZ2hhaTERMA8GA1UEBxMIU2hhbmdoYWkxGzAZ
// SIG // BgNVBAoUElBQTGl2ZSBDb3Jwb3JhdGlvbjE+MDwGA1UE
// SIG // CxM1RGlnaXRhbCBJRCBDbGFzcyAzIC0gTWljcm9zb2Z0
// SIG // IFNvZnR3YXJlIFZhbGlkYXRpb24gdjIxHzAdBgNVBAsU
// SIG // FlByb2R1Y3QgUGxhbm5pbmcgRGVwdC4xGzAZBgNVBAMU
// SIG // ElBQTGl2ZSBDb3Jwb3JhdGlvbjCBnzANBgkqhkiG9w0B
// SIG // AQEFAAOBjQAwgYkCgYEA2Oyn30zab4xsusxnJNLtf0vg
// SIG // edM7sURdB6yJT33KUhprEMn85m9nooE89CrjfqWpWUna
// SIG // cdGpoPYA/gDRUjaPbqn5NTy3j1e9fO0SIeLWgBlJ2ETq
// SIG // pD6/ZFQLFP0nqYI2C1QFOOvgbhbQ5gmnp6W+q6hPtGOg
// SIG // FedEXN4gQNKx72sCAwEAAaOCAYMwggF/MAkGA1UdEwQC
// SIG // MAAwDgYDVR0PAQH/BAQDAgeAMEQGA1UdHwQ9MDswOaA3
// SIG // oDWGM2h0dHA6Ly9jc2MzLTIwMDktMi1jcmwudmVyaXNp
// SIG // Z24uY29tL0NTQzMtMjAwOS0yLmNybDBEBgNVHSAEPTA7
// SIG // MDkGC2CGSAGG+EUBBxcDMCowKAYIKwYBBQUHAgEWHGh0
// SIG // dHBzOi8vd3d3LnZlcmlzaWduLmNvbS9ycGEwEwYDVR0l
// SIG // BAwwCgYIKwYBBQUHAwMwdQYIKwYBBQUHAQEEaTBnMCQG
// SIG // CCsGAQUFBzABhhhodHRwOi8vb2NzcC52ZXJpc2lnbi5j
// SIG // b20wPwYIKwYBBQUHMAKGM2h0dHA6Ly9jc2MzLTIwMDkt
// SIG // Mi1haWEudmVyaXNpZ24uY29tL0NTQzMtMjAwOS0yLmNl
// SIG // cjAfBgNVHSMEGDAWgBSX0GuoJnDIoT+UHwgtxDWbpKEe
// SIG // 8jARBglghkgBhvhCAQEEBAMCBBAwFgYKKwYBBAGCNwIB
// SIG // GwQIMAYBAQABAf8wDQYJKoZIhvcNAQEFBQADggEBABB8
// SIG // kusx33gK+1V1dPuHsRhiGqHcrbJzsHsCJhJWXbQ11dD/
// SIG // a+AeoKdRI9WU/A3XKNPfV3OzRHu4Mn8wkIdu1Kug0lUD
// SIG // 9D/cwSNlYMFmji8HqZTBxaiqw6tA180QLF4mArteboJL
// SIG // algUVYbDgqzRT3GAIhB4THkSWNaeaQPk8l0rNl+QMi9c
// SIG // 36Wqqw3ySpobKVmm5DG48i1hvY5EL+yuK74j4P//UH82
// SIG // aGBt3CQTA6iwtyrEhV7Ohjc16OxA2Ic4CRY6WpSVFdJ+
// SIG // 0y/mjxYEWOv5Rt1GD2bbEbRgSFtn66T2ctMMNL3EzM12
// SIG // C/tkxspGuNKglhd11MNePs54XsjDQOUxggHkMIIB4AIB
// SIG // ATCByzCBtjELMAkGA1UEBhMCVVMxFzAVBgNVBAoTDlZl
// SIG // cmlTaWduLCBJbmMuMR8wHQYDVQQLExZWZXJpU2lnbiBU
// SIG // cnVzdCBOZXR3b3JrMTswOQYDVQQLEzJUZXJtcyBvZiB1
// SIG // c2UgYXQgaHR0cHM6Ly93d3cudmVyaXNpZ24uY29tL3Jw
// SIG // YSAoYykwOTEwMC4GA1UEAxMnVmVyaVNpZ24gQ2xhc3Mg
// SIG // MyBDb2RlIFNpZ25pbmcgMjAwOS0yIENBAhBtUn9NfWwa
// SIG // K6fvgdUMiddlMAkGBSsOAwIaBQCgcDAQBgorBgEEAYI3
// SIG // AgEMMQIwADAZBgkqhkiG9w0BCQMxDAYKKwYBBAGCNwIB
// SIG // BDAcBgorBgEEAYI3AgELMQ4wDAYKKwYBBAGCNwIBFTAj
// SIG // BgkqhkiG9w0BCQQxFgQUDymYmYdzr+YepQzgwcicmVqV
// SIG // gBMwDQYJKoZIhvcNAQEBBQAEgYBx1n9UEtyGjjcixC58
// SIG // gRXe5rl0ovURt4SYeWRCKPcscLMCx/uEqarDyy0TBvgV
// SIG // tLhXVbnDgWLVT0UJBN2deF0SimBZfeiWof8P0QsIT8iM
// SIG // +4KyH08/MPKmOT/dcYTvL0gFSlL5BOHukusTgg+chSfG
// SIG // f2kAG0uPqBGR9epl7KbbAw==
// SIG // End signature block
