package org.zhios.website.admin.pagelet;

import org.w3c.dom.Document;
import org.zhios.website.admin.model.HtmlOpt;
import org.zhios.website.admin.model.Strings;

public class HtmlPL extends BasePageLet implements PageLetObj {
	public HtmlPL(String divid) {
		super(divid);
		// TODO Auto-generated constructor stub
	}
	
	public HtmlPL(String divid, String okfunc, String cancelfunc, String outterhtml) {
		super(divid,okfunc,cancelfunc,outterhtml);
		// TODO Auto-generated constructor stub
	}
	
	public void setFillStr(){
		//Get previous value of every fills.
		String width="",height="",divfloat="",htmlcode="";
		if(!OuterStr.isEmpty()){
			width=HtmlOpt.readFirstTagStyleAttrValue(OuterStr,"div","width");
			if(!width.isEmpty()){
				int px=width.indexOf("px");
				if(px>0) width=width.substring(0,px);
			}
			
			height=HtmlOpt.readFirstTagStyleAttrValue(OuterStr,"div","height");
			if(!height.isEmpty()){
				int px=height.indexOf("px");
				if(px>0) height=height.substring(0,px);
			}
			
			divfloat=HtmlOpt.readFirstTagStyleAttrValue(OuterStr,"div", "float");
			if(divfloat.isEmpty()) divfloat="center";
						
			htmlcode=HtmlOpt.replaceLineFeedtoRN(HtmlOpt.readFirstTaginnerHTML(OuterStr,"div"));
		}
		
		//Set every fills.
		setFill("text",Strings.PLWidth,"width",width,"",true);
		setFill("text",Strings.PLHeight,"height",height,"",true);
		setFill("select",Strings.PLFloat,"float",divfloat,"left,center,right",true);
		setFill("textarea",Strings.PLHtml,"htmlcode",htmlcode,"",true);
	}

	public void setViewStr(Document doc){
		String divfloat=doc.getElementsByTagName("float").item(0).getTextContent();
		String floatstr;
		if(divfloat.equals("center")) floatstr="margin:auto";
		else floatstr="float:"+divfloat;
		String width=doc.getElementsByTagName("width").item(0).getTextContent();
		String height=doc.getElementsByTagName("height").item(0).getTextContent();
		String temphtmlcode=doc.getElementsByTagName("htmlcode").item(0).getTextContent();
		
		String htmlcode=HtmlOpt.replaceLineFeedtoBR(temphtmlcode);
		
		ViewStr="<div id='"+DivID+"' tmstyle='html' style='"+floatstr+";width:"+width+"px;height:"+height+"px;word-wrap:break-word;overflow:hidden'>"+htmlcode+"</div>";

		return;
	}
}
