/*
 * @file
 * Common Getfit Javascript functions
 */
(function($) {

  Drupal.Haven = {};

  // change urls when using https
  Drupal.behaviors.secureadmin = {
    attach: function() {
      if (window.location.protocol === 'https:' && window.location.host === 'secure3098.hostgator.com') {
        $(a).each(function() {
          $(this).attr('href', $(this).attr('href').replace('~asphodel/', '~asphodel/?q='));
        });
      }
    }
  };

  // add jQuery UI date picker to date fields
  Drupal.behaviors.datepick = {
    attach: function() {
      if ($.datepicker) {
        $.datepicker.setDefaults({
          dateFormat: 'mm/dd/yy'
        });

        $(function() {
          $('input.datetime').datepicker();
        });
      }
    }
  };

  // new window
  Drupal.behaviors.new_window = {
    attach: function() {
      var host_pattern = new RegExp('https?://' + location.host); // get the host of the current site
      $('a').not('.external, .new-window, .no-new-window').each(function() {
        if ($(this).attr('href') && $(this).attr('href').match(/^https?:\/\//)
                && !$(this).attr('href').match(host_pattern)) {
          $(this).addClass('external');
        }
      });

      $('a.external, a.new-window').click(function() {
        if (!$(this).hasClass('no-new-window')) {
          window.open(this.href);
          return false;
        }
      });

    }
  };

  Drupal.behaviors.script = {
    attach: function() {
      // "search" text in search box
      $('#search-block-form .form-text').val('Search');
      $('#search-block-form .form-text').click(function () {
        if ($(this).val() == 'Search') {
          $(this).val('');
        }
      });
      // -- menu clicking: make sure the box and not just the anchor is clickable -- //
      $('#region-menu ul.links li, #region-menu ul.main-menu li').click(function() {
        var anchor = $(this).find('a');
        if (anchor) {
          window.location = anchor.attr('href');
        }
      });
      // -- popup hover / click -- //
      if ($('.popup').length) {
        $('.popup-trigger').hover(
          function() { // hover in
            $(this).parents('.popup-wrapper').find('.popup').css('visibility','visible');
          },
          function() { // hover out
            $(this).parents('.popup-wrapper').find('.popup').css('visibility','hidden');
          }

          );
      }

      $('.section-resources .meta .print-icon a').click(function() {
        window.print();
      });
    }
  };

})(jQuery);;
