<%@ page import="com.novell.webaccess.common.JSPConduit"%>
<%@  page pageEncoding="utf-8" contentType="text/html;charset=utf-8"%>
<%@ taglib uri="/WEB-INF/iman.tld" prefix="iman"%>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c"%>
<%@ taglib uri="/WEB-INF/x.tld" prefix="x"%>

<iman:stringtable bundle="DevResources" />
<iman:stringtable bundle="FwResources" />

<%
JSPConduit c = JSPConduit.getJSPConduit(request);
%>
<%
	c.stringTable("FwResources");
	int myButtonWidth = c.string("Book.MovePageDown").length();
	if (c.string("Book.MovePageUp").length() > myButtonWidth)
		myButtonWidth = c.string("Book.MovePageUp").length();
	if (c.string("Book.MovePageUp").length() > myButtonWidth)
		myButtonWidth = c.string("Book.DisablePage").length();
	if (c.string("Book.MovePageUp").length() > myButtonWidth)
		myButtonWidth = c.string("Book.EnablePage").length();
%>
<HTML>
<HEAD>
<TITLE><iman:string key="ProductName" /></TITLE>

<iman:stylesheet />
<iman:eMFrameScripts />
<SCRIPT
	src="<c:out value="${ModulesPath}"/>/fw/javascripts/PageOrder.js"></SCRIPT>

<%
	int fontSize = 16;
	int padding = 5;
	double wFactor = .55;
	String theLocale = (String) session.getAttribute("ClientLocale");
	if (theLocale.startsWith("zh") || theLocale.equalsIgnoreCase("ja")
			|| theLocale.equalsIgnoreCase("ru")) {
		wFactor = 1.0;
	}
	double w = myButtonWidth;
	w = ((w > 0) ? w : 10);
	w = w * fontSize * wFactor + padding * 2 + 2;
