var AdHelper = {
  populateDynamicValues: function() {
      var ord = Math.random()*1e09;
      var tile = 0;
      $('.ad_margin_src').each(function(){
        var obj = $(this);
        tile++;
        if(!obj.attr('data-src')) return true;
        var url = obj.attr('data-src');        
        url = url.replace(/tileval/, tile);  
        url = url.replace(/ordval/, ord.toString().replace('.', '').substring(0,9));
        obj.attr('src', url);
        return true;
      });
      
      return $('.ad_margin_href').each(function(){
        var obj = $(this);   
        url = obj.find('img').attr('src');
        url = url.replace(/http:\/\/ad\.doubleclick\.net\/ad/, "http://ad.doubleclick.net/jump");
        obj.attr('href', url);
        return true;
      });
  }
};

var FavoriteArticles = {
  init: function(){
    var pane_links = $('ul.most_popular');
    var anchors    = $('ul.most_popular a'); // Text in Anchors does not trigger the li.click
    $.each(anchors, function() {
	self = $(this);
	self.click(function(e) {
	  e.preventDefault();
	  $(e.target).parent('li').click();
	});
    });
    if ( pane_links.length > 0 ){
      var self, pane_lis = pane_links.find('li');
      $.each(pane_lis, function(){
        self = $(this);
        if ( self.hasClass('first') ){
          $('#' + self.attr('rel')).show();
        }
        self.click(function(e){
          var clicked = $(this);
          $.each(pane_lis, function(){
            var li = $(this);
            li.removeClass('active');
            $('#' + li.attr('rel')).hide();
          });
          $('#' + clicked.attr('rel')).show();
          clicked.addClass('active');
        });
      });
    }
  }
  
};

var ArticleCarousel = {
  init: function(){		
    var article_carousel = $('div.article_slider_wrapper');
    if ( article_carousel.find('li').length < 2 ) { return; }

    article_carousel.jcarousel({
      auto: 8,
      scroll: 1,
      wrap: 'circular',
      initCallback: ArticleCarousel.postInit,
      itemFirstInCallback: ArticleCarousel.postAnimate,
      buttonNextHTML: null,
      buttonPrevHTML: null
    });
  },
  postAnimate: function(carousel, current_item, current_index) {
    $('.jcarousel-control a').removeClass('active');
    $($('.jcarousel-control a').get((current_index % 5) - 1)).addClass('active');
  },
  postInit: function(carousel){
    $('.jcarousel-control a').bind('click', function() {
      carousel.scroll(parseInt(jQuery(this).text(), 10));
      $('.jcarousel-control a').removeClass('active');
      $(this).addClass('active');
      
      carousel.stopAuto();
      $('.btn_pause').hide();
      $('.btn_play').show();
      return false;
    });
    
	  // Disable autoscrolling if the user clicks the prev or next button.
	  $('.btn_play').hide();

    $('.btn_pause').bind('click', function(){
      carousel.stopAuto();
      $('.btn_pause').hide();
      $('.btn_play').show();
    });

    $('.btn_play').bind('click', function(){
      carousel.startAuto();
      $('.btn_play').hide();
      $('.btn_pause').show();
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
      carousel.stopAuto();
    }, function() {
      if($('.btn_pause').is(":visible")) {carousel.startAuto();}
    });
  }
};

var Comments = {
  init: function(){
    var respond = $('#respond');
    if( respond.length ) { respond.find('#url').parent().hide(); }
  }
};

$(function(){
  window.PRIMEDIA = {};
  PRIMEDIA.WebTrendsTag = { // TODO: define this when webtrends is added  
    fire: function(){}, 
    getMetaAttr: function(){} 
  };
  ArticleCarousel.init();
  FavoriteArticles.init();
  AdHelper.populateDynamicValues();
  Comments.init();
  
  WH.init({ warehouseURL: 'http://wh.consumersource.com/wtd.gif' });
});

