$(document).ready(function() {
  $('#page-right').click(function() {
    changePage('next', 35);
  });
  $('#page-left').click(function() {
    changePage('last', 35);
  });

  function changePage(action, max) {

    var leftSrc = $('#page-left').attr('src').split('.');
    var rightSrc = $('#page-right').attr('src').split('.');

    var posInUrl = rightSrc.length - 2; // Gets the page num part of the URL

    var rightPage = Number(rightSrc[posInUrl]);
    var leftPage = Number(leftSrc[posInUrl]);

    if (action == 'next') {
      if (rightPage < max) {
        rightPage += 2;
        leftPage += 2;
      }
    }
    else {
      if (leftPage > 1) {
        rightPage -= 2;
        leftPage -= 2; 
      }
    }

    if (rightPage < 10) rightPage = '0' + rightPage;
    if (leftPage < 10) leftPage = '0' + leftPage;

    leftSrc[posInUrl] = leftPage;
    rightSrc[posInUrl] = rightPage;
    
    if (leftPage != '00') {
      $('#flip-yearbook').css('background', 'url(/allyearbooks/brochureware/img/flipbook_background.gif)');
    }
    else {
      $('#flip-yearbook').css('background', '');
    }

    $('#page-left').attr('src', leftSrc.join('.'));
    $('#page-right').attr('src', rightSrc.join('.'));
  }

});
