/***********************************************************************
Elsevier "Smart Tabs"

This object makes the use of the Dojo tab container widget "smarter" in
that the last selected tab will be retained as the active one if/when
the page containing the tabs is redrawn. 

It uses a cookie to remember the last selected tab.
	
Note:  You must assign a unique id to each of your tabs (i.e. to each
       child of the TabContainer), since this is what gets stored in the
       cookie.	
************************************************************************/	
	
function elsSmartTabs(tabContainerId, initialTab) {
	
	// Include the elsCookie javascript object: (for now include via page)	
	// document.write('<script type="text/javascript" src="elsCookie.js"></script>');		
		
	//////////////////////////////////////////////////////////
	// Overridable Settings:		
		
	// The html id of the TabContainer div tag as it resides 
	// on the current page	
	this.tabContainerId = tabContainerId;

	// A (client) function to be called when the user selects on a tab
	this.onSelect = null; 

	// A (client) function to be called when the active tab changes
	this.onChange = null; 

	// The name to use for the cookie (default is "selectedTab"):
	this.cookieName = 'SelectedTab';		
	
	// By default prefix the cookieName with the TabContainer id,
	// to ensure uniqueness if/when multiple TabContainers exist on a page:	
	this.prefixCookieName = true;
	
	// The cookie object (normally created automatically based on the above):	
	this.elsCookie = null;	
						
	//////////////////////////////////////////////////////////
	// Not Overridable:
	
	// The root div element for the TabContainer:
	var tabContainerDiv;
	
	// The actual dojo Tab Container widget itself:
	var tabContainerWidget;	
	
	// Which tab is active/forward:		
	
	var selectedTab = '';	
	if(initialTab && initialTab != 'none') {
		selectedTab = initialTab;
	}
				
	// Get the cookie name to use, according to the elsSmartTab's 
	// configuration settings:	
	this.getCookieName = function ()
	{
		var theCookieName = '';
		if(this.prefixCookieName) {
			theCookieName = this.tabContainerId;
		}
		theCookieName = theCookieName + this.cookieName;
		
		return theCookieName;		
	};		

	// Find the elsSmartTabs object for the TabContainer containing
	// the given element.		
	function getSmartTabs(elem)
	{	
		if(!elem) {
			return null;
		}

		var smartTabs =  null;		
		while(!smartTabs && elem && elem != document) {
			smartTabs = elem.getAttribute('smartTabs');
			if(!smartTabs) smartTabs = elem.smartTabs;			
			if(!smartTabs) {
				elem = elem.parentNode;
			}
		}
		
		return smartTabs;
	};	

	// A new tab has been clicked on	
	// (write which tab is active/forward to the cookie)		
	function tabSelected(selectChildEvt)
	{	
		if(!selectChildEvt || !selectChildEvt.domNode) {
			return;
		}
	
		selectedTab = selectChildEvt.domNode.id;
		if(!selectedTab) {
			selectedTab = selectChildEvt.widgetId;		
		}
		if(!selectedTab) {
			return;
		}
		
		// alert('In tabSelected: ' + selectedTab);		
		
		var theSmartTabs = getSmartTabs(selectChildEvt.domNode);
		if(!theSmartTabs) {
			return;
		}
	
		theSmartTabs.elsCookie.set(selectedTab);
		
		// Did they provide a custom onSelect handler? 
		if(theSmartTabs.onSelect != null)
		{
			theSmartTabs.onSelect(selectChildEvt, theSmartTabs, selectedTab, this);
		}				
	};
	
	// Make the specified tab the active one.	
	this.activateTab = function(selectedTab)
	{	
		var tabContainer = this.getTabContainer();
		if(!tabContainer) {
			return;		
		}
		// alert('The cookie indicated to active: ' + selectedTab);
		tabContainer.selectChild(selectedTab);	
		
		// And make sure the cookie knows about the change
		this.elsCookie.set(selectedTab);
		
		// Did they provide a custom onSelect handler? 
		if(this.onChange != null)
		{
			this.onChange(this, selectedTab);
		}	
						
	};		
	
	// Which is the currently active tab?	
	this.activeTab = function(selectedTab)
	{	
		var theActiveTab;
		var tabContainer = this.getTabContainer();
		if(tabContainer) {
			theActiveTab = tabContainer.selectedChild;
		}
		
		return theActiveTab;
	};			
						
	// "Activate" the tabs on the page.
	// Note:  Call activateTabs after you've created your elsSmartTabs object, 
	// and then overridden any of the configuration settings that you need to.		
	this.activateTabs = function ()
	{
		// Store away the tab container div tag
		tabContainerDiv = document.getElementById(this.tabContainerId);
		
		// Make ourselves accessible to the (non-object) callback 
		// function that's called when they click on a tab:		
		tabContainerDiv.smartTabs = this;  			
				
		// Install the callback which writes the cookie when they 
		// click on a tab:
		tabContainerWidget = dojo.widget.byId(this.tabContainerId);	
		if(tabContainerWidget) {				
     		dojo.event.connect(tabContainerWidget, 'selectChild', tabSelected);
     	}
										
		// Get which tab should initially be active/forward from the cookie:
		if(!this.elsCookie) {
			this.elsCookie = new elsCookie(this.getCookieName(),500);
		}
		
		// If we weren't told which tab to initially activate			
		if(!selectedTab || selectedTab == '') {			
			// Get the default active tab from the cookie:
			selectedTab = this.elsCookie.get();
		}
				
		if(selectedTab) {	
			// alert('The cookie indicated to active: ' + selectedTab);
			this.activateTab(selectedTab);												
		}		
	};	
	
	
	// Get the tab container widget.
	// Note:  In the RI (at least) sometimes the "activateTabs" method
	//        was failing to get the tabContainer so this was written
	//        to retry getting it each time it's referenced.		
	this.getTabContainer = function ()
	{	
		// If we've never successfully gotten it before:
		if(!tabContainerWidget) {
			// Try and get it
			tabContainerWidget = dojo.widget.byId(this.tabContainerId);	
			if(tabContainerWidget) {				
				// When we finally get it, install the callback which writes 
				// the cookie when they click on a tab:			
	     		dojo.event.connect(tabContainerWidget, 'selectChild', tabSelected);
	     	}
		}

		return tabContainerWidget;
	};	
}
	
