// needs to be the first line
(function($) {
	 
document.documentElement.className = 'js';

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function toggleObj(obj) {
	var el = document.getElementById(obj);
	if ( el.style.display != 'none' ) {
		el.style.display = 'none';
	}
	else {
		el.style.display = 'block';
	}
}


function toggleEl(el) {	
	if(el) {
		if ( el.style.display != 'none' ) {
			el.style.display = 'none';
		}
		else {
			el.style.display = 'block';
		}
	}
}


function toggleVisibility() {	

	var CSSclass = 'jstoggle';
	var ts = getElementsByClass(CSSclass);	
	
	if(ts) {	
		for(var i = 0; i < ts.length; i++) {	
			
			var n = getNextDiv(ts[i]);
			if(n) {
				toggleEl(n);				
				ts[i].onclick = function() { 
				toggleEl(getNextDiv(this)); return false; }
			}
		}
	}	
	
	function getNextDiv(elem) { 
		do {			
			// if element has siblings, check if they're divs
			if(elem.nextSibling) { 
				elem = elem.nextSibling;
			} else { // if not, go up the DOM
				// note: parent itself (even if it's a div) can't be used, we can only use subsequent siblings
				if(elem.parentNode.nextSibling) { // if parent has siblings, check them
					elem = elem.parentNode.nextSibling;
				} else { // otherwise, continue up going up the DOM until we find parent with siblings					
					do {
						elem = elem.parentNode;
					} while(elem.nextSibling == null)					
					elem = elem.nextSibling; // start checking the sibling(s)
				} 
			}
		} while (elem && elem.nodeName.toUpperCase() != 'DIV');
		
		return elem;
	}	
	
}

// window.addEvent('domready', toggleVisibility);
addLoadEvent(toggleVisibility);

/*function stripeTable() {
	
	var CSSclass = 'stripe';
	var ts = getElementsByClass(CSSclass);
	
	$("tr:even").css("background-color", "#f0f0f0");	
	
}
addLoadEvent(stripeTable);*/

td = {}
td.nma = {}
td.nma.whyjoin = {
	
	frequency: 6, // in seconds
	index: 1, // start animation from the second frame, as first shown onload
	framesSelector: '#tabbedPromo > dl',
	navSelector: '#tabbedPromo > ul:first > li',
	activeClass: 'active',
	
	total: 0,
	timer: null,
	nodes: [],
	nodesNav: [],

	init: function() {		
		
		var nodeSet = $(td.nma.whyjoin.framesSelector);
		var nodeSetNav = $(td.nma.whyjoin.navSelector);
		td.nma.whyjoin.total = nodeSet.size();
		if(td.nma.whyjoin.total == 0) { return; }
		td.nma.whyjoin.nodes = nodeSet.get(); // caching DOM node references
		td.nma.whyjoin.nodesNav = nodeSetNav.get(); // caching DOM node references
		nodeSetNav.find('a').click(function() {
			var index = this.hash.substr(5);
			if(!isNaN(index)) { 
				index--;
				clearInterval(td.nma.whyjoin.timer);
				td.nma.whyjoin.timer = null;
				td.nma.whyjoin.index = index;
				td.nma.whyjoin.increment();
				td.nma.whyjoin.timer = setInterval(td.nma.whyjoin.increment, td.nma.whyjoin.frequency * 1000);
			}
			return false;
		});
		td.nma.whyjoin.timer = setInterval(td.nma.whyjoin.increment, td.nma.whyjoin.frequency * 1000);
	},
	
	increment: function() {
		if (td.nma.whyjoin.index == td.nma.whyjoin.total) { 
			td.nma.whyjoin.index = 0;
		}
		td.nma.whyjoin.activate(td.nma.whyjoin.index);	
		td.nma.whyjoin.index++;
	},
	
	activate: function(index) {
		td.nma.whyjoin.index = index;
		$(td.nma.whyjoin.framesSelector).each(function(i) {
			if( $(this).is('.' + td.nma.whyjoin.activeClass) ) {
				$(this).hide().removeClass(td.nma.whyjoin.activeClass);
			}
		});		
		$(td.nma.whyjoin.nodes[index]).fadeIn();
		$(td.nma.whyjoin.nodes[index]).addClass(td.nma.whyjoin.activeClass);
		
		$(td.nma.whyjoin.navSelector).each(function(i) {
			if( $(this).is('.' + td.nma.whyjoin.activeClass) ) {
				$(this).removeClass(td.nma.whyjoin.activeClass);
			}
		});			
		$(td.nma.whyjoin.nodesNav[index]).addClass(td.nma.whyjoin.activeClass);
	}
	
};
$(document).ready(td.nma.whyjoin.init);
	
	
	
	
$(document).ready(function() {                         
	$('.recipeBtn').click(function() {
		if( $(this).is('.activated') ) {
		$(this).html('<span>View Recipe</span>').removeClass('activated');
		} else {
		$(this).html('<span>Hide Recipe</span>').addClass('activated'); }
	});
}); 

$(document).ready(function() {
	$('#tourOpen').click(function() {
			var win = window.open("../take_a_tour.html", "poptour", "width=567,height=416,menubar=no,location=no,resizable=noresize,scrollbars=no,status=no");
			win.focus();
			return false;		
	});
});

$(document).keydown(function(e) {
	if(e.which == 13) {
		return false;	
	}
});

$(document).ready(function() {
	var fdpselect = $('#fdpdietselect');
	var selectedplan = fdpselect.val();
	$('#' + selectedplan).show();
	fdpselect.change(function() {
		$('.fdp_planCTR').hide();
		$('#' + this.value).show();							
	});
});

})(jQuery);





/* legacy functions */

var DOM = true;

function popUp(url) {
	sealWin=window.open(url,"win",'toolbar=0,location=0,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,width=500,height=450');
	self.name = "mainWin"; 
}

function toggleFieldsNow( Mid, Iid ) {
		   	var e = document.getElementById( Mid );
			 var c = document.getElementById( Iid );
		    if ( e.style.visibility == 'visible' ||  e.style.display == 'block' ) {
		      e.style.visibility = 'hidden';
		      e.style.display    = 'none';
			  c.style.visibility = 'visible';
		      c.style.display    = 'block';
		    } else {
		      e.style.visibility = 'visible';
		      e.style.display    = 'block';
			  c.style.visibility = 'hidden';
		      c.style.display    = 'none';
		    }	
			
}
		  
function getDietChoice(choice) {
		var ChoiceVar = choice;		
		document.FreeProfileForm.dietchoice.value = ChoiceVar		
}

function showHelp(Qno) {
	if (!document.getElementById) return
		var	help=Qno; 	
		var flipOn = document.getElementById(help); 
		flipOn.style.display=flipOn.style.display=='block'?'none':'block';
		return false;
}

function toggle(toggler) {
	if(document.getElementById) {
	targetElement = toggler.parentNode.nextSibling;
	
	if(targetElement.className == undefined) {
	targetElement = toggler.parentNode.nextSibling.nextSibling;	
	}	

if (targetElement.style.display == "block")
	{
	targetElement.style.display = "none";
	}
	else
	{
	targetElement.style.display = "block";
	}
	}
}
tdtoggle = toggle;
