//
// Undergraduate Studies
// Display-time layout modifications
// Originally by Adam Mark Finlayson, WTS
// amf%northwestern!edu
// Updated by Ethan Romba
//

// Declare a global object with default behavior settings
// All site-specific global variables belong in this object
WCAS = {
	equalizeCols: false,
	fadeDuration: 200
};

//
// equalizeColumns(select0, select1, ..., selectorN)
//
// Used to set heights of elements (by CSS selector) to the
// largest height of the group. Simulates table-based layouts.
//
// Example:
//     equalizeHeight("#first-column", ".other-columns");
//
// This function differs from the version in
// /shared/js/layout.js in the following ways:
//   - Called with CSS selectors
//   - Accounts for the top- and bottom-padding of each column
//   - Does not resize the tallest column.
//
function equalizeColumns() {
	var columns = equalizeColumns.arguments ;
	var $column = null;
	var height = 0;
	var maxHeight = 0;
	var newHieght = 0;
	var tallestColumn = 0;
	// Find the tallest column and record its height
	for (var i=0; i < columns.length; i++) {
		$column = $(columns[i]);
		if ($column.length != 0) {
			height =
				$column.height() 
				+ parseInt($column.css('padding-top'))
				+ parseInt($column.css('padding-bottom'));
			if (height > maxHeight) {
				maxHeight = height;
				tallestColumn = i;
			}
		}
	}
	// Resize the other columns to be the same height
	for (var i=0; i < columns.length; i++) {
		$column = $(columns[i]);
		if ($column.length != 0 && i != tallestColumn) {
			newHeight = 
				maxHeight
				- parseInt($column.css('padding-top'))
				- parseInt($column.css('padding-bottom'));
			$column.height(newHeight);
		}
	}
}

//
// hidePrinterFriendlyView()
//
// Used to remove printer-friendly stylesheets and
// layout modifications and return the page to standard-view
//
function hidePrinterFriendlyView() {
	// In IE7, the main-navigation highlight images misbehave when
	// returning from the printer-friendly view, so we refresh the page
	if ($.browser.msie && $.browser.version == '7.0') {
		location.reload();
	}
	// On all other browsers, we manipulate the existing DOM
	else {
		// Hide the page (with a fade-out on modern browsers)
		$('body').fadeOut(WCAS.fadeDuration, function() {
			// Hide printer-friendly navigation
			$('#printer-friendly-nav').hide();
			// Remove print.css and added header/footer elements
			$('#print-css, #wcas-logo, #current-url, .mmhide_nu-wcas, .mmhide_separator').remove();
			// Change header logo to the full-size version
			$('#logo img').attr('src', '/images/logo.png');
			// Restore footer elements to their original locations in the DOM
			$('#copyright').detach().appendTo('#contact-info');
			$('div.mmhide_org').detach().prependTo('.mmhide_vcard');
			// Show the page (with a fade-in on modern browsers)
			$('body').fadeIn(WCAS.fadeDuration);
			// Equalize columns on IE6
			if (WCAS.equalizeCols) {
				if (document.getElementById('content-col1') != null) {
					equalizeColumns('#content-col1', '#content-col2');
				}
				equalizeColumns('#col1', '#col2', '#col3');
			}
			// Resume the sidebar slideshow
			$('#sidebar-slideshow').cycle('resume');
		});
	}
	$(document).unbind('keydown');
}

