(function ($) {

  /**
   * Open the overlay, or load content into it, when an admin link is clicked.
   */
  Drupal.behaviors.hunterCatalog = {
    attach: function (context, settings) {
      if (this.processed) {
        return;
      }
      this.processed = true;

      $("a.catalog-overlay")
        // Instead of binding a click event handler to every link we bind one to
        // the document and only handle events that bubble up. This allows other
        // scripts to bind their own handlers to links and also to prevent
        // overlay's handling.
        .bind('click.catalog-overlay mouseup.catalog-overlay', $.proxy(Drupal.hunterCatalog, 'eventhandlerOverrideLink'));
    }
  };

  Drupal.hunterCatalog = Drupal.hunterCatalog || {};


  /**
   * Event handler: overrides href of administrative links to be opened in
   * the overlay.
   *
   * This click event handler should be bound to any document (for example the
   * overlay iframe) of which you want links to open in the overlay.
   *
   * @param event
   *   Event being triggered, with the following restrictions:
   *   - event.type: click, mouseup
   *   - event.currentTarget: document
   *
   * @see Drupal.overlayChild.behaviors.addClickHandler
   */
  Drupal.hunterCatalog.eventhandlerOverrideLink = function (event) {
    // In some browsers the click event isn't fired for right-clicks. Use the
    // mouseup event for right-clicks and the click event for everything else.
    if ((event.type == 'click' && event.button == 2) || (event.type == 'mouseup' && event.button != 2)) {
      return;
    }
    var $target = $(event.target);

    // Only continue if clicked target (or one of its parents) is a link.
    if (!$target.is('a')) {
      $target = $target.closest('a');
      if (!$target.length) {
        return;
      }
    }

    var target = $target[0];


    var href = target.href;
    // Only handle links that have an href attribute and use the http(s) protocol.
    if (href != undefined && href != '' && target.protocol.match(/^https?\:/)) {
      var anchor = href.replace(target.ownerDocument.location.href, '');
      if ($target.hasClass('catalog-overlay')) {

        // If the link contains the overlay-restore class and the overlay-context
        // state is set, also update the parent window's location.
        var parentLocation = ($target.hasClass('overlay-restore') && typeof $.bbq.getState('overlay-context') == 'string')
          ? Drupal.settings.basePath + $.bbq.getState('overlay-context')
          : null;
        href = Drupal.overlay.fragmentizeLink($target.get(0), parentLocation);
        // Only override default behavior when left-clicking and user is not
        // pressing the ALT, CTRL, META (Command key on the Macintosh keyboard)
        // or SHIFT key.
        if (event.button == 0 && !event.altKey && !event.ctrlKey && !event.metaKey && !event.shiftKey) {
          // Redirect to a fragmentized href. This will trigger a hashchange event.
          Drupal.overlay.redirect(href);
          // Prevent default action and further propagation of the event.
          return false;
        }
        // Otherwise alter clicked link's href. This is being picked up by
        // the default action handler.
        else {
          $target
            // Restore link's href attribute on blur or next click.
            .one('blur mousedown', { target: target, href: target.href }, function (event) { $(event.data.target).attr('href', event.data.href); })
            .attr('href', href);
        }
      }
    }
  };



})(jQuery);
;
;(function($) {
  Drupal.HunterModals = Drupal.HunterModals || {};

  Drupal.HunterModals.eventhandlerHashChange = function (event) {
    var link = $.bbq.getState('lightbox');
    if (!link || link.length === 0) {
      if ($('#fancybox-content').length !== 0) {
        $.fancybox.close();
      }
      return;
    }
    // If we don't call this first, fancybox doesn't init itself properly when
    // we're triggered from loading page with a hash value
    $.fancybox.init();
    $('a.lightbox[href$="' + link + '"]')
      .fancybox({
        type: 'ajax',
        href: Drupal.settings.basePath + 'lightbox?path=' + link,
        padding: 15,
				autoDimensions: false,
        scrolling: 'no',
        overlayColor: '#333',
				width: '800',
				height: '430',
        onComplete: function () {
          //var context = '#fancybox-content';
          //// Activate JS behaviors within the new content
          //Drupal.attachBehaviors(context);
					$('#fancybox-content .field-name-body').jScrollPane();
        },
        onClosed: function () {
          window.location.hash = '';
          $('a.lightbox').unbind('click');
        }
      }).trigger('click');
  };

  Drupal.behaviors.hunterModals = {
    attach: function(context, settings) {
      var location = window.location.href.substr(0, window.location.href.indexOf(window.location.hash));

      // Don't modify links *already* in a lightbox
      if (context != '#fancybox-content') {
        $('a.lightbox', context).attr('href', function(index, attr) {
          return location + '#lightbox=' + attr.replace(settings.basePath, '');
        });
      }

      // Bind our hashchange callback (only once)
      if (context == document) {
        $(window)
          .bind('hashchange.hunter-modal', $.proxy(Drupal.HunterModals, 'eventhandlerHashChange'))
          .triggerHandler('hashchange.hunter-modal');
      }
    }
  };
})(jQuery);
;

