var currPageNo = 0;
$(document).ready(function() {
  $('.srt').srt();
  $('.icon').tooltip({
    showURL: false
    });
  $('.toggleButton').click(function() {
    $(this).parent().parent().find('.toggleButton').each(function() { $(this).parent().show(); });
    $(this).parent().hide();
    });
  $('.audioBtn').toggle(function() {
    $(this).next().slideDown();
    return false;
    }, function() {
    $(this).next().slideUp();
    return false;
    });
/*
  var playerPath = "/static/images/player.swf";
  var so = new SWFObject(playerPath, 'mpl', '300', '30', '9');
  so.addParam("allowfullscreen", "false");
  so.addParam('flashvars', 'file=/static/images/GentlemanMiddleman.mp3');
  so.write("audioPlayer");
*/
/*
  var so2 = new SWFObject(playerPath, 'mpl2', '300', '30', '9');
  so2.addParam("allowfullscreen", "false");
  so2.addParam("flashvars", "file=/static/images/GMD.mp3");
  so2.write("audioPlayer2");
*/

//  $('.audioBtn').colorbox({width: '350px', height: '180px', inline: true, href: '#audioWrap'});
  $('.videoBtn').colorbox({width: '445px', height: '550px', inline: true, href: '#videoWrap'});
  $('.videoBtn2').colorbox({width: '445px', height: '550px', inline: true, href: '#videoWrap2'});
  scrollPages();
});

function scrollPages() {
  var DOT_DEFAULT = '/static/images/pageOff.png';
  var DOT_HOVER = '/static/images/pagehover.png';
  var DOT_CURRENT = '/static/images/pageOn.png';
  var PAGE_HEIGHT = 1270;
  var OFFSET_VAL = 80;
  var noOfPages = $('.page').length;
  var currentPage = -1;
//  var dotHtml = '<img src="/static/images/oldenglish/dot.gif" alt="dot" class="dot" /><br />';
  for (var i=0; i < noOfPages; i++) {
    var img = $('<img />');
    img.attr('src', DOT_DEFAULT).attr('alt', 'o').addClass('dot').data("index", i);
    img.click(function() {
      var index = $(this).data("index");
      var page = $('.page').eq(index);
      var top = page.offset().top - 20;
      top = top < 0 ? 0: top;
      $('html, body').animate({'scrollTop': top}, 'slow');
      selectDot($(this));
      });
    img.hover(function() {
      if (!$(this).hasClass('selectedDot')) {
        $(this).attr('src', DOT_HOVER);
        }
      }, function() {
      if ($(this).data("index") == currentPage) {
        $(this).attr("src", DOT_CURRENT);
        } else {
        $(this).attr('src', DOT_DEFAULT);
        }
      });
    var d = $('<div />').addClass('dotDiv');
    d.append(img);
    $('#scrollDots').append(d);
    previousScrollTop = 0;
    }

  $(document).scroll(documentScroll);
  documentScroll();
  $('#scrollDots').append('<br />');

  function documentScroll() {
    var scrollTop = $(document).scrollTop();
    var pageNo = Math.floor((scrollTop + OFFSET_VAL) / PAGE_HEIGHT);
    if (pageNo < 0) pageNo = 0;
    if (pageNo != currentPage) {
      selectDot($('.dot').eq(pageNo));
      }
    }


  function selectDot(e) {
    $('.selectedDot').attr('src', DOT_DEFAULT).removeClass('selectedDot');
    e.attr("src", DOT_CURRENT).addClass("selectedDot");
    currentPage = e.data("index");
    }
  }


