// 对象clone

function CloneObj(o)
{
	var newObj=new Object();
	for(var i in o){
		newObj[i] = o[i];
	}
	return newObj;
};

// tinymce默认设置
var tinyset={
	mode : "textareas",
	save_callback : "mceSave",
	urlconverter_callback : "mceURLConverter",
	theme : "advanced",
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	theme_advanced_statusbar_location : "bottom",
	language : "cn",
	dialog_type : "modal",
	relative_urls : false,
	remove_linebreaks : false,
	forced_root_block : "p",
	force_br_newlines : false,
	event_elements : "a,img,span,div",
	extended_valid_elements : "iframe[src|frameborder=0|width|height|align|scrolling|name|id|style|marginheight|marginwidth|border],"+
		"script[src|type|language],"+
		"form[action|method|name|style],"+
		"center,"+
		"input[type|name|value|checked|src|alt|size|maxlength|onfocus|onclick|onblur|id],"+
		"button[name|value|type|onclick],"+
		"select[name|size|multiple|onchange|id],"+
		"textarea[name|rows|cols|onfocus|onclick|onblur|id]",
	width : 550,
	
	add_form_submit_trigger : true,
	submit_patch : true,
	add_unload_trigger : false,
	content_css : "style/tinymceContent.css",
	
	plugins : "preview,media,inlinepopups",
	
	theme_advanced_disable : "styleselect,formatselect,justifyfull,strikethrough",
	theme_advanced_resizing : true,
	theme_advanced_resizing_use_cookie : false,
	invalid_elements : "",
	media_strict : false,
	
	plugin_preview_width : 700,
	plugin_preview_height : 600
	
};

// 新闻内容编辑
var tinysetCon=CloneObj(tinyset);
	tinysetCon.editor_selector="mceContent";
	tinysetCon.height=260;
	tinysetCon.width=550;
	tinysetCon.plugins+=",fullscreen,table,searchreplace,paste";
	tinysetCon.paste_auto_cleanup_on_paste=false;
	tinysetCon.paste_text_use_dialog=true;
	tinysetCon.theme_advanced_buttons1="undo,redo,|,bold,italic,underline,strikethrough,sub,sup,removeformat,|,forecolor,backcolor,|,outdent,indent,|,fontselect,fontsizeselect";
	tinysetCon.theme_advanced_buttons2="paste,pasteword,pastetext,cleanup,|,link,unlink,|,bullist,numlist,|,justifyleft,justifycenter,justifyright,justifyfull,|,hr,|,formatselect,styleselect";
	tinysetCon.theme_advanced_buttons3="image,media,|,tablecontrols,|,charmap,search,code,preview,fullscreen,help";
	tinysetCon.theme_advanced_disable="";
	tinysetCon.theme_advanced_fonts="Andale Mono=andale mono,times;Arial=arial,helvetica,sans-serif;Arial Black=arial black,avant garde;Book Antiqua=book antiqua,palatino;"+
		"Comic Sans MS=comic sans ms,sans-serif;Courier New=courier new,courier;Georgia=georgia,palatino;Helvetica=helvetica;Impact=impact,chicago;Symbol=symbol;"+
		"Tahoma=tahoma,arial,helvetica,sans-serif;Terminal=terminal,monaco;Times New Roman=times new roman,times;Trebuchet MS=trebuchet ms,geneva;Verdana=verdana,geneva;"+
		"Webdings=webdings;Wingdings=wingdings,zapf dingbats;宋体=宋体;黑体=黑体;楷体=楷体_GB2312;仿宋=仿宋_GB2312";
	tinysetCon.theme_advanced_styles="引用=quote;代码=qcode";
	tinysetCon.theme_advanced_resizing_use_cookie=true;
	tinysetCon.swfupload_log_function=window.AddUpload;	// 记录上传文件函数
	tinysetCon.setup=mceSetup;

//新闻简介编辑
var tinysetSum=CloneObj(tinyset);
	tinysetSum.editor_selector="mceSummary";
	tinysetSum.theme_advanced_buttons2="";
	tinysetSum.theme_advanced_buttons3="";
	tinysetSum.theme_advanced_buttons1_add_before="undo,redo,|";
	tinysetSum.theme_advanced_buttons1_add="forecolor,backcolor,fontsizeselect,removeformat,|,link,unlink,image,media,preview,help";
	tinysetSum.theme_advanced_disable="styleselect,formatselect,justifyfull,strikethrough";
	tinysetSum.swfupload_log_function=window.AddUpload;
	tinysetSum.setup=mceSetup;
	
// 系统设置中编辑
var tinysetSys=CloneObj(tinyset);
	tinysetSys.editor_selector="mceEditor";
	tinysetSys.theme_advanced_buttons2="";
	tinysetSys.theme_advanced_buttons3="";
	tinysetSys.theme_advanced_buttons1_add_before="undo,redo,|";
	tinysetSys.theme_advanced_buttons1_add="forecolor,backcolor,fontsizeselect,removeformat,|,link,unlink,image,media,preview,code,help";
	tinysetSys.theme_advanced_disable="styleselect,formatselect,justifyfull,strikethrough";
	tinysetSys.swfupload_sys=true;	// 直接上传

// 内容
tinyMCE.init(tinysetCon);

// 简介
tinyMCE.init(tinysetSum);

// 系统设置
tinyMCE.init(tinysetSys);

// 保存数据前处理
function mceSave(eleId, html, body){
	var reP=/^\s*<p>.*<\/p>\s*$/ig;
	if(-1<html.search(reP) && html.match(/<p>/ig).length==1){
		html=html.replace(/<\/?p>/g, "");
	}else{
		var reDiv=/^\s*<div>(.*)<\/div>\s*$/ig;
		if(-1<html.search(reDiv) && html.match(/<div>/ig).length==1){
			html=html.replace(/<\/?div>/g, "");
		}
	}
	html=html.replace(/&nbsp;&nbsp;/g,"&nbsp; ");
	return html;
}

// tinyMCE地址转换有点问题
function mceURLConverter(url, node, on_save) {
	var du=tinyMCE.activeEditor.documentBaseURI.getURI();
	if(url.length>du.length && url.toLowerCase().indexOf(du.toLowerCase())==0){
		url=url.substring(du.length);
	}
	return url;
}

// ctrl+enter提交
function mceSetup(ed){
	ed.onKeyUp.add(function(ed,e){
		if(e.ctrlKey && e.keyCode==13){
			$(":submit:last",ed.getElement().form).click();
		}
	});
}