/**
 * jquery.utilities.js (Utility functions)
 * @version 2.0
 * @author Andrew Ramsden <irama.org>
 * @see http://irama.org/web/dhtml/jquery/utilities/
 * @license Common Public License Version 1.0 <http://www.opensource.org/licenses/cpl1.0.txt>
 */
(function($) {// start closure
	/**
	 * Gets or sets the anchor portion of the URL (after the #)
	 * @param String hash The value to set the anchor portion of the URL.
	 *                    If not set, function will return the current value
	 * @param Boolean jumpToAnchor Whether to allow the browser window
	 *        to scroll to place the anchor specified (targeted by the 
	 *        hash).
	 */
	$.frag = function ( /* optional */ hash, /* optional - default = false */ jumpToAnchor)
	{
		// Initialise variables
			hash = (typeof hash !== 'undefined')?hash:null;
			jumpToAnchor = (typeof(jumpToAnchor) != 'undefined')?(jumpToAnchor)?true:false:false;
		
		if (hash === null) {
			// get
			return window.location.hash.replace('#','');
		} else {
			// set
			if (jumpToAnchor) {
				// Just reset hash, allow browser to move
					window.location.hash = hash;
			} else {
				// If nothing has changed, just quit now
					if (hash == $.frag()) {
						return true;
					}
				
				// get current scroll position
					currentY = $(window).scrollLeft();
					currentX = $(window).scrollTop();
				
				// Reset hash
					window.location.hash = hash;
				
				// Correct the scroll position
					$(window).scrollTop(currentX);
					$(window).scrollLeft(currentY);
			}
			return true;
		}
	};
	
	/**
	 * plugins
	 */
	$.fn.extend({
		
		/**
		 * @author Brandon Aaron <brandonaaron.net>
		 * @see http://blog.brandonaaron.net/2007/06/25/where-is-the-hasclass-method/
		 */
		hasClass : function(c) {
			return this.is('.'+c);
		},
		
		
		/**
		 * Get the the first and biggest heading for the container element
		 * @version 2.0
		 * @author Andrew Ramsden <irama.org>
		 */
		firstHeading : function() {
			heading = $(this).children('h1:lt(1)');
			if (heading.text()!=='') { return heading; }
			
			heading = $(this).children('h2:lt(1)');
			if (heading.text()!=='') { return heading; }
			
			heading = $(this).children('h3:lt(1)');
			if (heading.text()!=='') { return heading; }
			
			heading = $(this).children('h4:lt(1)');
			if (heading.text()!=='') { return heading; }
			
			heading = $(this).children('h5:lt(1)');
			if (heading.text()!=='') { return heading; }
			
			heading = $(this).children('h6:lt(1)');
			if (heading.text()!=='') { return heading; }
			
			// nothing return blank heading
			return $('<h2></h2>');
		}
	});
		
})(jQuery); // end closure