// JavaScript Document
/**
 * AjaxTabs
 * Tabs object so when clicked, the body content is loaded via AJAX
 *
 * @uses jquery-1.3.2
 */
function AjaxTabs(url, urlPrefix, tabsContainer, tabBody, selectedClass, deselectedClass)
{
	/**
	 * @var string
	 */
	this.url = url;
	
	/**
	 * @var string
	 */
	this.urlPrefix = urlPrefix;
	
	/**
	 * @var HTML_Element
	 */
	this.tabsContainer = $(tabsContainer);
	
	/**
	 * @var HTML_Element
	 */
	this.tabBody = $(tabBody);
	
	/**
	 * @var string
	 */
	this.selectedClass = selectedClass;
	
	/**
	 * @var string
	 */
	this.deselectedClass = deselectedClass;
	
	/**
	 * _init
	 *
	 * Initialisation method.
	 *
	 * @return void
	 */
	this._init	= function()
	{
		var t = this;
		
		if ($(this.tabBody).find('.paginator a').attr("href")) {
			$(this.tabBody).find('.paginator a').each(function(){
				var href, url = t.url + t.getSelectedTabName();
				
				href	= $(this).attr('href');
				url		+= href.substring(href.indexOf('?'));
				
				$(this).attr("href", url);
			});
			
			this.initPagination();
		}
		
		this.initTabClick();
	}
	
	/**
	 * initTabClick
	 *
	 * Tabs initialisation method. Locates all the link tags within the tabs container
	 * and set a trigger so when clicked, they load an AJAX url
	 *
	 * When a link is clicked, it returns false
	 *
	 * @return void
	 */
	this.initTabClick	= function()
	{
		var t = this;
		
		$(this.tabsContainer).find('a').click(function(){
			var a = this, url = t.url;
			
			$(t.tabBody).fadeTo(250, 0);
			
			url	+= $(this).attr('href').replace(t.urlPrefix, "");
			$(t.tabBody).load(url, function(){
				t.clearTabs();
				$(a).parent('div').removeClass(t.deselectedClass);
				$(a).parent('div').addClass(t.selectedClass);
				$(this).fadeTo(250, 100);
				t.initPagination();
			});
			
			return false;
		});
	}
	
	/**
	 * initPagination
	 * 
	 * Initialises the pagination. Checks for a pagination helper,
	 * and when clicked do an AJAX load of the content
	 *
	 * When a link is clicked, it returns false
	 *
	 * @return void
	 */
	this.initPagination	= function()
	{
		var t = this;
		
		$(this.tabBody).find('.paginator a').click(function(){
			$(t.tabBody).fadeTo(250, 0);
			$(t.tabBody).load($(this).attr('href'), function(){
				$(this).fadeTo(250, 100);
				t.initPagination();
			});
			return false;
		});
	}
	
	/**
	 * clearTabs
	 * 
	 * Resets the CSS attributes of the tabs container
	 * links to deselected
	 *
	 * @return void
	 */
	this.clearTabs	= function()
	{
		var t = this;
		
		$(this.tabsContainer).children('div').each(function(){
			$(this).removeClass(t.selectedClass);
			$(this).addClass(t.deselectedClass);
		});
	}
	
	/**
	 * Returns the name of the currently selected tab name
	 * 
	 * @return string
	 */
	this.getSelectedTabName	= function()
	{
		return $(this.tabsContainer).find("." + this.selectedClass).find('a').attr('href').replace(this.urlPrefix, "");
	}
	
	/**
	 * Initialise the _init method
	 */
	this._init();
}
