
/* Omniture event tracking */
function fireOmnitureEvent(event) {
	var str = 'Element: ' + event.data.element_name,
		cmid = event.data.element_name;
		
	if (event.data.headline !== undefined) {
		str += ' Headline: ' + event.data.headline;
		var title = event.data.headline
	} else {
		var title = '';
	}
	
	if (event.data.tmi_type !== undefined) {
		str += ' TMI Type:' + event.data.tmi_type;
	}
	
	linkTrack(cmid,title);
}


/* The Home DL carousel */
var home_dl_switch, home_dl = function(){
	(function($){
	
	if( $('#home-dl').length > 0 ){
		
		/* Set animation options */
		var auto_time = 4200,  //4.2 seconds is ideal for auto-scrollers
			animation_duration = 800,
			animation_easing = 'easeOutQuart';			
		
		/* Set our references and scroll functions */
		var dl_scroller = $('#home-dl'),
			gall = dl_scroller.find('ul#home-dl-list');
		
		gall.append('<li class="home-dl-ad"><iframe id="home-dl-iframe" src="/ew/ad/iframe/0,,,00.html" width="320" height="300" frameborder="0" scrolling="no" allowtransparency="true"></iframe></li>');
			
		var dl_slides = gall.find("> li"),
			slide_count = dl_slides.length,
			dl_ad = $( document.getElementById('home-dl-iframe') ),
			dl_ad_up = false,
			new_html = '<div id="home-dl-nav">'+
				'<ul id="home-dl-numbers">'+
					'<li class="active"><a href="#">1</a></li>'+
					'<li><a href="#">2</a></li>'+
					'<li><a href="#">3</a></li>'+
					'<li><a href="#">4</a></li>'+
					'<li><a href="#">5</a></li>'+
					'<li><a href="#">6</a></li>'+
					'<li><a href="#">7</a></li>'+
				'</ul>'+
				'<ul id="home-dl-controls">'+
					'<li id="home-dl-prev"><a href="#" title="Previous">Previous</a></li>'+
					'<li id="home-dl-play"><a href="#" title="Play">Play</a></li>'+
					'<li id="home-dl-next"><a href="#" title="Next">Next</a></li>'+
				'</ul>'+
			'</div>';
		
		if( slide_count > 0 ){
			
			dl_scroller.append( new_html );
			
			/* Prepare DOM elements */
			dl_slides
				.filter(':first').css({'z-index':'3','display':'block'})
				.siblings('li').css({'opacity':'0','display':'block'});
			
			/* The slide switching magic */
			home_dl_switch = function( direction ){ //can be direction or index
				
				active_position = dl_slides.index(dl_slides.filter('.active:first'));
				
				if( !isNaN(direction) ){ //if this is a number
					if( active_position === direction ){
						return false;
					} else {
						new_index = direction;
					}
				} else {
					
					new_index = ( direction == 'previous' ) ? active_position-1 : active_position+1;
					
					if( new_index===slide_count ){
						new_index = 0;					
					} else if( (new_index===slide_count-1) || (new_index===slide_count-2) ) { //populate DL CM ad
						trigger_ad();
					} else if( new_index < 0 ){
						new_index = slide_count-1;
						trigger_ad();
					}
					
				}				
				
				new_slide = dl_slides.filter(':eq('+ new_index +')');
				new_slide.css({'opacity':'1.0','z-index':'2'});
				
				dl_slides.filter('.active').stop(true,true).animate(
						{ opacity:0 }, 400,
						function(){
							$(this).removeClass('active').css('z-index','1');
							new_slide.addClass('active').css('z-index','3');
						}
					);
				
				num_nav.find('> li:eq('+ new_index +')').addClass('active').siblings().removeClass('active');
				
			};
			
			var trigger_ad = function(){
				if( !dl_ad_up ){
					var dlAdSlideUrl = adFactory.getAd(320, 300)._getAdUrl();
					dl_ad.attr('src', '/ew/ad/iframe/0,,,00.html?url=' + encodeURIComponent( dlAdSlideUrl ));							
					dl_ad_up = true;
				}
				if( new_index===slide_count-1 ){
					num_nav.find('li.active').removeClass('active');
				}
			};
			
			var controller = dl_scroller.find('ul#home-dl-controls'),
				btn_next = controller.find('#home-dl-next'),
				btn_prev = controller.find('#home-dl-prev'),
				btn_play_pause = controller.find('#home-dl-play'),
				scroll_back = function(){					
					if( gall.is(':animated') ){ return false; }					
					home_dl_switch( 'previous' );					
				},
				scroll_forward = function(){
					if( gall.is(':animated') ){ return false; }
					home_dl_switch( 'next' );
				},
				play_pause = function(){
					if( btn_play_pause.hasClass('pause') ){  //auto-scrolling in progress
						clearInterval( auto_switch );
						btn_play_pause.removeClass('pause').find('a:first').attr('title','Play');
					} else {  //scroller is paused
						scroll_forward();
						auto_switch = setInterval( "home_dl_switch()", auto_time);
						btn_play_pause.addClass('pause').find('a:first').attr('title','Pause');
					}
				},
				num_nav = dl_scroller.find('ul#home-dl-numbers'),
				auto_switch;
			
			/* The auto scroller */
			if( !gall.hasClass('no-auto-play') ){
				btn_play_pause.addClass('pause').find('a:first').attr('title','Play');
				auto_switch = setInterval( "home_dl_switch()", auto_time);
			}
			
		}
		
		/* Linear nav*/
		controller.delegate('li', 'click', function(e) {
			e.preventDefault();
			
			var $this_id = $(this).attr("id");
				
			if( $this_id === 'home-dl-prev' ){
				btn_play_pause.removeClass('pause');
				clearInterval( auto_switch );
				scroll_back();
				linkTrack( 'DLNav' , 'DL_LEFT' );
			} else if( $this_id === 'home-dl-next' ){
				btn_play_pause.removeClass('pause');
				clearInterval( auto_switch );
				scroll_forward();
				linkTrack( 'DLNav' , 'DL_RIGHT' );
			} else if( $this_id === 'home-dl-play' ){
				play_pause();
				linkTrack( 'DLNav' , 'DL_PAUSE' );
			}
		});
		
		/* Non-linear nav */
		num_nav.delegate('li', 'click', function(e) {
			e.preventDefault();
			btn_play_pause.removeClass('pause');
			clearInterval( auto_switch );
			if( gall.is(':animated') ){ return false; }
			
			var next_item = num_nav.find('> li').index( $(this) ),
				lt_num = next_item + 1;
				
			home_dl_switch( next_item );
			linkTrack( 'DLNav' , 'DL_'+ lt_num );
		});
		
		/* Link tracker */
		gall.delegate('a', 'click', function(){
			
			var $this = $(this),
				li_index = dl_slides.index( $this.closest('li') );
			
			if( $this.find('img').length > 0 ){ //user clicked on the image
				linkTrack( 'P_DL'+ li_index , 'Photo' );
			} else { //user clicked on the title				
				var link_title = $this.text();				
				linkTrack( 'H_DL'+ li_index , link_title );			
			}
			
		});
		
	}
	})(jQuery);
};



























