/*Function required for SlideShow */
function disableAnchorCarousel(obj, disable)
{

  if(disable)
    {   
      var href = obj.getAttribute("href");
      if(href && href != "" && href != null)
      {
         obj.setAttribute('href_bak', href);
      }
      obj.removeAttribute('href');
    }
    else
    { 
      obj.setAttribute('href', obj.attributes['href_bak'].nodeValue);
    }
 }
 
 /**
 * function to return hex color values.
 * @param {String} rgbval  Color value in hex or RGB.
 * @return {String} Returns a hex color code.
 */
rgbhex = function(rgbval){
	if (!rgbval) {return "#000000";}
	var s = rgbval.match(/rgb\s*\x28((?:25[0-5])|(?:2[0-4]\d)|(?:[01]?\d?\d))\s*,\s*((?:25[0-5])|(?:2[0-4]\d)|(?:[01]?\d?\d))\s*,\s*((?:25[0-5])|(?:2[0-4]\d)|(?:[01]?\d?\d))\s*\x29/);
	
	if(s){ s=s.splice(1); }
	if(s && s.length==3){
	    d='';
	    for(i in s){
	        e=parseInt(s[i],10).toString(16);
	        e == "0" ? d+="00":d+=e;
	    } return '#'+d;
	} else { return rgbval; }
}

/**
 * function to init nav rollovers.
 */
$(function () {
	var navItems = $("#nav-items li:has(div)");
	navItems.each(function(i){
		var navItem = $("div.nav-item",this);
		var navHover = $(this);
		var nav = $("#nav");
		navHover.hover(function(e){
			//show rollover
			nav.css('background-position',"0px -"+ (i+1)*38+'px');
			navItem.slideDown("fast");					
			},function(e){
			//hide rollover
			navItem.slideUp("fast");
			nav.css('background-position','0px 0px');
		});
	});	
});

// Init popular terms
$(function() {
    var searchDds = $(".search-box div.search-toggle a");
    searchDds.each(function() {
        var prnt = $(this).parent().parent();
        $(this).toggle(
			function() {
			    $(prnt).parent().addClass('search-box-open');
			    $(prnt).addClass('search-popular-bg');
			    $("div.search-terms-bg", prnt).addClass("search-terms-on");
			    $("div.search-toggle", prnt).addClass("search-toggle-on");
			},
			function() {
			    $(prnt).parent().removeClass('search-box-open');
			    $(prnt).removeClass('search-popular-bg');
			    $("div.search-terms-bg", prnt).removeClass("search-terms-on");
			    $("div.search-toggle", prnt).removeClass("search-toggle-on");
			}
		);
    });
});

/**
 * function to replace elements with sIFR text.
 * @param {Object}  els  Elements to be replaced.
 * @param {Object}  font Font to change to (Specified in #initSIFR).
 * @param {Integer} spacing Letter-spacing (positive or negative). Default is 0.
 * @param {String}  weight Font weight (bold/normal). Default is 'normal'.
 * @param {Boolean} nowrap Determines if text is forced to a single line.  Default is 'false'.
 */
replaceElements = function(els,font,spacing,weight,nowrap) {
	spacing = spacing ? spacing : 0;
	weight = weight ? weight : "normal";
	els.each(function() {
		sIFR.replace(font, {
		  	elements:$(this),
			forceSingleLine : nowrap,
			wmode: 'transparent',			
			css: {'.sIFR-root' : { 
				'kerning':true,
				'letter-spacing': spacing,			
		  		'color': rgbhex($(this).css('color')),
		  		'text-transform': $(this).css('text-transform'),
		  		'text-decoration': 'none',
				'font-weight': weight
				},
				'.highlight' : {
						'color' : rgbhex($('span:first',this).css('color'))
				}		
			}			
		});
	});
}

// Init sIFR replacements
$(function() {
    if (typeof din != "undefined") {
        replaceElements($('h1:not(:has(a))'), din, -1, 'normal', false);
        replaceElements($('h2:not(:has(a))'), din, -1, 'normal', false);
        replaceElements($('h3:not(:has(a))'), din, -1, 'normal', false);
    }
});

function embeddedForumPostLoad() {
    if (typeof din != "undefined") {
        replaceElements($('.RDEmbeddedForum h1:not(:has(a))'), din, -1, 'normal', false);
        replaceElements($('.RDEmbeddedForum h2:not(:has(a))'), din, -1, 'normal', false);
        replaceElements($('.RDEmbeddedForum h3:not(:has(a))'), din, -1, 'normal', false);
    }
}


/**
 * function to init dailybites display and events.
 */
$(function () {
	if ($('#dailybites-container').length > 0)
	{
		currDb = -1;
		today = new Date().getDay();
		var dbDays = $('#db-days div');
		dbDays.each(function(i){
			if (i > today) {
				//disable future days of week
				$('a', this).addClass('db-day-disabled');
				$('a', this).css('cursor', 'default');
				$('a:hover', this).css('background-position', -(i * 28) + 'px 0px');
			}
			else {
				//bind actions
				$('a', this).bind('click', function(e){
					e.preventDefault();
					showDb(i);
				});
			}
		});
		showDb(today);
	}
});

/**
 * function that will rotate the dailybites based on day selected.
 * @param {Integer} db	Day index (0-6) to start with. Default is today.
 */
showDb = function(db){
	dest = $('#db-feature'+db).position().left;
	if (currDb > -1) {
		/* reset nav indicato r*/
		$('div:eq('+currDb+')','#db-days').removeClass('on');
	} 
	/* scroll to day selected */	
	$('#db-features-wrapper').animate(
		{left: -dest + 'px'}, 
		0
	);
	/* show current day indicator */
	$('div:eq('+db+')','#db-days').addClass('on');
	currDb = db;	
}


// Weekly Menu Planner UI - J. Covington

$(function () {
	var today = new Date().getDay();
	var wmpDays = ["#wmp-sunday","#wmp-monday","#wmp-tuesday","#wmp-wednesday","#wmp-thursday","#wmp-friday","#wmp-saturday"];
	for (var i = 0; i < 7; i++){	
		if (i === today){
			$(wmpDays[i]).addClass("wmp-today");
			var todaySrc = $("img", wmpDays[i]).attr("src");
			var todayAlt = $("img", wmpDays[i]).attr("alt");
			$("#wmp-main-image").attr("src", todaySrc);
			$("#wmp-main-image").attr("alt", todayAlt);
		}
		if (i !== today){
			$(wmpDays[i]).hover(function(e){
				$(this).css("background-position","0px -50px");
				$("a:link", this).css("color","#99ccff");
				var thumbSrc = $("img", this).attr("src");
				var thumbAlt = $("img", this).attr("alt");
				$("#wmp-main-image").attr("src", thumbSrc); 
			$("#wmp-main-image").attr("alt", thumbAlt);
			},function(e){
				$(this).css("background-position","0px 0px");
				$("a:link", this).css("color","#848484");
				$("#wmp-main-image").attr("src", todaySrc);
			$("#wmp-main-image").attr("alt", todayAlt);
			});
		}
	}
});




