/*
	Some custom JavaScript stuff lives here
*/

$(document).ready(function(){
	
	/* Put in a "previous page" link in the breadcrumbs */
	$("#breadcrumbs ul").after('<a class="go-back" href="javascript:history.go(-1)" title="Click here to go back to the previous page">&#171; Previous Page</a>');
	
	/* Autofocus for faster form input */
	$(".autofocus").focus();

	/* Only hide the Admissions sub-menus if the parent item isn't active */
	$("body#apply .menu ul li:not(.active) ul").hide();
	$("body#apply .menu ul li ul li.active").parent().show();

	/* Simple menu click-drops. Add a class of no-click-drop-menu to the body if you don't want the parent menu item to be the trigger */
    $("body:not(.no-click-drop-menu) .menu ul ul").hide().siblings("a").click(function () {
		$(this).siblings("ul").slideToggle();
		return false;
    });

	/* Click-drop Menu on homepage */
	$("#click-drop-menu").addClass("js").find("div").prev("a").click(function () {
        $(this).parent().siblings().removeClass("show-menu").end().toggleClass("show-menu");
		return false;
    });

	// Open gallery in popup window (target="blank" doesn't validate or give us sizing/toolbar control)
	$("a[@rel='gallery']").click(function(){
		window.open($(this).attr("href"),"gallery","height=530,width=780");
		return false;
	});
	
	// Create link to open music player in pop-up window
	$("#music-player").append("<p class='pop-out'><a href='http://www.loyno.edu/assets/shared/mp3s/mfa/pop-up.html' title='Click here to open the music player in a pop-up window'>pop-out</a></p>");

	// Open music-player in popup window (target="blank" doesn't validate or give us sizing/toolbar control)
	$("#music-player p.pop-out a").click(function(){
		window.open($(this).attr("href"),"musicplayer","height=95,width=300,location=no,resizable=no");
		return false;
	});

	// External links (target="blank" doesn't validate with strict doctype)
	$("a[@rel='external']").click(function(){
		window.open($(this).attr("href"),"external");
		return false;
	});
	
	/* Put an icon after external links to show that they pop up in a new window */
	var ex_link_gif = '<img style="margin-left:5px;vertical-align:middle" src="http://www.loyno.edu/assets/shared/images/css/icons/external.gif" alt="link opens in a new window">';
	$("#mfa .menu a[@rel=='external'], #montage .menu a[@rel='external'], #music .menu a[@rel='external'], #theatre .menu a[@rel='external'], #visualarts .menu a[@rel='external']").append(ex_link_gif);
	var ex_link_png = '<img style="margin-left:5px;vertical-align:middle" src="http://www.loyno.edu/assets/shared/images/css/icons/external.png" alt="link opens in a new window">';
	$("body.business .menu a[rel='external'], body.chn .menu a[rel='external'], body.css .menu a[rel='external'], body.law .menu a[rel='external']").append(ex_link_png);
	
	// For the search page: Duplicate the search form below the search results.
	// This avoids us having two search forms right on top of each other in some situations (e.g. JS disabled, or users landing on search page via URL and not via a form post).
	if ( $("#cse-search-results iframe").length > 0 ) {
		$("form.cse-search-box").clone().appendTo("#col-main-content .wrapper").end().siblings("h1").replaceWith("<h1>Search Results</h1>");
	}
	
	// Generic fadeout effect. Whatever you want faded out should be given a class of fadeout[i], with i being the number of seconds to wait before fading out.
	if ( $("[class*='fadeout']").length > 0 ) {
		var secs = $("[class*='fadeout']").attr("class").match(/fadeout\[(\d+)\]/)[1];
		myInterval = setInterval(fadecount, 1000);
	}

	function fadecount() { // Related to and called from the above.
		if (secs === 0) { $("[class*='fadeout']").fadeOut("slow"); clearInterval(myInterval); }
		secs -= 1;
	}

});
 