(function($) {
  window.yikes_hit = function yikes_hit(keys) {
    keys = keys instanceof Array ? keys : [keys];
    var path = Drupal.settings.yikes.path + '/pixel.gif?key=' + keys.join('&key=');
    return $('<img>')
      .attr('alt', '')
      .attr('src', path)
      .addClass('yikes-tracking-pixel')
      .appendTo($('body'))
    ;
  };
})(jQuery);;
(function ($) {
  Drupal.behaviors.rate = {
    attach: function(context) {
      $('.rate-widget:not(.rate-processed)', context).addClass('rate-processed').each(function () {
        var widget = $(this);
        var ids = widget.attr('id').match(/^rate\-([a-z]+)\-([0-9]+)\-([0-9]+)\-([0-9])$/);
        var data = {
          content_type: ids[1],
          content_id: ids[2],
          widget_id: ids[3],
          widget_mode: ids[4]
        };

        $('a.rate-button', widget).click(function() {
          var token = this.getAttribute('href').match(/rate\=([a-zA-Z0-9\-_]{32,64})/)[1];
          return Drupal.rateVote(widget, data, token);
        });
      });
    }
  };

  Drupal.rateVote = function(widget, data, token) {
    // Invoke JavaScript hook.
    $.event.trigger('eventBeforeRate', [data]);

    $(".rate-info", widget).text(Drupal.t('Saving vote...'));

    // Random number to prevent caching, see http://drupal.org/node/1042216#comment-4046618
    var random = Math.floor(Math.random() * 99999);

    var q = '?q=rate%2Fvote%2Fjs&widget_id=' + data.widget_id + '&content_type=' + data.content_type + '&content_id=' + data.content_id + '&widget_mode=' + data.widget_mode + '&token=' + token + '&destination=' + escape(document.location) + '&r=' + random;
    if (data.value) {
      q = q + '&value=' + data.value;
    }

    $.get(Drupal.settings.basePath + q, function(data) {
      if (data.match(/^https?\:\/\/[^\/]+\/(.*)$/)) {
        // We got a redirect.
        document.location = data;
      }
      else {
        // get parent object
        var p = widget.parent();

        // Invoke JavaScript hook.
        $.event.trigger('eventAfterRate', [data]);

        widget.before(data);

        // remove widget
        widget.remove();
        widget = undefined;

        Drupal.attachBehaviors(p.get(0));
      }
    });

    return false;
  }
})(jQuery);;
(function($) {
  function registerHit(key) {
    return function(e) {
      e.preventDefault();
      var href = $(this).attr('href');

      yikes_hit(key).bind('load error', function() {
        window.location.href = href;
      });
    };
  }

  Drupal.behaviors.jobboardYikes = {
    attach: function(context) {
      if(Drupal.settings.yikes.keyPattern) {

        $('.teaser.listing').each(function(i, elem) {
          var matches = $(elem).attr('class').match(/node-([0-9]+)/)
            , nid     = matches[1]
            , key     = Drupal.settings.yikes.keyPattern.replace(':nid', nid)
          ;

          $('a', elem)
            .not('.contextual-links-wrapper a')
            .click(registerHit(key))
          ;
        });

        $('.related-jobs .views-row').each(function(i, elem) {
          var matches = $('.entry-title > .field-content', $(elem)).attr('class').match(/node-job-([0-9]+)/)
            , nid     = matches[1]
            , key     = Drupal.settings.yikes.keyPattern.replace(':nid', nid)
          ;

          $('a', elem)
            .not('.contextual-links-wrapper a')
            .click(registerHit(key))
          ;

        });

        $('.top-job .views-row').each(function(i, elem) {
          var field_title = $('.description .field-content', $(elem));
          if (!field_title.length) {
            return;
          }

          var matches = field_title.attr('class').match(/node-job-([0-9]+)/)
            , nid     = matches[1]
            , key     = Drupal.settings.yikes.keyPattern.replace(':nid', nid)
          ;

          $('a', elem).not('.contextual-links-wrapper a').click(registerHit(key));
        });

      }
    }
  };
})(jQuery);;
(function($) {
  Drupal.behaviors.pressReleases = {
    attach: function(context) {
      $('article.node div.press-release').each(function(i, elem) {
        $(elem).hide().before(
          $('<div>').addClass('press-release-toggle').append(
            $('<a>')
              .text(Drupal.t('Show press release'))
              .click(function(link) {
                $(this).text( $(this).text() ==  Drupal.t('Show press release')
                              ? Drupal.t('Hide press release')
                              : Drupal.t('Show press release'));
                $(elem).slideToggle();
              })
          )
        );
      });
    }
  }
})(jQuery);;

