jQuery(document).ready(function () {
  if (typeof(window.slider) !== 'undefined')
    return false;

  $('.popup > a.widget_closer').live('click', function () {
    popupManager.hide();
  });

  $('#arrow_tooltip_up').live('click', function () {
    var $el = $('#nav_social_pullup')
    , visibility = $el.css('visibility') === 'visible' ? 'hidden' : 'visible';
    $el.css('visibility', visibility);
  });
  
  /**
   * Navigational javascript addition.
   */
  var nav_timeout
  , current_nav;
  $('.nav_main.nav_level_0 > li').live('mouseenter', function () {
    clearTimeout(nav_timeout);
    var el = $(this);
    if (current_nav != el.index())
      $('.nav_main.nav_level_1').hide();
    current_nav = el.index();
    if (!window.sliding) {
      el.addClass('highlight');
      el.find('ul').fadeIn(200);
    }
  }).live('mouseleave', function () {
    var self = $(this);
    self.removeClass('highlight');
    nav_timeout = setTimeout(function () {
      self.find('ul').fadeOut();
    }, 1000);
  });
  
  // stuff that needs to be done on ready and on every new page that is slided in.
  $(document).bind('slided_or_ready', function (e, $context) {

    // check the navigation items if one of their links references this page. if so, set to active.
    var page_regex = /^(.*)\/(.*)$/
    , current_page = window.location.pathname.replace(page_regex, "$2")
    , active_found = false
    , setActive = function ($elem) {
      if ($elem.hasClass('hiddenNav')) {
        setActive($elem.parent('ul').parent('li')); // theoretically this could be .closest('li'), but this is safer.
      } else {
        $elem.addClass('active');
      }
    }
    , nav_active_parser = function () {
      if (active_found) {
        return false;
      }

      var $this = $(this)
      , page = $this.children('a').attr('href').replace(page_regex, "$2");

      if (page === current_page) {
        setActive($this);
        active_found = true;
        return false;
      }
    }
    
    // TODO: If the brandpage goes to production change 'js!mcb...' to 'js!/mcb...'
  
    $('ul.nav_main > li', $context).each(nav_active_parser);
    
    curl([
      'js!mcb/brand_selection.js'
    ]).then(function () {
      makeBrandCarousel($context)
    });
  });
  

  // workaround for .ready in slided content
  // if you want your scripts to load (and have some effect) upon the ready event do this: 
  // $(document).bind('slided_or_ready', function ($context) {});
  $(document).trigger('slided_or_ready');
});


// Scholz & Volker Flash webtracking function:
/**
 * Called from JS and Flash to submit a tag
 * @param String Page
 * @param String Title
 * @param String MainContentGroup
 * @return void
 */
function trackWT(Page, Title, contentgroup) {
  if (typeof(contentgroup) === 'undefined') {
    dcsMultiTrack('DCS.dcsuri', Page, 'WT.ti', Title);
  } else {
    dcsMultiTrack('DCS.dcsuri', Page, 'WT.ti', Title, 'WT.cg_n', contentgroup);
  }
}
