//====================================2007-12-17 Editor start =========================================

//初始化全部信息
function initMenuText(){
	var sName = ["dvEditor_color","dvEditor_font","dvEditor_fontNum","dvEditor_PhFormat","dvEditor_ImgUrl","dvEditor_FlashUrl"];
	for (var i = 0; i < sName.length; i++) {
		var div = getObj(sName[i]);
		if (!div) continue 
		div.className=sName[i]+'_Out';
		div.onclick =function () {
			dvInitCode(this.id)
		}
		div.onmouseover = function() {menuMouseOver(this,this);};
		div.onmouseout = function() {menuMouseOut(this,this);};

	}
}
function menuMouseOver( o, css ) {
	if (o.className != css.id+'_Click')
	{
	o.className = css ? css.id+'_Over' :  "menu_item_high";
	}
}
function menuMouseOut( o, css ) {
	if (o.className != css.id+'_Click')
	{
		o.className = css ? css.id+'_Out' :  "menu_item";
	}
}

function dvEditorOver( o ) {
	if (o.className != 'Dvbbs_BtnMouseOverUp')o.className = "Dvbbs_BtnMouseOverUp";
}
function dvEditorOut( o ) {
	if (o.className != 'Dvbbs_Btn')o.className = "Dvbbs_Btn";
}


//主要部分
var __oold = null;
function dvInitCode(o) {
	if (o.indexOf('dvEditor') == -1)
	{
		return false;
	}
	if (o)
	{
		removeOld(__oold) //移除旧的
		__oold = o;

		var __oARR = o.split("_"); 
		var div;
		switch(__oARR[1]){
			case 'color':
				div=initColorText(o);
			break;
			case 'font':
				div=initFontText(o);
			break;
			case 'fontNum':
				div=initFontNumText(o);
			break;
			case 'PhFormat':
				div=initPhFormatText(o);
			break;
			case 'ImgUrl':
				div=initImgText(o);
			break;
			case 'FlashUrl':
				div=initImgText(o);
			break;
			
		}
		//按钮打开菜单div点击
		//div.onkeydown=__onkeydown;
		div.onblur =function (e){
			getObj(o).className=o+'_Out';
			document.body.removeChild(div);
		}
		//div.onmouseout = function() {document.body.removeChild(this);};
		//按钮的o点击
		getObj(o).onclick=function (e){	
			if (getObj(o).className == o+'_Click')
			{
				removeOld(o)
				__oold = null;
			}else{
				dvInitCode(o);
			}
		}
	}
	
}
function removeOld(o){
	if (o)
	{
		var __divoold = o+'_child';
		if (typeof(getObj(__divoold)) != 'undefined')
		{
			getObj(o).className=o+'_Out';//按钮样式改变
			document.body.removeChild(getObj(__divoold));//移除新创建DIV
		}
	}
}


