// Carleton College Admissions Slideshow
// Notes:
//	- Lightbox image links that point to the Reason image displayer are changed
//    to point to where the actual image is assumed to be. This script will need
//    to be modified if the path of the displayer script changes, or if there
//    are ever images displayed that do not have a ".jpg" extension.
// Author: Eric Naeseth '10 <naesethe@carleton.edu>

jQuery(function initialize_admissions_slideshows($) {
	var page_title = $("#content .title > h2").html();
	var displayer_pattern = /\/displayers\/image.php\?id=(\d+).*$/;
	
	function get_non_path() {
		var m = /(^\w+:\/\/[^\/]+)\//.exec(window.location);
		return (m && m[1]) ? m[1] : ''; 
	}
	
	var image_path = get_non_path() + '/global_stock/js/lightbox/images';
	
	function get_full_image_uri(link) {
		var uri = link.href;
		var image_path;
		var match;
		if ((match = displayer_pattern.exec(uri))) {
			image_path = "/images/" + match[1] + ".jpg";
			uri = uri.replace(displayer_pattern, image_path);
		}
		uri = uri.replace(/^about:blank/, ''); // WTF Internet Explorer?!?! part 2 -NW
		return uri.replace(/^about:/, ''); // WTF Internet Explorer?!?!  -EN
	}
	
	function get_image_caption(link) {
		var rich_caption = $(link).parent().find("div.imageInfo").html();
		return rich_caption || link.title;
	}
	
	function get_position_caption(pos, total) {
		var caption = "Photo " + pos + " of " + total;
		var links = [];
		
		if (pos > 1) {
			links.push('<a href="#prev-image" rel="prev">Previous</a>');
		}
		if (pos < total) {
			links.push('<a href="#next-image" rel="next">Next</a>');
		}
		
		return caption + " " + links.join(" | ");
	}
	
	$(".basicSlideshow").each(function init_slideshow() {
		var q = $(this);
		var trigger;
		
		function get_trigger(trigger_function) {
			trigger = trigger_function;
		}
		
		var link_list = q.children("ul")[0];
		var links = q.find("li > a").filter(function is_lightbox_link() {
			return this.parentNode.parentNode == link_list;
		});
		
		links.lightBox({
			getFullImageURI: get_full_image_uri,
			getImageCaption: get_image_caption,
			getSetPositionCaption: get_position_caption,
			triggerCallback: get_trigger,
			
			imageLoading: image_path + '/lightbox-ico-loading.gif',
			imageBtnPrev: image_path + '/lightbox-btn-prev.gif',
			imageBtnNext: image_path + '/lightbox-btn-next.gif',
			imageBtnClose: image_path + '/lightbox-btn-close.gif',
			imageBlank: image_path + '/lightbox-blank.gif'
		});
		
		var link = $('<a href="#slideshow" class="invoke"></a>');
		
		// Add the emblematic image.
		var first_link = q.find("li:first-child > a")[0];
		var emblem = $(first_link).find("img").clone().addClass("emblematic");
		link.prepend(emblem);
		
		// Add the slideshow text.
		if (links.length > 1)
		{
			var prefix = '<em>Slideshow:</em> ';
			var suffix = ' <span class="count">(' + links.length + ' photos)' +'</span>';
			link.append(prefix + page_title + suffix);
		}
		else // we have a single image
		{
			var prefix = '<em>Photo:</em> ';
			link.append(prefix + page_title);
		}
		
		link.click(function open_slideshow() { return trigger(first_link); });
		
		var launcher = $('<div class="slideshow"></div>');
		launcher.append(link);
		q.replaceWith(launcher);
	});
});
