jQuery(function() {
        jQuery('.flipbook').flipbook();
});

(function(jQuery){
  jQuery.fn.flipbook = function(settings) {
    // Future settings: activateDelay
    var settings = jQuery.extend({  // Time in ms before menu shows
                      showDelay:              200,
                      // Time in ms before menu hides
                      hideDelay:              750,
                   }, settings);
      var mainDiv = null;
      var currentIndex = 0;
      var slideShowInterval = 0;
      var ul = null;
      var totalCount = 0;
      var isPlaying = false;

      return this.filter('.flipbook').each(function() {
         mainDiv = jQuery(this);

         ul = jQuery('ul', mainDiv);
         totalCount = jQuery('li', ul).size();
         //jQuery('li', ul).hide();

         jQuery("#play", mainDiv).click(function() {
           fb_start();
           jQuery("#play", mainDiv).hide();
           jQuery("#pause", mainDiv).show();
         });
         jQuery("#pause", mainDiv).click(function() {
           fb_stop();
           jQuery("#play", mainDiv).show();
           jQuery("#pause", mainDiv).hide();
         });
         jQuery("#prev", mainDiv).click(function() {
           fb_stop();
           fb_increment(-1);
         });
         jQuery("#next", mainDiv).click(function() {
           fb_stop();
           fb_increment(1);
         });
         jQuery("#first", mainDiv).click(function() {
           fb_stop();
           fb_displayIndex(0);
           currentIndex = 0;
         });
         jQuery("#last", mainDiv).click(function() {
           fb_stop();
           fb_displayIndex(-1);
           currentIndex = 1000;
         });
         jQuery("a[rel]", mainDiv).click(function() {
           var index = parseInt(jQuery(this).attr('rel')) - 1;
           if (index >= 0) {
             fb_stop();
             fb_displayIndex(index);
             currentIndex = index;
           }
         });
      });

      function fb_start() {
        if (!isPlaying) {
          ul.show();
          ul.siblings().filter('img').hide();
          slideShowInterval = setInterval(function() { fb_next(); }, 400);
          isPlaying = true;
        }
      }

      function fb_stop() {
        isPlaying = false;
        if (slideShowInterval)
          clearInterval(slideShowInterval);
      }

      function fb_next() {
        fb_increment(1);
/*
        var index = currentIndex + 1;
        if (index >= totalCount)
          index = 0;
        else if (index < 0)
          index = totalCount - 1;

        jQuery('li', ul).hide();
        jQuery(jQuery('li', ul).get(index)).show();
        currentIndex = index;*/
      }
      function fb_increment(increment) {
        var index = currentIndex + increment;
        if (index >= totalCount)
          index = 1;
        else if (index < 0)
          index = totalCount - 1;

        fb_displayIndex(index);
        currentIndex = index;
      }
      function fb_displayIndex(index) {
        jQuery('li', ul).hide();
        jQuery(jQuery('li', ul).get(index)).show();
        jQuery("a[rel]", mainDiv).removeClass('fb-selected').each(function() {
           var a_index = parseInt(jQuery(this).attr('rel')) - 1;
           if (a_index >= 0) {
             if (a_index == index) {
               jQuery(this).addClass('fb-selected');
             }
           }
        });
      }
    };
})(jQuery);

