// title:           Advanced Aquarist jQuery Functions
// author:          Shane Graber < l i q u i d (at) r e e f s (dot) o r g >
// filename:        jquery.aaolm.js
// last modified:   2007-01-11
// dependencies:    jQuery 1.0.4, Thickbox 2.0

function getURLVar(urlVarName, url) {
	//divide the URL in half at the '?'
	var urlHalves = String(url).split('?');
	var urlVarValue = '';
	if(urlHalves[1]){
		//load all the name/value pairs into an array
		var urlVars = urlHalves[1].split('&');
		//loop over the list, and find the specified url variable
		for(i=0; i<=(urlVars.length); i++){
			if(urlVars[i]){
				//load the name/value pair into an array
				var urlVarPair = urlVars[i].split('=');
				if (urlVarPair[0] && urlVarPair[0] == urlVarName) {
					//I found a variable that matches, load it's value into the return variable
					urlVarValue = urlVarPair[1];
				}
			}
		}
	}
	return urlVarValue;   
}

$(function() {

        // add amazon.com affiliate id onto our amazon.com links
        $("a[href*='amazon.com']").each(function() {
            var url = $(this).attr("href");
            $(this).attr("href", url+"/&tag=advaaquasonli-20");
        });

	// allow user to show/hide various portlets
	$("#portlet-navigation-tree h5, #portlet-photoalbum h5").append("&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"cursor:pointer;font-size:10px;font-weight:normal;text-transform:lowercase;\">[ show \/ hide ]<\/span>");

	$("#portlet-navigation-tree .portletBody, #portlet-photoalbum .portletBody").hide();

	$("#portlet-navigation-tree h5").toggle(function() {
		$("#portlet-navigation-tree .portletBody").slideDown();
	}, function() {
		$("#portlet-navigation-tree .portletBody").slideUp();  
	});

	$("#portlet-photoalbum h5").toggle(function() {
		$("#portlet-photoalbum .portletBody").slideDown();
	}, function() {
		$("#portlet-photoalbum .portletBody").slideUp();  
	});


	// Create Table of Contents for all articles with more than 2 <h2 /> tags
	var size = $("#region-content").find("h2").size();

	if (size > 1) {

		var heading = 1;

		// Place the TOC right after the keyword listing in all articles
		$("#keywords-list").after("<div id=\"menu\"><div id=\"toc\" \/><\/div><p class=\"discreet\">[ <a id=\"tocnav\">Show \/ Hide<\/a> | <a id=\"tocremove\">Remove<\/a> ]<\/p><\/div>");

		// Iterate over all <h2 /> tags and set their id's 
		$("#region-content h2").each(function() {
			$(this).attr("id", "h" + heading);
			heading = heading + 1;
		});

		// Build the Table of Contents
		$("#toc").prepend("<h3>Table of Contents<\/h3><ol id=\"genTOC\"><\/ol>");

		$("#content h2").each(function() {
			$("#toc ol").append("<li><a href=\"#" + $(this).attr('id') + "\">" + $(this).html() + "<\/a><\/li>");
		});
                $("#toc ol").append("<li><a href=\"#social\">Comments</a></li>");

		// Allow user to show/hide/remove the TOC
		$("#tocnav").toggle( function() {
			$("#toc").slideUp();
		}, function() {
			$("#toc").slideDown();  
		});

		$("#tocremove").click(function() {
			$("#menu").slideUp();
		});

	}


	// Create pullquotes from html source that's wrapped with <span class="quote" />
	$("#region-content span.quote").each(function() {
		$(this.parentNode).before("<blockquote class=\"pullquote\"><p>" + $(this).html() + "<\/p><\/blockquote>"); 
	}); 
	// Alternate pullquotes left and right
	$("#region-content blockquote.pullquote:odd").addClass("alt");


	// Make image links compatible with Thickbox gallery plugin
	// Set captions for Thickbox and center them if less than 65 characters long
	$("#region-content div a.popup").each(function() {

		var caption = $(this).parent().find("p.caption");
		var size = $(caption).size();
        	if (size > 0) {
			var captionText = $(caption).html();
			var captionLength = $(caption).text().length;

			$(this).attr("title", captionText);

			if (captionLength < 65) {
				$(caption).addClass("centered");
			}
		}
	});

	// backwards compatibilize articles with Thickbox.  /2005 articles are prone to this problem.
	$("#region-content p a img.image-inline, #region-content p a img.image-left, #region-content p a img.image-right").each(function() {
		$(this).parent().attr("class", "popup");
	});

	// Make all Thickbox images into a gallery by setting a common rel attribute and then changing the class to 'thickbox'
	$("#region-content a.popup").attr("rel", "gallery");
	// $("#region-content a.popup").attr("class", "thickbox");   <--- remove hashes when upgrading to next version of Thickbox!


	// Add 'even' class to every other table row (Zebra Tables)
	$("#region-content tr:nth-child(odd)").addClass("even");


	// make each issue toc entry clickable.  (larger clickable area = good)
	// $("#table-of-contents div.newsItem").attr("style", "cursor: pointer;");
	// $("#table-of-contents div.newsItem").each(function() {
	// 	$(this).bind(
	// 		"click",
	// 		function() {
	// 			var link = $(this).find("div.toc-entry a").attr("href");
	// 			window.location.href = link;
        //         return false;
	// 		}
	// 	);
	// });


	// create embedded youtube videos w/in the content
	// gets the youtube 'watch' url from the link, parses out the video id
	// and creates the embedded object directly below the paragraph that contained the link
	$("a[href*='youtube.com']").each(function() {
		var video = $(this).attr("href");
		var videoId = getURLVar("v", video);
		var videoToEmbed = "http://www.youtube.com/v/" + videoId + "&hl=en&fs=1";
		$(this).parent().after("<div class=\"youtube-embedded centered\"><object width=\"425\" height=\"344\"><param name=\"movie\" value=\"" + videoToEmbed + "\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"" + videoToEmbed +"\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"425\" height=\"344\"></embed></object></div>");
	});


	// create embedded pdf viewer using google's pdf viewer
	// source: http://googlesystem.blogspot.com/2009/09/embeddable-google-document-viewer.html
	// added 09/12/09, sbg
	$("p a[href$='.pdf']").each(function() {
		var pdf = $(this).attr("href");
		var embedPdf = "<iframe src=\"http://docs.google.com/gview?url=" + pdf + "&embedded=true\" style=\"width:600px; height:500px;\" frameborder=\"0\"></iframe><br /><br />";
		$(this).parent().after(embedPdf);
	});


	// Make the cover of the magazine 'fade' when clicked on by the person coming into the site
	// $("#cover-photo a").bind(
	// 	"click",
	// 	function () {
	// 		var linkHref = this.href;
	// 		$(this.parentNode).fadeTo(
	// 			250, 0.25,
	// 			function(){
	// 				window.location.href = linkHref;
	// 			}
	// 		);
	// 	return false;
	// 	}
	// );

	// fancybox gallery: http://fancybox.net/home
	$('a.popup').fancybox({
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	false,
		'autoScale'	    :   true,
		'titlePosition' :   'inside',
		'overlayShow'   :   true,
		'overlayOpacity':   0.9,
		'opacity'       :   true
	});

});


// notes:   1) Make certain to call this script in the HTML source before calling thickbox.js as the source changes need to take place before Thickbox is initialized.
//          2) Uncomment line 72 when upgrading Thickbox to make current images compatible with Thickbox.  Current Thickbox script is hacked to use class="popup" instead of class="thickbox".