
jQuery(function($) {
	initializePartnerBar();
	initTabs();
	changeTabs();
	initSSComponents();
});
/********************* Begin slideshow module *********************/
var delay = 5000; //in milliseconds
var ssTouts = new Array(), ssNums = new Array(), ssBtns = new Array();
var ppBtn, timer1;
var currPic = 1;
var totalPic = 7;
var dlAdSlideUrl = adFactory.getAd(320, 300)._getAdUrl(); 
/* Load all components on the page */
function initSSComponents() {
	totalPic = jQuery('#lnewsCtrlNums ul li :visible').length + 1; 
	currPic = jQuery('#lnewsCtrlNums ul li .on').text(); 
	jQuery('#lnewsCtrlNums a:eq(0)').addClass('on').click();
	var i = 0;
	jQuery('#lnewsCtrlNums a').each(function() {
		jQuery(this).bind("click", function() {
			jQuery('#lnewsCtrlNums .on').removeClass('on');
			jQuery(this).addClass('on');
			doNumber(jQuery(this).text() - 1);
			return false;
		});
		i++;
	});
	jQuery('.ctrl .prev a').bind("click", function() {
		doNumber((currPic + totalPic -1)%totalPic);
		return false;
	});
	jQuery('#ppBtn a').bind("click", function() {
		doButton();
		return false;
	});
	jQuery('.ctrl .next a').bind("click", function() {
		doNumber((currPic+1)%totalPic);
		return false;
	});
	timer1 = setTimeout('timedStory()', delay);
}

/* change picture, wait some seconds, repeat */
function timedStory() {
	currPic = jQuery('#lnewsCtrlNums ul li .on').text(); 
	if (currPic == totalPic)
	{
		currPic = 0;
	}
	change2(currPic);
	timer1 = setTimeout('timedStory()', delay);
}

/* executed when the play/pause button is selected */
function doButton() {
	if (jQuery('#ppBtn').hasClass('pause')) {
		clearTimeout(timer1); /* stop the image loop */
		jQuery('#ppBtn').removeClass('pause').addClass('play');
	}
	else
	{
		jQuery('#ppBtn').removeClass('play').addClass('pause');
		timer1 = setTimeout('timedStory', delay);
	}
}

/*executed when a number link is selected */
function doNumber(num) {
	
	if (jQuery('#ppBtn').hasClass('pause')) {
		jQuery('#ppBtn').removeClass('pause').addClass('play');
		clearTimeout(timer1); /* stop the image loop */
	}
	change2(num);
	clearTimeout(timer1);
} 

/* update the play/pause button image */


function change2(num)
{
	 currPic = num;
/* Change the currently displayed tout */
		var i = 0;
		jQuery('.lnewsBox').each(function() {
			if (i == currPic) {
				jQuery(this).show().addClass('on');
				if (jQuery(this).find('img').attr('src') == '')
				{
					jQuery(this).find('img').attr('src', jQuery(this).find('img').attr('rel'));
				}
			}
			else
			{
				if (jQuery(this).is(':visible'))
				{
					jQuery(this).hide().removeClass('on');
				}
			}
			i++;
		});
		jQuery('#ctrlID .on').removeClass('on');
		jQuery('#ctrlID a:eq(' + num + ')').addClass('on');
		if ( currPic == (totalPic - 1) ) {
			var dlAdSlideUrl = adFactory.getAd(320, 300)._getAdUrl(); 
			if (jQuery('#dlAdSlide').is(':visible')) {
				if (jQuery('#dlAdSlide').attr('src') =='/ew/ad/iframe/0,,,00.html')
				 {
					jQuery('#dlAdSlide').attr('src', '/ew/ad/iframe/0,,,00.html?url=' + encodeURIComponent( dlAdSlideUrl ));
				} 
			} 
		}
}
jQuery("#lnewsAfter").bind("load", function () {
	initSSComponents();
});
/********************* End slideshow module *********************/

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

/**
 *
 * Foo.
 *
 * @object	TabConfig	Foo.
 * @param	tabModID	Foo.
 * @param	config		Foo.
 *
 */

function TabConfig( tabModID , configTimes ) {

	this.modID = document.getElementById( tabModID );
	this.cTimes = configTimes.times;
	this.mDefault = configTimes.masterDefault;	
	this.tCounter = 0;
	this.today = new Date();
    this.curDay = this.today.getDay();
    this.curHour = this.today.getHours();
    this.curMin = this.today.getMinutes();
    this.ct = ( ( this.curDay * 24 ) * 60 ) + ( this.curHour * 60 ) + ( this.curMin );
    
    var self = this;
	
	if( this.mDefault !== "" ) {
    	this.setTab( this.mDefault );
	} else {
		this.compTimes( this.cTimes[ this.tCounter ] );
	}
}


