(function ($) {
  Drupal.behaviors.manipulateFormElements = {
    attach: function(context, settings) {
    // give the login form some love
    $('#user-login-form .login-submit-link').click(function(){
    	$('#user-login-form').submit();
    	return false;
    });
    }
  };
  Drupal.behaviors.correctActiveTrails = {
    attach: function(context, settings) {
      // fix menus that don't respect active trail because drupal links are stoopid
    $('#region-menu ul li.active').parents('li').addClass('active-trail');
    }
  };
})(jQuery);
;
(function($, undefined) {

  // global namespace, perhaps we should make our own
  v2Menu = function() {
    // todo, menu clickhandler in this file, not inline

    var toggle = function(element_id) {

      var $menu = $('#region-menu')
      ,   $el = $('#'+element_id.data('popup'))
      ;

      if ($el) {
        $el.toggle();
        if ($el.css('display') !== 'none') {
          // calculate the offset to the top (the site menus offset to the top)
          $el.css('top', $menu.outerHeight() + 'px');
        }
      }
    }

    ;


    $(document).ready(function() {
      $('#region-menu').bind('click', function(event){
        var $target = $(event.target).closest('a[data-popup]');

        if ($target.data('popup') !== undefined) {
          event.preventDefault();
          // toggle the menu and bring keyboard focus to it
          toggle($target.focus());
        }
        // else, just act as a normal link
      });
    });

    ;return {
      toggle: toggle
    }
  ;}();

}(jQuery));
;
/** reactToReferrer
 * @Author Martin Gausby
 * Use regular expressions to define rules that is triggered when
 * the user navigates to the site from a different site.
 *
 * When being referred from facebook we could use this to pimp our
 * facebook page.
 *
 * This could be stand-alone, but we are adding it to the jQuery
 * name-space so we don't pollute the global scope.
 */
(function($, undefined){
  /**
   * @requires jQuery
   */
  $.reactToReferrer = (function() {
    /* private variables */
    /**
     * sites contain the rules and callback functions
     * @name sites
     * @private
     */
    var sites = []

    /**
     * Add a rule to the checklist.
     *
     * addSite will return the $.reactToReferrer object, so
     * it is possible to chain multible addSites calls and
     * execute it with the react function like so:
     *
     *     $.reactToReferrer
     *         .addSite(...)
     *         .addSite(...)
     *         .addSite(...)
     *         .react()
     *     ;
     *
     * @name addSite
     * @author Martin Gausby
     * @public
     * @function
     * @param {string} site The name of the site, this has no purpose other than it is documenting which site the rule target
     * @param {regexp} rule a regexp matching the site we target
     * @param {function} callback function that should be run when the site matches, remember to use $(document).ready() when altering the DOM
     * @returns {object} the $.reactToReferrer object, making it possible to chain further calls
     * */
    ,addSite = function(site, rule, callback) {
      /* push the site test and its reaction to the poll */
      sites.push({
        'site': site,
        'rule': rule,
        'reaction': callback
      });

      /* make it possible to chain sites and reactions */
      return $.reactToReferrer;
    }

    /**
     * Match the rules with the rules in the sites array,
     * added with the addSite function. The rules will be
     * checked in last-in-first-out order, and it uses pop
     * to do so, the sites array will be emptiet in this
     * process.
     * @name react
     * @author Martin Gausby
     * @public
     * @function
     * @param {string} url overwrite the document.referrer string, this is mainly for testing purposes
     * @returns {object} the object that matched the referrer url, the object is empty if there was no matches. This is primarily for debugging
     */
    ,react = function(url) {
      url = url || document.referrer;

      for (var site; site = sites.pop();) {
        if (site.rule.test(url)) {
          site.reaction();
          sites = []; /* reset the sites array */

          return site;
        }
      }

      return {};
    }

    /* Public functions */
    ;return { addSite: addSite, react: react }
  }());
}(jQuery));

