// For IE hover flicker bug
try {
  document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}

// Get jQuery ready...
$(function(){
	$("body").addClass("jsEnabled");
	
	hoverBox();
	$("a.gallery-link").click(function() {
		window.open(this.getAttribute('href'), '_blank', 'width=550px,height=400px,fullscreen=no,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no');
		return false;
	});
	
	searchBox("home_searchinput");
	searchBox("searchinput");

// Navigation Arrows (left-hand nav)
	$("#primary > li > ul > li > a").prepend("<span>&gt;</span>");
	$("#primary .back_to a span").remove();
	$("#primary > li > ul > li.back_to > a").prepend("<span>&lt;</span>");

// Flyout menu
	$(".flyout_menu").mouseover(function() {$(this).addClass("flyout_menu_over")});
	$(".flyout_menu").mouseout(function() {$(this).removeClass("flyout_menu_over")});

// Section Profile arrows
	$(".section_faculty_profile a, .section_donor_profile a, .section_career_profile a, .section_capital_campaign a").mouseover(function() {$(this).addClass("over")});
	$(".section_faculty_profile a, .section_donor_profile a, .section_career_profile a, .section_capital_campaign a").mouseout(function() {$(this).removeClass("over")});

// Section news/events hovers
	$(".section_events a").mouseover(function() {
		$(this).parent().parent(".section_events dl").addClass("over");
	});
	$(".section_events a").mouseout(function() {
		$(this).parent().parent(".section_events dl").removeClass("over");
	});

// Inline support hover arrow
	$(".support_inline a").mouseover(function() {$(".support_inline a span").addClass("over")});
	$(".support_inline a").mouseout(function() {$(".support_inline a span").removeClass("over")});

// zebra tables
	$("table").each(function(){
		$("tr:nth-child(even)").addClass("even");
	});


// unescape html
//	$.cleanedhtml = unescape($('#uneschtml').html());
////	alert($.cleanedhtml.replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>'));
////	$('#uneschtml').html($.cleanedhtml.replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>'));



});// jQuery says bye-bye!


/*
 * Show/Hide the Google logo in the search box
 */
function searchBox(el) {
	el = $("." + el);

	if(!el.val()) {
		el.addClass("google-logo");
	}
	el.focus(function() {
		$(this).removeClass("google-logo");
	});
	el.blur(function() {
		if($(this).val().length == 0) {
			$(this).addClass("google-logo");
		}
	});
}

function featureReveal() {
	// Show/hide homepage Feature
	$(".feature_button").click(function() {$("#feature_container").toggleClass("feature_container_visibility")});
	$("h2.feature_button").hover(
		function () {
			var img = $(this).css("background-image").replace("home", "feature");
			$(this).css("background-image", img);
		}, 
		function () {
			var img = $(this).css("background-image").replace("feature", "home");
			$(this).css("background-image", img);
		}
	);
}

function featureNav() {
	//Open content links in the parent window, not the iframe
	$(".explore_merrimack a, #feature_content a").click(function(){
		window.parent.location.href = $(this).attr("href");
		return false;
	});
	
	// Feature Navigation arrow
	$("#feature_primary a").mouseover(function() {$(this).addClass("show")});
	$("#feature_primary a").mouseout(function() {$(this).removeClass("show")});	

	// Feature layer switch
	$("#feature_storylinks a").click(function() {
		
		//Remove active state from current items
		$("#feature_storylinks a").removeClass("active");
		$("div.active").removeClass("active");
		
		//Set active state on new item
		if($(this).attr("class") == "one") $("div.one").addClass("active");
		if($(this).attr("class") == "two") $("div.two").addClass("active");
		if($(this).attr("class") == "three") $("div.three").addClass("active");
		$(this).addClass("active");
		
		return false;
	});
}

function CampusLife() {
	$("#campus-life li").each(function(){
		
		var h3Text = $(this).children("h3").text();
		var img = $(this).children("img");
		h3HTML = h3Text.replace(/&/g, "and");
		h3HTML = h3HTML.replace(/ /g, "-");
		$(this).wrapInner("<div></div>");
		$(this).prepend("<a href='#"+h3HTML+"'>"+h3Text+"</a>");
		
		$(this).children("a").click(function(){
			$(this).parent("li").siblings().removeClass("active");
			$(this).parent("li").addClass("active");
			$(".header_image span").empty();
			$(".header_image span").append(img);
			return false;
		});
	});
	var rand = Math.floor (Math.random() * $("#campus-life li").length);
	$("#campus-life li:eq("+rand+")").addClass("active");
	var firstImg = $("#campus-life li:eq("+rand+") img");
	$(".header_image span").append(firstImg);
}

/* 
 * Toggle a class of open on an object's parent when clicked
 * accepts a single parameter "item" that is a string containing a jQuery selector
 * examples: "#content p", ".date", ".time a" etc
 */
function openOnClick(item) {
	$(item).click(function(){
		$(this).parent().toggleClass("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() {
	$(".hoverbox").each(function(){
		$(this).mouseover(function() {$(this).addClass("hoverlink")});
		$(this).mouseout(function() {$(this).removeClass("hoverlink")});
		$(this).mouseup(function() {window.location = $("a", $(this)).attr("href")});
	});
};
