/**
* @desc Tab plugin - part of the Codereck jQuery Plugin Set.
*
* @copyright © Timothy Haroutunian 2007-2008, All Right Reserved
*
* @license Dual MIT/GPL
*/
(function($)
{
	//create the codereck scope
	$.cr = $.cr || {};

	//initialization of codereck tabs
	$.fn.tabs = function(options)
	{
		var temp = new $.cr.tabs(this, options);
		return this;
	}

	//codereck tab class
	$.cr.tabs = function(element, options)
	{
		var self = this;

		self.element = element;
		self.options = $.extend(
		{
			//element id and class names(css must match)
			element_id: self.element.attr('id'),
			tabs_id: null,
			ul_class: 'cr_tabs',
			content_class: 'cr_tab_content',
			selected_tab_class: 'cr_tab_selected',
			unselected_tab_class: 'cr_tab',
			loading_animation: 'cr_tab_loading',
			loading_text: 'cr_tab_loading_text',
			tab_id: null,
			content_id: null,

			//callbacks
			before_load: function(){},
			after_load: function(){},
			add: function(){},
			remove: function(){},
			trigger: function(){},
			enable: function(){},
			disable: function(){},
			hide: function(){},
			show: function(){}
		},
		options);


		if(self.options.tabs_id == null)
		{
			self.options.tab_set_id = self.element.attr('id');
		}
		else
		{
			self.options.tab_set_id = self.options.tabs_id;
		}

        // save instance for later
        $(element).data($.cr.tabs.instance_key, this);

        //hide all div elements inside this data
        var children_selector = null;
        if(self.options.content_id == null)
        {
        	self.options.children_selector = '#' + self.options.element_id + ' > div';
			$(self.options.children_selector).css('display', 'none');
			children_selector = 'div';
		}
		else
		{
        	self.options.children_selector = '#' + self.options.element_id + ' div#' + self.options.content_id + ' > div';
			$(self.options.children_selector).css('display', 'none');
			children_selector = '#' + self.options.content_id + ' div';
		}

		//get the ul element that contains the list of tabs
		var ul_element = self.element.children('ul:eq(0)');
		var list_items = ul_element.children('li');

		//add content box after ul list - current only used for ajax calls but future to use it for all content
		ul_element.after('<div class="' + self.options.content_class + '"></div>');
		$(self.options.children_selector).addClass(self.options.content_class);

		//all css classes to necessary elements
		ul_element.addClass(self.options.ul_class);
		list_items.addClass(self.options.unselected_tab_class);

		//click event - we do want to use livequery but for some reason trigger does not work with livequery.
		$('#' + self.options.tab_set_id).children('ul').children('li').children('a').each(function(count)
		{
			$(this).click(function()
			{
				var id = $(this).attr('href');

				//before loading callback function
				self.options.before_load();
				self.element.children('.cr_tab_content:first').empty();
				$('#' + self.options.tab_set_id).children('div.' + self.options.content_class).css('display', 'none');

				//add select class tag to this link and remove from all other
				$(this).parent('li').parent().children('li.' + self.options.selected_tab_class).removeClass(self.options.selected_tab_class);
				$(this).parent('li').addClass(self.options.selected_tab_class);

				if(id.substring(0, 5) !== 'ajax:')
				{
				   /*self.element.children('.cr_tab_content').append(self.element.children('#' + id).clone(true));

					//after load callback function*/
					$(self.options.children_selector).css('display', 'none');
					$(self.options.children_selector + '[id=' + id + ']').css('display', 'block');
					self.options.after_load();
				}
				else
				{
	                //start animation
					self.element.children('.cr_tab_content:first').addClass(self.options.loading_animation);
					self.element.children('.cr_tab_content:first').append('<div class="' + self.options.loading_text + '">Loading</div>');

					//get the page to load remotely
					var page = id.split(':')[1];

					$.ajax(
					{
						'type': 'post',
						'url': page,
						'success': function(message)
						{
							//stop animation
							self.element.children('.cr_tab_content:first').removeClass(self.options.loading_animation);
							self.element.children('.cr_tab_content:first').html(message);
						},
						'error': function (XMLHttpRequest, textStatus, errorThrown)
						{
							//stop animation
							self.element.children('.cr_tab_content:first').removeClass(self.options.loading_animation);
							self.element.children('.cr_tab_content:first').html('Error loading page.');
						},
						'complete': function()
						{
							//after load callback function
							self.options.after_load();
						}
					});

					self.element.children('.cr_tab_content:first').css('display', 'block');
				}
				return false;
			});
		});

		//trigger first tab
		self.trigger_tab(1);
	}

    //static information
    $.cr.tabs.instance_key = 'codereck_tab_instance';
    $.cr.tabs.get_instance = function(el)
    {
        return $(el).data($.cr.tabs.instance_key);
    };

    //add chainable method to element selector with tabs
	$.each(['trigger_tab'], function(i, method)
	{
		$.fn[method] = function()
		{
			var args = arguments;
			return this.each(function()
			{
				var instance = $.cr.tabs.get_instance(this);
				instance[method].apply(instance, args);
			});
		}
	});

	$.extend($.cr.tabs.prototype,
	{
        //trigger a click even on a certain tab
		trigger_tab: function(tab)
		{
			if(typeof(tab) === 'number')
			{
				$('#' + this.options.tab_set_id + ' a').slice(tab - 1, tab).trigger('click');
			}
			else if(typeof(tab) === 'string')
			{
				$('#' + this.options.tab_set_id + ' a[@href=' + tab + ']').trigger('click');
			}
		},

		add: function(position, title, content, rel)
		{
			//add the list element is the correct position
		},

		remove: function(tab)
		{
			//switch to the first tab(or second tab is first tab is being removed

			//remove the list element

			//remove the div with the tab content
		},

		enable: function(tab)
		{
			//check to see if tab is disabled

			//add click event back to the tab and make text black again
		},

		disable: function(tab)
		{
			//check to see if tab is enabled

			//remove click event from and make the text gray
		},

		show: function(tab)
		{
            if(typeof(tab) === 'number')
			{
				$('#' + this.options.tab_set_id + ' li').slice(tab - 1, tab).show();
			}
			else if(typeof(tab) === 'string')
			{
				$('#' + this.options.tab_set_id + ' a[@href=' + tab + ']').parent('li').show();
			}
		},

		hide: function(tab)
		{
            if(typeof(tab) === 'number')
			{
				$('#' + this.options.tab_set_id + ' li').slice(tab - 1, tab).hide();
			}
			else if(typeof(tab) === 'string')
			{
				$('#' + this.options.tab_set_id + ' a[@href=' + tab + ']').parent('li').hide();
			}
		}
	});
})(jQuery);