;
(function($){

  var resizeFigure = function($el) {
    $el
      /* the selector we use is figure > img, so we know
       * the parent element is a figure */
      .parent()
      .width(parseInt($el.first().width(), 10))
    ;
  }

  $(document).ready(function(){
    $('article figure.left > img, article figure.right > img').each(function(){
      var $this = $(this);

      /* we check it width of the image. Sometimes there's
       * a race condition, if we try to access the image too
       * soon, it will have a width of zero */
      if ($this.width()) {
        resizeFigure($this);
      }
      else {
        $this.load(function() {
          resizeFigure($this);
        });
      }
    });
  });

  Drupal.behaviors.placeFlagPlaceholderBubbles = {
    attach: function(context) {
      $('.flag-placeholder', context).each(function(i, elem) {
        var $elem = $(elem)
          , $bubble = $('.teaser-bubble', elem);

        $elem.hover(function(){
          $bubble
            .css({
              top: ($elem.position().top - ($bubble.outerHeight() - $elem.outerHeight()) / 2) + "px",
              left: ($elem.position().left - $bubble.width() - 2) + "px"
            })
            .show()
          ;
        }, function(){
          $bubble
            .hide()
          ;
        });
      });
    }
  }

  Drupal.behaviors.checkSubscribeCheckboxOnFlag = {
    attach: function(context) {
      // Find the flag-comment-subscribe element. After flagging, the context _is_ the
      // wrapper, which is why we have this workaround to finding it..
      var elem = $(context);
      if (!elem.hasClass('flag-comment-subscribe')) {
        var elem = $('.flag-comment-subscribe', context);
      }

      // Bind a click-element that toggles the value of the checkbox depending on
      // the button either flagging or unflagging the subscription flag.
      $('a', elem).click(function(e) {
        if($(this).hasClass('flag-action')) {
          $('#comment-form input[name="subscribe"]').attr('checked', 'checked');
        } else {
          $('#comment-form input[name="subscribe"]').removeAttr('checked');
        }
      })
    }
  }

  Drupal.behaviors.addResizeBehavior = {
    attach: function(context) {
      /* this should only be applied to nodes in full view mode */
      var $el = $('article.full-view', context);

      if (! $el.find('figure img[data-original-image]').length) {
        return;
      }

      /* from this point on, we should have images that is zoomable */
      $el.bind('click', function(event) {
        var $target = $(event.target);

        if ($target.is('img') && $target.data('original-image')) {

          var $img = $([
            '<div class="image-zoom">'
              ,'<figure>'
                ,'<img src="',$target.data('original-image'),'" />'
              ,'</figure>'
              ,'<button>Luk billede</button>'
            ,'</div>'
          ].join(''))

          ,keyboardHandler = function(event){
            if (event.which === 27) {
              /* remove the image on esc */
              $img.click();
            }
          }

          ,clickHandler = function(event) {
            /* remove the keyboard handler */
            $(document).unbind('keyup', keyboardHandler);
            /* remove the image */
            $(this).remove();
          };

          var caption = $target.parent('figure').find('figcaption').text();
          if (caption) {
            /* prevent ie from barfing and dying on the figcaption */
            document.createElement('figcaption');

            $img.find('figure').append(
              $('<figcaption></figcaption>').text(caption)
            );
          }

          $('body').append($img);
          $(document).bind('keyup', keyboardHandler);
          $img.one('click', clickHandler);

        }

      });
    }
  }

  /**
   * Make the radio buttons on the it-company guide search page clickable
   */
  Drupal.behaviors.clickOnRadioInputsInTheCompanyGuideShouldFollowLink = {
    attach: function(context) {
      var clickHandler = function(event) {

        var newLocation = $(event.target)
          .parent()
          .find('a')
          .first()
          .attr('href')
        ;

        if (newLocation) {
          document.location = newLocation;
        }
      }

      /* find and apply the */
      $('#block-industry-guide-industry-guide-search-type')
        .find('.form-radio')
        .click(clickHandler)
      ;
    }
  }

}(jQuery));
;

