<%
	/**
	 * Created by The eXo Platform SARL
	 * Author : Tran The Trong
	 *          trongtt@gmail.com
	 * Aug 14, 2006
	 * version: $Id$
	 */  
%>
<%
  import org.gatein.common.text.EntityEncoder;
  import org.exoplatform.commons.utils.HTMLEntityEncoder;
  
	String popupId = uicomponent.getId();
	
	boolean printMessages(List messages, String messType, boolean isSelected) {
	  if (messages.size() == 0) return false;
		def style = "";
		def icon = "uiIconArrowRight uiIconLightGray";
		if(!isSelected) {
			style = "in";
			icon = "uiIconArrowDown uiIconLightGray";
			isSelected = true;
		}
		def ltype = messType.toLowerCase();
		def id = uicomponent.getId();
	
		println "<li>";
		println "  <a data-toggle=\"collapse\" href=\"#${id}-${messType}\">";
		println "     <i class=\"$icon\"></i>";
		println "     <span class=\"${ltype}Icon\">";
		def quantity = messages.size() > 1 ? "s" : "";
		println messages.size() + " " + _ctx.appRes("UIPopupMessages.label."+"$messType" + quantity);
		println "     </span>";
		println "  </a>";
		println "  <ul id=\"${id}-${messType}\" class=\"collapse $style\">";
		for(mess in messages) {
			// if(mess.messageKey == null) continue;
			print "		<li>- ";
			String msgValue = mess.getMessage();
			EntityEncoder entityEncoder = EntityEncoder.FULL;
			println entityEncoder.encode(msgValue);
			println "		</li>";
		}
		println "	  </ul>";
		println "</li>";
		return isSelected;
	}
%>
<div class="UIPopupWindow UIDragObject uiPopup" id="$popupId" style="width: 550px; display: none;">
  <%  
    def rcontext = _ctx.getRequestContext();
    def reqJS = rcontext.getJavascriptManager().require("SHARED/uiPopupMessages", "popupMsg");
    reqJS.addScripts("popupMsg.init('$popupId');");

    if(uicomponent.hasMessage()){
      reqJS.addScripts("popupMsg.show('$popupId', $uicomponent.showMask);");

      def errors = uicomponent.getErrors();
      def warnings = uicomponent.getWarnings();
      def infos = uicomponent.getInfos();  
      def single = errors.size() + warnings.size() + infos.size() == 1;
      def msg, msgType;
      if (single) {
         if(errors.size() != 0) {
            msgType = "error";
            msg = errors.get(0);
         } else if (warnings.size() != 0) {
            msgType = "warning";
            msg = warnings.get(0);
         } else {
            msgType = "info";
            msg = infos.get(0);
         }
      }
  %>
	<div class="popupHeader clearfix">
		<a class="uiIconClose pull-right" title="<%=_ctx.appRes("UIPopupMessages.Close")%>" onclick="<%=uicomponent.event("Close")%>"></a>
		<span class="PopupTitle popupTitle">
			<%
				if (single) {
				   println _ctx.appRes("UIPopupMessages.label.${msgType}");
				} else {
				   println _ctx.appRes("UIPopupMessages.title.exoMessages"); 
				}
			%>
		</span>
	</div>
	
	<div class="PopupContent popupContent">	
	<%
		  if(single) {
		    println "<ul class=\"singleMessage popupMessage resizable\">";
			  println "	 <li>";
			  println "    <span class=\"${msgType}Icon\">";
			  println        HTMLEntityEncoder.getInstance().encode(msg.getMessage());
			 	println "    </span>";
				println "  </li>";
				println "</ul>";
		  } else {
		    //Multiple style popup message
	%>
		<ul class="multipleMessage popupMessage resizable">
			<%
			def flag = false;
			flag = printMessages(errors, "error", flag);
			flag = printMessages(warnings, "warning", flag);
			flag = printMessages(infos, "info", flag);
			%>
		</ul>			
	<%  } %>			
				
		<div class="uiAction <%=single ? "uiActionBorder" : ""%>">
			<a href="javascript:void(0);" onclick="<%=uicomponent.event("Close")%>" class="btn"><%=_ctx.appRes("UIPopupMessages.button.ok")%></a>
		</div>
	</div>
	
	<%
    } else if (rcontext.useAjax()) {
        reqJS.addScripts("popupMsg.hide('$popupId', $uicomponent.showMask);");
    }
	%>
</div>