//
// showPrinterFriendlyView()
//
// Used to display a "printer-friendly" version of the current page.
//
// This function adds print.css as a media="screen" stylesheet and 
// performs a few DOM manipulations to optimize the page for printing.
//
function showPrinterFriendlyView() {
	$('body').fadeOut(WCAS.fadeDuration, function() {
		// Pause the sidebar slideshow
		$('#sidebar-slideshow').cycle('pause');
		// Add print.css (media="screen" by default)
		$('head').append('<link id="print-css" rel="stylesheet" href="/css/print.css" type="text/css" />');
		// Show printer-friendly navigation
		$('#printer-friendly-nav').show();
		// Change header image to the smaller-size version and insert Weinberg logo next to it
		$('#logo img')
			.attr('src', '/images/logo-small.gif')
			.removeAttr('height')
			.removeAttr('width');
		$('#logo').after('<img id="wcas-logo" alt="Weinberg College of Arts &amp; Sciences" src="/images/layout/weinberg-logo-small.gif" height="62" width="267" />');
		// Print the current URL after the main content
		$('#col2').append('<span id="current-url"><span>URL for this page:</span> '+window.location+'</span>');
		// Move certain footer elements around
		$('#copyright').detach().insertBefore('#contact-info');
		$('div.mmhide_org').detach().insertBefore('div.mmhide_adr');
		$('div.mmhide_vcard div').prepend('<span class="mmhide_separator">| </span>');
		$('div.mmhide_vcard').after('<p class="mmhide_nu-wcas">Northwestern University // Judd A. and Marjorie Weinberg College of Arts and Sciences</p>');
		// Remove height rules set by equalizeColumns() on IE6/IE7
		if (WCAS.equalizeCols) {
			$('#main div[id^=col]').height('auto');
			$('#col2 div[id^=content-col]').height('auto');
		}
		// IE7 needs some extra help hiding just about everything
		if ($.browser.msie && $.browser.version == '7.0') {
			$('#nav, #col1, #col3').remove();
		}
		$('body').fadeIn(WCAS.fadeDuration);
		// Make the backspace key (used to go "back" by default) return the page to standard-view 
		// We use .keydown() instead of .keypress() since IE does not trigger the latter when modifier keys are pressed
		$(document).keydown(function(event) {
			if (event.which == 8) {
				$('#printer-friendly-back').click();
				return false;
			}
		});
	});
}

