<%
  import javax.jcr.Node;
  import javax.jcr.PathNotFoundException;
	import java.util.List;
	import org.exoplatform.services.cms.taxonomy.TaxonomyService;
	import org.exoplatform.services.cms.folksonomy.NewFolksonomyService;
	import org.exoplatform.services.wcm.images.RESTImagesRendererService;
	import org.exoplatform.services.wcm.core.NodeLocation;
	import org.exoplatform.services.wcm.skin.XSkinService;
	import org.exoplatform.services.wcm.javascript.XJavascriptService;
	
	public String getRimOfImage(String property, Node currentNode) throws Exception {
    String rim = "";
    try {
      rim = currentNode.getProperty(property).getString();
      if (!(rim.indexOf("%") > 0)) rim = rim + "px";
    } catch(PathNotFoundException pnf) {
      rim = "100px";
    }
    return rim;
  }
	
	public boolean getPortletPreference(String name) {
    String value = _ctx.getRequestContext().getRequest().getPreferences().getValue(name, null);
    if(value != null) return Boolean.parseBoolean(value);
    return false;
  }
  
  	def currentNode = uicomponent.getNode() ; 
    def html = currentNode.getNode("default.html/jcr:content").getProperty("jcr:data").getString();
  	def css = currentNode.getNode("css/default.css/jcr:content").getProperty("jcr:data").getString();
  	def js = currentNode.getNode("js/default.js/jcr:content").getProperty("jcr:data").getString();  
  	// NOTE : You can use services if you're adding external css or js thru WebDAV. If not, direct access to the node is more efficient.
  	//def css = uicomponent.getApplicationComponent(XSkinService.class).getActiveStylesheet(currentNode);
  	//def js =  uicomponent.getApplicationComponent(XJavascriptService.class).getActiveJavaScript(currentNode);
  
	boolean isShowCategories = getPortletPreference("ShowCategories");
	boolean isShowTags = getPortletPreference("ShowTags");

	if (currentNode.hasProperty("exo:image")) {
		RESTImagesRendererService imagesRenderer = uicomponent.getApplicationComponent(RESTImagesRendererService.class);								
		def imageURI = imagesRenderer.generateImageURI(currentNode,"exo:image")
		String imageWidth = getRimOfImage("exo:imageWidth", currentNode);
		String imageHeight = getRimOfImage("exo:imageHeight", currentNode);
		%>
			<div style="float:left; padding-right: 10px;"><img src="$imageURI" width="<%=imageWidth%>" height="<%=imageHeight%>"/></div>
		<%
	}
	if((css != null) && (css.length() > 0)) {
		%>
			<style type="text/css">
				/* <![CDATA[ */			
					<%=css%>
				/* ]]> */
			</style>
		<%
	}

	if((js != null) && (js.length() > 0)) {
		%>
			<script type="text/javascript">
				/* <![CDATA[ */	
					<%=js%>
				/* ]]> */		
			</script>
		<% 
	}
	
	if (isShowCategories || isShowTags) {
		%>
			<div class="WebContentContainer">
				<div class="WebContentInformation">
					<% 
						if(isShowCategories) {
							TaxonomyService taxonomyService = uicomponent.getApplicationComponent(TaxonomyService.class);
			      	List<Node> categoryNodeList = taxonomyService.getAllCategories(currentNode);
			      	if (!categoryNodeList.isEmpty()) {
			      		StringBuffer categories = new StringBuffer();
				       	for (Node category: categoryNodeList) {
				        	categories.append(category.getName()).append(", ");
				       	}
				    	 	categories.replace(categories.lastIndexOf(", "), categories.size(), "");
								%>
						  		<span class="Categories"><strong><%=_ctx.appRes("Category.view.label")%>:</strong> <%= categories.toString(); %></span>
								<%		
			      	}
						}
						if(isShowTags) {
							NewFolksonomyService folkService = uicomponent.getApplicationComponent(NewFolksonomyService.class);
							def nodeLocation = NodeLocation.make(currentNode);
							List<Node> tagList = folkService.getLinkedTagsOfDocument(currentNode, nodeLocation.getRepository(), nodeLocation.getWorkspace());
							if(!tagList.isEmpty()) {
								StringBuffer tagBuff = new StringBuffer("[");
								for(Node tag: tagList) {
									tagBuff.append(tag.getName()).append(", ");
								}
								tagBuff.replace(tagBuff.lastIndexOf(", "), tagBuff.size(), "]");
								%>
									<span class="Tags"><strong><%=_ctx.appRes("Tag.view.label")%>:</strong> <%= tagBuff.toString(); %></span>
								<%	
							}	
						}
						%>
					</div>
					<%= html %>
				</div>
		<%
	} else {
		%><%= html %><%
	}
%>