var objAjax=new JAjax();
var tools=new toolsClass();

var isSave=0;
var fname=null;
var fpath=null;

function loadp(){
	
	tools.add("","打开","openFile()","打开一个文件准备修改")
	tools.add("","保存代码","saveFile()","保存编辑器中的代码")
	tools.create(toolsDiv);
	loadBodyclick();
	
	var option=new optionClass();
    tObj=document.getElementsByName("optionTitle");
	oObj=document.getElementsByName("optionBody")
	option.titleObj=tObj;
	option.bodyObj=oObj;
	option.create();
	setWH();
		
}


function setWH(){
	var h=Element.getHeight(document.getElementById("winDiv"));
	var h2=h-54;
    var b=$name("optionBody");
	$("jobDiv").style.height=h2+"px";
	$("editIf").style.height=h2+"px";
	b[0].style.height=h2+"px";
	b[1].style.height=h2+"px";
	$("editCodeText").style.height=(h2-20)+"px";
	
	//alert(Element.getHeight("editIf")+"|"+h2+"|"+h);
	
}


function  saveFile(){
    if (isSave==0){
        alert("你没有修改文件代码, 无需保存");
        return;
    }
    var objResponse=new JHttpResponse();
    objResponse.write("fileName",fname);
	objResponse.write("path",fpath);
	objResponse.write("body",$("editCodeText").value.replace(/<script/g,"<ＳＣＲＩＰＴ"));
	var out=objAjax.getText("post","editFile.aspx?type=saveFile",objResponse,null);
    if (out=="2"){
        alert("你没有权限修改文件代码,只有系统管理员才有这个权限");
        return;
    }
    if (out=="1"){
       isSave=0;
       alert("文件修改成功");
        return;
    }else{
        alert(out);
    }
    
}




function openFile(){
	var fileName= openSelectEditFile("",".aspx,.html,.htm");
	if (fileName!=null&fileName!=""){
	    fname=fileName;
	    if (fileName.indexOf("bbs/")!=-1){
	        var f=fileName.split("/");
	        fileName=f[f.length-1];
		    editIf.location.href="../../bbs/editLabelReadFile.aspx?fileName="+fileName;
		    readFileCode(fileName,"bbs");
		    fpath="bbs";
		    
	    }else{
	        readFileCode(fileName,"");
		    editIf.location.href="../../editLabelReadFile.aspx?fileName="+fileName;
		    fpath=""
		}
		writeFettleLeft("正在编辑:"+fileName+"  请点击页面中灰色的标签名，修改这些标签的参数。");
	}
}


function readFileCode(fileName,path){
    var objResponse=new JHttpResponse();
	objResponse.write("fileName",fileName);
	objResponse.write("path",path);
	var out=objAjax.getText("post","editFile.aspx?type=readFile",objResponse,null);
	$("editCodeText").value=out;
}


function loadPage()
{
	var objs=editIf.document.getElementsByName("myLabelName");
	if (objs.length==0){
	    try{
		    alert("此页面中没有发现可供修改的标签");
		    editIf.document.body.innerHTML="";
		}catch(e) {}
		return;
	}
	for (var i =0; i<objs.length;i++){
		objs[i].style.cssText="cursor:hand;background-color:#cfcfcf;padding:2px 4px 2px 4px;";
		objs[i].onclick=function(){
			var labelText=this.innerText;
			
			var objResponse=new JHttpResponse();
			objResponse.write("labelText",labelText);
			var out=objAjax.getText("post","editFile.aspx?type=rLabelPar",objResponse,null);
			if (out=="-1"){
				alert("标签不存在,可能已经删除");
				return;	
			}
			if (out.indexOf(c1)==-1){
				alert("错误:\n"+erStr(out))
				return;	
			}
			out=out.split(c1);
			
			var objnum=parent.win.objNum;
			//createLabel.htm?temp=createid()&wNum=&ID="+out[2]+"&lType="+out[0]+"&lClass="+out[1]+"&lName="+labelText;
			var par="wNum="+objnum+"&ID="+out[2]+"&lType="+out[0]+"&lClass="+out[1]+"&lName="+labelText;
			parent.win.pCreate("p-createLabel_Modi",par,"修改标签",true);
		}
	}
	
	
}
