/*
#####################################################
# 
# Neil Lerner Kitchens
# Global JavaScript utilities library
#
# Aggregated by Phenotype (phenotype.net)
# See source comments for individual credits
#
#####################################################
*/

	////////////////////////////////////////////////////////////////////
	//	Function definitions
	////////////////////////////////////////////////////////////////////
	
	/* 
	externalLinks()
	
	Function to open links to external sites in a new browser window
	(target attribute not allowed by XHTML 1.0 Strict)
	
	(c) SitePoint.com 2003
	(http://www.sitepoint.com/article/standards-compliant-world/)
	-----------------------------------------------------------------------
	*/
	function externalLinks() {
		if (!document.getElementsByTagName) return;
		var anchors = document.getElementsByTagName("a");
		for (var i=0; i<anchors.length; i++) {
			var anchor = anchors[i];
			if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
				anchor.target = "_blank";
		}
	} // End externalLinks()

	/* Example usage:

	<a href="#" rel="external">Link</a>
	*/
	
	/* 
	clickClear()
	
	Function to clear a text input field on click (eg search box)
	-----------------------------------------------------------------------
	*/
	function clickClear(thisfield, defaulttext) {
		if (thisfield.value == defaulttext) {
			thisfield.value = "";
		}
	} // End clickClear()
	
	/* 
	clickRecall()
	
	Function to write default text to a text input field (eg search box)
	-----------------------------------------------------------------------
	*/
	function clickRecall(thisfield, defaulttext) {
		if (thisfield.value == "") {
			thisfield.value = defaulttext;
		}
	} // End clickRecall()

	/* Example usage:

	<input type="text" value="Value" onclick="clickclear(this, 'Value')" onblur="clickrecall(this,'Value')" />
	*/
	
	/* 
	randomBackgroundImage()
	
	Function to randomly select background image
	-----------------------------------------------------------------------
	*/
	function randomBackgroundImage(backgrounds, imagePath, target) {
		// Select background from array
		var backgroundNumber = Math.floor(Math.random() * backgrounds.length);
		var backgroundURL = backgrounds[backgroundNumber];
		// Apply background
		jQuery(target).attr({src: imagePath + backgroundURL});
		
		// Resize image
		var $winWidth = jQuery(window).width();
		var $winHeight = jQuery(window).height();
		
		if ($winWidth > $winHeight) {
			jQuery(target).css({
				width: $winWidth,
				height: "auto"
			});
		} else {
			jQuery(target).css({
				width: "auto",
				height: $winHeight
			});
		}
		
		// Bind to window resize
		jQuery(window).bind("resize", function(){
			var $winWidth = jQuery(window).width();
			var $winHeight = jQuery(window).height();
			
			if ($winWidth > $winHeight) {
				jQuery(target).css({
					width: $winWidth,
					height: "auto"
				});
			} else {
				jQuery(target).css({
					width: "auto",
					height: $winHeight
				});
			}
		 });

	} // End randomBackgroundImage()