$(function() {
	$('body').append('<div id="debug" style="display:none;"></div>');
	$('#debug').append('<span>' + new Date().getTime() / 1000 + '</span>');
	$('#lnews').mouseover(function() {
		if ($(this).hasClass('omniture')) {
			
		}
		else
		{
		var i = 1;
		$('#lnewsCol1').find('div.lnewsBox').each(function() {
			var thing = 'DL' + i;
			title = $(this).find('h2 a').text();
			$(this).find('.lnImg a').bind("click", {element_name: 'P_' + thing, headline: title}, fireOmnitureEvent);
			$(this).find('.dek a').bind("click", {element_name: 'H_' + thing, headline: title}, fireOmnitureEvent);
			i++;
		});
		
		$('#debug').append('<br/><span>News:' + new Date().getTime() / 1000 + '</span>');
		var i = 1;
		$('#lnewsCtrlNums').find('a').each(function() {
			$(this).bind("click", {element_name: 'DL_NUM_' + i}, fireOmnitureEvent);
			i++;
		});
		var ctrlbtns = $('#lnewsCtrlBtns');
		ctrlbtns.find('.prev a').bind("click", {element_name: 'DL_LEFT'}, fireOmnitureEvent);
		ctrlbtns.find('.next a').bind("click", {element_name: 'DL_RIGHT'}, fireOmnitureEvent);
		ctrlbtns.find('.pause a').bind("click", {element_name: 'DL_PAUSE'}, fireOmnitureEvent);
		$(this).addClass('omniture');
		$('#debug').append('<br/><span>Controls:' + new Date().getTime() / 1000 + '</span>');
		}
	});
	//Latest Headlines
	i = 1;
	var latestHeadlines = $('.listLatestHeadlines');
	latestHeadlines.find('li a').each(function() {
		title = $(this).text();
		$(this).bind("click", {element_name: 'LH_' + i, headline: title}, fireOmnitureEvent);
		i++;
	});
	latestHeadlines.find('h3 a').bind("click", {element_name: 'LH_MORE'}, fireOmnitureEvent);
	
	$('#debug').append('<br/><span>Headlines 2:' + new Date().getTime() / 1000 + '</span>');
	
	
	
	
	
	
	
	
	//TV Watch Module
	//Tabs
	$('#tvTabMod').mouseover(function() {
		if ($(this).hasClass('omniture')) {
			
		}
		else {
			
		i = 1;
		var tab_defs = {1: 'TVRECAP', 2: 'TVTON', 3: 'TVHED'};
		var tvtab = $('#tvTabMod');
		tvtab.find('.tabCnt li a').each(function() {
			$(this).bind("click", {element_name: tab_defs[i] + '_TAB'}, fireOmnitureEvent);
			i++;
		});
		i = 1;
		var j = 1;
		tvtab.find('.toutCnt .contCnt').each(function() {
			//RECAP SPECIFIC
			if (i == 1) {
				$(this).find('h4 a').bind("click", {element_name: tab_defs[i] + '_H1', headline: $(this).find('h4 a').text()}, fireOmnitureEvent);
				$(this).find('.tout .imgcont a').bind("click", {element_name: tab_defs[i] + '_P', headline: $(this).find('h4 a').text()}, fireOmnitureEvent);
			
				j = 2;
				$(this).find('.tier_2 li a').each(function(){
					$(this).bind("click", {element_name: tab_defs[i] + '_H' + j, headline: $(this).text()}, fireOmnitureEvent);
					j++;
				});
				j = 1;
				$(this).find('.tier_3 li a').each(function(){
					if ($(this).parent().hasClass('last'))
					{
						$(this).bind("click", {element_name: tab_defs[i] + '_MORE'}, fireOmnitureEvent);
					}
					else
					{
						$(this).bind("click", {element_name: tab_defs[i] + '_S' + j, headline: $(this).text()}, fireOmnitureEvent);
					}
					j++;
				});
			}
			//Tonight
			if (i == 2)
			{
				$(this).find('h4 a').bind("click", {element_name: tab_defs[i] + '_S1', headline: $(this).find('h4 a').text()}, fireOmnitureEvent);
				$(this).find('.tout .imgcont a').bind("click", {element_name: tab_defs[i] + '_P', headline: $(this).find('h4 a').text()}, fireOmnitureEvent);
				j = 2;
				$(this).find('.tier_2 li a').each(function(){
					$(this).bind("click", {element_name: tab_defs[i] + '_S' + j, headline: $(this).text()}, fireOmnitureEvent);
					j++;
				});
				$(this).find('.tier_3 li a').each(function(){
					if ($(this).parent().hasClass('last'))
					{
						$(this).bind("click", {element_name: tab_defs[i] + '_FULL'}, fireOmnitureEvent);
					}
					else
					{
						$(this).bind("click", {element_name: tab_defs[i] + '_MORE'}, fireOmnitureEvent);
					}
					j++;
				});
			}
			//Headlines
			if (i == 3)
			{
				$(this).find('h4 a').bind("click", {element_name: tab_defs[i] + '_1', headline: $(this).find('h4 a').text()}, fireOmnitureEvent);
				$(this).find('.tout .imgcont a').bind("click", {element_name: tab_defs[i] + '_P', headline: $(this).find('h4 a').text()}, fireOmnitureEvent);
				j = 2;
				$(this).find('.tier_2 li a').each(function(){
					$(this).bind("click", {element_name: tab_defs[i] + '_' + j, headline: $(this).text()}, fireOmnitureEvent);
					j++;
				});
				$(this).find('.tier_3 li a').bind("click", {element_name: tab_defs[i] + '_MORE'}, fireOmnitureEvent);
			}
			i++;
		});
		//TV Watch is done!
		$(this).addClass('omniture');
		$('#debug').append('<br/><span>TV Tab:' + new Date().getTime() / 1000 + '</span>');
	
		}
	});
	$('#movieTabMod').mouseover(function() {
		if ($(this).hasClass('omniture')) {
			
		}
		else
		{
			
		//Movies
		i = 1;
		tab_defs = {1: 'MOVHED', 2: 'MOVREV', 3: 'MOVTRA'};
		var movietab = $('#movieTabMod');
		movietab.find('.tabCnt li a').each(function() {
			$(this).bind("click", {element_name: tab_defs[i] + '_TAB'}, fireOmnitureEvent);
			i++;
		});
		i = 1;
		j = 1;
		movietab.find('.toutCnt .contCnt').each(function() {
			//Headline SPECIFIC
			if (i == 1) {
				$(this).find('h4 a').bind("click", {element_name: tab_defs[i] + '_H1', headline: $(this).find('h4 a').text()}, fireOmnitureEvent);
				$(this).find('.tout .imgcont a').bind("click", {element_name: tab_defs[i] + '_P', headline: $(this).find('h4 a').text()}, fireOmnitureEvent);
			
				j = 2;
				$(this).find('.tier_2 li a').each(function(){
					$(this).bind("click", {element_name: tab_defs[i] + '_H' + j, headline: $(this).text()}, fireOmnitureEvent);
					j++;
				});
				j = 1;
				$(this).find('.tier_3 li a').each(function(){
					if ($(this).parent().hasClass('last'))
					{
						$(this).bind("click", {element_name: tab_defs[i] + '_MORE'}, fireOmnitureEvent);
					}
					else
					{
						$(this).bind("click", {element_name: tab_defs[i] + '_S' + j, headline_text: $(this).text()}, fireOmnitureEvent);
					}
					j++;
				});
			}
			//Reviews
			if (i == 2)
			{
				$(this).find('.tier_1 .movTitle a').bind("click", {element_name: tab_defs[i] + '_1', headline: $(this).find('.tier_1 .movTitle a').text()}, fireOmnitureEvent);
				$(this).find('.tout .imgcont a').bind("click", {element_name: tab_defs[i] + '_P', headline: $(this).find('.tier_1 .movTitle a').text()}, fireOmnitureEvent);
				j = 1;
				$(this).find('.tier_2 li a').each(function(){
					$(this).bind("click", {element_name: tab_defs[i] + '_' + j, headline: $(this).text()}, fireOmnitureEvent);
					j++;
				});
				$(this).find('.tier_3 li a').each(function(){
					if ($(this).parent().hasClass('last'))
					{
						$(this).bind("click", {element_name: tab_defs[i] + '_MORE'}, fireOmnitureEvent);
					}
					j++;
				});
			}
			//Trailers
			if (i == 3)
			{
				j = 1;
				$(this).find('.clips li a').each(function(){
					$(this).bind("click", {element_name: tab_defs[i] + '_' + j, headline: $(this).text()}, fireOmnitureEvent);
					j++;
				});
				$(this).find('.tier_3 li a').bind("click", {element_name: tab_defs[i] + '_MORE'}, fireOmnitureEvent);
			}
			i++;
		});
		//End Movies!
		$(this).addClass('omniture');
		$('#debug').append('<br/><span>Movie Tab:' + new Date().getTime() / 1000 + '</span>');
		}
	});
	
	//Music
	$('#musicTabMod').mouseover(function() {
		if ($(this).hasClass('omniture')) {
			
		}
		else
		{
		i = 1;
		tab_defs = {1: 'MUSHED', 2: 'MUSREV'};
		var musictab = $('#musicTabMod');
		musictab.find('.tabCnt li a').each(function() {
			$(this).bind("click", {element_name: tab_defs[i] + '_TAB'}, fireOmnitureEvent);
			i++;
		});
		i = 1;
		j = 1;
		musictab.find('.toutCnt .contCnt').each(function() {
			if (i == 1) {
				$(this).find('.artist a').bind("click", {element_name: tab_defs[i] + '_H1', headline: $(this).find('.artist a').text()}, fireOmnitureEvent);
				$(this).find('.tout .imgcont a').bind("click", {element_name: tab_defs[i] + '_P', headline: $(this).find('.artist a').text()}, fireOmnitureEvent);
			
				j = 2;
				$(this).find('.tier_2 li a').each(function(){
					$(this).bind("click", {element_name: tab_defs[i] + '_H' + j, headline: $(this).text()}, fireOmnitureEvent);
					j++;
				});
				j = 1;
				$(this).find('.tier_3 li a').each(function(){
					if ($(this).parent().hasClass('last'))
					{
						$(this).bind("click", {element_name: tab_defs[i] + '_MORE'}, fireOmnitureEvent);
					}
					else
					{
						$(this).bind("click", {element_name: tab_defs[i] + '_S' + j, headline_text: $(this).text()}, fireOmnitureEvent);
					}
					j++;
				});
			}
			//Reviews
			if (i == 2)
			{
				$(this).find('.tier_1 .txtcont a').bind("click", {element_name: tab_defs[i] + '_1', headline: $(this).find('.tier_1 .txtcont a').text()}, fireOmnitureEvent);
				$(this).find('.tout .imgcont a').bind("click", {element_name: tab_defs[i] + '_P', headline: $(this).find('.tier_1 .txtcont a').text()}, fireOmnitureEvent);
				j = 1;
				$(this).find('.tier_2 li a').each(function(){
					$(this).bind("click", {element_name: tab_defs[i] + '_' + j, headline: $(this).text()}, fireOmnitureEvent);
					j++;
				});
				$(this).find('.tier_3 li a').each(function(){
					if ($(this).parent().hasClass('last'))
					{
						$(this).bind("click", {element_name: tab_defs[i] + '_MORE'}, fireOmnitureEvent);
					}
					j++;
				});
			}
			i++;
		});
		$(this).addClass('omniture');
		$('#debug').append('<br/><span>Music Tab:' + new Date().getTime() / 1000 + '</span>');
		//End Music!
	
		}
	});	
	//Picks
	$('#homePick').mouseover(function() {
		if ($(this).hasClass('omniture')) {
			
		}
		else
		{
		var homepicks = $('#homePick');
		$('#picksLink').bind("click", {element_name: 'PICKS_L', headline: homepicks.find('.txtcont h4 a').text()}, fireOmnitureEvent);
		homepicks.find('div.imgcont a').bind("click", {element_name: 'PICKS_P', headline: homepicks.find('.txtcont h4 a').text()}, fireOmnitureEvent);
		homepicks.find('div.txtcont h4 a').bind("click", {element_name: 'PICKS_H', headline: homepicks.find('.txtcont h4 a').text()}, fireOmnitureEvent);
		homepicks.find('a.more').bind("click", {element_name: 'PICKS_MORE', headline: homepicks.find('.txtcont h4 a').text()}, fireOmnitureEvent);
		$(this).addClass('omniture');
		$('#debug').append('<br/><span>Picks:' + new Date().getTime() / 1000 + '</span>');
	
		}
	});
	//Lists & Games - reader favorites etc...
	$('div.listsgames').mouseover(function() {
		if ($(this).hasClass('omniture'))
		{
			
		}
		else
		{
		i = 1;
		var lists_def = {1: 'RFAVE', 2: 'PGALL', 3: 'TECH', 4: 'PSTYLE'};		  
		var listsgames = $('div.listsgames');
		listsgames.find('div.tout').each(function() {
			$(this).find('div.imgcont a').bind("click", {element_name: lists_def[i] + '_P'}, fireOmnitureEvent);
			j = 1; 
			$(this).find('div.txtcont a').each(function() {
				$(this).bind("click", {element_name: lists_def[i] + '_' + j, headline: $(this).text()}, fireOmnitureEvent);
				j++;
			});
			i++;
		});
		$(this).addClass('omniture');
		$('#debug').append('<br/><span>Lists:' + new Date().getTime() / 1000 + '</span>');
	
			
		}
	});
	$('#blogs').mouseover(function() {
		if ($(this).hasClass('omniture'))
		{
			
		}
		else
		{
				
		i = 1;
		j = 1;
		//Blogs
		var headline_text = '';
		$('#blogs').find('li.blogItem a').each(function() {
			if ($(this).parent().hasClass('blogHeader'))
			{
				header_text = $(this).text();
				$(this).bind("click", {element_name: 'N_BLOG' + j, headline: header_text}, fireOmnitureEvent)
			}
			if ($(this).parent().hasClass('blogTitle'))
			{
				headline_text = $(this).text();
				$(this).bind("click", {element_name: 'H_BLOG' + j, headline: headline_text}, fireOmnitureEvent);
			}
			if ($(this).parent().hasClass('blogDeck'))
			{
				$(this).bind("click", {element_name: 'M_BLOG' + j, headline: headline_text}, fireOmnitureEvent);
				i++;
				j++;
			}
			
		});
		$('.fromOurBlogs').find('a').bind("click", {element_name: 'BLOG_FULL'}, fireOmnitureEvent);
		$('.centercol .more:eq(2)').bind("click", {element_name: 'BLOG_MORE'}, fireOmnitureEvent);
		$(this).addClass('omniture');		
		$('#debug').append('<br/><span>Blogs:' + new Date().getTime() / 1000 + '</span>');
		}
	});
	
/**** start old ****/
	//Top Navigation
	$('#nav-features').mouseover(function() {
		if ($(this).hasClass('omniture'))
		{
			
		}
		else {
			
		i = 1;
		$('#nav-features').children().each(function() {
			j = 1;
			$(this).find('a:first').bind("click", {element_name: 'NAV' + i}, fireOmnitureEvent);
			$(this).find('ul li a').each(function() {
				$(this).bind("click", {element_name: 'NAV' + i + '_SUB' + j, headline: $(this).text()}, fireOmnitureEvent);
				j++;	
			});
			i++;
		});
		$('input#btn_search').bind("click", {element_name: 'SEARCH', headline: $('#searchbox').val()}, fireOmnitureEvent);
		var searchbox = $('input#searchbox');
		searchbox.blur(function() {
			$(this).unbind("click");
			$(this).bind("click", {element_name: 'SEARCH', headline: $('#searchbox').val()}, fireOmnitureEvent);
		});
		$('#debug').append('<br/><span>Nav:' + new Date().getTime() / 1000 + '</span>');
		$(this).addClass('omniture');
		}
	});	
	$('#header2').mouseover(function() {
		if ($(this).hasClass('omniture')) {
			
		}
		else {
			
		i = 1;
		$('div#header2 .siteLinks a').each(function() {
			$(this).bind("click", {element_name: 'SCT_' + i, headline: $(this).text()}, fireOmnitureEvent);	
			i++;
		});
		i = 1;
		$(this).addClass('omniture');
			$('#debug').append('<br/><span>Header2:' + new Date().getTime() / 1000 + '</span>');
		}
	});
	$('#ew-footerNav').mouseover(function() {
		if ($(this).hasClass('omniture')) {
			
		}
		else
		{
			
		$('div#ew-footerNav a').each(function() {
			$(this).bind("click", {element_name: 'SCB_' + i, headline: $(this).text()}, fireOmnitureEvent);	
			i++;
		});
			$(this).addClass('omniture');
		}
	});
/**** end old ****/

/**** start new ****/
	//NEW Top Navigation
	$('#header').mouseover(function() {
		if ($(this).hasClass('omniture'))
		{
			
		}
		else {
			
		i = 1;
		$('#header #menu').children().each(function() {
			j = 1;
			$(this).find('a:first').bind("click", {element_name: 'NAV' + i}, fireOmnitureEvent);
			i++;
		});
		$('input.searchButton').bind("click", {element_name: 'SEARCH', headline: $('#searchbox').val()}, fireOmnitureEvent);
		var searchbox = $('input#searchbox');
		searchbox.blur(function() {
			$(this).unbind("click");
			$(this).bind("click", {element_name: 'SEARCH', headline: $('#searchbox').val()}, fireOmnitureEvent);
		});
		$('#debug').append('<br/><span>Nav:' + new Date().getTime() / 1000 + '</span>');
		$(this).addClass('omniture');
		}
	});
	$('#blogListWrapper').mouseover(function() {
		if ($(this).hasClass('omniture')) {
			
		}
		else {
			
		i = 1;
		$('#blogListWrapper a').each(function() {
			$(this).bind("click", {element_name: 'BLOG_' + i, headline: $(this).text()}, fireOmnitureEvent);	
			i++;
		});
		i = 1;
		$(this).addClass('omniture');
			$('#debug').append('<br/><span>Header2:' + new Date().getTime() / 1000 + '</span>');
		}
	});
	$('#siteLinks').mouseover(function() {
		if ($(this).hasClass('omniture')) {
			
		}
		else {
			
		i = 1;
		$('#siteLinks a').each(function() {
			$(this).bind("click", {element_name: 'SCT_' + i, headline: $(this).text()}, fireOmnitureEvent);	
			i++;
		});
		i = 1;
		$(this).addClass('omniture');
			$('#debug').append('<br/><span>Header2:' + new Date().getTime() / 1000 + '</span>');
		}
	});
	$('#moreFromEW').mouseover(function() {
		if ($(this).hasClass('omniture')) {
			
		}
		else {
			
		i = 1;
		$('#moreFromEW a').each(function() {
			$(this).bind("click", {element_name: 'MFE_' + i, headline: $(this).text()}, fireOmnitureEvent);	
			i++;
		});
		i = 1;
		$(this).addClass('omniture');
			$('#debug').append('<br/><span>Header2:' + new Date().getTime() / 1000 + '</span>');
		}
	});
	$('#footerLinks').mouseover(function() {
		if ($(this).hasClass('omniture')) {
			
		}
		else {
			
		i = 1;
		$('#footerLinks a').each(function() {
			$(this).bind("click", {element_name: 'SCB_' + i, headline: $(this).text()}, fireOmnitureEvent);	
			i++;
		});
		i = 1;
		$(this).addClass('omniture');
			$('#debug').append('<br/><span>Header2:' + new Date().getTime() / 1000 + '</span>');
		}
	});
/**** end new ****/



	$('#centercolPartnerBar').mouseover(function() {
		if ($(this).hasClass('omniture')) {
		
		}
		else {
		
			i = 1;
			var domain = '';
			$('#centercolPartnerBar').find('div.pb-partner-cell').each(function(){
				url_array = $(this).find('a.pb-partner-logo').attr('href').split('/');
				domain = url_array[2].replace('.com', '').replace('www.', '');
				$(this).find('a.pb-partner-logo').bind("click", {
					element_name: 'FOP' + i + '_L',
					headline: domain + ' logo'
				}, fireOmnitureEvent);
				j = 1;
				$(this).find('a.pb-title').each(function(){
					$(this).bind('click', {
						element_name: 'FOP' + i + '_H' + j,
						headline: $(this).text()
					}, fireOmnitureEvent);
					j++;
				});
				$(this).find('a.pb-partner-moreLink').bind("click", {
					element_name: 'FOP' + i + '_MORE'
				}, fireOmnitureEvent);
				i++;
			});
			$(this).addClass('omniture');
			$('#debug').append('<br/><span>Partners:' + new Date().getTime() / 1000 + '</span>');
		}
	});
	$('#ways2get').mouseover(function() {
		if ($(this).hasClass('omniture')) {
			
		}
		else
		{	
		i = 1;
		$('#ways2get li a').each(function() {
			$(this).bind('click', {element_name: 'WAYS_' + $(this).text().substr(0,1).toUpperCase(), headline: $(this).text()}, fireOmnitureEvent);
		});
		$(this).addClass('omniture');
		$('#debug').append('<br/><span>Ways 2 Get:' + new Date().getTime() / 1000 + '</span>');
		
		}
	});	
	$('#featuredVideo').mouseover(function() {
		if ($(this).hasClass('omniture'))
		{
			
		}
		else
		{
		var fv = $('#featuredVideo');
		var featured_vid_title = $('#featuredVideo').find('.tout .txtcont a').text();
		fv.find('div.imgcont a').bind("click", {element_name: 'VID_P', headline: featured_vid_title}, fireOmnitureEvent);
		fv.find('div.txtcont a').bind("click", {element_name: 'VID_1', headline: featured_vid_title}, fireOmnitureEvent);
		fv.find('div.txtcont span.more a').bind("click", {element_name: 'VID_MORE', headline: featured_vid_title}, fireOmnitureEvent);
		i = 2;
		fv.find('ul.otherFeaturedVideos li a').each(function() {
			$(this).bind("click", {element_name: 'VID_' + i, headline: $(this).text()}, fireOmnitureEvent);
			i++;
		});
		i = 1;
		$(this).addClass('omniture');
		$('#debug').append('<br/><span>Featured Videos:' + new Date().getTime() / 1000 + '</span>');
	
				
		}
	});
	$('div.tmostpopH').mouseover(function() {
		if ($(this).hasClass('omniture')) {
			
		}
		else
		{
		$('div.tmostpopH').find('li').each(function() {
			var headline = $(this).find('div.txtcont a').text();
			$(this).find('div.imgcont a').bind("click", {element_name: 'TMPOP' + i + '_IMG', headline: headline}, fireOmnitureEvent);
			$(this).find('div.txtcont a').bind("click", {element_name: 'TMPOP' + i + '_TXT', headline: headline}, fireOmnitureEvent);
			i++;
		});
		$(this).addClass('omniture');
	$('#debug').append('<br/><span>Most Popular 2:' + new Date().getTime() / 1000 + '</span>');
		}
	});
	$('div.currentmag').mouseover(function() {
		if ($(this).hasClass('omniture'))
		{
			
		}
		else
		{
				var currentmag = $('div.currentmag');
		currentmag.find('h3 a').bind("click", {element_name: 'TWI_L', headline: 'This Week\'s Issue'}, fireOmnitureEvent);
		currentmag.find('div.imgcont a').bind("click", {element_name: 'TWI_P', headline: 'This Week\'s Issue Photo'}, fireOmnitureEvent);
		i = 1;
	
		currentmag.find('ul a').each(function() {
			if (i == 4) {
				$(this).bind("click", {element_name: 'TWI_SUB', headline: $(this).text()}, fireOmnitureEvent);
			}
			else if (i == 5) {
				$(this).bind("click", {element_name: 'TWI_BUY', headline: $(this).text()}, fireOmnitureEvent);
			}
			else {
				$(this).bind("click", {element_name: 'TWI_H' + i, headline: $(this).text()}, fireOmnitureEvent);
			}
			i++;
		});
		$(this).addClass('omniture');
		$('#debug').append('<br/><span>Current Mag 2:' + new Date().getTime() / 1000 + '</span>');
		}
	});
	
	/*
	*/
	$('#debug').append('<br/><span>' + new Date().getTime() / 1000 + '</span>');
});

