// ---------- Tabs ----------

RHG.Classes.Tabset = new RHG.Class();
RHG.Classes.Tabset.selectors = {
	"panelsSelector": ".tabset-panel",
	"panelTitleSelector": ".tab-title"
};
RHG.Classes.Tabset.getState = function(classFunctionInstance) {
	return {
		"defaultTab": classFunctionInstance.currentTabIndex
	};
}
RHG.Classes.Tabset.classFunction = function(configObject) {
	var classFunctionPub = {};
	var thisClass = RHG.Classes.Tabset;
	
	var showTabEventFunction = (typeof configObject.showTabEventFunction != 'function') ? function(tab){} : configObject.showTabEventFunction;
	var selectors = thisClass.selectors;
	var targetJq; // Main HTML element jQuery wrapped 
	classFunctionPub.tabArray = []; // Storage for all the tabs
	classFunctionPub.currentTabIndex = 0;
	var defaultTab = (configObject.defaultTab == undefined) ? 0 : parseInt(configObject.defaultTab);
	var tabsListJq; // UL containing tabs
			
	var checkHashInside	= function(event) {
		var loadEvent = true;
		if (event) {
			if (event.type == "hashchange") {
				var loadEvent = false;
			}			
		}


		
		// if theres a hash in the url that links inside the tabset, open the appropriate tab and scroll to it
		if (window.location.hash) {
			jQuery.each(classFunctionPub.tabArray, function(i){
				
				var hashTargetJq = null;
				var hashTargetOffset = null;
				
				// Look for the hash inside the tab content
				var hashInsideJq = classFunctionPub.tabArray[i].panelElemJq.find(window.location.hash);		
				if (hashInsideJq.length > 0) {
					classFunctionPub.tabArray[i].show();
					hashTargetJq = hashInsideJq.eq(0);
					hashTargetOffset = hashTargetJq.offset().top;
										
					jQuery('html, body').animate({scrollTop: hashTargetOffset}, 1000);
					hashTargetJq.focus();
					
				// try the tab itself
				} else if ('#'+classFunctionPub.tabArray[i].hashString == window.location.hash) {
					classFunctionPub.tabArray[i].show();					
					hashTargetJq = classFunctionPub.tabArray[i].panelElemJq;
					hashTargetOffset = tabsListJq.offset().top;

					jQuery('html, body').animate({scrollTop: hashTargetOffset}, 1000);
					hashTargetJq.focus();
				}
			})
		}		
	}
			
	//CLASSES
	var Tab = function(panelElem, index) {
		// Creates a Tab button for a panel and makes it live
		// Provides externl control with functions and variables defined as part of TabPub
				
		// Local variables to Tab
		var TabPub = {};
		var panelElemJq = jQuery(panelElem);				
		var panelTitleJq = panelElemJq.find(selectors.panelTitleSelector);	
		TabPub.hashString = panelTitleJq.attr('id');
		var enabledTabElemJq = jQuery('<li><a href="#">'+panelTitleJq.text()+'</a></li>');
		var disabledTabElemJq = jQuery('<li class="current"><strong>'+panelTitleJq.text()+'</strong></li>');
		var callbackTitleText = panelTitleJq.text().replace(/\s\(\d\)/g,'').replace(/ /g,'-');
		panelTitleJq.remove();
		tabsListJq.append(enabledTabElemJq);
		var tabElemContainerJq = enabledTabElemJq.parent();

		var enable = function() { // Makes the tab clickable				
			if (disabledTabElemJq.parent()[0] == tabElemContainerJq[0]) { // Makes sure the element we want to swap out is actually in the DOM
				RHG.Utilities.PIEDetach(disabledTabElemJq[0]);
				RHG.Utilities.PIEDetach(disabledTabElemJq.find("strong")[0]);
				disabledTabElemJq.replaceWith(enabledTabElemJq);
			}
			RHG.Utilities.PIEAttach(enabledTabElemJq.find("a")[0]);
		}
		
		var disable = function() { // Makes the tab unclickable and gived it the apperance of being the current tab
			if (enabledTabElemJq.parent()[0] == tabElemContainerJq[0]) { // Makes sure the element we want to swap out is actually in the DOM
				RHG.Utilities.PIEDetach(enabledTabElemJq.find("a")[0]);
				enabledTabElemJq.replaceWith(disabledTabElemJq);
			}				
			RHG.Utilities.PIEAttach(disabledTabElemJq[0]);
			RHG.Utilities.PIEAttach(disabledTabElemJq.find("strong")[0]);
		}

		TabPub.hide = function() { // Hide the panel (and enable the tab)
			if (TabPub.visible == true) { 
				enable()
				if (panelElemJq != undefined) {
					panelElemJq.addClass('hidden'); // .hidden sets display: none
				}
				TabPub.visible = false;
				enabledTabElemJq.click(function(evt){ // The click even has to be added each time as it gets removed when er remove the HTML element dfrom the page
					classFunctionPub.showTab(TabPub);
					return false
				});
			}
		}
		TabPub.show = function(firstLoad) { // Show the panel (and diable the tab)
			disable();
			
			if (panelElemJq != undefined) {
				panelElemJq.removeClass('hidden');
			}
			
			jQuery.each(classFunctionPub.tabArray, function(i) {
				if (classFunctionPub.tabArray[i] != TabPub) {
					classFunctionPub.tabArray[i].hide();
				}
			});		
			
			classFunctionPub.currentTabIndex = TabPub.tabIndex;
			showTabEventFunction(TabPub);						
			TabPub.visible = true;
			
			if (configObject.onTabClick)
			{
			    if(firstLoad != true) configObject.onTabClick(callbackTitleText);
			}
		}
			
		// Initial setup
		TabPub.visible = true;
		TabPub.hide();
		TabPub.panelElemJq = panelElemJq;	
		TabPub.tabIndex = index;
		
		return TabPub; // exposes only the TabPub object to external code
	}
	
	classFunctionPub.showTab = function(tab) {
		// Hides all but the specified tab in tabArray and shows the specified one
		tab.show();					
	}

	// INIT	
	classFunctionPub.init = function() {
		targetJq = jQuery(configObject.uniqueTargetNodeOrSelector)
		var panelsJq = targetJq.find(selectors.panelsSelector)
		
		// Create structure
		tabsListJq = jQuery('<ul class="tabset-tabs"></ul>');
		panelsJq.eq(0).before(tabsListJq);
				
		// for each tab found inside targetJq wrap it in a Tab Class
		panelsJq.each(function(i){		
			classFunctionPub.tabArray.push(new Tab(panelsJq[i], i))
		});
		
		targetJq.addClass('js-active');
		
		classFunctionPub.tabArray[defaultTab].show(true);					
					
		checkHashInside();
		jQuery(window).hashchange(checkHashInside);
	}

	return classFunctionPub;
}
