 /* multipage article script.
 
     handles the paging for long articles. allows paging back and forth, and a view all 
     option. logs page requests to server for auditing.

     dependencies: Spry framework - SpryData.js and SpryHTMLDataSet.js
                   be sure to include the Spry libs FIRST, before you include mp_multipaging.js

     namespace:    uses the "namespace" mp_ for all functions and variables.

     Author: Internet Services
     Revised: May 21, 2009
              July 23, 2009 - added "continued to" features for paging forward
     

     description:
     this script works when you have a series of divs (pages) nested in a container of a certain id:

                < script src="mp_multipage_article.js">
        
                <div id="mp_multipage_article">
                   <div>...</div>
                   <div>...</div>
                   <div>...</div>
                </div>
                <div id="mp_multipage_article_pagebar"></div>
                
                < script type='text/javascript' language="javascript">
                   // start out with page 1 only.
                   mp_init(); mp_switchToPage(1);
                < /script>

     both the id of the containing div (mp_multipage_article) and the id of the pagebar
     (mp_multipage_article_pagebar) are fixed. the triggering script is called,

        mp_init(); 
 
     to invoke the paging feature. if there is only one nested div, there is only one page
     and this routine does nothing.


  */

  /* 
  
    you need to use this CSS somewhere for show/hide effects and other text treatments:
    
    // pagebar starts out hidden, until there is a need to show it 
    #mp_multipage_article_pagebar { display:none; clear:both; background-color:#ffffff; text-align:center; padding:4px 0 4px 0; border-top:1px #eeeeee solid; border-bottom:1px #eeeeee solid; margin:20px 0 10px 0; }

    // continued-from statements appear on page tops. start out hidden in case user does not have JS 
    .mp_continued_froms { display:none; color:#666666; font-size:smaller; }
    .mp_continued_froms a { color:#666666; }

    // continued-to statements appear on page bottoms. start out hidden in case user does not have JS 
    .mp_continued_froms { display:none; color:#666666; font-size:smaller; }
    .mp_continued_froms a { color:#666666; }

    
    
  */
  

// set up array to hold paging divs, used throughout the multipaging script
var mp_pages = new Array();
// this empty dataset will be used to support logging of page requests for multi-paged articles. useCache is important so the request is actually sent to the server
var mp_ds1 = new Spry.Data.HTMLDataSet("", "", {useCache:0});  

// set up the children divs, call this to invoke the paging
function mp_init(){
  // abort if the expected divs do not exist.
  if(!(document.getElementById('mp_multipage_article'))) return;
  if(!(document.getElementById('mp_multipage_article_pagebar'))) return;
  
  var children = document.getElementById('mp_multipage_article').childNodes;
  for( var i=0, il=children.length; i<il; i++ ){
    //alert("Trying "+children[i].nodeName);
    if( children[i].nodeName.toUpperCase()=='DIV' )
      mp_pages[mp_pages.length]=children[i];
  }
  
  // if all OK, switch to page 1 to start us off. (also ok to call if less than two pages)
  mp_switchToPage(1);
}
  
/* **************************
   will be called with n=1 to initiate the paging; called subsequently to page through the multipage article. 
   n is the page number you want to jump to (1...N). be sure to call mp_init() before calling this function.
   **************************** */
function mp_switchToPage(n){
  var o;
  
  // do nothing if there is only one page (or any required div is missing, etc. for which mp_pages is empty)
  if(mp_pages.length<2) return;
  
  // loops through all pages (divs) setting them to hidden
  for( var i=0, il=mp_pages.length; i < il; i++ ){
    if(n>0) mp_pages[i].style.display='none';
    else mp_pages[i].style.display='block';
  }
  // unless the current page requests
  if(mp_pages[n-1]) mp_pages[n-1].style.display='block';
  
  // and update the pagebar to correspond
  mp_buildPagebar(n);

  // and show all the continued-froms in case they were previously hidden via view all
  mp_showhideContinuedFroms(1);
  mp_showhideContinuedTos(1);
  
  // and show or hide the Quest main image and main image caption
  mp_showhideQuestMainImage(n);

  // and send a logging event to the server for statistics
  mp_logPageRequest(n);

  // sending back true will let the browser follow the href=# to the page top. choose false to prevent that effect.
  return true;  
}

/* creates the paging bar (1 - 2 - 3 - etc.)
   send in the "current" page (n) that will not be clickable, send -1 for view all */
