/*====
 * style search buttons
 *================================================ */

$(document).ready(function() {
	$('#searchLabels').addClass('active'); // allows me to style the radio buttons differently when JS is enabled
	$('#searchLabels :radio[id]').click( function() {
		var selectedInput = $(this).attr("id");
		$('#searchLabels label').removeClass("selected");
		$('#searchLabels label[for="' + selectedInput + '"]').addClass("selected");
	});
	$('#searchLabels label').click( function() {
		$('#searchLabels label').removeClass("selected");
		$(this).addClass("selected");
	});
});

/*====
 * replace search box input with label text
 *================================================ */

$(document).ready(function() { 
	var searchLabel = $('div.searchInputs label').remove().text();
	if ($('#searchTerm').val() == '') {
		$('#searchTerm').addClass('placeholder').val(searchLabel).focus(function() {
			if (this.value == searchLabel) {
				$(this).removeClass('placeholder').val('');
			};
		}).blur(function() {
			if (this.value == '') { 
				$(this).addClass('placeholder').val(searchLabel);
			};
		});
		$('#search form').submit(function() {
			if ($('#searchTerm').val() == searchLabel) {
				$('#searchTerm').val('');
			}
		});
	}
});

/*====
 * rotator
 *================================================ */

var headline_count;
var headline_interval;
var old_headline = 0;
var current_headline = 0;

$(document).ready(function(){
	$("div#topStories").removeClass('noscript'); 
	$("div#topStories").addClass("active");
	headline_count = $("div.topStoryItem").size();
	$("div.topStoryItem:eq("+current_headline+")").css('display','block');

	headline_interval = setInterval(headline_rotate,7000); //time in milliseconds
	$("div#topStories").append('<div id="topStoryButtons"><ul></ul></div>');
	$('div#topStories .topStoryItem h2').each(function(index) {
		var $sectionLabel = $(this).remove().text();
		$("div#topStoryButtons ul").append('<li class="' + $sectionLabel + '">' + $sectionLabel + '</li>');
		$("#topStoryButtons li:eq(0)").addClass('activeControls'); 
	});
	$("div#topStories").append('</ul></div>');
	$("#topStoryButtons li:eq(0)").click(function() { 
		selected_headline = 0;
		$("div.topStoryItem:eq(" + current_headline + ")").fadeOut("slow");
		$("#topStoryButtons li:eq(" + current_headline + ")").removeClass('activeControls'); 
		$("#topStoryButtons li:eq(0)").addClass('activeControls'); 
		$("div.topStoryItem:eq(0)").fadeIn("slow"); 
		current_headline = selected_headline;
		clearInterval(headline_interval);
		return false;
	});
	$("#topStoryButtons li:eq(1)").click(function() { 
		selected_headline = 1;
		$("div.topStoryItem:eq(" + current_headline + ")").fadeOut("slow");
		$("#topStoryButtons li:eq(" + current_headline + ")").removeClass('activeControls'); 
		$("#topStoryButtons li:eq(1)").addClass('activeControls'); 
		$("div.topStoryItem:eq(1)").fadeIn("slow"); 
		current_headline = selected_headline;
		clearInterval(headline_interval);
		return false;
	});
	$("#topStoryButtons li:eq(2)").click(function() { 
		selected_headline = 2;
		$("div.topStoryItem:eq(" + current_headline + ")").fadeOut("slow");
		$("#topStoryButtons li:eq(" + current_headline + ")").removeClass('activeControls'); 
		$("#topStoryButtons li:eq(2)").addClass('activeControls'); 
		$("div.topStoryItem:eq(2)").fadeIn("show"); 
		current_headline = selected_headline;
		clearInterval(headline_interval);
		return false;
	});
	$("#topStoryButtons li:eq(3)").click(function() { 
		selected_headline = 3;
		$("div.topStoryItem:eq(" + current_headline + ")").fadeOut("slow");
		$("#topStoryButtons li:eq(" + current_headline + ")").removeClass('activeControls'); 
		$("#topStoryButtons li:eq(3)").addClass('activeControls'); 
		$("div.topStoryItem:eq(3)").fadeIn("slow"); 
		current_headline = selected_headline;
		clearInterval(headline_interval);
		return false;
	});
});

function headline_rotate() {
	current_headline = (old_headline + 1) % headline_count; 
	$("div.topStoryItem:eq(" + old_headline + ")").fadeOut("slow");
	$("#topStoryButtons li:eq(" + old_headline + ")").removeClass('activeControls'); 
	$("#topStoryButtons li:eq(" + current_headline + ")").addClass('activeControls'); 
	$("div.topStoryItem:eq(" + current_headline + ")").fadeIn("slow"); 
	old_headline = current_headline;
}

/*====
 * healthNotes
 *================================================ */

$(document).ready(function() { 
	$('div#healthNotes').addClass("active");
	$('div#healthNotes #conditions').css('display', 'none');
	$('div#healthNotes h2.healthCenters').click(function() {
		$('div#healthNotes #conditions').hide();
		$('div#healthNotes #healthCenters').show();
	});	
	$('div#healthNotes h2.conditions').click(function() {
		$('div#healthNotes #healthCenters').hide();
		$('div#healthNotes #conditions').show();
	});	
});

$(document).ready(function() { 
	$('a.popup').click(function() {
		this.target = "_blank";
	});
});

/*
function dropdown () {
	if (document.all&&document.getElementById) {
		var navRoot = document.getElementById("topNav");
		if (!navRoot) { return; }
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				}
			}
		}
	}
}
*/

