(function($){
  /**
   * JavaScript helper scripts for nflol_add_ajax().
   */
  Drupal.behaviors.nflolAjax = {
    attach: function(context, settings)
    {
      if (settings.nflol && settings.nflol.ajax) {
      
        for(var base in settings.nflol.ajax) {
          
          if (settings.nflol.ajax[base].lightbox) {
            $('#' + base).once(function() {
              $(this).bind('click', function() {
                
                var base = $(this).attr('id');
                $('#nflol-generic-lightbox')
                  .trigger('lightbox-show-ajax',
                      // Pass the settings themselves to the event.
                      $.extend(true, settings.ajax[base] || {}, settings.nflol.ajax[base] || {}));
              });
            });
          }
        }
      
      }
    }
  };
    
})(jQuery);
;
(function($){
  /**
   * NFLOL Lightbox.
   */
  Drupal.theme.prototype.nflolLightbox = function(selector, options) {
    
    // Setup initial values.
    if (!selector) {
      // All lightboxes are wrapped in an element with this class. 
      selector = '.nflol-lightbox-wrapper';
    }
    
    if (!options) {
      options = {};
    }
    
    // Setup options.
    options = $.extend(true, {
      // Default dimensions (.nflol-lightbox-box).
      width: null,
      height: null,
      // Default overlay opacity.
      opacity: 0.7,
      // Default is to animate open/close behaviours.
      animateOpenClose: true
    }, options);
    
    /**
     *  Process all the lightboxes in found with the selector.
     */
    $(selector).each(function(i, lightbox){
      // Just a temporary fix.
      if ($(this).hasClass('nflol-lighbox-once-processed')) {
        _refreshPosition(lightbox);
      }
      
      $(this).once('nflol-lighbox-once', function() {
        __log('init on element: ');
        __dump($(lightbox));
        
        $(lightbox).bind('lightbox-show', function(){
          _displayBox(lightbox);
        });
        
        $(lightbox).bind('lightbox-hide', function() {
          _hideBox(lightbox);
        });
        
        // FIXME: all these event should be in appropriate functions.
        
        // Helper event - looks for an element that has a title class
        // and assigns the html found in the title argument to it.
        $(lightbox).bind('lightbox-title', function(event, title) {
          if (!title) {
            return;
          }
          
          $('.nflol-lightbox-header .title', this).first().html(title);
          Drupal.theme('replaceFonts', $('.nflol-lightbox-header .title', this));
        });
        
        // Helper event - clear the lightbox's content and optionally add
        // html.
        $(lightbox).bind('lightbox-clear', function(event, html) {
          $('.nflol-lightbox-content-wrapper', this).html('');
          if (!html) {
            return;
          }
          $('.nflol-lightbox-content-wrapper', this).html(html);
        });
        
        // Simply bundles up a few of the above helpers.
        $(lightbox).bind('lightbox-show-ajax', function(event, params) {
          if (!params.lightboxTitle) {
            params.lightboxTitle = Drupal.t('NFL On Location');
          }
          
          $(this).trigger('lightbox-clear', 
              '<div id="' + params.wrapper + '" class="nflol-lightbox-loading-gadget"></div>')
            .trigger('lightbox-title', params.lightboxTitle)
            .trigger('lightbox-show');
          
          $('.nflol-lightbox-box', this)
            .attr('id', params.wrapper + '-lightbox-box');
          
          Drupal.attachBehaviors($('#' + params.wrapper, this));
        });
        
        // Set overlay opacity.
        $('.nflol-lightbox-overlay', lightbox).css('opacity', options.opacity);
        
        // Setup close functionality.
        $('.nflol-lightbox-overlay, .nflol-lightbox-box-wrapper, .nflol-lightbox-close', lightbox)
        .bind('click', function(event) {
          // Lame, I know.
          if ($(event.target).hasClass('nflol-lightbox-overlay') ||
              $(event.target).hasClass('nflol-lightbox-box-wrapper') ||
              $(event.target).hasClass('nflol-lightbox-close')) {
            _hideBox(lightbox);
          }
        });
        
        // Setup initial content size.
        if (options.width) {
          $('.nflol-lightbox-box, .nflol-lightbox-box-bg', lightbox)
          .width(options.width);
        }
        if (options.width) {
          $('.nflol-lightbox-content-wrapper', lightbox).height(options.height);
        }
        
        // Resize overlay as soon as the window resizes.
        $(window).bind('resize', function(){
          _resize(lightbox);
        });
      });
    });
    
    
    _resize();
    /* HELPER FUNCTIONS */
    function _displayBox(lightbox) {
      _refreshPosition(lightbox);
      
      if (options.animateOpenClose) {
        $('.nflol-lightbox-overlay, .nflol-lightbox-box-wrapper', lightbox)
          .removeClass('nflol-lightbox-hide')
          .hide()
          .fadeIn('slow');
      } else {
        $('.nflol-lightbox-overlay, .nflol-lightbox-box-wrapper', lightbox)
          .removeClass('nflol-lightbox-hide');
      }
    }
    
    function _refreshPosition(lightbox) {
      var size = __getPageSize();
      var scroll = __getPageScroll();
      var scrollYBox = scroll.yScroll;
      var lightboxHeight = $('.nflol-lightbox-box-wrapper', lightbox).height();
      
      if (size.pageHeight > lightboxHeight) {
        scrollYBox += parseInt((size.windowHeight - lightboxHeight) / 2);
      }
      $('.nflol-lightbox-box-wrapper', lightbox)
        .css('top', scrollYBox);
    }
    
    function _hideBox(lightbox) {
      if (options.animateOpenClose) {
        $('.nflol-lightbox-overlay, .nflol-lightbox-box-wrapper', lightbox)
          .fadeOut('slow', function(){
            $(this).addClass('nflol-lightbox-hide')
              .trigger('lightbox-clear');
          });
      } else {
        $('.nflol-lightbox-overlay, .nflol-lightbox-box-wrapper', lightbox)
          .addClass('nflol-lightbox-hide');
        $(lightbox).trigger('lightbox-clear');
      }
    }
    
    /**
     * Called on window/document resize event.
     */
    function _resize(lightbox) {
      var size = __getPageSize();
      var scroll = __getPageScroll();
      
      $('.nflol-lightbox-overlay', lightbox)
      .width(size.pageWidth)
      .height(size.pageHeight);
      
      // Make sure the box wrapper is as wide as the content of the page
      // not only the window or the body. In some cases the body is smaller
      // then it's content (when page is resized to be smaller then the body's
      // contents). Getting the document width seems to work for most recent
      // browsers.
      $('.nflol-lightbox-box-wrapper', lightbox)
      .width(size.pageWidth);
    }
    
    function __dump(msg) {
      __log(msg, true);
    }
    
    function __log(msg, suppressHeader) {
      if (!suppressHeader) {
        msg = 'NFLOL Lightbox: ' + msg;
      }
    
      if (window.console) {
        window.console.log(msg);
      } else {
        // Don't log.
      }
    };
    
    /**
     * / THIRD FUNCTION getPageSize() by quirksmode.com
     */
    function __getPageSize() {
      var xScroll, yScroll;
      if (window.innerHeight && window.scrollMaxY) {
        xScroll = window.innerWidth + window.scrollMaxX;
        yScroll = window.innerHeight + window.scrollMaxY;
      } else if (document.body.scrollHeight > document.body.offsetHeight) { 
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
      } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla
                // and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
      }
      var windowWidth, windowHeight;
      if (self.innerHeight) { // all except Explorer
        if (document.documentElement.clientWidth) {
          windowWidth = document.documentElement.clientWidth;
        } else {
          windowWidth = self.innerWidth;
        }
        windowHeight = self.innerHeight;
      } else if (document.documentElement
          && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
      } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
      }
      // for small pages with total height less then height of the viewport
      if (yScroll < windowHeight) {
        pageHeight = windowHeight;
      } else {
        pageHeight = yScroll;
      }
      // for small pages with total width less then width of the viewport
      if (xScroll < windowWidth) {
        pageWidth = xScroll;
      } else {
        pageWidth = windowWidth;
      }
    
      return {
        pageWidth : pageWidth,
        pageHeight : pageHeight,
        windowWidth : windowWidth,
        windowHeight : windowHeight
      };
    };
      
    /**
     * / THIRD FUNCTION getPageScroll() by quirksmode.com
     * 
     * @return Array Return an array with x,y page scroll values.
     */
    function __getPageScroll() {
      var xScroll, yScroll;
      if (self.pageYOffset) {
        yScroll = self.pageYOffset;
        xScroll = self.pageXOffset;
      } else if (document.documentElement && document.documentElement.scrollTop) {
        yScroll = document.documentElement.scrollTop;
        xScroll = document.documentElement.scrollLeft;
      } else if (document.body) {// all other Explorers
        yScroll = document.body.scrollTop;
        xScroll = document.body.scrollLeft;
      }
    
      return {
        xScroll : xScroll,
        yScroll : yScroll
      };
    };
  };
    
  Drupal.behaviors.nflolLigbox = {
    attach: function(context, settings)
    { 
      Drupal.theme('nflolLightbox', '.nflol-lightbox-wrapper');
    }
  };
    
})(jQuery);
;

