jQuery.fn.extend({ 
  disableSelection : function() { 
    return this.each(function() { 
      this.onselectstart = function() { return false; }; 
      this.unselectable = "on"; 
      jQuery(this).css('-moz-user-select', 'none'); 
      jQuery(this).css('-khtml-user-select', 'none'); 
      jQuery(this).css('-webkit-user-select', 'none'); 
      jQuery(this).css('user-select', 'none'); 
    }); 
  } 
});

(function($) {

  $.fn.pdf_viewer = function(title, filename, add_to_planner) {
    var HIDDEN = "HIDDEN", LOADING = "LOADING", ACTIVE = "ACTIVE";

    this.cur_page = 0;
    this.total_pages = 0;

    this.utilities = null;
    this.pagination = null;
    this.zooming = null;
    this.loading = null;
    
    this.title    = title;
    this.filename = filename;
    this.add_to_planner = add_to_planner;
        
    var bind = function(object, method) {
      return function() {
        return method.apply(object, arguments);
      };
    }
    
    var setupUI = function() {
      /* Remove the <div> if it already exists */
      $(this).html('');
      
      /* setup the container */
      var container = $('<div class="lightbox_content"></div>');
      $(this).append(container);
      
      /* Create the Title */
      container.append('<h1 class="title">'+this.title+'</h1>');

      /* Create the controls */
      this.utilities = $('<div class="lightbox_utilities"></div>');
      container.append(this.utilities);

      /* Create the "Add to MyPlanner" link */
      /* TODO what if they already have added this PDF ? */
      this.utilities.append($("<div class='utility_add'>"+this.add_to_planner+"</div>"));
      // since we added a new add button to the DOM, we need to initialize all our behaviours
      $.setupAddItemButtons();
      $.setupTooltips();
      $.setupPostables();
      
      /* Create the Zoom control */ 
      this.zooming = $( "<div class='utility_zoom'>" +
                            "<span>Zoom</span>" +
                            "<span class='utility_zoom_in_out'>" +
                              "<a class='zoom_out' href='#'></a>" +
                              "<a class='zoom_in' href='#'></a>" +
                            "</span>" +
                          "</div>" );
      this.utilities.append(this.zooming);
      this.utilities.find('a.zoom_out').click(bind(this, this._onZoomOut));
      this.utilities.find('a.zoom_in' ).click(bind(this, this._onZoomIn ));

      /* Create the Pagination control */
      this.pagination = $(  "<div class='utility_page'>" +
                                "<span>Page</span>" +
                                "<span class='utility_pagination'>" +
                                  "<a class='prev' href='#'></a>" +
                                  "<span class='status'></span>" +
                                  "<a class='next' href='#'></a>" +
                                "</span>" +
                              "</div>" );
      this.utilities.append(this.pagination);
      this.utilities.find('a.prev').click(bind(this, this._onPagePrev))
      this.utilities.find('a.next').click(bind(this, this._onPageNext))
      
      this.zooming.disableSelection();
      this.pagination.disableSelection();

      /* Create the Loading control */
      this.loading = $( "<div class='utility_loading'>" +
                          "Loading ..." +
                        "</div>" );
      this.utilities.append(this.loading);
      this.loading.hide();
      
      /* Embed the SWF file */
      /*  ... including a path to the SWF of the PDF */
      $(this).append($('<div class="lightbox_media"><div id="Viewer"></div></div>'));

      /*    ... SWF file then activates JavaScript UI using $.pdf_viewer methods */
    };
    this.setState = function(state) {
      if (state === LOADING) {
        this.zooming.hide();
        this.pagination.hide();
        this.loading.show();
        $(this).show();
      }
      if (state === ACTIVE) {
        this.zooming.show();
        this.pagination.show();
        this.loading.hide();
        $(this).show();
      }
    }
    this.setTitle = function(string) {
      // we are setting the title in intialize now
      // no need to have flash set the title
      //$(this).find("h1.title").html(string);      
    }
    this.setMaxPages = function(n) {
      this.total_pages = n;
      this.setPage(0)
    }
    this.setPaginationEnabled = function(shouldEnable) {
      $('.utility_pagination a.prev').addClass('prev_disabled');
      $('.utility_pagination a.next').addClass('next_disabled');
    }
    this.setZoomEnabled = function(selection, shouldEnable) {
      /* e.g.: $.pdf_viewer.setZoomEnabled('in', false); */
      if (shouldEnable) {
        this.utilities.find('a.zoom_' + selection).removeClass('zoom_' + selection + '_disabled');
      } else {
        this.utilities.find('a.zoom_' + selection).addClass('zoom_' + selection + '_disabled');
      }        
    }
    this.setZoom = function(n) {
      // 
    }
    this.setPage = function(n) {
      this.cur_page = n;
      this.pagination.find('span.status').html(this.cur_page + 1 + " of " + this.total_pages);
    }
    this.initialize = function() {
      setupUI.apply(this);
      swfobject.embedSWF(
        "/swf/Viewer.swf", 
        "Viewer", 
        "800", "600",
        "9.0.0",
        null,
        { 
          title:    this.title,
          filename: this.filename
        },
        null,
        null,
        bind(this, this._onSWFReady)
      );
    }

    this._onZoomOut = function(event) {
      if ($(event.target).hasClass('zoom_out_disabled')) return false;
      if (this.viewer) this.viewer.zoomOut();
      return false;
    }
    this._onZoomIn = function(event) {
      if ($(event.target).hasClass('zoom_in_disabled')) return false;
      if (this.viewer) this.viewer.zoomIn();
      return false;      
    }
    this._onPagePrev = function(event) {
      if (this.viewer) this.viewer.pagePrev();
      return false;
    }
    this._onPageNext = function(event) {
      if (this.viewer) this.viewer.pageNext();
      return false;
    }
    this._onSWFReady = function() {
      this.setState(LOADING); /* start loading */
      this.viewer = $('#Viewer')[0];
    }

    this.initialize();
    
    return this;
  };

})(jQuery);
