<%
  import java.util.ArrayList;
  import java.util.GregorianCalendar;
  import java.util.List;
  import javax.jcr.Node;
  import javax.jcr.Session;
  import javax.jcr.NodeIterator;
  import org.exoplatform.webui.core.UIComponent;
  import org.exoplatform.services.jcr.util.Text;
  import org.exoplatform.ecm.webui.utils.Utils;
  import org.exoplatform.services.jcr.RepositoryService;
  import java.util.Calendar;
  import org.exoplatform.wcm.webui.Utils;
  import org.exoplatform.services.jcr.ext.hierarchy.NodeHierarchyCreator;
  import org.exoplatform.services.security.ConversationState;
  import org.exoplatform.services.wcm.core.NodetypeConstant;
  import org.exoplatform.services.wcm.utils.WCMCoreUtils;

  UIComponent uiParent = uicomponent.getParent();

  List<String> removeNodes = new ArrayList<String>();
  def presentNodes = uicomponent.getPresentNodes();
  def links = new ArrayList<String>();
  def title = "";
  if (presentNodes.size() == 0) {
    def currentNode = uicomponent.getFileLangNode(uiParent.getNode()) ;
    def originalNode = uiParent.getOriginalNode();
    title = Text.unescapeIllegalJcrChars(originalNode.getName());
    presentNodes.add(currentNode);
  }
  RepositoryService rService = uicomponent.getApplicationComponent(RepositoryService.class) ;
  String repository = rService.getCurrentRepository().getConfiguration().getName() ;
  def link = "";
  StringBuffer urlBuf = new StringBuffer();
  String userName = ConversationState.getCurrent().getIdentity().getUserId();
  NodeHierarchyCreator nodeHierarchyCreator = WCMCoreUtils.getService(NodeHierarchyCreator.class);
  Node userNode = nodeHierarchyCreator.getUserNode(WCMCoreUtils.getSystemSessionProvider(), userName);
  for(nodedata in presentNodes) {
    link = Utils.getWebdavURL(nodedata, false);
    urlBuf.append("\n {url: '").append(link).append("'");
    def captionChild = uicomponent.getChildNode(nodedata, NodetypeConstant.EXO_CAPTIONS);
    def realNodeData = Utils.getRealNode(nodedata);
    if (nodedata.isNodeType(NodetypeConstant.NT_FROZEN_NODE) &&
        realNodeData.isNodeType(NodetypeConstant.EXO_ACCESSIBLE_MEDIA)) {
      //add node with name "CCPublished.txt" inside nodedata and get it as caption for published node's caption
      //getRealNodeBySystemSession;
      Session systemSession = WCMCoreUtils.getSystemSessionProvider().getSession(realNodeData.getSession().getWorkspace().getName(), 
                                                                                 WCMCoreUtils.getRepository());
      //add CCPublished.txt as nt:file
      String ccPublished = "CCPublished.txt";
      realNodeData = systemSession.getNodeByUUID(realNodeData.getUUID());
      if (realNodeData.hasNode(ccPublished)) {
        realNodeData.getNode(ccPublished).remove();
        realNodeData.save();
      }
      Node ccNode = realNodeData.addNode(ccPublished, NodetypeConstant.NT_FILE);
      Node jcrContent = ccNode.addNode(NodetypeConstant.JCR_CONTENT, NodetypeConstant.NT_RESOURCE);
      Node jcrContentCaption = captionChild.getNode(NodetypeConstant.JCR_CONTENT);
      //add jcr:content
      jcrContent.setProperty(NodetypeConstant.JCR_MIME_TYPE, jcrContentCaption.getProperty(NodetypeConstant.JCR_MIME_TYPE).getValue());
      jcrContent.setProperty(NodetypeConstant.JCR_DATA, jcrContentCaption.getProperty(NodetypeConstant.JCR_DATA).getValue());
      jcrContent.setProperty(NodetypeConstant.JCR_ENCODING, jcrContentCaption.getProperty(NodetypeConstant.JCR_ENCODING).getValue());
      jcrContent.setProperty(NodetypeConstant.JCR_LAST_MODIFIED, new GregorianCalendar());
      realNodeData.save();
      //add mix:hiddenable
      ccNode.addMixin(NodetypeConstant.EXO_HIDDENABLE);
      realNodeData.save();
      captionChild = ccNode;    
    }
    if (captionChild != null) {
      urlBuf.append(", captionUrl : '").append(Utils.getWebdavURL(captionChild, false)).append("'");
    }
    urlBuf.append("},");
    links.add(link);
  }
  def url = urlBuf.toString();
  url = url.length() == 0 ? url : url.substring(0, url.length() - 1);
  def rqcontext = _ctx.getRequestContext();
  def size = links.size();

  Calendar lCDateCalendar = Calendar.getInstance();
  def timeCreated = lCDateCalendar.getTimeInMillis();
%>
<script type="text/javascript">
  var finFpl = false;
