/*
 * ajax_paginate - jQuery Plugin
 * http://www.tree-nation.com
 *
 * Copyright (c) 2009 John Soms (tree-nation.com) 
 *
 * $Date: 2009-1-24 (Sat, 24 Jan 2009) $
 * $version: 1
 */

;(function(jQuery) 
{
	var opts;
	var pContClass;
	
	jQuery.fn.ajax_paginate = function(settings) 
	{
		opts		= jQuery.extend({}, jQuery.fn.ajax_paginate.defaults, settings);			
		pContClass	= $(this).parents('p').attr('class');
		if (pContClass == null)
			return false;
			
		//console.debug(pContClass);
		//console.debug($(this));	
			
		return this.each(function(e)
		{	
			// get jQuery version of 'this'
			var t	= jQuery(this);

			t.click(function(e)
			{	
				//console.debug(t);
				//e.preventDefault();
				var url		= t.attr('ajax');
				var hash	= t.attr('href');
				hash = hash.replace(/^.*#/, '');

				//jQuery.historyLoad(hash);
				load_content(url, t);			
				e.preventDefault();
			}); 
		});
	};
	
	jQuery.fn.ajax_paginate.defaults = {
		callback: 	null
	};

	function load_content(url, t)
	{
		if (t!==false)
		{
			var t_content	= t.parents(".ajaxBlock");
			var t_loading	= t_content.prev(".ajaxLoading");
		}
		else
		{
			var t_content	= jQuery(".ajaxBlock");
			var t_loading	= t_content.prev(".ajaxLoading");
		}

		jQuery.ajax(
		{			
			url:		url,
			//async:	false,
			type:		"POST",
			beforeSend: function() //show loading just when link is clicked
						{
							t_content.hide();
							t_loading.show();
						}, 
			complete: 	function() //stop showing loading when the process is complete
						{ 
							t_loading.hide();
						}, 
			success:	function(html)
						{	
							t_content.html(html); //show the html inside .content div
							t_content.show();
							if (jQuery.isFunction(opts.callback))								
								opts.callback();			
								
							jQuery("p[class='"+pContClass+"']").find('a').ajax_paginate({callback: opts.callback});
						}
		});	
	};
})(jQuery);