// plugin to ajax in content into an element

(function($)
{
	$.fn.ajaxContent = function(callback, options)
	{
		var main_opts = $.extend({}, $.fn.ajaxContent.defaults, options);
		
		return this.each(function(targetCounter)
		{
			var _$this = $(this);
				
			// build element specific options
			// if metadata plugin is present, extend main_opts, otherwise just use main_opts
			var opts = $.meta ? $.extend({}, main_opts, $this.data()) : main_opts;
				
			//simplify the vars into regular globals
			var errorMsg = opts._errorMsg;
			var startMsg = opts._startMsg;
			var ajaxTarget = opts._ajaxTarget;
			
			_$this.click(function(event)
			{
				event.preventDefault(); //added 24nov09
				
				var myUrl = _$this.attr('href');
				
				//event listener to perform function on ajaxStart
				_$this.ajaxStart(function()
				{
					$(ajaxTarget).html('<p>'+startMsg+'</p>');
				});
				
				$.ajax({
					url: myUrl,
					error: function()
					{
						$(ajaxTarget).html('<p class="error">' + errorMsg + '</p>');
					},
					success: function(html)
					{
						$(ajaxTarget).html(html);
					},
					complete: function()
					{
						if($.isFunction(callback))
						{
							callback();
						}
					}
				});					  
			});
		});
	};
	
	//
	//plugin defaults
	//
	$.fn.ajaxContent.defaults =
	{
		_errorMsg: 'The ajax request has failed.',
		_startMsg: 'Loading...',
		_ajaxTarget: '.ajaxTarget'
	}; 
})(jQuery);