	/************************************************************************************************************
	(C) www.dhtmlgoodies.com, October 2005
	
	This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.	
	
	Terms of use:
	You are free to use this script as long as the copyright message is kept intact. However, you may not
	redistribute, sell or repost it without our permission.
	
	Updated:
		
		March, 14th, 2006 - Create new tabs dynamically
		March, 15th, 2006 - Dynamically delete a tab
		
	Thank you!
	
	www.dhtmlgoodies.com
	Alf Magne Kalleland
	
	************************************************************************************************************/		

var listTabs = new Array ();
var tabs_creadas = new Array();

function Tabs (mainContainerID,tabTitles,activeTab,width,height,closeButtonArray,additionalTab)
{
	
	this.textPadding = 3; // Padding at the left of tab text - bigger value gives you wider tabs
	this.strictDocType = true; 
	this.tabView_maxNumberOfTabs = 6;	// Maximum number of tabs
	
	/* Don't change anything below here */
	this.dhtmlgoodies_tabObj = new Array();
	this.activeTabIndex = new Array();
	this.MSIE = navigator.userAgent.indexOf('this.MSIE')>=0?true:false;
	
	this.regExp = new RegExp(".*this.MSIE ([0-9]\.[0-9]).*","g");
	this.navigatorVersion = navigator.userAgent.replace(this.regExp,'$1');
	
	this.ajaxObjectss = new Array();
	this.tabView_countTabs = new Array();
	this.tabViewHeight = new Array();
	this.tabDivCounter = 0;
	this.closeImageHeight = 8;	// Pixel height of close buttons
	this.closeImageWidth = 8;	// Pixel height of close buttons
	var self=this;
	
	this.setPadding = function (obj,padding){
		var span = obj.getElementsByTagName('SPAN')[0];
		span.style.paddingLeft = padding + 'px';	
		span.style.paddingRight = padding + 'px';	
	}
	
	this.showTab = function (parentId,tabIndex)
	{
		var parentId_div = parentId + "_";
		if(!document.getElementById('tabView' + parentId_div + tabIndex)){
			return;
		}
		if(this.activeTabIndex[parentId]>=0){
			if(this.activeTabIndex[parentId]==tabIndex){
				return;
			}
	
			var obj = document.getElementById('tabTab'+parentId_div + this.activeTabIndex[parentId]);
			
			obj.className='tabInactive';
			var img = obj.getElementsByTagName('IMG')[0];
			if(img.src.indexOf('tab_')==-1)img = obj.getElementsByTagName('IMG')[1];
			img.src = 'images/tab_right_inactive.gif';
			document.getElementById('tabView' + parentId_div + this.activeTabIndex[parentId]).style.display='none';
		}
		
		var thisObj = document.getElementById('tabTab'+ parentId_div +tabIndex);	
			
		thisObj.className='tabActive';
		var img = thisObj.getElementsByTagName('IMG')[0];
		if(img.src.indexOf('tab_')==-1)img = thisObj.getElementsByTagName('IMG')[1];
		img.src = 'images/tab_right_active.gif';
		
		document.getElementById('tabView' + parentId_div + tabIndex).style.display='block';
		this.activeTabIndex[parentId] = tabIndex;
		

		var parentObj = thisObj.parentNode;
		var aTab = parentObj.getElementsByTagName('DIV')[0];
		countObjects = 0;
		var startPos = 2;
		var previousObjectActive = false;
		while(aTab){
			if(aTab.tagName=='DIV'){
				if(previousObjectActive){
					previousObjectActive = false;
					startPos-=2;
				}
				if(aTab==thisObj){
					startPos-=2;
					previousObjectActive=true;
					this.setPadding(aTab,this.textPadding+1);
				}else{
					this.setPadding(aTab,this.textPadding);
				}
				
				aTab.style.left = startPos + 'px';
				countObjects++;
				startPos+=2;
			}			
			aTab = aTab.nextSibling;
		}
		
		return;
	}
	
	this.tabClick = function()
	{
		var idArray = this.id.split('_');
		self.showTab(this.parentNode.parentNode.id,idArray[idArray.length-1].replace(/[^0-9]/gi));
		
		return;
	}
	
	this.rolloverTab = function()
	{
		if(this.className.indexOf('tabInactive')>=0){
			this.className='inactiveTabOver';
			var img = this.getElementsByTagName('IMG')[0];
			if(img.src.indexOf('tab_')<=0)img = this.getElementsByTagName('IMG')[1];
			img.src = 'images/tab_right_over.gif';
		}
		
		return;
	}

	this.rolloutTab = function()
	{
		if(this.className ==  'inactiveTabOver'){
			this.className='tabInactive';
			var img = this.getElementsByTagName('IMG')[0];
			if(img.src.indexOf('tab_')<=0)img = this.getElementsByTagName('IMG')[1];
			img.src = 'images/tab_right_inactive.gif';
		}
		
		return;
	}
	
	this.hoverTabViewCloseButton = function()
	{
		this.src = this.src.replace('close.gif','close_over.gif');

		return;
	}
	
	this.stopHoverTabViewCloseButton = function()
	{
		this.src = this.src.replace('close_over.gif','close.gif');

		return;
	}
	
	this.initTabs = function (mainContainerID,tabTitles,activeTab,width,height,closeButtonArray,additionalTab)
	{
		if(!closeButtonArray)closeButtonArray = new Array();
		
		if(!additionalTab || additionalTab=='undefined'){			
			this.dhtmlgoodies_tabObj[mainContainerID] = document.getElementById(mainContainerID);
			width = width + '';
			if(width.indexOf('%')<0)width= width + 'px';
			this.dhtmlgoodies_tabObj[mainContainerID].style.width = width;
						
			height = height + '';
			if(height.length>0){
				if(height.indexOf('%')<0)height= height + 'px';
				this.dhtmlgoodies_tabObj[mainContainerID].style.height = height;
			}
			

			this.tabViewHeight[mainContainerID] = height;
			
			var tabDiv = document.createElement('DIV');		
			var firstDiv = this.dhtmlgoodies_tabObj[mainContainerID].getElementsByTagName('DIV')[0];	
			
			this.dhtmlgoodies_tabObj[mainContainerID].insertBefore(tabDiv,firstDiv);	
			tabDiv.className = 'dhtmlgoodies_tabPane';			
			this.tabView_countTabs[mainContainerID] = 0;

		}else{
			var tabDiv = this.dhtmlgoodies_tabObj[mainContainerID].getElementsByTagName('DIV')[0];
			var firstDiv = this.dhtmlgoodies_tabObj[mainContainerID].getElementsByTagName('DIV')[1];
			height = this.tabViewHeight[mainContainerID];
			activeTab = this.tabView_countTabs[mainContainerID];		
	
			
		}
		
		
		
		for(var no=0;no<tabTitles.length;no++){
			var aTab = document.createElement('DIV');
			aTab.id = 'tabTab' + mainContainerID + "_" +  (no + this.tabView_countTabs[mainContainerID]);
			aTab.onmouseover = this.rolloverTab;
			aTab.onmouseout = this.rolloutTab;
			aTab.onclick = this.tabClick;
			aTab.className='tabInactive';
			tabDiv.appendChild(aTab);
			var span = document.createElement('SPAN');
			span.innerHTML = tabTitles[no];
			span.style.position = 'relative';
			aTab.appendChild(span);
			
			if(closeButtonArray[no]){
				var closeButton = document.createElement('IMG');
				closeButton.src = 'images/close.gif';
				closeButton.height = this.closeImageHeight + 'px';
				closeButton.width = this.closeImageHeight + 'px';
				closeButton.setAttribute('height',this.closeImageHeight);
				closeButton.setAttribute('width',this.closeImageHeight);
				closeButton.style.position='absolute';
				closeButton.style.top = '6px';
				closeButton.style.right = '0px';
				closeButton.onmouseover = hoverTabViewCloseButton;
				closeButton.onmouseout = stopHoverTabViewCloseButton;
				
				span.innerHTML = span.innerHTML + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';	
				
				var deleteTxt = span.innerHTML+'';

				closeButton.onclick = function(){ this.deleteTab(this.parentNode.innerHTML) };
				span.appendChild(closeButton);
			}
			
			var img = document.createElement('IMG');
			img.valign = 'bottom';
			img.src = 'images/tab_right_inactive.gif';
			// IE5.X FIX
			if((this.navigatorVersion && this.navigatorVersion<6) || (this.MSIE && !this.strictDocType)){
				img.style.styleFloat = 'none';
				img.style.position = 'relative';	
				img.style.top = '4px'
				span.style.paddingTop = '4px';
				aTab.style.cursor = 'hand';
			}	// End IE5.x FIX
			aTab.appendChild(img);
		}

		var tabs = this.dhtmlgoodies_tabObj[mainContainerID].getElementsByTagName('DIV');
		var divCounter = 0;
		for(var no=0;no<tabs.length;no++){
			if(tabs[no].className=='dhtmlgoodies_aTab' && tabs[no].parentNode.id == mainContainerID){
				if(height.length>0)tabs[no].style.height = height;
				tabs[no].style.display='none';
				tabs[no].id = 'tabView' + mainContainerID + "_" + divCounter;
				divCounter++;
			}			
		}	
		this.tabView_countTabs[mainContainerID] = this.tabView_countTabs[mainContainerID] + tabTitles.length;	
		this.showTab(mainContainerID,activeTab);

		return activeTab;
	}	
	
	this.showAjaxTabContent = function(ajaxIndex,parentId,tabId)
	{
		var obj = document.getElementById('tabView'+parentId + '_' + tabId);
		obj.innerHTML = this.ajaxObjectss[ajaxIndex].response;		

		return;
	}
	
	this.resetTabIds = function(parentId)
	{
		var tabTitleCounter = 0;
		var tabContentCounter = 0;
		
		
		var divs = this.dhtmlgoodies_tabObj[parentId].getElementsByTagName('DIV');

		
		for(var no=0;no<divs.length;no++){
			if(divs[no].className=='dhtmlgoodies_aTab'){
				divs[no].id = 'tabView' + parentId + '_' + tabTitleCounter;
				tabTitleCounter++;
			}
			if(divs[no].id.indexOf('tabTab')>=0){
				divs[no].id = 'tabTab' + parentId + '_' + tabContentCounter;	
				tabContentCounter++;
			}	
			
				
		}
	
		this.tabView_countTabs[parentId] = tabContentCounter;
	}
	
	
	this.createNewTab = function(parentId,tabTitle,tabContent,tabContentUrl,closeButton)
	{
		if(this.tabView_countTabs[parentId]>=this.tabView_maxNumberOfTabs)return;	// Maximum number of tabs reached - return
		var div = document.createElement('DIV');
		div.className = 'dhtmlgoodies_aTab';
		this.dhtmlgoodies_tabObj[parentId].appendChild(div);		

		var tabId = initTabs(parentId,Array(tabTitle),0,'','',Array(closeButton),true);
		if(tabContent)div.innerHTML = tabContent;
		if(tabContentUrl){		
			var ajaxIndex = this.ajaxObjectss.length;
			this.ajaxObjectss[ajaxIndex] = new sack();
			this.ajaxObjectss[ajaxIndex].requestFile = tabContentUrl;	// Specifying which file to get

			this.ajaxObjectss[ajaxIndex].onCompletion = function(){ this.showAjaxTabContent(ajaxIndex,parentId,tabId); };	// Specify function that will be executed after file has been found
			this.ajaxObjectss[ajaxIndex].runAJAX();		// Execute AJAX function	
		
		}
				
	}
	
	this.getTabIndexByTitle = function(tabTitle)
	{
		var regExp = new RegExp("(.*?)&nbsp.*$","gi");
		tabTitle = tabTitle.replace(regExp,'$1');
		for(var prop in this.dhtmlgoodies_tabObj){
			var divs = this.dhtmlgoodies_tabObj[prop].getElementsByTagName('DIV');
			for(var no=0;no<divs.length;no++){
				if(divs[no].id.indexOf('tabTab')>=0){
					var span = divs[no].getElementsByTagName('SPAN')[0];
					var regExp2 = new RegExp("(.*?)&nbsp.*$","gi");
					var spanTitle = span.innerHTML.replace(regExp2,'$1');
					
					if(spanTitle == tabTitle){
						
						var tmpId = divs[no].id.split('_');						
						return Array(prop,tmpId[tmpId.length-1].replace(/[^0-9]/g,'')/1);
					}		
				}
			}
		}
		
		return -1;
		
	}
	
	/* Call this function if you want to display some content from external file in one of the tabs 
	Arguments: Title of tab and relative path to external file */
	
	this.addAjaxContentToTab = function(tabTitle,tabContentUrl)
	{
		var index = getTabIndexByTitle(tabTitle);
		if(index!=-1){
			var ajaxIndex = this.ajaxObjectss.length;
			
			tabId = index[1];
			parentId = index[0];
			
			
			this.ajaxObjectss[ajaxIndex] = new sack();
			this.ajaxObjectss[ajaxIndex].requestFile = tabContentUrl;	// Specifying which file to get

			this.ajaxObjectss[ajaxIndex].onCompletion = function(){ showAjaxTabContent(ajaxIndex,parentId,tabId); };	// Specify function that will be executed after file has been found
			this.ajaxObjectss[ajaxIndex].runAJAX();		// Execute AJAX function				
			
		}	
	}

	
	
	this.deleteTab = function(tabLabel,tabIndex,parentId)
	{

		if(tabLabel){
			var index = getTabIndexByTitle(tabLabel);
			if(index!=-1){
				deleteTab(false,index[1],index[0]);
			}
			
		}else if(tabIndex>=0){
			if(document.getElementById('tabTab' + parentId + '_' + tabIndex)){
				var obj = document.getElementById('tabTab' + parentId + '_' + tabIndex);
				var id = obj.parentNode.parentNode.id;
				obj.parentNode.removeChild(obj);
				var obj2 = document.getElementById('tabView' + parentId + '_' + tabIndex);
				obj2.parentNode.removeChild(obj2);
				resetTabIds(parentId);
				this.activeTabIndex[parentId]=-1;
				showTab(parentId,'0');
			}			
		}
		
	}
	this.initTabs (mainContainerID,tabTitles,activeTab,width,height,closeButtonArray,additionalTab);
	listTabs.push(this);
}
