(function($, w, d) {
  
  var $w = $(w),
      $d = $(d);

  $.easing.def = 'easeOutCubic';

  $(function() {
    
    if(typeof addthis !== 'undefined') {
      addthis_config = {
        ui_language: "da"
      }
      addthis.init();
    }
    
    // search
    $('#top input, .m_search_results input').focus(function() { $(this).select(); });
    
    // target
    $('.target').live('click', function(e) {
      if (e.button === 0) {
        var target = $(this),
            hasLink = target.find('a.link'),
            link = hasLink.length ? hasLink : target.find('a:not(.editbutton):first');
        if (link.length > 0) {
          var href = link.attr('href');
          if (href) {          
            if (link.attr('target') === '_blank') {
              window.open(href);
            } else {
              document.location = href;
            }
            return false;
          }
        }
      }
    }).find('a').click(function(e) { e.stopPropagation(); });
    
    // carousel
    $('.m_carousel').each(function() {
      var carousel = $(this),
          images = carousel.find('li'),
          imagesLastIndex = images.length - 1,
          first = images.eq(0),
          active = 0,
          target,
          interval,
          startInterval = function() {
            clearInterval(interval);
            interval = setInterval(function() {
              target = (active === imagesLastIndex) ? 0 : active + 1;
              images.eq(target).css({ opacity: 1, zIndex: 1 });
              images.eq(active).animate({ opacity: 0 }, 1000, function() {
                images.eq(active).css('zIndex', 0);
                images.eq(target).css('zIndex', 2);
                active = target;
              });

            }, 4000);
          };
      
      first.siblings().css({ zIndex: 0, opacity: 0 }).show();
      
      // play nice when not in focus
      $w.blur(function() { clearInterval(interval); });
      $w.focus(function() { startInterval(); });
      
      startInterval();
      
    });
    
    // tabs
    $('.tabbed').each(function() {
      var tabbed = $(this),
          tabs = tabbed.find('.tabs a'),
          tabContent = tabbed.find('.tabContent'),
          index;
      
      tabs.mousedown(function() {
        index = tabs.index(this);
        tabs.removeClass('active').eq(index).addClass('active');
        tabContent.hide().eq(index).show();
      }).click(false);
        
    });
    
    // pagination
    $('.m_pagination').each(function() {
      var pagination = $(this),
          form = pagination.find('form'),
          select = form.find('select');
          
      select.change(function() {
        form.submit();
      });
    });
        
    
  });

})(this.jQuery, this.window, this.document);