TabConfig.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
};


TabConfig.prototype.compTimes = function( tObj ) {
	var sd = TabConfig.DAYS[ tObj.startDay.toUpperCase() ];
	
	if( tObj.startTime.split( ":" )[ 1 ].split( " " )[ 1 ].toUpperCase() === "PM" ) {
		if( tObj.startTime.split( ":" )[ 0 ] !== "12" ) {
			var sh = parseInt( tObj.startTime.split( ":" )[ 0 ] ) + 12;
		} else {
			var sh = 12;
		}
	} else {
		if( tObj.startTime.split( ":" )[ 0 ] === "12" ) {
			var sh = 0;
		} else {
			var sh = parseInt( tObj.startTime.split( ":" )[ 0 ] );
		}
	}
	
	var sm = parseInt( tObj.startTime.split( ":" )[ 1 ].split( " " )[ 0 ] );
	var ed = TabConfig.DAYS[ tObj.endDay.toUpperCase() ];
	
	if( tObj.endTime.split( ":" )[ 1 ].split( " " )[ 1 ].toUpperCase() === "PM" ) {
		if( tObj.endTime.split( ":" )[ 0 ] !== "12" ) {
			var eh = parseInt( tObj.endTime.split( ":" )[ 0 ] ) + 12;
		} else {
			var eh = 12;
		}
	} else {
		if( tObj.endTime.split( ":" )[ 0 ] === "12" ) {
			var eh = 0;
		} else {
			var eh = parseInt( tObj.endTime.split( ":" )[ 0 ] );
		}
	}
	
	var em = parseInt( tObj.endTime.split( ":" )[ 1 ].split( " " )[ 0 ] );
	
	var st = ( ( sd * 24 ) * 60 ) + ( sh * 60 ) + ( sm );
	var et = ( ( ed * 24 ) * 60 ) + ( eh * 60 ) + ( em );
	
	if( st <= this.ct && et >= this.ct ) {
		this.setTab( parseInt( tObj.activeTab ) );
	} else {
		this.tCounter++
    	if( this.tCounter < this.cTimes.length ) {
        	this.compTimes( this.cTimes[ this.tCounter ] );
    	}
	}
};


TabConfig.prototype.setTab = function( tab ) {
	//	window.alert( "Entering: setTab on " + this.modID.id + " to " + tab );
	//	window.alert( first_child( this.modID ).nodeName );
	
	if( tab > 0 && tab <= child_nodes( first_child( this.modID ) ).length ) {
		for( var i = 0; i < child_nodes( first_child( this.modID ) ).length; i++ ) {
			if( i != 0 ) {
				//window.alert( child_nodes( first_child( this.modID ) )[ i ].className );
				child_nodes( first_child( this.modID ) )[ i ].className = "";
				child_nodes( node_after( first_child( this.modID ) ) )[ i ].className = "contCnt";
			} else {
				child_nodes( first_child( this.modID ) )[ i ].className = "first";
				child_nodes( node_after( first_child( this.modID ) ) )[ i ].className = "contCnt";
			}
		}
		if( tab == 1 ) {
			child_nodes( first_child( this.modID ) )[ tab - 1 ].className = "first on";
			child_nodes( node_after( first_child( this.modID ) ) )[ tab - 1 ].className = "contCnt on";
		} else {
			child_nodes( first_child( this.modID ) )[ tab - 1 ].className = "on";
			child_nodes( node_after( first_child( this.modID ) ) )[ tab - 1 ].className = "contCnt on";
		}
	}
	//
	//window.alert( "Exitting: setTab on " + this.modID.id + " to " + tab );
};

/*
	
*/ 
 

