/***********/
/* showMap */
/***********/
/* shows the county map box on the search page
 */
function showMap()
{
	/* just for easy reference to the multi-select boxes */
	county_list = document.getElementById('counties');
	state_protection_list = document.getElementById('state_protection');
	community_list = document.getElementById('communities');

	/* only proceed if the county box is selected */
	if( !county_list.disabled )
	{
		/* make the county map box be displayed */
		document.getElementById('county_map_box').style.display = 'block';
		
		/* make the multi-select boxes hidden, because Internet Explorer is broken
		 * (try commenting this out and you just might cry) */
		county_list.style.visibility = 'hidden';
		state_protection_list.style.visibility = 'hidden';
		community_list.style.visibility = 'hidden';
		
		/* synchronize county list selection and map selection */
		for( i = 0; i < county_list.length; i++ )
		{
			/* if the county is selected on the list */
			if( document.getElementById( county_list.options[i].value.toLowerCase() ) )
			{
				/* make the map selection (shaded region image is visibly) match the list selection */
				document.getElementById( county_list.options[i].value.toLowerCase() ).style.visibility = ( county_list.options[i].selected ? 'visible' : 'hidden' );
			}
		}
	}
}

/***********/
/* hideMap */
/***********/
/* hides the county map box on the search page
 */
function hideMap()
{
	/* just for easy reference to the multi-select boxes */
	county_list = document.getElementById('counties');
	state_protection_list = document.getElementById('state_protection');
	community_list = document.getElementById('communities');
	
	/* remove county map box from display */
	document.getElementById('county_map_box').style.display = 'none';
	
	/* make the multi-select boxes visible again (see showMap function) */
	county_list.style.visibility = 'visible';
	state_protection_list.style.visibility = 'visible';
	community_list.style.visibility = 'visible';
}

/********************/
/* updateCountyName */
/********************/
/* simply sets the hover-overed county as the text (with some formatting) shown in the county map box
 */
function updateCountyName( county )
{
	document.getElementById('county_name').innerHTML = '<span style="text-decoration: underline;">County Name</span><br /><span style="font-size: 24px;">'+(county?county.title:"")+"</span>";
}

/****************/
/* 1toggleCounty */
/****************/
/* updates a county's selection status when clicked
 */
function toggleCounty( county )
{
	/* just for easy reference to the county multi-select list */
	county_list = document.getElementById('counties');
	
	/* only proceed if the county box is selected */
	if( !county_list.disabled )
	{
		/* find the county that was clicked by going through each county on the list
		 * ??? can this be done in constant time, as in county_list.options[county]? */
		for( i = 0; i < county_list.length; i++ )
		{
			/* if we've found the one that was clicked */
			if( county_list.options[i].value == county )
			{
				/* toggle the select state of the county*/
				county_list.options[i].selected = !county_list.options[i].selected;

				/* set the image that is that county's shading to correspond to the the state of the county's selection */
				document.getElementById( county.title.toLowerCase() ).style.visibility = ( county_list.options[i].selected ? 'visible' : 'hidden' );
				
				/* stop the loop, since we've found the county we're looking for */
				break;
			}
		}
	}
}

/***************/
/* toggleInput */
/***************/
/* toggles the enabled/disabled state of a multi-select box
 * checkbox is the checkbox used to enable or disabled the search method
 * input is the multi-select box being enabled or disabled
 */
function toggleInput( checkbox, input )
{
	/* set the disabled state to match the checked state */
	document.getElementById( input ).disabled = !checkbox.checked;
	
	/* if the multi-select box in question is the county one,
	 * set the 'show map' button to the same state */
// 	if( input == 'counties' )
// 	{
// 		document.getElementById( 'map_button' ).disabled = !checkbox.checked;
// 	}
	
	/* for each option of the multi-select box */
	for( i = 0; i < document.getElementById( input ).options.length; i++ )
	{
		/* set it to be deselected */
		document.getElementById( input ).options[i].selected = false;
	}
}

/* variable for timing the fade in of tooltips */
var timeout;