/*
	
*/ 
	function is_all_ws( nod )
	{
	  // Use ECMA-262 Edition 3 String and RegExp features
	  return !(/[^\t\n\r ]/.test(nod.data));
	}
	
	function is_ignorable( nod )
	{
	  return ( nod.nodeType == 8) || // A comment node
	         ( (nod.nodeType == 3) && is_all_ws(nod) ); // a text node, all ws
	}
	
	
	function node_before( sib )
	{
	  while ((sib = sib.previousSibling)) {
	    if (!is_ignorable(sib)) return sib;
	  }
	  return null;
	}
	
	
	function node_after( sib )
	{
	  while ((sib = sib.nextSibling)) {
	    if (!is_ignorable(sib)) return sib;
	  }
	  return null;
	}
	
	
	function last_child( par )
	{
	  var res=par.lastChild;
	  while (res) {
	    if (!is_ignorable(res)) return res;
	    res = res.previousSibling;
	  }
	  return null;
	}
	
	
	function first_child( par )
	{
	  var res=par.firstChild;
	  while (res) {
	    if (!is_ignorable(res)) return res;
	    res = res.nextSibling;
	  }
	  return null;
	}
	
	function child_nodes( elem ) {
    var childs = [];
    var elemLength = elem.childNodes.length;
    for( var i = 0; i < elemLength; i++ ) {
        if( is_ignorable( elem.childNodes[ i ] ) ) {
            continue;
        } else {
            childs.push( elem.childNodes[ i ] );
        }
    }
    return childs;
}

	
	function data_of( txt )
	{
	  var data = txt.data;
	  // Use ECMA-262 Edition 3 String and RegExp features
	  data = data.replace(/[tnr ]+/g, " ");
	  if (data.charAt(0) == " ")
	    data = data.substring(1, data.length);
	  if (data.charAt(data.length - 1) == " ")
	    data = data.substring(0, data.length - 1);
	  return data;
	}
	
	function addEvent(elem, evtType, func, capture) {
	   capture = (capture) ? capture : false;
	   if (elem.addEventListener) {
	      elem.addEventListener(evtType, func, capture);
	   } else if (elem.attachEvent) {
	      elem.attachEvent("on" + evtType, func);
	   } else {
	      // for IE/Mac, NN4, and older
	      elem["on" + evtType] = func;
	   }
	}
	
	function removeEvent(elem, evtType, func, capture) {
	   capture = (capture) ? capture : false;
	   if (elem.removeEventListener) {
	      elem.removeEventListener(evtType, func, capture);
	   } else if (elem.attachEvent) {
	      elem.detachEvent("on" + evtType, func);
	   } else {
	      // for IE/Mac, NN4, and older
	      elem["on" + evtType] = null;
	   }
	}
	
	function addOnLoadEvent(func) {
	    if (window.addEventListener || window.attachEvent) {
	        addEvent(window,"load", func, false);
	    } else {
	        var oldQueue = (window.onload) ? window.onload : function( ) {};
	        window.onload = function( ) {
	            oldQueue( );
	            func( );
	        }
	    }
	}
	
	function changeTabs() {
		var MODROOT = document.getElementById("what2cntID");		
		var MODROOT2 = document.getElementById("what2cntID2");		
		
		function init() {
			for(var i = 0; i < getTabMods().length; i++) {
				doTabs(getTabMods()[i]);
			}
		}
		
		
		function getTabMods() {
			var tabMods = new Array();
			
			var allDivs = MODROOT.getElementsByTagName("div");
			var allDivsLen = allDivs.length;
			for(var i = 0; i < allDivsLen; i++) {
				if(allDivs[i].className.toUpperCase() == "TABMOD") {
					tabMods.push(allDivs[i]);
				}
			}
			
			var allDivs = MODROOT2.getElementsByTagName("div");
			var allDivsLen = allDivs.length;
			for(var i = 0; i < allDivsLen; i++) {
				if(allDivs[i].className.toUpperCase() == "TABMOD") {
					tabMods.push(allDivs[i]);
				}
			}

			return(tabMods);
		}
		
	
		function doTabs(param) {
			var tabs = new Array();
			var allLIs = param.getElementsByTagName("li");
			var allLIsLen = allLIs.length;
			for(var i = 0; i < allLIsLen; i++) {
				if(allLIs[i].parentNode.className.toUpperCase() == "TABCNT") {
					tabs.push(allLIs[i]);
				}
			}
			
			var touts = new Array();
			var allDivs = param.getElementsByTagName("div");
			var allDivsLen = allDivs.length;
			for(var i = 0; i < allDivsLen; i++) {
				if(allDivs[i].parentNode.className.toUpperCase() == "TOUTCNT") {
					touts.push(allDivs[i]);
				}
			}
			
			for(var i = 0; i < tabs.length; i++) {
				first_child(tabs[i]).cur = i;
				addEvent(first_child(tabs[i]), "click", tabOn, false)
				addEvent(first_child(tabs[i]), "click", toutOn, false)
			}
			
			function tabOn(evt) {
				var evt = (evt) ? evt : window.event;
				var elem = (evt.target) ? evt.target : evt.srcElement;
				for(var i = 0; i < elem.parentNode.parentNode.childNodes.length; i++) {
					if(elem.parentNode.parentNode.childNodes[i].nodeType == 1) {
						tabOff(elem.parentNode.parentNode.childNodes[i]);
					}
				}
				if(elem.parentNode.className.indexOf("first") == -1) {
					elem.parentNode.className = "on";
				} else {
					elem.parentNode.className = "first on";
				}
			}
			
			function tabOff(elem) {
				if(elem.className.indexOf("first") == -1) {
					elem.className = "";
				} else {
					elem.className = "first";
				}
			}
			
			function toutOn(evt) {
				var evt = (evt) ? evt : window.event;
				var elem = (evt.target) ? evt.target : evt.srcElement;
				for(var i = 0; i < touts.length; i++) {
					touts[i].className = "contCnt";
				}
				touts[elem.cur].className = "contCnt on";
			}
			
		}
		
		init();
		
	}

