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;
import org.zhios.website.admin.model.SysConfig;


public class ImagePL extends BasePageLet implements PageLetObj {

	public ImagePL(String divid) {
		super(divid);
		// TODO Auto-generated constructor stub
	}
	
	public ImagePL(String divid, String okfunc, String cancelfunc, String outerhtml) {
		super(divid,okfunc,cancelfunc,outerhtml);
		// TODO Auto-generated constructor stub
	}
	
	public void setFillStr(){
		//Get previous value of every fills.
		String width="",height="",image="",divfloat="",link="";
		if(!OuterStr.isEmpty()){
			divfloat=HtmlOpt.readFirstTagStyleAttrValue(OuterStr,"div","float");
			if(divfloat.isEmpty()) divfloat="center";
			
			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);
			}
			
			image=HtmlOpt.readFirstTagAttrValue(OuterStr,"img","src");
			if(!image.isEmpty()){
				image=image.substring(image.indexOf("/userimage/")+"/userimage/".length());
			}

			link=HtmlOpt.readFirstTagAttrValue(OuterStr,"a","href");
			int checkint=link.indexOf(SysConfig.RootUrl+"/");
			if(checkint>=0) link=link.substring(checkint+(SysConfig.RootUrl+"/").length());
		}
		
		//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("image",Strings.PLImage,"image",image,"",true);
		setFill("text",Strings.PLLink,"link",link,"",false);
	}

	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 image=SysConfig.ResourceUrl+"/userimage/"+doc.getElementsByTagName("image").item(0).getTextContent();;
		String link=doc.getElementsByTagName("link").item(0).getTextContent();
		
		ViewStr="<div id='"+DivID+"' tmstyle='image' style='"+floatstr+";width:"+width+"px;height:"+height+"px'>";
		if(link.isEmpty()){
			ViewStr+="<img src='"+image+"' width='100%' height='100%' border='0'>";
		}
		else{
			link=SysConfig.RootUrl+"/"+link;
			ViewStr+="<a href='"+link+"'><img src='"+image+"' width='100%' height='100%' border='0'></a>";
		}
		ViewStr+="</div>";
	}
}