%>
<style>
      .trigger {cursor: pointer; cursor: pointer;}
      .leafNode {cursor: pointer; cursor: pointer; border: 1px white solid}
      .branch {display: none; margin-left: 16px;}
      .myButton { font-size: .8em; cursor: pointer; width: <%= w%>px; background-color: #c9c6bf; color: #; border-bottom-width:1; border-right-width:1; border-bottom-color:gray; border-right-color:gray }
   </style>

<script language="JavaScript">
      function onInit()
      {
         disableTree(document.getElementById('pageTree'), disabledIds);
      }

      function okPressed()
      {
         var list = new Array();
         var disabledList = new Array();
         walkTree(document.getElementById('pageTree'), list, disabledList);
         document.form.pageOrder.value = pack(list);
         document.form.disabledPages.value = pack(disabledList);
         document.form.submit();
      }

      function disableItem(span)
      {
      	if(span != null &&  span.getAttribute("disableItem") != "true")
      	{
         if(span.id.indexOf("chapter_")>=0)
         {
            alert("<iman:string key="Book.diabledPageMsg"/>");
            return;
         }
		
		 var childrenNodes = selectedSpanForColor.childNodes;
         for(i=0; i<childrenNodes.length; i++)
         {
         		if(childrenNodes[i].style != null)
         		{
	         		childrenNodes[i].style.color = '#aaaaaa';
	         		childrenNodes[i].setAttribute("origTitle", childrenNodes[i].innerHTML);
	         		childrenNodes[i].innerHTML = childrenNodes[i].innerHTML + "(<iman:string key="Book.Disabled"/>)"; 
	         	}
         }
         span.setAttribute("disableItem", "true");
         span.setAttribute("origTitle", span.title);
         span.title = span.title + ":<iman:string key="Book.Disabled"/>";
         
        }
      }

      function enableItem(span)
      {
      	if(span != null && span.getAttribute("disableItem") != "false")
      	{
        	if(span.id.indexOf("chapter_")>=0)
         	{
            	alert("<iman:string key="Book.diabledPageMsg"/>");
            	return;
         	}
         	
         	var childrenNodes = selectedSpanForColor.childNodes;
         	for(i=0; i<childrenNodes.length; i++)
         	{
         		if(childrenNodes[i].style != null)
         		{
	         		childrenNodes[i].style.color = '#ffffff';
	         		if(childrenNodes[i].getAttribute("origTitle") != null)
		         		childrenNodes[i].innerHTML = childrenNodes[i].getAttribute("origTitle");
	         	}
         	}

         	span.setAttribute("disableItem", "false");
         	if(span.getAttribute("origTitle") != null)
         	{
         		span.title = span.getAttribute("origTitle");
         	}
        }
      }
      
      function checkKeyAndSetFocus(evt)
      {
      	if(evt.keyCode == 32) //13 is enter and 32 is space
      	{
      		document.form.moveUpButton.focus();
      	}
      }
      
      function expandChapter(tabi)
      {
		showBranch("pageGroup_" + tabi); 
		swapFolder("folder" + tabi)
      }
      

      var openImg = new Image();
      openImg.src = "<c:out value="${ModulesPath}"/>/fw/images/minus.gif";
      var closedImg = new Image();
      closedImg.src = "<c:out value="${ModulesPath}"/>/fw/images/plus.gif";

      var pageIds = [
      <%
         int count = Integer.parseInt((String)request.getAttribute("chapter.count"));
         for(int tabi=0; tabi<count; tabi++)
         {
            int subTabSize = Integer.parseInt((String)request.getAttribute("chapter."+tabi+ ".count"));
            for(int subtabi=0; subtabi<subTabSize; subtabi++)
            {
               out.print("\"");
               out.print(request.getAttribute("chapter."+tabi+"."+subtabi+".id"));
               out.print("\"");

               if(subtabi<subTabSize-1 || tabi<count-1)
               {
                  out.print(",");
               }
            }
         }
       %>
      ];

      var disabledIds = [
      <%
         String[] disabledIds = (String[])request.getAttribute("disabledIds");
         for(int i=0; i<disabledIds.length; i++)
         {
            out.print("\"" + disabledIds[i] + "\"");

            if(i<disabledIds.length-1)
            {
               out.print(",");
            }
         }
       %>
      ];


   </script>
</HEAD>

<BODY onLoad="onInit();">
<FORM name="form" method=post action="webacc"><INPUT
	type="hidden" name="taskId" value="<c:out value="${taskId}"/>">
<INPUT type="hidden" name="nextState" value="savePageOrder"> <INPUT
	type="hidden" name="pageOrder" value=""> <INPUT type="hidden"
	name="disabledPages" value=""> <iman:taskHeader
	titleKey="Book.PageOptions" titleBundle="FwResources" /><BR>

<TABLE>
	<TR>
		<TD><%-- This looks like crap, because it CANNOT have whitespace --%>
		<%-- In Mozilla, node.nextSibling returns a text node, IE    --%> <%-- doesn't.  To keep the javascript code simple, this menu --%>
		<%-- can't include whitespace                                --%>
		<div id="pageTree"
			style="width: 300px; height: 300px; overflow:auto; border:1px black solid; padding: 5px">
		<%
			int pageIndex = 0;
			count = Integer.parseInt((String) request
					.getAttribute("chapter.count"));
			for (int tabi = 0; tabi < count; tabi++) {
				out.print("<div id=\"chapter_" + tabi + "\">");
				out.print("<div id=\"chapterName_" + tabi
				+ "\" style=\"border: 1px white solid\">");
				out
				.println("<a href=javascript:expandChapter("
						+ tabi
						+ ") onKeyUp=checkKeyAndSetFocus(event); style=text-decoration:none;color:inherit onFocus=toggleSelection(this.parentNode) title='"
						+ request.getAttribute("chapter." + tabi
						+ ".name")
						+ "'>&nbsp;<img src=\""
						+ c.getModulesUrl()
						+ "/fw/images/plus.gif\" border=\"0\" id=\"folder"
						+ tabi + "\"></a>");
				out
				.println("<span class=\"trigger\" onClick=\"toggleSelection(this.parentNode)\" >&nbsp;"
						+ request.getAttribute("chapter." + tabi
						+ ".name") + "&nbsp;</span>");
				out.print("</div>");

				out.print("<div id=\"pageGroup_" + tabi
				+ "\" class=\"branch\">");

				int subTabSize = Integer.parseInt((String) request
				.getAttribute("chapter." + tabi + ".count"));
				for (int subtabi = 0; subtabi < subTabSize; subtabi++) {
					out
					.print("<div title='"
					+ request.getAttribute("chapter." + tabi
							+ "." + subtabi + ".name")
					+ "' id=\"pageNode_"
					+ pageIndex
					+ "\" class=\"leafNode\" style=border-left:8px;color:inherit><a href=# style=\"text-decoration:none;color:black;\" onFocus=toggleSelection(document.getElementById('pageNode_"
					+ pageIndex
					+ "')) onKeyUp=checkKeyAndSetFocus(event)>"
					+ request.getAttribute("chapter." + tabi
							+ "." + subtabi + ".name")
					+ "&nbsp;&nbsp;</a></div>");
					pageIndex++;
				}

				out.print("</div>");
				out.print("</div>");
			}

			out.print("</div>");
		%>
		
		</TD>
		<TD valign="top"><INPUT type="button" name="moveUpButton"
			onClick="moveUp(window.selectedSpan)" class="myButton"
			value="<iman:string key="Book.MovePageUp"/>"><BR>
		<INPUT type="button" name="moveDownButton"
			onClick="moveDown(window.selectedSpan)" class="myButton"
			value="<iman:string key="Book.MovePageDown"/>"><BR>
		<INPUT type="button" name="disableItemButton"
			onClick="disableItem(window.selectedSpan)" class="myButton"
			value="<iman:string key="Book.DisablePage"/>"><BR>
		<INPUT type="button" name="enableItemButton"
			onClick="enableItem(window.selectedSpan)" class="myButton"
			value="<iman:string key="Book.EnablePage"/>"></TD>
	</TR>
</TABLE>
<BR>

<br>
<iman:bar /> <iman:button key="OK" onClick="okPressed()" /> <iman:button
	key="Cancel" onClick="window.close()" /><BR>

</FORM>
</BODY>
</HTML>