function TabConfig(tabModID, configTimes){

	var DAYS = {
		"MON": 1,
		"TUE": 2,
		"WED": 3,
		"THU": 4,
		"FRI": 5,
		"SAT": 6,
		"SUN": 0,
		"MONDAY": 1,
		"TUESDAY": 2,
		"WEDNESDAY": 3,
		"THURSDAY": 4,
		"FRIDAY": 5,
		"SATURDAY": 6,
		"SUNDAY": 0
	};
	
	this.today = new Date();
	this.curDay = this.today.getDay();
	this.curHour = this.today.getHours();
	this.curMin = this.today.getMinutes();
	ct = ((this.curDay * 24) * 60) + (this.curHour * 60) + (this.curMin);
	$(configTimes.times).each(function(i, time){
		start_day = DAYS[time.startDay.toUpperCase()];
		var start_day_time = (start_day * 24 * 60);
		start_time = (start_day * 24 * 60) + configDateToTime(time.startTime);
		var end_day = DAYS[time.endDay.toUpperCase()];
		end_time = (end_day * 24 * 60) + configDateToTime(time.endTime);
		if ((ct > start_time) && (ct < end_time)) {
			var activeTab = time.activeTab - 1;
			$('#' + tabModID + ' .tabCnt .on').removeClass('on');
			$('#' + tabModID + ' .tabCnt li').eq(activeTab).addClass('on');
			$('#' + tabModID + ' .toutCnt .contCnt.on').removeClass('on');
			$('#' + tabModID + ' .contCnt').eq(activeTab).addClass('on');
		}
	});
}
function configDateToTime(date){
	if (date.split(":")[1].split(" ")[1].toUpperCase() === "PM") {
		if (date.split(":")[0] !== "12") {
			var sh = parseInt(date.split(":")[0]) + 12;
		}
		else {
			var sh = 12;
		}
	}
	else {
		if (date.split(":")[0] === "12") {
			var sh = 0;
		}
		else {
			var sh = parseInt(date.split(":")[0]);
		}
	}
	var sm = parseInt(date.split(":")[1]);
	nice_time = (sh * 60) + sm;
	return nice_time;
}