</script>
<script type="text/javascript" src="/eXoWCMResources/javascript/eXo/wcm/flowplayer/flowplayer-3.2.6.min.js"></script>
  <div class="ContentDetail">
	  <%
		  def style = "float:none; margin:auto;";
//		  if (size > 1) {
//		    style = "";
//		  }
		%>
  	<div id="player-$timeCreated" class="PlayerContent" style="$style ; z-index:0;"></div>
  	<script type="text/javascript">
     var timemili = 0;
     function insertFlv() {
      // install flowplayer into container
    	flowplayer("player-$timeCreated", {wmode: 'transparent', src: "/eXoWCMResources/javascript/eXo/wcm/flowplayer/flowplayer-3.2.7.swf"}, {
      // fullscreen button not needed here
        plugins: {
          captions: {
            url: '/eXoWCMResources/javascript/eXo/wcm/flowplayer/flowplayer.captions-3.2.3.swf',
    
            // pointer to a content plugin (see below)
            captionTarget: 'content'
          },    	  
    	  
          /*
          configure a content plugin so that it 
          looks good for showing subtitles
          */
          content: {
            url:'/eXoWCMResources/javascript/eXo/wcm/flowplayer/flowplayer.content-3.2.0.swf',
            bottom: 35,
            height:40,
            backgroundColor: 'transparent',
            backgroundGradient: 'none',
            border: 0,
                  textDecoration: 'outline',
            style: { 
              body: { 
                fontSize: 14, 
                fontFamily: 'Arial',
                textAlign: 'center',
                color: '#ffffff'
              } 
            } 
          },
          
          controls: {
            url: '/eXoWCMResources/javascript/eXo/wcm/flowplayer/flowplayer.controls-tube-3.2.5.swf',
            height: 30,
            // setup auto hide
            autoHide: false,
            playlist: true,
            tooltips:  {
              // a shortcut for enabling tooltips for buttons (English)
              buttons: true
            },
            stop: true, 
            play: true,
            scrubber: true,
            time: true, 
            mute: true,
            volume: true,
            fullscreen: true,
          }
        },

        playlist: [ ${url} ],
        canvas: {
          backgroundImage: 'url(/eXoDMSResources/skin/images/file/MP3Player.jpg)'
        },
        clip: {
            autoPlay: false,
            autoBuffering: true,
            // optional: when playback starts close the first audio playback
            onBeforeBegin: function() {
              flowplayer("player-$timeCreated").close();
            }
        }
      });
     }
     
    function timedCount() {
       timemili++;
       if (finFpl == false && timemili < 10) {
         setTimeout("timedCount()", 100);
       } else {
         insertFlv();
       }
     }
     
     timedCount();
  </script>
 </div>
<style>
.NavigationContainer {
  padding-bottom: 5px;
  color: #0e396c;
  background: url('/eXoDMSResources/skin/images/file/TitleBG1x21.gif') repeat-x top;
  border: 1px solid #cbcbcb;
  margin: auto;
}

 .TopTitle {
  padding-left: 10px;
  height: 22px; line-height: 22px;
  color: #058ee6; font-weight: bold;
}

 .TextContent pre {
   white-space: normal;
 }

 .ContentDetail {
   text-align: center;
   overflow: visible;
 }

.ListPlayer{
	float: right;
	overflow-y: auto;
	overflow-x: hidden;
	height: 420px;
}

.PlayerContent{
	padding: 10px;
	display:block;
	float:left;
	height:320px;
	width:475px;
}

div.clips.petrol a.playing {
	width: 242px;
	background: url('/eXoDMSResources/skin/images/file/Selected.gif') no-repeat right center;
	color: black;
	display: block;
	font-family: verdana;
	font-size: 11px;
	font-weight: bold;
	height: 29px;
	line-height: 28px;
	margin: 0 0 0 4px;
	overflow: hidden;
	padding: 0 0 0 8px;
	text-align: left;
}

div.clips.petrol .first {
	background: #F0F0F0;
	width: 238px;
	border: none;
	border-top: 1px solid white;
	color: black;
	display: block;
	font-family: verdana;
	font-size: 11px;
	font-weight: bold;
	height: 29px;
	line-height: 28px;
	margin: 0 0 0 8px;
	overflow: hidden;
	padding: 0 0 0 8px;
	text-align: left;
  outline-style: none;
}

.BackPage{
	background: url('/eXoDMSResources/skin/images/file/BackPage.gif') no-repeat center;
	width: 19px;
	height: 7px;
	cursor: pointer;
	margin: 16px auto;
}

.NextPage{
	background: url('/eXoDMSResources/skin/images/file/NextPage.gif') no-repeat center;
	width: 19px;
	height: 7px;
	cursor: pointer;
	margin: 16px auto;
}

div.clips.petrol a:hover {
	background: url('/eXoDMSResources/skin/images/file/Selected.gif') no-repeat right center;
	width: 242px;
	margin: 0 0 0 4px;
}

</style>