/**
 * copyright Eric Malone 2006
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 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 Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * The LGPL can also be obtained at http://em.hopto.org/lgpl.txt
 */

/**
 * Javascript pagination of HTML code
 *
 * This library makes really long pages of text more manageable to read by
 * dividing the text into 'paginated' chunks that can be navigation using page
 * numbers.
 *
 * @package web framework
 * @author Eric Malone
 * 
 * @copyright Eric Malone 2006
 *
 * $Header: /home/cvsroot/http/em/scripts/paginate.js,v 1.2 2006/03/28 21:25:06 eric Exp $
 *
 * Dependencies
 * + Microsoft IE, Firefox, Opera
 *
 * Usage
 * Two ways to initiate this pagination: 
 * 1. Make a function that runs at time of body load (example below).
 * 2. Place a small snippet of javascript at bottom of the page, calling the 
 *    paginate function.
 *
 * <code>
 *   <head>
 *     <script src="paginate.js" type="text/javascript"></script>
 *     <script type="text/javascript">
 *       function init()
 *       {
 *         paginate( "paginate_me", "p", "page_nav", 2, 0 );
 *       }
 *     </script>
 *   </head>
 *
 *   <body onload="init();">
 *     
 *     <div id="page_nav"></div> <!-- will be filled in automatically -->
 *     <div id="paginate_me">
 *       <p>yadda yadda yadda</p>
 *       <p>yadda yadda yadda</p>
 *     </div>
 *   </body>
 * </code>
 * 
 * @todo Make sure that tables and respective rows paginate under all
 *       circumstances
 *
 * Notes
 * + Be sure to style (using css) your navigation DIV (or other appropriate container)
 *
 * Milestones
 * + initial revision
 */
 
 
/**
 * @param contain_id    string id attribute of the container tag
 * @param seperator_tag string type of tag to seperate pieces to 
 *                      paginate by (i.e. 'p', 'div', etc..)
 * @param nav_id        strong id attribute of the navigation container 
 *                      (a 'div','p', etc...)
 * @param page_size     int number of seperator tags for each page
 * @param curr_page     int the current page
 */
function paginate( container_id, seperator_tag, nav_id, page_size, curr_page  )
{
  seperator_tag = seperator_tag.toLowerCase();
  
  // special case for table
  if( seperator_tag.toLowerCase() == 'tr' )
  {
    var container = document.getElementById( container_id );
    for( var i=0; i < container.childNodes.length; ++i )
    {
      if( container.childNodes[i].nodeName.toLowerCase() == 'tbody' )
      {
        var children = container.childNodes[i].childNodes;
        break;
      }
    }
  }
  else
    var children = document.getElementById( container_id ).childNodes;
  
  var num_displayed = 0; // keep track of how many we've already displayed
  
  
  // keep track of total number of seperator_tags there are, since 
  // container_id also contains text nodes and whatnot.
  var total_children = 0;
  
  for( var i=0; i < children.length; i++ )
  {
    var child = children[i];
    
    if( child.nodeName.toLowerCase() == seperator_tag )
    {
      if( ( total_children  < curr_page * page_size ) ||
          ( total_children  > curr_page * page_size + ( page_size - 1 ) ) 
        )
        child.style.display = 'none';
      else
        child.style.display = '';
      ++total_children;
    }
  }
  
  generateMenu( Math.ceil( total_children / page_size ), 
                container_id, seperator_tag, nav_id, page_size, curr_page );
                
  return false;
}

/**
 * Create a selection menu to display which page, based on total
 * number of our seperator tags
 *
 * @param total_pages total number of pages that make up our pagination
 * @param contain_id    string id attribute of the container tag
 * @param seperator_tag string type of tag to seperate pieces to 
 *                      paginate by (i.e. 'p', 'div', etc..)
 * @param nav_id        strong id attribute of the navigation container 
 *                      (a 'div','p', etc...)
 * @param page_size     int number of seperator tags for each page
 * @param curr_page     int the current page
 * @todo Would it be better to replace innerHTML w/ node creation?
 */
function generateMenu( total_pages, container_id, seperator_tag, nav_id, page_size, curr_page  )
{
  var navigation_div = document.getElementById( nav_id );
  var tmp_html = '';
  
  for( var i = 0; i < total_pages; i++ )
  {
    tmp_html += "<a href='' " +
                  "onclick='return paginate(" + 
                  "\"" + container_id  + "\", " +
                  "\"" + seperator_tag + "\", " +
                  "\"" + nav_id        + "\", " +
                  "\"" + page_size     + "\", " +
                  i +
                ");' " +
                "style='cursor:pointer" +
                ( ( curr_page == i ) ? "; font-weight: bold" : "" ) +
                "'>" + (i+1) + "</a>" + 
                ( ( i+1 == total_pages ) ? "" : " - " );
  }
  
  navigation_div.innerHTML = tmp_html;
}

