//Fix an issue with IE caching of images causing some images to flicker on mouseover
try {document.execCommand('BackgroundImageCache', false, true);} catch(e) {}


$(function() {
	$("tr:nth-child(odd)").addClass("odd"); //Add class of "odd" to alternating rows in a table for a 'zebra' effect
});


/*
 *	Creates hover effects and adds links to multiple elements
 *	grouped inside a single container
 *
 *	How it works:
 *	Finds all elements with a class of "hoverbox"
 *	Finds an "a" tag inside the element and pulls out the href
 *	Adds a mouseup event to the container to load the url specified in the href
 *	Adds mouseover and mouseout events to the container which add and remove
 *		class "hoverlink" from the container
 */
function hoverBox() {
	$(".hoverbox[a]").each(function(){
		$(this).mouseover(function() {$(this).addClass("hoverlink")});
		$(this).mouseout(function() {$(this).removeClass("hoverlink")});
		$(this).mouseup(function() {window.location = $("a", $(this)).attr("href")});
	});
};


function flexibleFeature() {
	$("#flex-feature").addClass("closed");
	$("#flex-feature p").append("<span id='reveallink'>... <a href='#reveal'>more &raquo;</a></span>");
	$("#reveallink").click(function(){

		$("#reveallink").empty();
		if($("#flex-feature").attr("class") == "closed") {
			$("#reveallink").append(" <a href='#reveal'> less &raquo;</a>");
			$("#flex-feature").removeClass("closed");
		}
		else {
			$("#reveallink").append("... <a href='#reveal'>more &raquo;</a>");
			$("#flex-feature").addClass("closed");
		}
	});
};

/*
 * Used to show and hide content
 * Applies a class of "closed" to the container when closed
 * Creates a span tag with a specified id that acts as the "link" that triggers
 * the open/close action
 *
 * Accepts the following parameters:
 *   container:	the id of the containing element
 *   trigger:		the id of the element that triggers the show/hide behavior
 *							default = [container id]_trigger ex. content-box_trigger
 *	 isClosed:	boolean - set to true to have the content be hidden on page load
 *							default = true
 *   toggle:		boolean - determines whether a container can be shown/hidden repeatedly or used only once
 *							default = true
 */
function revealContent(container, trigger, isClosed, toggle) {
	//Set default values is no parameter provided
	trigger = 	(typeof trigger == 'undefined') ? container + "_trigger" : trigger;
	isClosed = 	(typeof isClosed == 'undefined') ? 1 : isClosed;
	toggle = 		(typeof toggle == 'undefined') ? 1 : toggle;

	//Add the toggle element to the page
	$("<span id='" + trigger + "'></span>").appendTo($("#" + container));

	if(isClosed) $("#" + container).addClass("closed");

	//Open or close the container
	$("#" + trigger).click(function(){
		//Remove the trigger if the container is not toggle-able
		if(!toggle) $("#" + trigger).remove();

		$("#" + container).toggleClass("closed");
	});
};

/*
 * Handles rotation of an image on page refresh
 * Will change either the background-image of an element
 * or the src attribute of an img element
 *
 *	Accepts five parameters:
 *		id: the id of the element
 *		imagePath: path to directory containing the images
 *		imagePrefix: the base name all the images share
 *		imageSufix: the file extension
 *		number: the number of images to choose from
 *
 *	Images must be named using the following convention:
 *	 imagePrefix + number + imageSufix
 *	 samplename + 1 + .jpg		samplename1.jpg
 *	 samplename + 2 + .jpg		samplename2.jpg
 */
function randomImage(id, imagePath, imagePrefix, imageSufix, number) {
	randomNumber = 1 + Math.floor(Math.random() * number);
	image = imagePath + imagePrefix + randomNumber + imageSufix;

	//Change either the image src or the background image
	//depending on the context
	if($("img[@id="+ id +"]").length > 0) {
		//Change the src of an img element
		$("img[@id="+ id +"]").attr({ src: image });
	}
	else {
		//Change the background image of an element
		$("#"+ id +"").css({backgroundImage: "url(" + image + ")" });
	}
}




/* Uses javascript to replicate hover functionality on elements
 * other than <a> in IE
 * Not needed for standards complaint browsers which can use CSS
 * This script is automatically called by IE on every page
 */
jsHover = function() {
	$(".dropdown").each(function(){
		$(this).mouseover(function() {$(this).addClass("jshover")});
		$(this).mouseout(function() {$(this).removeClass("jshover")});
	});
}
//Only target IE.   Firefox, Safari, and Opera do not need
if (document.all&&document.getElementById) window.onload=jsHover;