//初始化====================================颜色
function initColorText(o){

	if (!getObj(o)) return;
	var sValue = dvColorValue;
	var sName = dvColorName;
	var arrS = ["彩色主题..."];
	var arrF = [null];
	for (var i = 0; i < sValue.length; i++) {
		arrS.push("<div id='"+sValue[i]+"' class='s"+sValue[i]+"_bg' style='margin-top:3px;width:15px;height:15px;float:left;'></div>&nbsp;"+sName[i]);
		arrF.push(function(e){
			var obj = e.target ? e.target : e.srcElement;
			if (obj.childNodes.length > 0)
			{
				getColorText(obj.childNodes[0].id);
				removeOld(o);//点击直接关闭
			}
		});
	}
	var div = createDivMenu(o, 70, 22 , arrS,arrF)
	return div;
}
//选择颜色设置
function getColorText(oName){
	/*(颜色|粗体-斜体-下划线-)*/
	var __v_f1;
	var __v_f2 = getObj('topicfont').value;
	var o = parseInt(oName) ;
	__v_f1 =  (__v_f2 !='') ? __v_f2+oName+'|' : __v_f1 = oName+'|';

	if (__v_f1.split(o+'|').length > 2)
	{
		var __v_arr = __v_f1.split(o+'|');
		__v_f1 = __v_arr[0]+__v_arr[1];
	}

	var __font;
	__font ='';
	__font += (__v_f1.indexOf('7|') !=-1)?'font-weight:bold;':'';
	__font += (__v_f1.indexOf('8|') !=-1)?'font-style:italic;':'';
	__font += (__v_f1.indexOf('9|') !=-1)?'text-decoration:underline;':'';
	if ( o === 7 || o === 8 || o === 9)
	{
		getObj('topicfont').value = __v_f1;
		getObj('topic').style.cssText = __font;
	}else{
		getObj('topic').className = 's'+oName;	
		getObj('topicximoo').value = oName;	
	}
}
//================================================控制创建DIV
function createDivMenu(oid, wid, hei , arrS, arrF) {
	var div = document.createElement('div');
	div.id = oid + '_child';
	div.className = 'eidtorListOut';//菜单外面
	document.body.appendChild(div);

	if (arrS !== undefined)
	{
		if (arrS.length > 5) var slist = 1; else var slist = 0;
		for (var i = 0; i < arrS.length; i ++) {
			var item = document.createElement("div");
			var width = 'width:'+(wid ? wid : '100')+'px;';
			var height = 'height:'+(hei ? hei : '22')+'px;';
			item.className = "menu_item";
			item.style.lineHeight = "2";
			if (i % 2 == 1)
			{
				item.style.cssText="float:right;border-bottom:1px dashed #dddddd;"+width+height;
			}else if (i % 2 == 0)
			{
				item.style.cssText="float:left;border-bottom:1px dashed #dddddd;"+width+height;
			}
			item.innerHTML = arrS[i];
			if (arrF[i]) {
				item.onmouseover = function() {itemMouseOver(this);};
				item.onmouseout = function() {itemMouseOut(this);};
				addEvent(item, "click", arrF[i]); //给条目增加事件
			}
			div.appendChild(item);
		}
	}
	div_location(oid, 1, div.id);
	getObj(oid).className = oid+'_Click';
	return div;
}

//初始化=====================================字体
function initFontText(o){
	if (!getObj(o)) return;
	var sValue = dvFontValue
	var sName = dvFontName;
	var arrS = [];
	var arrF = [];
	for (var i = 0; i < sValue.length; i++) {
		arrS.push(sValue[i]+"@@@"+sName[i]);
		arrF.push(function(e){
			var obj = e.target ? e.target : e.srcElement;
			FormatText ('fontname',obj.id);
			//getObj(o).className=o+'_Out';
			//document.body.removeChild(obj.parentNode);
			removeOld(o);
		});
	}
	var div = createMenu(o, 85, 22 , arrS,arrF)
	return div;
}


//初始化==========================================字号
function initFontNumText(o){
	if (!getObj(o)) return;
	var sValue = dvFontNumValue
	var sName = dvFontNumName;
	var arrS =[];
	var arrF =[];
	for (var i = 0; i < sValue.length; i++) {
		arrS.push(sValue[i]+"@@@"+sName[i]);
		arrF.push(function(e){
			var obj = e.target ? e.target : e.srcElement;
			FormatText ('fontsize',obj.id);
			removeOld(o);
		});
	}
	var div = createMenu(o, 85, 22 , arrS,arrF)
	return div;
}

//初始化==========================================段落方式
function initPhFormatText(o){
	if (!getObj(o)) return;
	var sValue = dvPhFormatValue
	var sName = dvPhFormatName;
	var arrS =[];
	var arrF =[];
	for (var i = 0; i < sValue.length; i++) {
		arrS.push(sValue[i]+"@@@"+sName[i]);
		arrF.push(function(e){
			var obj = e.target ? e.target : e.srcElement;
			FormatText ('FormatBlock',obj.id);
			removeOld(o);
		});
	}
	var div = createMenu(o, 85, 22 , arrS,arrF)
	return div;
}

