// JS for NFNS LocalHeader
if( typeof(SNI.Food.LocalHeader) == "undefined" ) {
	SNI.Food.LocalHeader = {};
}

SNI.Food.LocalHeader = {
	/*GLOBAL NAV*/
	localNav: function(mysel) {
		var navTimer = null;
		var initializeMenu = mysel + " #hubnav li";
		hideAll();
		initMenu(initializeMenu);
	
		function initMenu(initializeMenu) {
		var menu = initializeMenu;
	
		$(menu).find('h3:first span').click( function() {
			hideAll();
			}
		);	
		
		  $(menu+"[class ^= 'nav-']").each(function(i) {
			$(this).hover(function(e){
				hideAll();
				var $this = $(this);
				var divs = $(this).prevAll();
				var width = 0;				
				
				jQuery.each(divs, function() {
					width = width + parseInt($(this).width()) + 4;
				});
											
				var offsetVal = width + 6;

				$('body').bind("click", function(e){bodyClick(e);});			
				
				navTimer = setTimeout(function(){
				
				  $this.find('div.drop').css({left: offsetVal, top: "28px", background: "none", display: "block"});
				 
				 /* * * * *
				 * was causing dropdown contents to display at the top of the page
				 * * * * *
				 $this.find('div.drop').css({top: "29px", position: 'relative', background: "none", display: "block"});
				 */
				  navTimer = null;
				}, 375);
			},
			function(e){
				clearTimeout( navTimer );
				navTimer = null;
				var closeDiv = $(this);
				if (jQuery.browser.msie ){ $(this).find('div.drop').css({background: "b2b2b2"});}
				
				navTimer = setTimeout(function(){
				  closeDiv.find("a:first").removeClass("dd-on");
				  closeDiv.find('div.drop').css({top: "28px", display: "none"});
				  navTimer = null;
				}, 100);
			  
		  });/*hover*/
		});/*each*/
		}
		
		function bodyClick(e) {
			var $clicked=$(e.target);
			if ($clicked.parents().is('.drop')) {
				return false;
			} else {
				hideAll();
			  $('body').unbind();
			}
		}
	
		function hideAll() {
			clearTimeout( navTimer );
			navTimer = null;
			
			$("#hubnav li[class ^= 'nav-']").each(function() {
				  $(this).find('a:first').removeClass("dd-on");
			});			
			
			$("#hubnav li[class ^= 'nav-'] div.drop").each(function() {
			  $(this).css({display: "none"});
			});
		}
		function fixpickle(){
			
		}
	},
	/*END GLOBAL NAV*/
	
	init: function(mysel){
		SNI.Food.LocalHeader.localNav(mysel)
	}	
};

if( typeof(SNI.Food.NFNS) == "undefined" ) {
	SNI.Food.NFNS = {};
}

SNI.Food.NFNS.applyElim = function(mysel) {

	if ((typeof eData == 'undefined')|| ($(mysel).length == 0)) return;

	origImgMatch = /(http:\/\/img.foodnetwork.com\/FOOD\/2009\/)\d\d\/\d\d(\/\w*)_s3x4_(\w*\.jpg)/;
	elimImgSub = "$104/08$2_elim_$3";
	for (erec in eData)
	{
		isrc = $(mysel).find("li.t-" + eData[erec].id + " img").attr("src");
		$(mysel).find("li.t-" + eData[erec].id + " img").attr("src", isrc.replace(origImgMatch, elimImgSub));
		myalt =	$(mysel).find("li.t-" + eData[erec].id + " img").attr("alt");
		$(mysel).find("li.t-" + eData[erec].id + " img").attr("alt", myalt + " eliminated week " + eData[erec].wk);
		$(mysel).find("li.t-" + eData[erec].id).addClass("e");
		$(mysel).find("li.t-" + eData[erec].id).addClass("w" + eData[erec].wk);
	}
	return;
};

SNI.Food.NFNS.insertElim = function() {

	if ((typeof eData == 'undefined') || (typeof tData == 'undefined')) return;
	
	latestWk = 0;
	for (erec in eData)
	{
		if (eData[erec].wk > latestWk)
		{
			latestWk = eData[erec].wk;
			theId = eData[erec].id;
		}
	}	
	bFound = false;
	for (irec in tData)
	{
		if (tData[irec].id == theId) { bFound = true; break; }
	}
	if (!bFound) return;
	var outStr = "";

	outStr += '<a href="';
	outStr += tData[irec].bio;
	outStr += '"><img src="';
	outStr += tData[irec].image;
	outStr += '" alt="';
	outStr += tData[irec].name;
	outStr += '" /></a>';
	outStr += '<p><a href="';
	outStr += tData[irec].bio;
	outStr += '"><strong>';
	outStr += tData[irec].name;
	outStr += '</strong></a></p>';
	outStr += '<ul class="list">';
	outStr += '<li><a href="';
	outStr += tData[irec].bio;
	outStr += '">Read ';
	outStr += tData[irec].firstname;
	outStr += "'s bio</a></li>";
	outStr += '<li><a href="';
	outStr += tData[irec].video;
	outStr += '">Watch ';
	outStr += tData[irec].firstname;
	outStr += "'s videos</a></li>";
	outStr += '</ul>';
	document.write(outStr);
	return;	
};

SNI.Food.BorderFix = function(){
	var center_column = $('.fn-we');
	if(center_column.length > 0){
		var left_rail = $('div.fn-ww'),
		    right_rail = $('#fn-e'),
		    main_content = $('#fn-w'),
		    extend_length = 0,
		    fixHeight = function(){
			    center_column.css('padding-bottom',extend_length + 30 + 'px');
		    };
		    
		if (right_rail.height() >  main_content.height()){
			extend_length = right_rail.height() -  main_content.height();
			fixHeight();
		}
		else if (left_rail.height() > center_column.height()) {
			extend_length = left_rail.height() - center_column.height();
			fixHeight();
		}
	}
};


