var unhideInterval = 0;		//Interval ID of unhideResult()

/*
Unhides an individual list item in the excerpts list.
It then checks to see if there are any more hidden elements, and unsets
its own interval if there are not.

@param	type			determines whether to pull data frome homepage or subpage elements
@global unhideInterval	this function's interval ID set by updateHeader() or loadSubResults()
*/
function unhideResult(type) {
	if(type=="home") {
		$(".excerpts ul li:hidden:first").fadeIn("normal");
		if($(".excerpts ul li:hidden").length==0 && $(".issue-center-results li:hidden").length==0)
			clearInterval(unhideInterval);
	}
	else {
		$(".secondary-search-results li:hidden:first").fadeIn("fast");
		if($(".secondary-search-results li:hidden").length==0)
			clearInterval(unhideInterval);
	}
}

/*
Checks secondary page results to see if default content needs to be loaded, then sets the unhideResult() function
@param	prefix			url prefix used to load the default results
@global	unhideInterval	used so unhideResult() knows when to stop looking for hidden results to fade in
*/
function loadSubResults(prefix) {
	defaultText="We're sorry, your search did not return any results. Loading most popular tags...";
	if($(".secondary-search-results li:first").text()==defaultText)
	{
		url = prefix+'issue-center-fetch/no-results';
		$(".secondary-search-results").load(url, {}, function(){
			unhideInterval = setInterval("unhideResult('sub')", 500);
		 });
	}
	else
		unhideInterval = setInterval("unhideResult('sub')", 500);
}

/*
Stores the url param in a cookie
@param	url		the string to save to the cookie
*/
function setLastHomeSearch(url) {
	cookieName='wwfh-issue-center-prefs';
	options = {expires: 1};
	$.cookie(cookieName, url, options);
}

/*
Retrieves the variable stored with setLastHomeSearch()
@return			the stored string
*/
function getLastHomeSearch() {
	cookieName='wwfh-issue-center-prefs';
	return $.cookie(cookieName);
}

/*
Redirects the window to a search or tag result page based on type and prefix params
@param	type		either "search" or "tag" - determines which elements from which to pull data
@param	prefix		url prefix used to load the correct page
*/
function goToResultPage(type, prefix)
{
	if(type=="search")
		url='issue-center/search/'+$('#issue-center-search').attr('value');
	else
		url='issue-center/tag/'+$('#popular-tags').attr('value');
	window.location.href= prefix+url;
	return false;
}

/*
Updates homepage results based on input from the search bar
*/
function updateResultsFromSearch()
{
	currentSearchText = $('#issue-center-search').attr('value');
	if(currentSearchText) {
		url = navSegment+'issue-center-fetch/search/'+escape(currentSearchText);
		setLastHomeSearch(url);
		updateHeader('Your Search Results', currentSearchText);
		loadResults(url);
	}
	return false;
}

/*
Updates homepage results based on input from the tag bar or tag dropdown.
If either parameter isn't specified, the function uses the current selection from the tag dropdown
@param	tag		optional tag to update (used only by the tag bars)
@param	title	optional title to display in excerpt header (used only by the tag bars
*/
function updateResultsFromTag(tag, title)
{
	if(!tag || !title) {
		tag=$('#popular-tags').attr('value');
		title=$('#popular-tags option:selected').text();
		}

	url=navSegment+'issue-center-fetch/tag/'+tag;
	setLastHomeSearch(url);
	updateHeader('Popular Topic', title);
	loadResults(url);
	
}

/*
Updates the excerpts header using new header text and/or new subheader text when applicable
@param	newHeaderText	new text for exerpts header
@param	newSubText		new text for excerpts subheader

*/
function updateHeader(newHeaderText, newSubText)
{
	headerText = $('.excerpts h2 span').text();
	subText = $('.excerpts h2 strong').text();
	
	if(headerText!=newHeaderText) {
		$('.excerpts h2 span').fadeOut('slow', function(){
			$('.excerpts h2 span').text(newHeaderText).fadeIn('slow');
		});
	}

	if(subText!=newSubText) {
		$('.excerpts h2 strong').fadeOut('slow', function() {
			$('.excerpts h2 strong').text(newSubText).fadeIn('slow');
		});
	}
}

/*
Loads the specified url into the excerpts list, check to see if it recieves no results, and loads the default if that happens
@param	url		the url to load
*/
function loadResults(url)
{
	$('.excerpts ul').html('<li class="loading">Loading...</li>').load(url, {}, function(){
		if($(".excerpts ul li:first").text()=="We're sorry, your search did not return any results. Loading most popular tags...")
			$('.excerpts ul').load(navSegment+'issue-center-fetch/no-results');
		$(".excerpts").css('min-height', ($(".excerpts ul li").length)*130);
		unhideInterval = setInterval("unhideResult('home')", 1000)
	});

}