// Define the Hesitate event. This event fires if a person has
// moused-over the given element and paused for the given duration
// without clicking.
//
// NOTE: To create a custom duration, update the duration
// property (in milliseconds):
// jQuery.event.special.hesitate.duration.
(function( $ ){
 
	// When the given element is entered, we need to set up the
	// timeout that will trigger the Hesitate event after the
	// appropriate pause duration.
	var prepareHesitate = function( event ){
		var target = $( this );
 
		// Store the timeout with the element's data so that we
		// can clear the timeout if the user acts within the
		// given duration.
		target.data(
			"hesitate.timer",
			setTimeout(
				function(){
					// Remove any hestitate context.
					removeHesitate( event );
 
					// Trigger the event handlers.
					target.triggerHandler( "hesitate" );
				},
				$.event.special.hesitate.duration
			)
		);
	};
 
 
	// When the user mouses out of the given element or clicks
	// on the given element, we want to remove the hesitation
	// timer.
	var removeHesitate = function( event ){
		// Remove the timer and its data key.
		removeHesitateTimer( $( this ) );
	}
 
 
	// This removes the hestation timer on the given element.
	var removeHesitateTimer = function( target ){
		// Clear the timer.
		clearTimeout(
			target.data( "hesitate.timer" )
		);
 
		// Remove the timer key.
		target.removeData( "hesitate.timer" );
	}
 
 
	// ------------------------------------------------------ //
	// ------------------------------------------------------ //
 
 
	// Define our special event, "hestiate":
	$.event.special.hesitate = {
 
		// This method gets called the first time this event
		// is bound to THIS particular element. It will be
		// called once and ONLY once for EACH element.
		setup: function( eventData, namespaces ){
			// Bind the three event handlers that we are going
			// to need to make sure this event fires correctly.
			$( this )
				.bind( "mouseenter", prepareHesitate )
				.bind( "mouseleave", removeHesitate )
				.bind( "click", removeHesitate )
			;
 
			// Return void as we don't want jQuery to use the
			// native event binding on this element.
			return;
		},
 
		// This method gets called when this event us unbound
		// from THIS particular element.
		teardown: function( namespaces ){
			var target = $( this );
 
			// Remove bound events.
			target
				.unbind( "mouseenter", prepareHesitate )
				.unbind( "mouseleave", removeHesitate )
				.unbind( "click", removeHesitate )
			;
 
			// We also want to remove the timer in case there is
			// one in progress.
			removeHesitateTimer( target );
 
			// Return void as we don't want jQuery to use the
			// native event binding on this element.
			return;
		},
 
		// This is the duration a user must pause over the
		// target element without acting before the hesitate
		// event will be triggered.
		duration: (2 * 1000)
 
	};
 
})( jQuery );