(function($) {
	$.fn.countDown2 = function(options) {
		var options = $.extend({
			container			: null,
			date				: 'January 1, 2099 00:00:00',
			updateInterval		: .5,
			wraps				: null,
			daysImage			: false,
			hoursImage			: false,
			minutesImage		: false,
			secondsImage		: false,
			imagePath			: '',
			onExpire			: function() {}
		}, options);
		return this.each(function(i) {
			var obj = this;
			var today = new Date();
			var endDate = new Date(options.date);
			var ms = Math.floor(endDate.getTime() - today.getTime())
			function two(x) {return ((x>9)?"":"0")+x}
			function three(x) {return ((x>99)?"":"0")+((x>9)?"":"0")+x}
			/* function MS2DHMSMS() - converts milliseconds to days : hrs : min : sec : ms */
			function MS2DHMSMS(ms) {
				var nt = {};
				var sec = Math.floor(ms/1000);
				ms = ms % 1000;
				nt.ms = three(ms).toString();
				var min = Math.floor(sec/60);
				sec = sec % 60;
				nt.ss = two(sec).toString();
				var hr = Math.floor(min/60);
				min = min % 60;
				nt.mm = two(min).toString();
				var day = Math.floor(hr/24);
				hr = hr % 24;
				nt.hh = two(hr).toString();
				nt.dd = day.toString();
				return nt;
			}
			function getTimeLeft() {
				var today = new Date();
				var ms = Math.floor(endDate.getTime() - today.getTime())
				if ( ms < 0 ) options.onExpire.call(this, obj);
				sLeft = MS2DHMSMS(ms).ss;
				mLeft = MS2DHMSMS(ms).mm;
				hLeft = MS2DHMSMS(ms).hh;
				dLeft = MS2DHMSMS(ms).dd;
				if ( dLeft <= 0 ) {
					if ( hLeft <= 0 ) {
						if ( mLeft <= 0 ) {
							if ( sLeft <= 0 ) {
								options.onExpire.call(this, obj);
							}
						}
					}
				}
				if ( options.wraps == null ) {
					$(obj).html('<span class="jcdDays">' + dLeft + '</span>&nbsp;<span class="jcdDaysText">day' + s + '</span>&nbsp;<span class="jcdHours">' + hLeft + '</span>:<span class="jcdMinutes">' + mLeft + '</span>:<span class="jcdSeconds">' + sLeft + '</span>');
				} else {
					for ( x in options.wraps ) {
						if ( x == 'days' ) {
							if ( options.daysImage ) {
								if ( options.imagePath ) {
									$(options.wraps[x]).html('<img src="' + options.imagePath + dLeft.toString() + '.jpg" />');
									if(dLeft < 2){
										if (dLeft == 0) {
											if(hLeft < 21) {
												$('span.days').addClass('today').removeClass('days').find('img').remove();
												$('span.days-hold').remove();
												$('div.hold').remove();
											}
											
										}
										else {
										$('span.plural').addClass('singular').removeClass('plural');
										}
									}
								} else {
									$(options.wraps[x]).html(dLeft);
								}
							} else {
								$(options.wraps[x]).html(dLeft);
							}
						}
						if ( x == 'hours' ) {
							if ( options.hoursImage ) {
								if ( options.imagePath ) {
									$(options.wraps[x]).html('<img src="' + options.imagePath + hLeft.toString() + '.jpg" width="20" height="20"/>');
								} else {
									$(options.wraps[x]).html(hLeft);
								}
							} else {
								$(options.wraps[x]).html(hLeft +' Hrs ');
							}
						}
						if ( x == 'minutes' ) {
							if ( options.minutesImage ) {
								if ( options.imagePath ) {
									$(options.wraps[x]).html('<img src="' + options.imagePath + mLeft.toString() + '.jpg" width="20" height="20"/>');
								} else {
									$(options.wraps[x]).html(mLeft);
								}
							} else {
								$(options.wraps[x]).html(mLeft+' Mins ');
							}
						}
						if ( x == 'seconds' ) {
							if ( options.secondsImage ) {
								if ( options.imagePath ) {
									$(options.wraps[x]).html('<img src="' + options.imagePath + sLeft.toString() + '.jpg" width="20" height="20"/>');
								} else {
									$(options.wraps[x]).html(sLeft);
								}
							} else {
								$(options.wraps[x]).html(sLeft+ ' Secs');
							}
						}
					}
				}
			};
			if ( options.container ) {
				obj.container = options.container;
			}
			if ( parseInt(MS2DHMSMS(ms).dd) < 0 ) {
				options.onExpire.call(this, obj);
			} else {
				if ( options.updateInterval ) {
					var TP_countdown = setInterval(function() { getTimeLeft(); }, options.updateInterval * 1000);
				} else {
					getTimeLeft();
				}
			}
		});
	};
})(jQuery);

//change href to absolute path if were on a pickle page or on the star blog
$(function(){
	$('#fn-full a').click(function(e){  e.preventDefault(); window.location='http://www.foodnetwork.com/the-next-food-network-star/'; }); 
	$('.fn-full.nfns-blog h1').click(function(e){  e.preventDefault(); window.location='http://www.foodnetwork.com/the-next-food-network-star/'; }).css('cursor','pointer')
	//temp hack to remove twitter module
	$('#twtr-frame').remove();
})

