// Version 09.09.22

function TabSet(options)
{
	var i=0, tabSetDiv, tabSetNavDiv, tabSetUL, tabSetLI, tabSetLIA, tabSetContentDivContainer, tabSetContentDiv;
	var ajaxObj, tabSetContentIFrame, fileref, tabSetLISpan;
	
	this._parentID = 1;
	this.tabs = [];
	this._previousTab = null;
	this.activeTab = 0;
	this._height = '';
	
	if(!!options.parentID)
		this._parentID = options.parentID;
	else if(!!options.tabID && !!options.contentID)
	{
		this._tabID = options.tabID;
		this._contentID = options.contentID;
	}
		
	if(!!options.ontabchange)
		this.ontabchange = options.ontabchange;
		
	if(!!options.oncreatetab)
		this.oncreatetab = options.oncreatetab;
			
	if(!!options.height)
		this._height = options.height;
	
	this._history=false;
	if(!!options.history)
		this._history=true;
	
	this._theme = 'default';
	if(!!options.theme)
		this._theme = options.theme;
	
	this.setTheme(this._theme);
	
	//create main tabset container
	if(!!this._parentID)
	{
		tabSetDiv = document.createElement('div');
		tabSetDiv.className = 'TabSet';
	}
	
	//create div that holds all the tabs
	tabSetNavDiv = document.createElement('div');
	tabSetNavDiv.className = 'TabSetNav';
	
	//create the UL for the tabs
	tabSetUL = document.createElement('ul');
	
	//create the main content container that will be the home of all tab content divs
	tabSetContentDivContainer = document.createElement('div');
	tabSetContentDivContainer.className = 'TabSetContent';
	if(!!this._height && parseInt(this._height)!=0)
		tabSetContentDivContainer.style.height = parseInt(this._height)+'px';
	
	//set private variables so I can reference the nav and the tabset main container
	this._TabSetNav = tabSetNavDiv.appendChild(tabSetUL);
	
	if(!this._tabID)
		tabSetDiv.appendChild(tabSetNavDiv);
	else
		document.getElementById(this._tabID).appendChild(tabSetNavDiv);
	
	if(!this._contentID)
		this._TabSetContentDivContainer = tabSetDiv.appendChild(tabSetContentDivContainer);
	else
		this._TabSetContentDivContainer = document.getElementById(this._contentID).appendChild(tabSetContentDivContainer);
	
	if(this._parentID!=1)
		this._TabSet = document.getElementById(this._parentID).appendChild(tabSetDiv);
	
	for(tab in options.tabs)
		this.addTab(options.tabs[tab]);
	
	if(this._history)
	{
		addEvent(window, 'load', function()
		{
			var div;
			
			div=document.createElement('div');
			div.innerHTML='<iframe style="display:none" src="/tabset_history.html?'+ this.activeTab +','+ escape(this.tabs[this.activeTab].contentURL) +'" onload="this.onload_handler();"></iframe>';
			document.body.appendChild(div);
			
			this._historyIframe=div.childNodes[0];
			this._historyIframe.onload_handler=function()
			{
				var url, search, num, url;
				
				if(this._historyIframe.contentWindow)
					url=this._historyIframe.contentWindow.location;
				else
					url=this._historyIframe.location;
				
				if(url.toString().indexOf('?'+ this.activeTab +','+ escape(this.tabs[this.activeTab].contentURL))==-1)
				{
					search=url.search.replace(/^\?/, '');
					
					if(search.length > 0)
					{
						search=search.split(',');
						num=parseInt(search[0]);
						url=unescape(search[1]);
						this.tabs[num].contentURL=url;
						this.showTab(num, false, true);
					}
				}
			}.bind(this);
		}.bind(this));
	}
	
	this.tabs[0].reload();
	this.showTab(0, true);
}

TabSet.prototype.addTab=function(options)
{
	var i = this.tabs.length;
	this.tabs[i] = new this.Tab(options, this, i);
	
	if(!!this.oncreatetab)
		this.oncreatetab(this.tabs[i]);
	
	return this.tabs[i];
}

TabSet.prototype.close = function(i, newTab)
{
	var tabToOpen;
	
	this.tabs[i].li.style.display = 'none';
	this.tabs[i].contentDiv.style.display = 'none';
	
	if(!!this.tabs[i].destroyOnClose && this.tabs[i].destroyOnClose==true)
	{
		this._TabSetNav.removeChild(this.tabs[i].li);
		this._TabSetContentDivContainer.removeChild(this.tabs[i].contentDiv);
	}
	
	if(!!this.tabs[i].onclose)
		this.tabs[i].onclose(this.tabs[i]);
		
	if(!!newTab && !isNaN(newTab))
		this.showTab(newTab);
	else if(i == this.activeTab)
	{
		if(this._previousTab!==null && !this.tabClosed(this._previousTab))
			this.showTab(this._previousTab);
		else
		{
			for(j=i; j>-this.tabs.length; j--)
			{
				//Jarett says, "Don't ask." If you want to make the tabs loop around when you close the left-most tab then uncomment this line 
				//tabToOpen = (this.tabs.length - (Math.abs(j-this.tabs.length)%this.tabs.length)) % this.tabs.length;
				
				//If you want the tabs to move right, like firefox, use the following line
				tabToOpen = Math.abs(j);
				if(!this.tabClosed(tabToOpen))
				{
					this.showTab(tabToOpen);
					break;
				}
			}
		}
	}
}

