<?xml version="1.0" encoding="iso-8859-1"?>
<!--                Copyright (c) 2008, Lexmark International, Inc.
        THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY
           INFORMATION WHICH IS THE PROPERTY OF LEXMARK INTERNATIONAL, INC.
      ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM
                 LEXMARK INTERNATIONAL, INC., IS STRICTLY PROHIBITED
-->

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html"/>
  
  <!--==========================================================================
  =  Localization 
  ===========================================================================-->

  <!-- Resource path -->
  <xsl:variable name="resPath" select="/deviceDashboard/resourcePath" />
  
  <!-- Variable holding the localization document. -->
  <xsl:variable name="local_doc" select="document(concat($resPath, 'strings.xml'))" />

  <!-- Callable template for replacing a stringID with a localized string.
       params:
         stringID   The ID of the localized string.
  -->
  <xsl:template name="InsertLocalizedString">
    <xsl:param name="stringID" />
    <xsl:for-each select="$local_doc">
      <xsl:value-of select="key('StringLookup', $stringID)" />
    </xsl:for-each>
  </xsl:template>

  <!-- Key for performing lookups on the string table -->
  <xsl:key name="StringLookup" match="StringTable/strings/string" use="@id" />

  <!-- readingOrder variable: should contain either 'rtl' or 'ltr' -->
  <xsl:variable name="readingOrder">
    <xsl:value-of select="$local_doc/StringTable/readingorder/@value"/>
  </xsl:variable>

  <!-- language ID -->
  <xsl:variable name="langID">
    <xsl:value-of select="$local_doc/StringTable/language/@id"/>
  </xsl:variable>

  <!-- alignWithReadingOrder variable: left for ltr, right for rtl -->
  <xsl:variable name="alignWithReadingOrder">
    <xsl:if test="$readingOrder = 'ltr'">
      <xsl:text>left</xsl:text>
    </xsl:if>
    <xsl:if test="$readingOrder = 'rtl'">
      <xsl:text>right</xsl:text>
    </xsl:if>
  </xsl:variable>

  <!-- alignAgainstReadingOrder variable: right for ltr, left for rtl -->
  <xsl:variable name="alignAgainstReadingOrder">
    <xsl:if test="$readingOrder = 'ltr'">
      <xsl:text>right</xsl:text>
    </xsl:if>
    <xsl:if test="$readingOrder = 'rtl'">
      <xsl:text>left</xsl:text>
    </xsl:if>
  </xsl:variable>

  <!-- imagePath variable: dependent on readingOrder -->
  <xsl:variable name="imagePath">
    <xsl:text>Images/</xsl:text>
    <xsl:value-of select="$readingOrder"/>
    <xsl:text>/</xsl:text>
  </xsl:variable>
  
  <!--==========================================================================
  =  Screen generation 
  ===========================================================================-->
  
  <!-- Config XML structure -->
  <xsl:template match="/">
     <xsl:apply-templates select="content"/>
  </xsl:template>

  <!-- Content portion of the screen. Includes all tasks. -->
  <xsl:template match="content">
    <table class="content">
      <table style="width:100%;" cellspacing="0">
        <xsl:for-each select="taskGroup">
          <xsl:for-each select="task">
          <xsl:variable name="pos" select="position() mod 2"/>
          <xsl:choose>
            <xsl:when test="$pos != 1">            
              <td><xsl:call-template name="task-button"></xsl:call-template></td><tr><td class="task-secondary-spacer"></td></tr>
            </xsl:when>
            <xsl:otherwise>
              <td><xsl:call-template name="task-button"></xsl:call-template></td>
            </xsl:otherwise>
          </xsl:choose>
          </xsl:for-each>
        </xsl:for-each>
      </table>
      <!-- Vertical padding space. -->
      <div class="main-secondary-spacer"></div>
    </table>
  </xsl:template>


  <!-- A big button. Includes all javascript hooks for behavior.
       params: 
         isMenu   'no' indicates a task button, anything else indicates this is
                  a menu button. Defaults to 'no'
  -->
  <xsl:template name="task-button">
    <xsl:param name="isMenu">no</xsl:param>
    <button>
      <xsl:variable name="ButtonName">
        <xsl:call-template name="InsertLocalizedString">
          <xsl:with-param name="stringID">
            <xsl:value-of select="@name"/>
          </xsl:with-param>
        </xsl:call-template>
      </xsl:variable>
      <!--Get hotkey position from reource.xml-->
      <xsl:variable name="hotkeyposfromresource">
        <xsl:if test="string-length(substring-after($ButtonName,'&amp;')) != 0">
          <xsl:value-of select="string-length(substring-before($ButtonName,'&amp;')) + 1"/>
        </xsl:if>
        <xsl:if test="string-length(substring-after($ButtonName,'&amp;')) = 0">
          <xsl:value-of select="string-length(substring-before($ButtonName,'&amp;'))"/>
        </xsl:if>
      </xsl:variable>
      <xsl:if test="$hotkeyposfromresource != '0'">
        <xsl:attribute name="ACCESSKEY">
          <xsl:value-of select="substring($ButtonName,$hotkeyposfromresource+1,1)"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:attribute name="class">
        <xsl:text>rbb </xsl:text>
        <!-- Add class 'text-rtl' or 'text-ltr' -->
        <xsl:value-of select="concat('text-', $readingOrder)"/>
        <xsl:if test="@enabled != 'true'">
          <xsl:text> rbb-disabled</xsl:text>
        </xsl:if>
      </xsl:attribute>
      <xsl:attribute name="style">
        <xsl:text>background-image:url(</xsl:text>
        <xsl:value-of select="$imagePath"/>
        <xsl:text>rbb324.jpg);</xsl:text>
      </xsl:attribute>
      <xsl:if test="@enabled != 'true'">
        <!-- Apply the 'disabled' attribute to prevent clicks and alter the 
             appearance of the button. -->
        <xsl:attribute name="disabled"/>
      </xsl:if>
      <xsl:attribute name="id">
        <xsl:value-of select="generate-id(.)"/>
      </xsl:attribute>
      <xsl:attribute name="onmouseover">
        <xsl:text>onRbbMouseOver('</xsl:text>
        <xsl:value-of select="generate-id(.)"/>
        <xsl:text>');</xsl:text>
      </xsl:attribute>
      <xsl:attribute name="onmouseout">
        <xsl:text>onRbbMouseOut('</xsl:text>
        <xsl:value-of select="generate-id(.)"/>
        <xsl:text>');</xsl:text>
      </xsl:attribute>
      <xsl:choose>
        <xsl:when test="$isMenu = 'no'">
          <!-- Regular buttons get the standard onTaskClick handler -->
          <xsl:attribute name="onclick">
            <xsl:text>javascript:onTaskClick('</xsl:text>
            <xsl:value-of select="@id"/>
            <xsl:text>');document.body.focus();</xsl:text>
          </xsl:attribute>
        </xsl:when>
        <xsl:otherwise>
          <!-- Menu buttons get the showMenu handler for click events -->
          <xsl:attribute name="onclick">
            <xsl:text>javascript:showMenu('</xsl:text>
            <xsl:value-of select="generate-id(.)"/>
            <xsl:text>');</xsl:text>
          </xsl:attribute>
          <!-- Menu buttons also get the drop down arrow -->
          <div>
            <xsl:attribute name="class">
              <xsl:value-of select="concat('arrow-', $readingOrder)"/>
            </xsl:attribute>
            <img>
              <xsl:attribute name="src">
                <xsl:value-of select="$imagePath"/>
                <xsl:text>dnarrow.jpg</xsl:text>
              </xsl:attribute>
            </img>
          </div>
          <!-- Register this menu button for use with Ogre -->
          <xsl:call-template name="ogre-registration">
            <xsl:with-param name="id" select="generate-id(.)"/>
            <xsl:with-param name="func_name">showMenu</xsl:with-param>
          </xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>
      <img>
        <xsl:attribute name="class">
          <xsl:value-of select="$readingOrder"/>
        </xsl:attribute>
        <xsl:attribute name="src">
          <xsl:value-of select="$imagePath"/>
          <xsl:value-of select="@image"/>
        </xsl:attribute>
      </img>
      <nobr>
        <span>
          <xsl:if test="$hotkeyposfromresource != '0'"> <!--If there is a hotkey for this button-->
            <xsl:value-of select="substring($ButtonName,1,$hotkeyposfromresource - 1)"/>
            <U>
              <xsl:value-of select="substring($ButtonName,$hotkeyposfromresource + 1,1)"/>
            </U>
            <xsl:value-of select="substring($ButtonName,$hotkeyposfromresource + 2)"/>
          </xsl:if>
          <xsl:if test="$hotkeyposfromresource = '0'"> <!--If there is no hotkey for this button-->
            <xsl:value-of select="$ButtonName"/>
          </xsl:if>          
        </span>
      </nobr>
      <small>
        <xsl:call-template name="InsertLocalizedString">
          <xsl:with-param name="stringID"><xsl:value-of select="@description"/></xsl:with-param>
        </xsl:call-template>
      </small>
    </button>
  </xsl:template>

  <!-- Register a control ID for use with Ogre.
       params:
         id         The ID of the element to register
         func_name  The name of the function to register for that element.
  -->
  <xsl:template name="ogre-registration">
    <xsl:param name="id" />
    <xsl:param name="func_name" />
    <!-- Skip ogre registration if Ogre is not enabled -->
    <xsl:if test="/deviceDashboard/ogreIndex">
      <script type="text/javascript">
        <xsl:text>registerOgreControl('</xsl:text>
        <xsl:value-of select="$id"/>
        <xsl:text>', '</xsl:text>
        <xsl:value-of select="$func_name"/>
        <xsl:text>');</xsl:text>
      </script>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>