<%
/*
<!--

    Copyright (C) 2003-2010 eXo Platform SAS.

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU Affero General Public License
    as published by the Free Software Foundation; either version 3
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, see<http://www.gnu.org/licenses/>.

-->
*/
%>
<%
  
  import org.exoplatform.webui.form.UIFormTextAreaInput;
  import org.exoplatform.social.webui.composer.UIComposer;
  import org.exoplatform.social.core.service.LinkProvider;

  def requestContext = _ctx.getRequestContext();

  def jsManager = requestContext.getJavascriptManager();
  def defaultInput = _ctx.appRes("UIComposer.input.Write_Something");

  jsManager.importJavascript("eXo.social.Util", LinkProvider.JAVASCRIPT_RESOURCE_URL);
  jsManager.importJavascript("eXo.social.webui.UINavigationComposer", "/eXoPlatformResources/javascript/");
  def params = """
    {
      composerId: '${uicomponent.getChild(UIFormTextAreaInput.class).getId()}',
      defaultInput: '$defaultInput',
      focusColor: null,
      blurColor: null,
      minHeight: '50px',
      maxHeight: null,
      focusHeight: null,
      focusCallback: composerFocus,
      blurCallback: composerBlur,
      keypressCallback: null,
      postMessageCallback: null
    }
  """

  jsManager.addOnLoadJavascript("initNavigationUIComposer");
%>

  <li class="$uicomponent.id ClearFix">
   <script type="text/javascript">
	  var currentOpenedMenu = null;
	  function composerFocus() {
		currentOpenedMenu = eXo.portal.UIPortalNavigation.currentOpenedMenu;
		eXo.portal.UIPortalNavigation.currentOpenedMenu = null;
	  }
	  function composerBlur() {
		eXo.portal.UIPortalNavigation.currentOpenedMenu = currentOpenedMenu;
	  }
	  var uinavigationComposer = uinavigationComposer || null;
	  function initNavigationUIComposer() {
	    if(uinavigationComposer){
	    	//get current value before re-init uicomposer instance
	    	var value = uinavigationComposer.getValue();
	
	    	//re-init uicomposer
	    	uinavigationComposer.configure($params);
	    	uinavigationComposer.init();
	
	    	//re-assign value for appropriate objects
	    	uinavigationComposer.composer.value = value;
	    	document.getElementById("navigationComposerInput").value = value;
	    	uinavigationComposer.composer.style.color = uinavigationComposer.focusColor;
			uinavigationComposer.composer.style.height = "50px";
	    } else {
	    	uinavigationComposer = new eXo.social.webui.UINavigationComposer($params);
			uinavigationComposer.composer.style.height = "50px";
	    }
	  }
	  function refreshNavigation() {
	    var composerExists = false;
	    try{
			composerExists = (''+uicomposer != ''+undefined);
		} catch(e){}
		if(composerExists){
			location.reload(true);
		} else {
			initNavigationUIComposer();
			uinavigationComposer.composer.value = '$defaultInput';
		}
	  }
	  function shareKeyPress(e) {
		// look for window.event in case event isn't passed in
		if (window.event) { e = window.event; }
		if (e.keyCode == 13) {
			//For enter.
			try {
				<%= uicomponent.event("PostMessage");%>;
			} catch(e) {
				alert(e);
			};
			//refreshNavigation();
		}
	}	  
  </script>
  <% uiform.begin() %>
		<div class="NavigationAvatar">
	      <img src="<%= uicomponent.getAvatarURL() %>">
	    </div>
		<div class="NavigationComposer" onkeypress="shareKeyPress(event);">
      		<% uicomponent.renderChild(UIFormTextAreaInput.class) %>
	    </div>
	<% uiform.end()%>
</li>