TabSet.prototype.showTab = function(i, firstLoad, ignoreHistory)
{
	if(this.activeTab!=i)
		this._previousTab = this.activeTab;
	
	if(!firstLoad)
		firstLoad=false;
	
	if(!ignoreHistory)
		ignoreHistory=false;
	
	this.activeTab = i;
	if(this.activeTab != this._previousTab && this._previousTab!=null)
	{
		if(!!this.ontabchange)
		{
			if(typeof(this.ontabchange)=='function')
				this.ontabchange();
			else if(typeof(this.ontabchange)=='string')
				eval(this.ontabchange);
		}
	}
	
	if(this._previousTab!=null && !!this.tabs[this._previousTab].onleave && this._previousTab!=i)
	{
		if(!!this.tabs[this._previousTab].onleave) 
			this.tabs[this._previousTab].onleave(this.tabs[this._previousTab]);
	}
	
	var a;
	
	//this.tabs[i].showThrobber();
	
	var j=this.tabs.length;
	while(j--)
	{
		this.removeClass(this.tabs[j].a, 'active');
		this.tabs[j].contentDiv.style.display = 'none';
	}
		
	this.addClass(this.tabs[i].a, 'active');
	
	this.tabs[i].a.blur();
	this.tabs[i].li.style.display = '';
	this.tabs[i].contentDiv.style.display = '';
	
	if(!firstLoad && !!this.tabs[i].onshow)
	{
		if(typeof(this.tabs[i].onshow)=='function')
			this.tabs[i].onshow();
		else if(typeof(this.tabs[i].onshow)=='string')
			eval(this.tabs[i].onshow);
	}
	
	if(this._history && (!ignoreHistory) && (!!this._historyIframe))
		this._historyIframe.src=this._historyIframe.src.replace(/\?.*$/, '')+'?'+ i +','+ escape(this.tabs[i].contentURL);
}

TabSet.prototype.tabClosed = function(i)
{
	return this.tabs[i].li.style.display == 'none';
}

TabSet.prototype._container = function()
{
	return document.getElementByID(_id);
}

TabSet.prototype.addClass=function(obj, className)
{
	if(obj.className.indexOf(className) == -1)
		obj.className += ' '+className;
}

TabSet.prototype.removeClass=function(obj, className)
{
	obj.className = obj.className.replace(className, '');
}

TabSet.prototype.setTheme = function(theme)
{
	if(!!this._themeLink)
		document.getElementsByTagName('head')[0].removeChild(this._themeLink);
	
	fileref=document.createElement('link');
	fileref.setAttribute('rel', 'stylesheet');
	fileref.setAttribute('type', 'text/css');
	fileref.setAttribute('href', '/themes/TabSet/'+theme+'/theme.css');
	
	this._themeLink = document.getElementsByTagName('head')[0].appendChild(fileref);	
}

/**
	Tab class
 */