//================================================图片和flash
function initImgText(o){
	
	if (!getObj(o)) return;
	var __oARR = o.split("_"); 
	var __name;
	if (__oARR[1]=='ImgUrl')
	{	
		__name='图片';
	}else if (__oARR[1]=='FlashUrl')
	{
		__name='Flash';
	}
	var str = '<div style="height:20px;margin-top:10px;">'+__name+'地址:<input type="text" id="' + o + '_url" style="width:70%;margin:0px;" value="http://"></div><div  style="height:20px;margin-top:10px;"> 宽度:<input type="text" id="' + o + '_w" style="width:50px;height:18px;margin:0px;" value="300">高度:<input type="text" id="' + o + '_h" style="width:50px;height:18px;margin:0px;" value="200"></div>';
	var div = createMenu(o, 70, 22 ,str)
	getObj(o+'_submit').onclick=function (){
		var oUrl= o + '_url';
		var oUrlValue = getObj(oUrl).value;
		var owValue = getObj(o+'_w').value;
		var ohValue = getObj(o+'_h').value;
		var f=oUrlValue.substr(oUrlValue.lastIndexOf(".")+1,3); 
		if(f=="gif"||f=="jpg" ||f=="swf" ){ 
			DB_form.DB_HTML.focus();
			insertAtCaret(DB_form.DB_HTML,owValue,ohValue,'',oUrlValue);
			removeOld(o);
		}else{
			alert('请填写正确的'+__name+'路径!');
		}
	}
	return div;
	
}

//================================================控制创建DIV
function createMenu(oid, wid, hei , arrS, arrF) {
	var div = document.createElement('div');
	div.id = oid + '_child';
	div.className = 'eidtorListOut';//菜单外面
	//div.style.width="200px";
	//div.style.cssText="width:180px;float:left;border:1px solid red";
	document.body.appendChild(div);
	if (arrS !== undefined && arrS[0]!==undefined)
	{
		if (arrS.length > 5) var slist = 1; else var slist = 0;

		for (var i = 0; i < arrS.length; i ++) {
			var item = document.createElement("input");
			var width = 'width:'+(wid ? wid : '100')+'px;';
			var height = 'height:'+(hei ? hei : '22')+'px;';
			item.className = "menu_item";
			item.style.lineHeight = "2";
			item.type="button";

			if (i % 2 == 1)
			{
				item.style.cssText="float:right;border-bottom:1px dashed #dddddd;"+width+height;
			}else if (i % 2 == 0)
			{
				item.style.cssText="float:left;border-bottom:1px dashed #dddddd;"+width+height;
			}
			var getarrS = arrS[i].split("@@@");
			item.id = getarrS[0];
			item.value = getarrS[1];
			item.name = getarrS[1];

			if (arrF[i]) {
				item.onmouseover = function() {itemMouseOver(this);};
				item.onmouseout = function() {itemMouseOut(this);};
				addEvent(item, "click", arrF[i]); //给条目增加事件
			}
			div.appendChild(item);
			

		}

			
	}else{
		var oidOut = oid+'_Out';
		div.innerHTML = '<div unselectable="on">' + arrS + '<br /><center><input type="button" id="' + oid + '_submit" value="确定" /> &nbsp; <input type="button" onClick="try{removeOld(\''+oid+'\');}catch(e){}" value="取消" /></center></div>';
	}
	div_location(oid, 1, div.id);
	getObj(oid).className = oid+'_Click';
	
	if(IframeID!=null)IframeID.focus();

	return div;
}

function __onkeydown(e) {
	e = e ? e : event;
	//alert(e+'fffffffffffffffff')
}
function addEvent(o, e, func, r){

	if (o.addEventListener) {
		
		r ? o.removeEventListener(e, func, false) : o.addEventListener(e, func, false);
	}
	else if (o.attachEvent) {
		r ? o.detachEvent("on" + e, func) : o.attachEvent("on" + e, func);
	}
	else {
		o["on" +e] = r ? null : func;
	}
}

function itemMouseOver( o ) {

	o.className =  "menu_item_high";
}
function itemMouseOut( o ) {
	o.className =  "menu_item";
}

//====================================2007-12-17 Editor start =========================================
