// ================================================================
// If javascript is enabled load in stylesheet for hiding elements
// ================================================================

(function () {
	var head = document.getElementsByTagName("head")[0];
	if (head) {
		var scriptStyles = document.createElement("link");
		scriptStyles.rel = "stylesheet";
		scriptStyles.type = "text/css";
		scriptStyles.href = "/css/enabledjavascript.css";
		head.appendChild(scriptStyles);
	}
}());

$(document).ready(function(){

	// =========================================
	// Make external links open in a new window 
	// =========================================
	
	$('a[href^="http://"]').not('[href*="creightontoday.com"]')
	.attr({target: "_blank", title: "Opens in a new window"})  
	
	// =========================================
	// Fade in content
	// =========================================
	
	$(window).load(function () {
		$("#home, #secondarywrapper").fadeIn("slow");
		
		// =========================================
		// Create accordion for the content area 
		// =========================================
		
		// Set up accordion
			$('ul.drawers').accordion({
					header: 'h2',
					selectedClass: 'selected',
					event: 'click',
					alwaysOpen: false,
					autoHeight: false,
					active: false
			});	
	}); 
	
	
	// =========================================
	// Open/close sidebar widgets 
	// =========================================
	
	//Hide ignatian notes div
	$(".ignatiannotesbody").hide();
	
	//Hide health and wellness div
	$(".healthandwellnessbody").hide();
	
	//Hide In the News div
	$(".inthenews > ul").hide();
	
	// Apply rollover state when rolling over any part of In the news ad
	$("li.inthenews").hover(
      function () {
			$(this).toggleClass("active");
      }, 
      function () {
			$(this).toggleClass("active");
      }
    );
	
	// Apply rollover state when rolling over any part of Events Calendar ad
	$("li.eventscalendar").hover(
      function () {
			$(this).toggleClass("active");
      }, 
      function () {
			$(this).toggleClass("active");
      }
    );

	// Variable that follows whether the in the news area is open or closed
	var newsToggle=false; 
			
	//Show and hide Creighton in the news area and switch out the text
	$("li.inthenews h3").click(function(){
		if(newsToggle==false){
			$("li.inthenews h3 a.image span").replaceWith("<span>Creighton<br />in the News</span>");
			$(this).next("ul").toggle("blind", {direction: "vertical"}, 400); newsToggle=true;}
		else{
			$("li.inthenews h3 a.image span").replaceWith("<span>Click Here for Creighton<br />in the News</span>");
			$(this).next("ul").toggle("blind", {direction: "vertical"}, 400); newsToggle=false;}
		
		return false;
	});
	
	// Show and Hide Ignatian Notes
	
	$("#sidebar2 #ignatiannotes").click(function(){
		$(".ignatiannotesbody").toggle("blind", {direction: "vertical"}, 400);
		$(this).toggleClass("open");
		
		return false;
	});

	// Show/Hide Health and Wellness
	
	$("#sidebar2 .healthandwellness").click(function(){
		$(".healthandwellnessbody").toggle("blind", {direction: "vertical"}, 400);
		$(this).toggleClass("open");
		
		return false;
	});
	
	// Enable Links in the Menus and make it so it close the sidebar
			$('#sidebar2 .healthandwellness p a, #sidebar2 .ignatiannotes p a').bind('click',function(){			
				window.open(this.href);
				return false;
			  });
		
	// Validate Submit an event form
	$("#submitEvent").validate();
			
});