// JavaScript Document


/*
 * initiallize jQuery scripts
 * @param $arg
 */

$(document).ready(function() {
	// call all necessary functions
	popupLinks();
	emailLinks();
	captions();
	expandBoxes();
    redirectOverlay();
});




/*
 * function popupLinks
 *
 * Build popup links
 */

function popupLinks() {
	var a = $('a.popup');
	a.each(function() {
		// add a popup warning (class will position offscreen)
		$(this).append('<span class="popupNote"> - Opens in a new window</span>');
		// create the popup link
		var href = $(this).attr('href');
		var title = $(this).attr('title');
		var newTitle = (title.length != 0) ? ' - Opens in new window' : 'Opens in new window';
		$(this).attr('title', newTitle);
		
		$(this).bind('click', function(e) {
			e.preventDefault();
			window.open(href, 'popup','');
		});
	});
}




/*
 * function emailLinks
 *
 * Build mailto: links
 */

function emailLinks() {
	var s = $('span.emailLink');
	s.each(function() {
		var email = $(this).text();
		email = email.replace(' [at] ','@');
		email = email.replace(' [dot] ','.');
		$(this).text(email);		
		$(this).wrap('<a href="mailto:'+email+'"></a>');
	});
}




/*
 * function captions
 *
 * Build captions for images on mouseover
 */

function captions() {
	$('.caption').each(function() {
		$(this).css({ 'opacity':0 , 'position':'absolute' , 'bottom':'0px' , 'left':'0px' });
		$(this).siblings('img').hover(function() {
			$(this).siblings('.caption').fadeTo(200, 1);															 
		}, function() {
			$(this).siblings('.caption').fadeTo(200, 0);															 
		});
	});
}




/*
 * function expandBoxes
 *
 * Build expanding / collapsing boxes
 */

function expandBoxes() {
	$('.expandSource').each(function() {
		$(this).wrapInner('<a href="#" class="expandLink"></a>');
		$(this).prepend('<span class="icon">&nbsp;</span>');
		$(this).next().addClass('expandTarget').css({ 'position':'relative' });
		if($(this).hasClass('noHide')) {
			$(this).addClass('expanded');
		} else {
			$(this).addClass('collapsed');
			$(this).next().hide();
		}
	});
	
	$('a.expandLink').bind('click', function(e) {
		$(this).find('img').toggleClass('plusIcon').toggleClass('minusIcon');
		var source = $(this).parents('.expandSource');
		source.next().slideToggle();
		if(source.hasClass('collapsed'))	source.removeClass('collapsed').addClass('expanded');
		else								source.removeClass('expanded').addClass('collapsed');
		// refire the equal columns function 
		e.preventDefault();
	});
}



/*
 * function redirectOverlay
 *
 * Build a modal overlay with a message if there was a redirect referer
 */

function redirectOverlay() {
    if(document.URL.indexOf("?redir=true") != -1) {
		// add the overlay
		$('body').prepend('<div id="dialogOverlay"></div><div id="dialogContainer"></div>');
		$('#dialogOverlay').css({ 'opacity':'0.9' , 'height':'100%' });
		// append a link to close the window
        $('#dialogContainer').append('<h1>Redirected</h1>')
            .append('<div>You have been redirected to the new Academic Affairs Website at</div>')
            .append('<div style="padding: 10px; font-style: italic; font-size: .9em; clear: both;"><a href="http://www.sfsu.edu/~academic">http://www.sfsu.edu/~academic</a></div>')
            .append('<div>Please update your bookmarks or links appropriately.</div>')
            .append('<div id="dialogClose"><a href="#" style="cursor: pointer;">[ close window ]</a></div>');
		// position the message
		$('#dialogContainer').css({ 'width':'50%' , 'height':'50%' , 'position':'absolute' , 'left':'25%' , 'top':'25%' , 'z-index':'20' , 'opacity':'0' }).fadeTo(1000, 1);
		// set focus
		$('#dialogContainer').focus();
		// add the link behavior
		$('#dialogClose').bind('click', function(e) {
			$('#dialogContainer').fadeTo(500, 0.0, function() { $(this).remove(); });
			$('#dialogOverlay').fadeTo(500, 0.0, function() { $(this).remove(); });
			$('body').attr('tabindex','-1').focus();
			e.preventDefault();
		});
    }
}

