// 自动格式化插件
UE.registerUI('plugins_autostyleset',function(editor,uiName){
	//自动化执行函数
	autoStyleSet = function(cmd, obj){
		
		var format_Options = G_Options["format"];
		var paragraphBeforeBlank = format_Options["paragraphBeforeBlank"];
		var imageJustify = format_Options["imageJustify"];
		var retainBold = format_Options["retainBold"];
		var retainBold = format_Options["retainBold"];
		var imageCaptionText = format_Options["imageCaptionText"];
		
//		var cont = this.getContent();
//		var t_div = document.createElement("div"); 
//		t_div.innerHTML = cont;
		this.fireEvent("plugins_autostyleset_before");
		
		var Bianji_IMGS = new Array();
		
		var t_imgs = this.document.getElementsByTagName("img");
		if (t_imgs != null && t_imgs.length > 0) {
			for (j =  t_imgs.length-1 ; j >= 0; j--) {
				var t = document.createElement("IMG");
				t.alt = t_imgs[j].alt;
				t.src = t_imgs[j].src;
				Bianji_IMGS[j] = t;
				t_imgs[j].outerHTML = "<br />#Bianji_IMGS_" + (j) + "#<br />";
			}
		}
		
		if(retainBold==true) {
			keepHtmlTag_before("strong", this);
		}
		
		var text = this.getContentTxt();
		var tmps = text.split("\n");
		var t_html = "";
		var t_sign = 0;
		
		for (i = 0; i < tmps.length; i++) {
			var tmp = tmps[i].trim();
			if (tmp.length > 0) {
				if (tmp.substr(0, 13)=="#Bianji_IMGS_"){
					t_html += "<p style=\"TEXT-ALIGN:" + imageJustify + "\">" + tmp + "</p>";
					t_sign = 1;
				}
				else{
					if ( t_sign==1&&imageCaptionTextSign(tmp) ){
					t_html += "<p style=\"TEXT-ALIGN:" + imageCaptionText + "\">" + tmp + "</p>";
					} else {
						tmp = tmp.replaceAll("#Bianji_Tag_S_strong#　　", "#Bianji_Tag_S_strong#");
						tmp = tmp.replaceAll("　　", "\r\n");
						tmp_split = tmp.split("\r\n");
						for(jishu_k = 0; jishu_k<tmp_split.length; jishu_k++){
							tmp_split[jishu_k] = paragraphBeforeBlank + tmp_split[jishu_k];
							t_html += "<p>" + tmp_split[jishu_k] + "</p>";
						}
					}
					t_sign = 0;
				}
			}
		}
		
		if (Bianji_IMGS != null && Bianji_IMGS.length > 0) {
			for (j = Bianji_IMGS.length-1; j >= 0; j--) {
				var imghtml = "<img src=\"" + Bianji_IMGS[j].src + "\" alt=\"" + Bianji_IMGS[j].alt + "\">";
				t_html = t_html.replace("#Bianji_IMGS_" + j + "#", imghtml);
			}
		}
		
		if(retainBold==true) {
			t_html = keepHtmlTag_after("strong", t_html);
		}
		//t_div.innerHTML = t_html;
		
		this.setContent(t_html);
		this.fireEvent("plugins_autostyleset_after");
		this.execCommand('selectall');
		this.execCommand('copy');
		this.fireEvent("plugins_autostyleset_complete");

	};
	
	autoStyleSetState = function(){
		return 0;
	};
	
	function imageCaptionTextSign( str ) {
		var format_Options = G_Options["format"];
		imageCaptionText_MaxLength = parseInt(format_Options["imageCaptionText_MaxLength"]);
		imageCaptionText_EndSign = format_Options["imageCaptionText_EndSign"];
		if(str.length>imageCaptionText_MaxLength) return false;
		strend = str.substr(str.length-1, 1);
		if(imageCaptionText_EndSign.indexOf(strend)>=0) return false;
		return true;
	};
	
	function keepHtmlTag_before( tag, me ) {
		
		var t_tags = me.document.getElementsByTagName(tag);
		if (t_tags != null && t_tags.length > 0) {
			for (j = t_tags.length-1; j >= 0; j--) {
				t_tags[j].outerHTML = "#Bianji_Tag_S_" + tag + "#" + t_tags[j].innerHTML + "#Bianji_Tag_E_" + tag + "#";
			}
		}
		
	}
	
	function keepHtmlTag_after( tag, t_html ) {
		
		t_html = t_html.replaceAll("#Bianji_Tag_S_" + tag + "#", "<" + tag + ">", "");
		t_html = t_html.replaceAll("#Bianji_Tag_E_" + tag + "#", "</" + tag + ">", "");
		return t_html;
		
	}
	
	//注册命令
	editor.registerCommand(uiName,{
		execCommand:autoStyleSet
		, queryCommandState:autoStyleSetState
	});

	//创建按钮
	var btn = new UE.ui.Button({
		name:"plugins_autostyleset",
		title:"自动排版",
		cssRules :'background-position: 0px -120px;',
		onclick:function () {
			editor.execCommand(uiName, "");
		}
	});
	
	editor.addListener('afterpaste', function () {
		var auto_format = G_Options["auto_format"];
		if(auto_format["format"]==true) {
			editor.execCommand(uiName, "");
		}
	});
	
	//监听事件
	editor.addListener('selectionchange', function () {
		var state = editor.queryCommandState(uiName);
		if (state == -1) {
			btn.setDisabled(true);
			btn.setChecked(false);
		} else {
			btn.setDisabled(false);
			btn.setChecked(state);
		}
	});

	return btn;
},2,0);