function mp_buildPagebar(n){
  var o=document.getElementById('mp_multipage_article');
  var o1=document.getElementById('mp_multipage_article_pagebar');

  // loops through all pages and builds HTML
  var pagebar_code='';
  // previous page - appears when you are not on page 1
  pagebar_code+= (n>1)?"<span style='padding-right:7px;'>&laquo; <a href='#' onclick='return mp_switchToPage("+(n-1)+");' title='View the previous page'>Previous page</a></span> " : "<span style='color:silver;padding-right:7px;'>&laquo; Previous page</span> ";
  for( var i=0, il=mp_pages.length; i < il; i++ ){
    pagebar_code+= (n!=(i+1)) ? "<span style='padding:0 7px 0 7px'><a href='#' onclick='return mp_switchToPage("+(i+1)+")' title='Jump to page "+(i+1)+"'>" + (i+1) + "</a></span> ": "<span style='font-weight:bold;padding:0 7px 0 7px'>" + (i+1) + "</span> ";
  }    
  // next page - appears when you are not on the last page and not viewing all pages
  pagebar_code+= (n<mp_pages.length&&n!=-1)?" <span style='padding-left:7px'><a href='#' onclick='return mp_switchToPage("+(n+1)+");' title='View the next page'>Next page</a> &raquo;</span>" : " <span style='color:silver;padding-left:7px'>Next page &raquo;</span>";
  pagebar_code+= (n==-1)? " <span style='color:silver;padding-left:30px;'>View all pages</span>" : " <span style='padding-left:40px;'><a href='#' onclick='return mp_showAllPages()' title='View the entire article on one page'>View all pages</a></span>";

  o1.innerHTML = pagebar_code;
  o1.style.display = 'block'; // reveal it if it is not yet visible (starts out display none)    
}


/* reveal all pages. */
function mp_showAllPages(){
  // loops through all pages (divs) setting them to show
  for( var i=0, il=mp_pages.length; i < il; i++ ){
    mp_pages[i].style.display='block';
  }
  
  // make the pagebar show all the pages clickable, except view all. 
  // this checks to see if it is needed, e.g., there is more than one page
  mp_buildPagebar(-1); 
  mp_showhideContinuedFroms(-1);
  mp_showhideContinuedTos(-1);

  // and show the Quest main image and main image caption
  mp_showhideQuestMainImage(-1);
  
  // and send a logging event to the server for statistics
  mp_logPageRequest(-1);

  return false;
}

/* show or hide the "continued from page n" content that appears at page tops when paging */
function mp_showhideContinuedFroms(i){
  var o; var which=(i>0)?'block':'none';
  if(o=document.getElementById('mp_continued_2')) o.style.display=which;
  if(o=document.getElementById('mp_continued_3')) o.style.display=which;
  if(o=document.getElementById('mp_continued_4')) o.style.display=which;
}

/* show or hide the "continues on next page" content that appears at page bottoms when paging */
function mp_showhideContinuedTos(i){
  var o; var which=(i>0)?'block':'none';
  if(o=document.getElementById('mp_continues_1')) o.style.display=which;
  if(o=document.getElementById('mp_continues_2')) o.style.display=which;
  if(o=document.getElementById('mp_continues_3')) o.style.display=which;
}

/* show or hide the Quest article "main image" and "main image caption," if they exist */
function mp_showhideQuestMainImage(page_num){
  var o; var which=(page_num==1||page_num==-1)?'block':'none'; /* show the image+caption when viewing all or on just page 1 */
  if(o=document.getElementById('Story1Image')) o.style.display=which;
  if(o=document.getElementById('Story1Caption')) o.style.display=which; 
}

/* send a page request to the server with this page so statistics can be captured. does not actually do anything with returned data. */
var mp_requestedPages = Array(); // keeps track of which pages the user has already viewed, so you do not double-count
function mp_logPageRequest(n){
  // uncomment next line in production:
  if(window.location.href.indexOf('cms.fhcrc.org')>-1||window.location.href.indexOf('cms-dev.fhcrc.org')>-1) return; // no need to log while in CMS
  if(n==1) return; // no need to log page 1 requests
  if(mp_requestedPages[n]) return; // only log new page requests
  mp_ds1.setURL("?page="+(n==-1?'all':n));
  mp_ds1.loadData(); //alert("logged: "+n);
  mp_requestedPages[n]=1; // remember that this page has been requested
}