//Fix an issue with IE caching of images causing some images to flicker on mouseover
try {document.execCommand('BackgroundImageCache', false, true);} catch(e) {}
	
$j(function() {
	$j("body").addClass("jsEnabled");
	
	featureToggle();
	tabify("#news-and-events");
	essentialTabs();
	zebraTable("#content table");
	toggleState("#web2-share h5", "open");
	toggleState("#navigation #nav-audience h3", "open");
	hoverBox();	
});

// given a JQuery selection string, the function will toggle a class of 'className'
// on the items parent whenever the item is clicked
function toggleState(target, className) {
	$j(target).toggle(
		function() {
	    	$j(this).parent().addClass(className);
	    },
	    function() {
	    	$j(this).parent().removeClass(className);
	    }
    );
}

function zebraTable(el) {
	$j(el).each(function() {
		$j("tr:nth-child(odd)").addClass("odd");
	});
}

function tabify(el) {
	var oldSafari = false;
	if(jQuery.browser.version <= 418.8 && $j.browser.safari) {
		oldSafari = true;
		$j("body").addClass("safari");
	}
	
	var container = $j(el);
	var className = $j(el + " div:eq(0)").attr("id");
	
	if(!oldSafari) {
		container.attr("class", className);
	}

	$j(el + " div h2").click(function() {
		var id = $j(this).parent().attr("id");
		container.attr("class", id);
	});
}
// needed to work around a bug in older versions of safari when using tabbed content
$j(window).load(function() {
	if(jQuery.browser.version <= 418.8 && $j.browser.safari) {
		var container = $j("#news-and-events");
		var className = $j("#news-and-events div:eq(0)").attr("id");			
		container.attr("class", className);			
	}
});

function drawers(el) {

	var oldSafari = false;
	if(jQuery.browser.version <= 418.8 && $j.browser.safari) {
		oldSafari = true;
	}

	var container = $j(el);
	$j(el + " ul").hide();

	$j(el + " .trigger").click(function() {

		var item = $j(this).parent();

		if(!oldSafari) {
			
			if(item.hasClass("open")) {
				item.children("ul").slideUp(200, function() {
					item.removeClass("open");
				});
			}
			else {
				item.siblings(".open").children("ul").slideUp(200, function() {
					item.siblings(".open").removeClass("open");
				});
				item.addClass("open");
				item.children("ul").slideDown(200);
			}
		}
		else {
			if(item.hasClass("open")) {
				item.children("ul").hide();
				item.removeClass("open");
			}
			else {
				item.siblings(".open").children("ul").hide();
				item.siblings(".open").removeClass("open");
				item.addClass("open");
				item.children("ul").show();				
			}
		}
		
		return false;
    });
}

function featureArea() {

	$j("#feature-area li h2").hover(
		function() {
			$j(this).addClass("jsHover");
			if(!$j("#feature-area").hasClass("open")) {
				var id = $j(this).parent().attr("id");
				$j("#feature-area").attr("class", id);
			}
		},
		function() {
			$j(this).removeClass("jsHover");
		}
	);

	$j("#feature-area li h2").click(function(){
		$j(this).parent().addClass("active");
		$j(this).parent().siblings(".active").removeClass("active");
		$j("#feature-area").attr("class", "open");
	});

}

/*
 *	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() {
	$j(".hoverbox").each(function(){
		var parent = $j(this);
		var children = $j(this).children(".hovergroup");
		
		if(children.length > 1) {
			children.mouseover(function() {
				children.each(function(){
					$j(this).addClass("hoverlink");
				});
			});
			children.mouseout(function() {
				children.each(function(){
					$j(this).removeClass("hoverlink")
				});					
			});
			children.mouseup(function() {window.location = $j("a", parent).attr("href")});
		}
		else {
			parent.mouseover(function() {$j(this).addClass("hoverlink")});
			parent.mouseout(function() {$j(this).removeClass("hoverlink")});
			parent.mouseup(function() {window.location = $j("a", parent).attr("href")});
		}
	});
};

function featureToggle() {
	if($j("#feature p").length > 1) {
		
		$j("#feature").each(function(){
			$j(this).addClass("closed");
			$j(this).append("<span class='button' title='View More'>View More</span>");
		});
		
		$j("#feature .button").toggle(
			function() {
				$j(this).parent().removeClass("closed");
				$j(this).text("View Less");
				$j(this).attr("title", "View Less");
			},
			function() {
				$j(this).parent().addClass("closed");
				$j(this).text("View More");
				$j(this).attr("title", "View More");
			}
		);
	}
}

function expandList(container, num) {
	var num = num || 5;
	if($j(container + " ul").length > 1) {
		
		$j(container).each(function() {
			var container = $j(this).children("ul");
			var fullList = container.children("li");
			var visibleList;
			
			if(fullList.length > num) {
				visibleList = $j(this).children("ul").children("li:lt("+num+")");
				container.empty().append(visibleList);
				$j(this).addClass("closed");
				$j(this).append("<span class='button' title='View More'>View More</span>");
				
				$j(this).children(".button").toggle(
					function() {
						$j(this).parent().removeClass("closed");
						container.empty().append(fullList);
						$j(this).text("View Less");
						$j(this).attr("title", "View Less");
					},
					function() {
						$j(this).parent().addClass("closed");
						container.empty().append(visibleList);
						$j(this).text("View More");
						$j(this).attr("title", "View More");
					}
				);
			}
		});
	}
}

// Faux tabs for the the Essentials modules
function essentialTabs() {
	$j(".tabbed-content").each(function() {
		var tabContainer = $j(this);
		var list = tabContainer.prepend("<ul class='clearfix tab-sections'></ul>");
		
		tabContainer.children("div").each(function(i) {
			var id = $j(this).attr("id");
			var header = $j(this).children("h3").text();
			$j(this).children("h3").hide();
			
			if(i != 0) {
				$j(this).hide();
				tabContainer.children(".tab-sections").append("<li> | <a href='#"+id+"'>"+header+"</a></li>");
			}
			else {
				tabContainer.children(".tab-sections").append("<li class='active'><a href='#"+id+"'>"+header+"</a></li>");
			}
			tabContainer.children(".tab-sections").children("li").children("a").click(function() {
				var href = $j(this).attr("href");
				var split = href.split('#');
				var id = split[split.length-1];
				$j(this).parent().siblings().removeClass("active");
				$j(this).parent().addClass("active");
				tabContainer.children("div").hide();
				$j("#" + id).show();
				return false;
			});
		});
	});
}

/*
 * Clear out the default text in a specified input when it gains focus
 * If focus is lost and field is empty replace with default text
 *
 * Takes an optional parameter which is a jQuery string selector "#search-form input", "form .focusInput", etc
 * By default it targets ALL text inputs on a page
 */
function inputClear(target) {
	var target = target || "input";
	
	$j(target).each(function() {
		if($j(this).attr("type") == "text" || $j(this).attr("type") == "password") {
			var value = $j(this).val();

			$j(this).focus(function() {
				if($j(this).val() == value) {
					$j(this).val("");
				}
			});

			$j(this).blur(function() {
				if($j(this).val() == "") {
					$j(this).val(value);
				}
			});
		}
		else {
			var value = $j(this).text();
			
			$j(this).focus(function() {
				if($j(this).text() == value) {
					$j(this).text("");
				}
			});

			$j(this).blur(function() {
				if($j(this).text() == "") {
					$j(this).text(value);
				}
			});
		}
	});
}