$(document).ready(function() {
	if ($.browser.msie) {
		switch ($.browser.version) {
			case "6.0":
				// Add hover class for navigation links in IE6
				$("#nav li").each(function() {
					$(this).hover(function(){
							$(this).addClass("mmhide_ie6-hover");
						},function(){
							$(this).removeClass("mmhide_ie6-hover");
						});
				}); 
			case "7.0":
				// Equalize column-height for IE6 and IE7
				WCAS.equalizeCols = true;
			case "8.0":
				// Disable fade effects on IE6, IE7, and IE8
				WCAS.fadeDuration = 0;
			break;
		}
		// IE7 needs some extra help sizing/displaying the
		// main-navigation highlight images due to its faulty support
		// for the sibling selector (+) in dynamic content
		// (See http://quirksmode.org/css/contents.html)
		if ($.browser.version == "7.0") {
			$('#nav img.mmhide_highlight').each(function() {
				if ($(this).prev().hasClass('mmhide_current')) {
					$(this).css('display', 'block');
				}
				$(this).width( $(this).parent().width() );
			});
		}
	}
	// Normally, we wouldn't want to use JavaScript to apply basic CSS styles.
	// But because the CSS selectors we use to target Dreamweaver/Contribute
	// also affect Firefox 2, we have no choice but to fix FF2 dynamically.
	else if ($.browser.mozilla && $.browser.version.indexOf('1.8') == 0) {
		$('#nav li, #nav ul li').css('display', 'inline');
		$('#nav > li > img').css('top', '-6px');
		$('#nav > li > ul').css('top', '22px');
		$('#main').css('position', 'static');
		$('#col1, #col2, #col3, #content-col1, #content-col2').css({
			display: 'table-cell',
			float: 'none'
		});
		$('#col2 #breadcrumb').css({
			margin: '1em 0 1em',
			position: 'relative'
		});
		$('#footer').css('line-height', '1.5');
		$('#footer .mmhide_logo').css('margin-top', '-47px');
	}
	
	// We do not add printer-friendly functionality
	// or social media links on the Home, News, and similar pages
	if ($('#main').hasClass('mmhide_two-col-content') == false) {

		// Add CSS that hides "printer-friendly" navigation links when printing
		$('head').append('<style type="text/css" media="print">#printer-friendly-nav { display: none !important; }</style>');
		// Add "printer-friendly" navigation links to the top of the page
		var backLink = '<a id="printer-friendly-back" href="#">&laquo;Back to standard view</a>';
		var printThisPageLink = '<a id="print-this-page" href="#">Print this page</a>';
		$('body').prepend('<div id="printer-friendly-nav">'+backLink+printThisPageLink+'</div>');
		$('#printer-friendly-back').click(function() {
			hidePrinterFriendlyView();
			return false;
		});
		$('#print-this-page').click(function() {
			window.print();
			return false;
		});
		
		// Add "Email this page" and "Printer-friendly version" links before the page content
		$('#col2 h1').first().wrap('<div id="heading-1"></div>').before('<div id="utility-links"><span></span></div>');
		$('#utility-links span').append('<a id="email-link" href="mailto:?body='+window.location+'" title="Email this page"></a>');
		$('#utility-links span').append('<a id="print-link" href="#" title="Printer-friendly version"></a>');
		$('#print-link').click(function() {
			showPrinterFriendlyView();
			return false;
		});
		
		// Add social-media links after the page content
		if (document.getElementById('content-col2') != null) {
			$('#content-col2').append('<div id="share-links"></div>');
		}
		else {
			$('#col2').append('<div id="share-links"></div>');
		}
		// These links are specified in /includes/share-links.html, which we load via AJAX
		$('#share-links').load('/includes/share-links.html');

	}
	
	// Add "view-larger" magnifying-glass overlay image to non-empty, right-sidebar lightbox images
	var overlayImage = $('<img src="/images/layout/view-larger.gif" alt="" class="mmhide_view-larger-overlay" />');
	$('#col3 a.mmhide_lightbox:parent, #col3 a.mmhide_slideshow-lightbox:parent').append(overlayImage);
	
	// Add lightbox functionality to any non-slideshow images in the sidebar
	// If any of these lightbox links are empty (ie. they have no thumbnail),
	// we also add an "All NAME photos" link after the last thumbnail
	// (where NAME is the text contained in the first H1 heading on the page)
	sidebarLightboxLinks = $('#col3 a.mmhide_lightbox');
	if (sidebarLightboxLinks) {
		$(sidebarLightboxLinks).lightBox();
		if ($('#col3 a.mmhide_lightbox:empty')) {
			var linkText = $('h1').first().text();
			linkText = $.trim(linkText);
			$(sidebarLightboxLinks).last()
				.siblings('p.image, p.caption').last()
					.after('<p style="text-align: right">&raquo; <a id="all-photos-link" href="#">All '+linkText+' photos</a></p>');
			$('#all-photos-link').click(function() {
				$(sidebarLightboxLinks).first().click();
				return false;
			});
		}
	}
	// Add lightbox functionality to any sidebar-slideshow images
	$('#sidebar-slideshow a.mmhide_slideshow-lightbox').lightBox({
		onStart: function() { $('#sidebar-slideshow').cycle('pause') },
		onClose: function() { $('#sidebar-slideshow').cycle('resume') }
	});
	// Enable the sidebar slideshow, if it exists
	$('#sidebar-slideshow').cycle({
		timeout: 7000,
		random: 1
	});
	
	// Equalize column-height for IE6 and IE7
	if (WCAS.equalizeCols) {
		if (document.getElementById('content-col1') != null) {
			equalizeColumns('#content-col1', '#content-col2');
		}
		equalizeColumns('#col1', '#col2', '#col3');
	}

	// Highlight links to the current page and section
	highlightSection_RootRelative('nav', 'mmhide_current');
	addHighlight('nav', 'mmhide_current');
	addHighlight('left-nav', 'mmhide_current');

	// Make search-box clear itself when clicked
	$('#search-text')
		.focus(function() {
			if (this.value == "Search site...") {
				this.value = '';
			}
		})
		.blur(function() {
			if (this.value == '') {
				this.value = "Search site...";
			}
		});

	// Move last-updated date from the bottom of the page to the footer
	$('#last-updated').detach().prepend('|&nbsp;Last Updated: ').appendTo('#copyright');
});

