(function($) {

/**
 * For anchors with local targets, we scroll "smoothly" to the target. This will
 * also allow CSS selectors to be used, in-place of simple targets (eg. 
 * &lt;a href="h1:eq(0)"&gt;Go somewhere&lt;/a&gt).
 */
$.fn.smoothScroll = function() {
	this.each(function() {
		var source = $(this);
		source.click(function() {
			var target = $(source.attr("href"));
			if (target.length == 0)
				return;
			
			try {
				$.scrollTo(target, { duration: 500 });
			} catch (e) {
				if (typeof console != "undefined" && console.log)
					console.log(e);
				return;		// Let click fall through if scrollTo fails.
			}
			
			return false;
		});
	});
};


$(document).ready(function() {
	
	// Links within a page with .smooth-scroll class will have a nicer motion
	$("a.smooth-scroll[href^='#']").smoothScroll();
	
	// Forms with class autosubmit will be automatically submitted when any
	// input element emits a change event.
	$("form.autosubmit").each(function() {
		var form = $(this);
		form.find("input[type='submit']").hide();
		form.find(":input").change(function() {
			form.submit();
		});
	});
	
	
	$(".showHoverableOnHover").live("mouseover", function() {
      $(this).find(".hoverable").slideDown(function() {
        var hoverable = $(this);
        $("body").one("click", function() { hoverable.fadeOut(); });
      });
    });
    $(".showHoverableOnHover .hoverable").live("click", function(e) {
    	e.stopPropagation();
    });

    $(".pmid.pretty-on-hover").prettyPubMedId({
    	format: "<span class='showHoverableOnHover'><em class='title'><a href='http://www.ncbi.nlm.nih.gov/pubmed/%Id'>%Title</a></em> (%Id)<div class='hoverable'><span class='authors'>%AuthorList</span>. <a href='http://www.ncbi.nlm.nih.gov/pubmed/%Id'><em class='title'><strong>%Title</strong></em></a> <span class='source'>%Source</span>. <span class='issue'>%SO</span>. PubMed PMID: <span class='pmid'>%Id</span>.</div></span>"
    });
});

/**
 * Returns a decorated version of the function f that will warn FireBug users
 * that the function has been deprecated. If the function is named (ie. has
 * been declared as "function NAME(...", then the name can be deduced 
 * automatically for the warnings, otherwise a human readable name has to be
 * given as well, via the fName parameter.
 * 
 * @param f A function to deprecated
 * @param fName A human-readable name for the function (Optional).
 * @return A function that decorates f
 */
function deprecated(f, fName) {
	if (typeof console == "undefined")
		return f;
	
	// Determine the function's name
	fName = fName || "*unknown function*";
	var m = /^function (\w*)\(/.exec(f.toString());
	if (m && m.length > 1 && m[1])
		fName = m[1];
	
	// Each call to the function returned will log a warning
	return function() {
		if (typeof console != "undefined" && console.log && console.trace) {
			console.group("Deprecated function: " + fName);
			console.trace();
			console.groupEnd();
		}
		return f.apply(this, arguments);
	}
}


// deprecated should be global. 
window.deprecated = deprecated;

})(jQuery);
