//
// Heartbeat --------------------------------------------------------------
//

(function($) {

$.fn.heartbeat = function( options )
{
	var opts = $.extend( {}, $.fn.heartbeat.defaults, options );	
	return this.each( 
		function()
		{ 
			var $this = $(this);
			var o = $.meta ? $.extend( {}, opts, $this.data() ) : opts;			
			$.fn.heartbeat.remove( $this, o );
			$.fn.heartbeat.add( $this, o );
		}
	);
};

$.fn.removeHeartbeat = function( options )
{
	var opts = $.extend( {}, $.fn.heartbeat.defaults, options );	
	return this.each( 
		function()
		{ 
			var $this = $(this);
			var o = $.meta ? $.extend( {}, opts, $this.data() ) : opts;			
			$.fn.heartbeat.remove( $this, o );
		}
	);
};	

$.fn.heartbeat.defaults = 
{
	timerIdAttr: "heartbeatTimerId",
	timeout: 5000,
	callback: function() 
	{
		if( window.console && window.console.log ) 
		{ 
			console.log( "heartbeat default callback" ) 
		} 
		else
		{
			alert( "heartbeat default callback" );
		}
	}
};	

$.fn.heartbeat.add = function( obj, options )
{
	$.data( 
		obj, 
		options.timerIdAttr, 
		setInterval( options.callback, options.timeout ) 
	);
	return this;
};

$.fn.heartbeat.remove = function( obj, options )
{
	var timerId = $.data( obj, options.timerIdAttr );
	clearInterval( timerId );
	$.removeData( obj, options.timerIdAttr );
	return this;
};

})(jQuery);	