/*
 * ResourceDisplay - jQuery Plugin
 *
 * Progressive Computing LLC.
 * jQuery 1.4.x
 *
 * Date: May 23, 2011
 *
 * EXAMPLE INITIALIZATION:
 * ----------------------------------------------
 * $(document).ready(function() {
 *            var callBack = function(){
 *                   // callBack scope is the Sub Menu DIV
 *                   alert(this.innerHTML)
 *            }
 *
 *            $("#resources").ResourceDisplay({defaultMenuItem:0, callBack:callBack});    
 * });
 */
(function($) {

	$.fn.ResourceDisplay = function(options) {
		/**
		 * @options
		 * @defaultMenuItem {int}The tab screen to display at runtime
		 * @callBackOut {obj} An event handler when a tab is chosen
		 * @callBackIn {obj} An event handler when a tab is chosen		 
		 */
		$.fn.ResourceDisplay.defaultOptions = {
			   defaultMenuItem:0,
			   callBackOut:null,
			   callBackIn:null
		}

		 // A member variable used to store the Tab container element. This will be the
		 // DIV element with the classname of 'tabControl' assigned.
		var m_controlWrapper = $(this),
		
			// If an options object is sent at runtime merge default options with user specified
			// options into a single options object. If none specified use default options.
			m_options = (typeof(options) == 'object') ?
			$.extend({}, $.fn.ResourceDisplay.defaultOptions, options) : $.fn.ResourceDisplay.defaultOptions,

			m_activeSlide = m_options.defaultSlide;

		// Assign Events To Selectors
		$('div h1', m_controlWrapper).each(function(i){

			var elm = $(this);
			
			elm.click(function(){
				closeItem($(this).parent());
			});
			
			if(i == m_options.defaultMenuItem){
				// Set menu item selected state
				elm.parent().addClass('selected');
			}
		
		});

		function closeItem(p_selected){

			var nextIndex = m_controlWrapper.children().index(p_selected),
				
				curItemContent = $('.selected ul:first', m_controlWrapper);
				
				callBack = function(){
					// Call function if callBackOut is specified
					if(m_options.callBackOut != null){
							m_options.callBackOut.apply(m_controlWrapper, arguments);
					}
					
					$('div.selected', m_controlWrapper).removeClass('selected');
				
					showItem(nextIndex);
				};
				
				// Fade Out Menu
				curItemContent.slideUp('normal', callBack);
		}
		
		function showItem(p_index){
				
				// Assign selected state to menu item
			var menuItemSelected = m_controlWrapper.children().eq(p_index).addClass('selected'),
				
				// Get Content Item Based On Index
				contentItemSelected = m_controlWrapper.children().eq(p_index),
				
				// Get UL reference for displayed content
				contentDisplay = $('ul:first', contentItemSelected);
		
				// Create Callback Function to be called after animation
				callBack = function(){

					// Call function if callBackOut is specified
					if(m_options.callBackOut != null){
						m_options.callBackIn.apply(m_controlWrapper, arguments);
					}
					
					// Add selected state to content item
					contentItemSelected.addClass('selected');
				};
		
			// Update Menu Item to Selected State
			menuItemSelected.addClass('selected');

			// Fade content Item In and Assign Callback	
			contentDisplay.slideDown('slow', callBack);
		}
	}
})(jQuery);