TabSet.prototype.Tab = function(options, tabset, index)
{
	var tabSetLI, tabSetLIA, tabSetSpan, tabSetContentDiv, tabSetClose, tabSetIcon;
	
	for(option in options)
		this[option]=options[option];
	
	tabSetLI = document.createElement('li');
	
	tabSetLIA = document.createElement('a');
	tabSetLIA.href = 'javascript:;';
	
	tabSetSpan = document.createElement('span');
	
	this._throbber=document.createElement('div');
	
	if(!!this.tabIcon || this.iframeURL || typeof(this.showIcon) == 'undefined' || this.showIcon==true)
	{
		tabSetIcon = document.createElement('img');
		tabSetIcon.className = 'TabSetThrobber';
		tabSetIcon.style.border = 'none';
		tabSetIcon.className = 'iepngfix';
		
		if(typeof(this.showIcon)=='undefined' || this.showIcon==true)
		{
			if(!!this.tabIcon)
				tabSetIcon.src = this.tabIcon;
			else if(!!this.iframeURL)
				tabSetIcon.src = /^(.*?:\/\/.*?)(\/|$)/i.exec(this.iframeURL)[1]+'/favicon.ico';
			//else
			//	tabSetIcon.src = '/images/TabSet/tab_default.png';
		}
		
		this.icon = this._throbber.appendChild(tabSetIcon);
	}
	tabSetSpan.appendChild(this._throbber);
	
	addEvent(tabSetLI, 'click', function(i)
	{
		if(!this.tabClosed(i))
			this.showTab(i);
		
		if(!!this.tabs[i].onclick && !this.tabClosed(i))
		{
			if(typeof(this.tabs[i].onclick)=='function')
				this.tabs[i].onclick();
			else if(typeof(this.tabs[i].onclick)=='string')
				eval(this.tabs[i].onclick);
		}
	}.partial(index).bind(tabset));
	
	tabSetSpan.appendChild(document.createTextNode(options.title));
	tabSetLIA.appendChild(tabSetSpan);
	
	if(!!this.showCloseButton && this.showCloseButton === true)
	{
		tabSetClose = document.createElement('div');
		tabSetClose.className = 'closeButton iepngfix';
		
		addEvent(tabSetClose, 'click', function(i)
		{
			this.close(i);
		}.partial(index).bind(tabset));
		
		this.closeButton = tabSetLIA.appendChild(tabSetClose);
	}
	
	tabSetContentDiv = document.createElement('div');	
	tabSetContentDiv.className = 'contentDiv';
	tabSetContentDiv.style.display = 'none';
	
	if(!!this.iframeURL)
	{
		tabSetContentIFrame = document.createElement('iframe');
		tabSetContentIFrame.style.width = '100%';
		if(tabset._height!='' && tabset._height!=0)
			tabSetContentIFrame.style.height = parseInt(tabset._height)+'px';
		tabSetContentIFrame.style.border = 'none';
		tabSetContentIFrame.scrolling = 'no';
		tabSetContentIFrame.border = '0';
		tabSetContentIFrame.frameBorder = '0';
		tabSetContentIFrame.onload = function(i)
		{
			if(this.iFrame.src!='')
			{
				if(!!this.oncontentload) 
					this.oncontentload(this); 
				this.hideThrobber()
			}
		}.bind(this);
		
		this.iFrame = tabSetContentDiv.appendChild(tabSetContentIFrame);
	}
	
	this.a = tabSetLI.appendChild(tabSetLIA);
	if(!!this.hidden)
		tabSetLI.style.display = 'none';
	this.li = tabset._TabSetNav.appendChild(tabSetLI);
	this.contentDiv = tabset._TabSetContentDivContainer.appendChild(tabSetContentDiv);
	
	if(!this.hidden)
		this.reload();
}

TabSet.prototype.Tab.prototype.showThrobber = function()
{
	this._throbber.className='TabSetThrobber';
}

TabSet.prototype.Tab.prototype.hideThrobber = function()
{
	this._throbber.className='TabSetIcon';
}

TabSet.prototype.Tab.prototype.reload = function(querystring)
{
	var a, err;
	
	this.showThrobber();
	
	if(!!this.content)
	{
		this.contentDiv.innerHTML = this.content;
		this.contentDiv.style.display = '';
		this.hideThrobber();
		if(!!this.oncontentload)
			this.oncontentload(this);
	}
	
	if(!!this.contentURL)
	{
		if(typeof(querystring)!='undefined')
			this.contentURL=this.contentURL.replace(/\?.*$/, '')+'?'+querystring;
		
		a=new Ajax(true, 'txt');
		try
		{
			a.get(this.contentURL, function()
			{
				if(a.ready())
				{
					this.hideThrobber();
					
					if(a.status()==200)
					{
						this.contentDiv.innerHTML=a.response();
						if(!!this.oncontentload) 
						{
							if(typeof(this.oncontentload)=='function')
								this.oncontentload(this);
							else if(typeof(this.oncontentload)=='string')
								eval(this.oncontentload);
						}

					}
					else if(a.status() == 403 && !!a.header('Location'))
						location.href=a.header('Location');
					else if(a.status()!=0)
						this.contentDiv.innerHTML = 'Couldn\'t load page: HTTP Error '+a.status()+'\n\n'+a.response();
				}
			}.bind(this));
		}
		catch(err)
		{
			this.hideThrobber();
			this.contentDiv.innerHTML = 'Couldn\'t load page: AJAX Error ('+err.name+') - '+err.message;	
		}
	}
	else if(!!this.iframeURL)
	{
		if(!!querystring)
			this.iframeURL=this.iframeURL.replace(/\?.*$/, '')+'?'+querystring;
		
		this.iFrame.src = this.iframeURL;
	}
	else
		this.hideThrobber();
}

addEvent = function(obj, evt, fn)
{
	if (obj.addEventListener)
		obj.addEventListener(evt, fn, false);
	else if (obj.attachEvent)
		obj.attachEvent('on'+evt, fn);
	else
		obj['on'+evt] = fn;
}

removeEvent = function(obj, evt, fn)
{
	if (obj.removeEventListener)
		obj.removeEventListener(evt, fn, false);
	else if (obj.detachEvent)
		obj.detachEvent('on'+evt, fn);
	else
		obj['on'+evt] = null;
}