/* string that is appended to each opacity assignment, because Micrsoft felt the best way 
 * to represent all of its proprietary rendering effects is with a single string, including a 
 * a shameless plug to themselves */
var IEshadowcrap = "progid:DXImageTransform.Microsoft.dropshadow(OffX=3,OffY=3,Color='#99666666',Positive='true')";

/**************/
/* setOpacity */
/**************/
/* sets the opacity of a tooltip using all the different ways that different browsers do it
 * opacity is in percentage (out of 100), and is prepended by "0." when needed as out of 1
 */
function setOpacity( tooltip, opacity )
{
	if( document.all )
	{
		/* for Internet Explorer */
		document.getElementById( tooltip ).style.filter = "alpha(opacity="+opacity+")"+IEshadowcrap;
	}
	else
	{
		/* Opera doesn't support opacity by any of these methods...*/
		
		/* for standards compliant browsers (which is only Mozilla browsers) */
		tooltipElement.style.opacity = ( opacity / 100 );
		
		/* for Mozilla browsers before the CSS2 standard came out, since they were ahead of the game */
		tooltipElement.style.MozOpacity = ( opacity / 100 );
		
		/* for KHTML based browsers, such as Konqueror and Safari (pre 1.2?) */
		tooltipElement.style.KhtmlOpacity = ( opacity / 100 );
		
		/* for text based browsers like Links and Lynx */
		
		/* gotcha, didn't I? */
	}
}

/**************/
/* getOpacity */
/**************/
/* returns the current opacity of an element as a percentage */
function getOpacity( tooltip, opacity )
{
	// check for IE
	if( document.all )
	{
		// since IE values don't exist until you modify them once
		if( !tooltipElement.style.filter )
		{
			// set the opacity to 10% if never set before
			setOpacity( tooltip, 10 );
		}
		
		// then return the opacity by finding the characters within the stupid filter string
		return parseFloat( tooltipElement.style.filter.substr( 14, 2 ) );
	}
	else
	{
		// for good browsers, jsut return the opacity value...seriously...
		return tooltipElement.style.opacity * 100;
	}
}

/************/
/* killFade */
/************/
/* brings the opacity of a tooltip to 'full' */
function killFade( tooltip )
{
	/* just for easy reference to the tooltip itself */
	tooltipElement = document.getElementById( tooltip );
	
	/* stop the timeout */
	clearTimeout( timeout );
	
	setOpacity( tooltip, 90 );
}

/***************/
/* showTooltip */
/***************/
/* makes a tooltip apear, with fadein */
function showTooltip( tooltip )
{
	/* just for easy reference to the tooltip itself */
	tooltipElement = document.getElementById( tooltip );
	
	/* tries to make things easier for really slow computers by
	 * killing the fade if it took longer than half a second */
	//setTimeout( "killFade('"+tooltip+"')", 500 );
	
	/* turn the visibility of the tooltip on */
	tooltipElement.style.visibility = 'visible';

	/* increase the opacity by 10% */
	setOpacity( tooltip, getOpacity( tooltip ) + 10 );
	
	/* if the opacity is less than 90% */
	if( getOpacity( tooltip ) < 90 )
	{
		/* do this all again in 50 milliseconds */
		timeout = setTimeout( "showTooltip('"+tooltip+"')", 50 );
	}
}

/***************/
/* hideTooltip */
/***************/
/* makes a tooltip dissapear */
function hideTooltip( tooltip )
{
	/* turn the visibilty of the tooltip to off */
	document.getElementById( tooltip ).style.visibility = 'hidden';
	
	/* set the opacity to 10% (to rest it for future showTooltip()s) */
	setOpacity( tooltip, 10 );

	/* stop any fadins going on */
	clearTimeout( timeout );
}

/**************/
/* openWindow */
/**************/
/* utility wrapper for window.open, just so I can pass only the parameters that matter for most popups */
function openWindow( page, width, height, name, scrollbars )
{
    return void( window.open( 	page,
				(name?name:"_blank"),
				"width="+width+",height="+height+",location=no,menubar=no,statusbar=no,toolbar=no,scrollbars=" +
				(scrollbars ? scrollbars : "no")  + ",resizable=no"
							) );
}