function initializePartnerBar() { 
jQuery(function() {
  var partners = [
  
		        {
		          "logo" : "http://img2.timeinc.net/ew/img/consumer_marketing/PopEater_Logo_01182010.gif",
		          "feed" : "http://www.ew.com/ew/static/json/popeater/feed.js",
		          "moreTitle" : "More from PopEater TV News",
		          "link" : "http://www.popeater.com/television",
		          "classSuffix" : "popeatertvnews"
		        }
			, 
		        {
		          "logo" : "http://img2.timeinc.net/ew/i/homepage/partners/people_logo.gif",
		          "feed" : "http://www.ew.com/ew/static/json/people/feed.js",
		          "moreTitle" : "More from People.com",
		          "link" : "http://www.people.com/people",
		          "classSuffix" : "people"
		        }
			, 
		        {
		          "logo" : "http://img2.timeinc.net/ew/i/homepage/partners/cnn_logo.gif",
		          "feed" : "http://www.ew.com/ew/static/json/cnn/feed.js",
		          "moreTitle" : "More from CNN",
		          "link" : "http://www.cnn.com/",
		          "classSuffix" : "cnn"
		        }
			, 
		        {
		          "logo" : "http://img2.timeinc.net/ew/img/newsletter/huffingtonpost-sr6.gif",
		          "feed" : "http://www.ew.com/ew/static/json/huffingtonpost/feed.js",
		          "moreTitle" : "More from Huffington Post",
		          "link" : "http://www.huffingtonpost.com/",
		          "classSuffix" : "huffpost"
		        }
			 ];

  // default is 2
  var options = {
        semanticMarkup : true,
        headerTitle : "From Our Partners",

        numEntries : 2,
  	    autoAdjustOnFailure : true
    }
  //new PartnerBar("partnerbar", partners, options);
  
  jQuery('#centercolPartnerBar').append('<div class="centercol-pb-root"><h3 class="centercol-pb-header">' + options.headerTitle + '</h3><div class="pb-partner-table">');
  jQuery.each(partners, function(i, partner) {
  	var uri = partner.feed;
	jQuery.getScript(uri, function() {
  		var html = '<div class="pb-partner-cell pb-partner-cell-' + partner.classSuffix + '"><h4 class="pb-partner-logo pb-partner-logo-' + partner.classSuffix + '"><a href="' + partner.link + '" class="pb-partner-logo pb-partner-' + partner.classSuffix + '" target="_blank"><img src="' + partner.logo + '" class="pb-partner-logo pb-partner-logo-' + partner.classSuffix + '"/></a></h4><ul class="pb-partner-feedData pb-partner-feedData-' + partner.classSuffix + '">';
  		 var j = 0;
		 jQuery.each(feed, function(i,entry){
		 	if ((entry.url != '') && (entry.title != '')) {
				j++;
				if (j <= options.numEntries) {
					html += '<li class="pb-entry pb-entry-' + partner.classSuffix + '"><div class="pb-title"><a href="' + entry.url + '" class="pb-title" target="_blank">' + entry.title + '</a></div></li>';
				}
			}
			});
  		html += '</ul><div class="pb-partner-moreLink pb-partner-moreLink-' + partner.classSuffix + '"><div class="pb-partner-moreLink pb-partner-moreLink-' + partner.classSuffix + '"><a href="' + partner.link + '" class="pb-partner-moreLink" target="_blank">' + partner.moreTitle + '</a></div></div>';
  		jQuery('#centercolPartnerBar .pb-partner-table').append(html);
  		});
  });
  jQuery('#centercolPartnerBar .pb-root').append('<div class="pb-footer"></div>');
  jQuery('#centercolPartnerBar').append('</div></div>');
  });
}



/**
 *
 * This is the configuration file for the default tab JavaScript.
 *
 * @param	masterDefault	Set this with a number corresponding to a tab, leave as empty quotes if you want no masterDefault.
 * @param	times			This is a data structure that holds all the time configurations, don't touch it.
 * @param	startDay		Set this with a start day in the following format( mon, tue, wed, thu, fri, sat, sun or monday or Monday or MONDAY or Mon ).
 * @param	endDay			Set this with an end day in the following format( mon, tue, wed, thu, fri, sat, sun ).
 * @param	startTime		Set this with a start time in the following format( 1:00 PM ).
 * @param	endTime			Set this with an end time in the following format( 12:00 AM ).
 * @param	activeTab		Set this with a number corresponding to a tab, do not leave empty.
 *
 */

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"
		} 
	]
};

var musicConfig = {
	masterDefault : "1" ,
    times : [
		{
			startDay :   "mon" ,
			startTime :  "12:00 AM" ,
			endDay :     "sun" ,
			endTime :    "11:59 PM" ,
			activeTab :  "1"      
		}
	]
};

function initTabs() {
	try {
		new TabConfig( "tvTabMod" , tvConfig );
		new TabConfig( "movieTabMod" , movieConfig );
		new TabConfig( "musicTabMod" , musicConfig );
	} catch( e ) {
		window.status = e;
		//window.alert( e );
	}
}



