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);
  });

  curl(
    ['js!'+res_path+'/js/widget_enter_contest_purchase_reward.js']
  );
  
  // 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) {
    // edit links
    $('a.edit_link', $context).parent().mouseenter(function () {
      $(this).children('a.edit_link').css('visibility', 'visible');
    }).mouseleave(function (e) {
      $(this).children('a.edit_link').css('visibility', 'hidden');
    });

    // 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")
    , setActive = function ($elem) {
      if ($elem.hasClass('hiddenNav')) {
        setActive($elem.parent('ul').parent('li'));
      } else {
        $elem.addClass('active');
        var $parent = $elem.parent('ul');
        if ($parent.hasClass('nav_level_1')) {
          setActive($parent.parent('li'));
          $parent.show().addClass('noFade');
        }
      }
    }
    , nav_active_parser = function () {
      var $this = $(this)
      , page = $this.children('a').attr('href').replace(page_regex, "$2");

      if (page === current_page) {
        setActive($this);
      }
    }
  
    $('ul.nav_main > li', $context).each(nav_active_parser);
    
    curl([
      'js!'+window.res_path+'/js/brand_selection.js'
    ]).then(function () {
      makeBrandCarousel($context)
    });
    // Resize Subnavi because width:auto doesnt work in IE7
  
    $('.nav_main li ul li a .nav_item_title').each( function(i, val) {
      var a = $(this).parent('a'),
      width = $(this).width();
      if(i != 0) {
        a.parent('li').width((width+15)+'px').css('min-width', '0');
        a.width((width+5)+'px').css('min-width', '0')
      } else {
        a.parent('li').width((width+5)+'px').css('min-width', '0');
      }
    });
    $('.nav_main.nav_level_0').css('visibility', 'visible'); // Display Navi after loading the page or slide
  });
  
  function backto_where () {

    if (window.location.search.search(/backto/) != -1) {
      var backto=window.location.search.substring(1, location.search.length).split('&')

      for (var x in backto) {
        var temp = backto[x].split('=')

        if (temp[0] == 'backto') {
          if (temp[1] != '') {
            $('.widget_back').attr('href', temp[1]+'.html');
          }
          break;
        }
      }
    }
  }
  
  backto_where();

  // 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');
  
});


// add webtrendsd tracking
if (typeof(window._tag) === 'undefined') {
  var _tag = new WebTrends();
  _tag.dcsGetId();

  _tag.dcsCustom = function() {
    _tag.DCSext.search_param = window.location.search;
  }
  _tag.dcsCollect();
}


// 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);
  }
}
