var ouploadItem = new FCKToolbarButton( 'FCKuploadCommand' , "抓取图片到本地") ;
ouploadItem.IconPath = FCKPlugins.Items['FCKuploadCommand'].Path + 'remoteupload.gif' ;
FCKToolbarItems.RegisterItem( 'FCKuploadCommand', ouploadItem ) ;
var oMyCMCommand = new Object() ;
oMyCMCommand.Name = '抓取图片' ;

oMyCMCommand.Execute = function()
{
	parent.qmeCms.dialogView.form("提示","<p class='loading'>数据分析中....</p>",{type:'fixed'});
	var doc = FCK.EditorDocument;
	var img = doc.getElementsByTagName('img');
	var map = [];
	for(var i=0;i<img.length;i++){
		if(img[i].src.indexOf(window.location.host) > -1) continue;
		var inMap = false;
		for(var j=0;j<map.length;j++){
			if(map[j].src == img[i].src){
				inMap = true;
				break;
			}
		}
		if(inMap) continue;
		map.push(img[i]);
	}
	if(map.length == 0){
		parent.qmeCms.dialogView.setMessage('系统没有发现可采集的远程图片');
		parent.qmeCms.dialogView.close(3000);
		return false;
	}
	var index = 0;
	(function(){
		if(index >= map.length){
			parent.qmeCms.dialogView.setMessage('远程图片抓取完毕');
			parent.qmeCms.dialogView.close(3000);
			return false;
		}
		var A = arguments;
		parent.qmeCms.dialogView.form("提示","<p class='loading'>第<strong style='color:green'>"+(index+1).toString()+"</strong>张图片抓取中....</p>",{type:'fixed'});
		parent.qmeCms.dialogView.ajaxGet("system.php?sys=fckupload&img="+encodeURIComponent(map[index].src)+"&channelid="+parent.config.channelid, function(resp){
			var json = parent.evaljson(resp);
			if(json == null || typeof json['status'] == 'undefined' || json['status'] != 200){
				alert("第"+(index+1)+"张图片抓取失败\n"+img[i].src);
			}else{
				for(var k=0;k<img.length;k++){
					if(img[k].src == json['img']){
						img[k].src = parent.config.url + json['url'];
						img[k].setAttribute('_fcksavedurl', parent.config.url + json['url']);
					}
				}
			}
			index++;
			A.callee();
		});
	})();
	return;
}
oMyCMCommand.GetState = function()
{
	return FCK_TRISTATE_OFF ;
}
FCKCommands.RegisterCommand( 'FCKuploadCommand', oMyCMCommand ) ;