// 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'); }
	});
}); 
	
	


// needs to be the last line
})(jQuery);