function prepareWeddingBook(){

  if( document.getElementById &&
      document.getElementsByTagName ){
    if( document.getElementById( 'weddingBook' ) ){
      
         //hide 
         var book = document.getElementById( 'weddingBook' );
         var pages = book.getElementsByTagName( 'li' );
         // loop through pages and only display the first one
         for( var i=0; i < pages.length; i++ ){
         
            // Only do the following for li elements that are direct children of weddingBook
            if(pages[i].parentNode.id=='weddingBook'){ 

               // add events to the wedding pages page links
               var pageLinks = pages[i].getElementsByTagName('a');

               // loop throught the links and add the events for the next and previous Page
               for( var j=0; j < pageLinks.length; j++ ){
                 if(pageLinks[j].className == 'nextPage' || pageLinks[j].className == 'previousPage'){
                    createEventListener(pageLinks[j]);
                 }               

              }

               if(i>0){pages[i].className='offscreen';}

            }//end test for direct children
         }//end loop for pages
    }//end test for Wedding book item
  }//end testing for DOM support
}//end function


function pageTurn(ItemHref){ 
//split out the name value form the hrefvalue  
         var stringSplitValues= ItemHref.split("#"); 
         nameValue  = stringSplitValues[1]

         var book = document.getElementById( 'weddingBook' );
         var pages = book.getElementsByTagName( 'li' );
         // loop through pages and only display the destination one
         for( var i=0; i < pages.length; i++ ){
         
            // Only do the following for li elements that are direct children of weddingBook
            if(pages[i].parentNode.id=='weddingBook'){ 
               pages[i].className='offscreen';
               
               // add events to the wedding pages page links
               var pageLinks = pages[i].getElementsByTagName('a');

               // loop throught the links and add the events for the next and previous Page
               for( var j=0; j < pageLinks.length; j++ ){
                      if(pageLinks[j].name == nameValue) {      
                          pages[i].className='weddingPage';
                      }
   

               }

               

            }//end test for direct children
         }//end loop for pages


}     

function createEventListener(linkItem){
    linkItem.addEventListener('click', function(){return pageTurn(linkItem.href);}, false);
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent(prepareWeddingBook);