/**
 * This is the configuration file for the default tab JavaScript.
 */

function initTabs() {
	
var tvConfig = {
	masterDefault : "" ,
	times : [
		{
			startDay :   "mon" ,
			startTime :  "12:00 AM" ,
			endDay :     "mon" ,
			endTime :    "1:59 PM" ,
			activeTab :  "1"
		} ,
		{
			startDay :   "mon" ,
			startTime :  "2:00 PM" ,
			endDay :     "mon" ,
			endTime :    "11:59 PM" ,
			activeTab :  "2"
		} ,
		{
			startDay :   "tue" ,
			startTime :  "12:00 AM" ,
			endDay :     "tue" ,
			endTime :    "1:59 PM" ,
			activeTab :  "1"
		} ,
		{
			startDay :   "tue" ,
			startTime :  "2:00 PM" ,
			endDay :     "tue" ,
			endTime :    "11:59 PM" ,
			activeTab :  "2"
		} ,
		{
			startDay :   "wed" ,
			startTime :  "12:00 AM" ,
			endDay :     "wed" ,
			endTime :    "1:59 PM" ,
			activeTab :  "1"
		} ,
		{
			startDay :   "wed" ,
			startTime :  "2:00 PM" ,
			endDay :     "wed" ,
			endTime :    "11:59 PM" ,
			activeTab :  "2"
		} ,
		{
			startDay :   "thu" ,
			startTime :  "12:00 AM" ,
			endDay :     "thu" ,
			endTime :    "1:59 PM" ,
			activeTab :  "1"
		} ,
		{
			startDay :   "thu" ,
			startTime :  "2:00 PM" ,
			endDay :     "thu" ,
			endTime :    "11:59 PM" ,
			activeTab :  "2"
		} ,
		{
			startDay :   "fri" ,
			startTime :  "12:00 AM" ,
			endDay :     "fri" ,
			endTime :    "1:59 PM" ,
			activeTab :  "1"
		} ,
		{
			startDay :   "fri" ,
			startTime :  "2:00 PM" ,
			endDay :     "fri" ,
			endTime :    "11:59 PM" ,
			activeTab :  "2"
		} ,
		{
			startDay :   "sat" ,
			startTime :  "12:00 AM" ,
			endDay :     "sun" ,
			endTime :    "11:59 PM" ,
			activeTab :  "3"
		} 
	]
};

var movieConfig = {
	masterDefault : "" ,
	times : [
		{
			startDay :   "mon" ,
			startTime :  "12:00 AM" ,
			endDay :     "wed" ,
			endTime :    "11:59 PM" ,
			activeTab :  "1"
		} ,
		{
			startDay :   "thu" ,
			startTime :  "12:00 AM" ,
			endDay :     "sun" ,
			endTime :    "11:59 PM" ,
			activeTab :  "2"
		} 
	]
};
	TabConfig( "tvTabMod" , tvConfig );
	TabConfig( "movieTabMod" , movieConfig );
	
}


var move_tv_watch = false, column_fix = function(){ //moving columns with doc.written ads, fun
(function($){
		
	if( $('body:first').hasClass('sliders') ){
		
		var the_content = $( document.getElementById('content') ),
			left_btm = the_content.find('> .leftcol.bottom:first'),
			center_btm = the_content.find('> .centercol.bottom:first'),
			right_btm = the_content.find('> .rightcol.bottom:first');
		
		if( left_btm.find('div.ad170x30:first').length > 0 ) {
			
			var left_btm_shell = left_btm.children().not('div#what2cntID2'), //select all children minus the ad container
				left_btm_no_ad = left_btm.find('> div#what2cntID2').children().not('.ad170x30'),  //since the <script> tags cannot be moved we have to keep the block here
				left_btm_ad_cont = $('<div id="what2cntID2-new" class="clear what2cnt"><div class="ad170x30 ad clear"></div></div>').append( left_btm_no_ad ), //building the new ad container
				left_btm_ad = left_btm.find('div#what2cntID2 .ad170x30').children().not('script'); //grabbing the ad DOM elements
			
			/* Append ad in memory */
			left_btm_ad_cont.find('.ad170x30:first').append( left_btm_ad );
			
			/* Now we append to the page */
			the_content.find('> .leftcol.top:first')
				.append( left_btm_shell )
				.find('.listsgames:first').after( left_btm_ad_cont );
				
			left_btm.hide();
		}		
		
		the_content.find('> .centercol.top:first').append( center_btm.children() );
		center_btm.hide();
		
		if( right_btm.find('div#ad336x70').length > 0 ) {
			
			var right_btm_shell = right_btm.children().not('div.tmostpopH:first, div#home_right, div#gsl_home_right'), //select all children minus the ad containers
				right_btm_no_ad = right_btm.find('> div.tmostpopH:first').attr('class','tmostpopH-old').children().not('#ad336x70'),  //since the <script> tags cannot be moved we have to keep the block here, but we're going to change the class
				right_btm_ad_cont = $('<div class="tmostpopH omniture"><div id="ad336x70-new" class="ad336x70"></div></div>').prepend( right_btm_no_ad ), //building the new ad container
				right_btm_ad = right_btm.find('div#ad336x70').children().not('script'), //grabbing the ad DOM elements
				right_btm_googads_cont = $('<div id="gsl_home_right-new" class="googlead"></div>'),
				right_btm_googads = right_btm.find('#gsl_home_right div.googlead').children().not('script');
			
			/* Append ads in memory */
			right_btm_ad_cont.find('div#ad336x70-new').append( right_btm_ad );
			right_btm_googads_cont.append( right_btm_googads );
			
			/* Now we append to the page */
			the_content.find('> .rightcol.top:first')
				.append( right_btm_shell )
				.append( right_btm_googads_cont )
				.find('#twitterModule').before( right_btm_ad_cont );
			
			right_btm.hide();
		}
		
	} else if( move_tv_watch ){
		
		var tv_watch_old = $( document.getElementById('what2cntID') ),
			tv_watch_no_ad = tv_watch_old.children().not('div.ad170x30'),
			tv_watch_new = $( '<div id="what2cntID-new" class="clear what2cnt"><div class="ad170x30 ad clear"></div></div>' ).prepend( tv_watch_no_ad ),
			tv_watch_ad = tv_watch_old.find('div.ad170x30').children().not('script');
		
		tv_watch_new.find( '.ad170x30' ).first().prepend( tv_watch_ad );		
		tv_watch_new.find( 'ul.tier_2' ).append( '<li><a href="http://tvrecaps.ew.com/viewer/"><span style="color:red">NEW!</span> Watch your favorite shows with EW writers and your friends - live or on your DVR!</a></li>' );
		tv_watch_new.css('display','none');
		
		$( document.getElementById('content') ).find( 'div.rightcol.top' ).first().append( tv_watch_new );
		
		tv_watch_new.find('div.toutCnt').css('height','280px');
		tv_watch_new
			.fadeIn('slow')
			.lazyloader();
			
		tv_watch_old.hide();
	}
	
	
})(jQuery);
};






jQuery(document).ready(function($) {
	
	home_dl();	
	initTabs();
	changeTabs();
	
	column_fix();
	
	$('ul#home-dl-list').lazyloader('> li > div.home-dl-image img');
	$('#homePick').lazyloader()
		.siblings('div.listsgames:first').lazyloader();
	$('#what2cntID, #what2cntID2, #what2cntID2-new').lazyloader();
	$('#featuredVideo').lazyloader()
		.siblings('.currentmag:first').lazyloader();	
	
	$('#ways2get').appear(function(){
		$(this).addClass('css-lazyload');
	});
	
	$('#centercolPartnerBar').appear(function(){
		$(this).addClass('css-lazyload');
	});
	
	
});
