var multiTabObj;

function MultiTab()
{
	this.tabs=[];
	this.activeTabRowIndex=0;
	this.activeTabColIndex=0;
	this.activeTabContent="";
	this.tabLeftInactive="SSTabLeft";
	this.tabRightInactive="SSTabRight";
	this.tabLeftActive="SSTabLeftActive";
	this.tabRightActive="SSTabRightActive";
	this.tabImage="";
	this.classTabActive="SSTabActive";
	this.classTabInactive="SSTabInactive";
	this.classTabInactiveLowest="SSTabInactiveLowest";
	this.classTabAnchor="SSTabAnchor";
	this.classTable="SSSlideShowTable";
	this.classTabName="SSTabName";
	this.classTabBackground="SSTabBackground";
	this.calculateSpan="calSpan";
	this.pixelsUsed=0;
	//this.perTabPixels=16;
	this.tabTable=null;
	this.OnClickHandler=null;
	this.columns=100;
	//this.width=100;
	if(MultiTab._initialized==undefined)
	{
		MultiTab.prototype.init=function(OnClickHandler, divName, tabImage, width)
		{
			var tabDiv=document.getElementById(divName);
			this.tabImage=tabImage;
			this.columns=width;
			this.tabTable=document.createElement("table");				
			this.tabTable.setAttribute("cellPadding", "0");
			this.tabTable.setAttribute("cellSpacing", "0");
			this.tabTable.setAttribute("border", "0");
			this.tabTable.setAttribute("width", "100%");
			this.tabTable.className=this.classTable;
			
			tabDiv.appendChild(this.tabTable);
			this.OnClickHandler=OnClickHandler;				
		}
		
		MultiTab.prototype.clearTabs=function()
		{
			var oTBody=this.tabTable.tBodies[0];
			this.tabTable.removeChild(oTBody);
			this.activeTabRowIndex=0;
			this.activeTabColIndex=0;
			this.pixelsUsed=0;
		}
		
		MultiTab.prototype.addTab=function(tabName, tabContent)
		{
			var textSpan=document.createElement("span");
			textSpan.id=this.calculateSpan;
			textSpan.innerHTML=tabName;
			textSpan.style.visibility="hidden";
			/*textSpan.style.fontFamily="Times New Roman, Serif";
			textSpan.style.fontSize="10pt";*/
			textSpan.className=this.classTabName;
			document.body.appendChild(textSpan);
			var colsNeeded=textSpan.offsetWidth+32;
			document.body.removeChild(textSpan);
			var colsRemaining=this.columns;	
			var newTable=false;		
			if(this.tabTable.rows.length==0)
			{
				var oTBody=document.createElement("tbody");
				this.tabTable.appendChild(oTBody);
				oTBody.insertRow(0);
				oTBody.rows[0].insertCell(0);
				oTBody.rows[0].insertCell(1);
				newTable=true;
				var div=document.getElementById(tabContent);
				div.style.display="block";
				this.activeTabContent=tabContent;
			}
				
			else
			{
				var colsUsed=[];
				var index=0;
				var div=document.getElementById(tabContent);
				div.style.display="none";
				if(this.tabTable.tBodies[0].rows.length==1)
				{
					for(var count=0;count<this.tabTable.tBodies[0].rows[0].cells.length-1;count++)
						colsUsed[count]=this.tabTable.tBodies[0].rows[0].cells[count].colsneeded;
				}
				else
				{
					for(var count=0;count<this.tabTable.tBodies[0].rows[0].cells.length;count++)
						colsUsed[count]=this.tabTable.tBodies[0].rows[0].cells[count].colsneeded;
				}
				for(var count=0;count<colsUsed.length;count++)
					colsRemaining=colsRemaining-colsUsed[count];
				if(colsRemaining>=colsNeeded)
				{
					if(this.tabTable.tBodies[0].rows.length==1)
					{
						index=this.tabTable.tBodies[0].rows[0].cells.length-1;
						this.tabTable.tBodies[0].rows[0].insertCell(index);
					}
					else
					{
						index=this.tabTable.tBodies[0].rows[0].cells.length;
						this.tabTable.tBodies[0].rows[0].insertCell(index);
					}
				}
				else
				{
				    
					if(this.tabTable.rows.length==1)
					{
						
						//this.tabTable.tBodies[0].rows[0].cells[this.tabTable.tBodies[0].rows[0].cells.length-2].setAttribute("colSpan",this.tabTable.tBodies[0].rows[0].cells[this.tabTable.tBodies[0].rows[0].cells.length-2].colSpan+colsRemaining);
						this.tabTable.tBodies[0].rows[0].cells[this.tabTable.tBodies[0].rows[0].cells.length-2].setAttribute("width", parseInt(this.tabTable.tBodies[0].rows[0].cells[this.tabTable.tBodies[0].rows[0].cells.length-2].width.substring(0, this.tabTable.tBodies[0].rows[0].cells[this.tabTable.tBodies[0].rows[0].cells.length-2].width.length-1))+colsRemaining+"px");
						//alert(this.tabTable.innerHTML);
						this.tabTable.tBodies[0].rows[0].deleteCell(this.tabTable.tBodies[0].rows[0].cells.length-1);	
					}
					for(var count=0;count<this.tabTable.tBodies[0].rows.length;count++)
					{
                        this.tabTable.tBodies[0].rows[count].className=this.classTabBackground;
						for(var cellCount=0;cellCount<this.tabTable.tBodies[0].rows[count].cells.length;cellCount++)
						{
						    var rowIndex=parseInt(this.tabTable.tBodies[0].rows[count].cells[cellCount].rowIndex);
                            this.tabTable.tBodies[0].rows[count].cells[cellCount].rowIndex=rowIndex+1;
						}
					}
					this.tabTable.tBodies[0].insertRow(0);
					this.tabTable.tBodies[0].rows[0].insertCell(0);
					var index=0;
					this.activeTabRowIndex=this.activeTabRowIndex+1;
				}
			}
			var oInnerTable=document.createElement("table");
			oInnerTable.setAttribute("width", "100%");
			oInnerTable.setAttribute("border", "0");
			oInnerTable.setAttribute("cellPadding", "0");
			oInnerTable.setAttribute("cellSpacing", "0");
			
			var oInnerTBody=document.createElement("tbody");
			oInnerTable.appendChild(oInnerTBody);
			oInnerTBody.insertRow(0);
			oInnerTBody.rows[0].insertCell(0);
			if(newTable)
				oInnerTBody.rows[0].cells[0].className=this.tabLeftActive;
			else
				oInnerTBody.rows[0].cells[0].className=this.tabLeftInactive;			
				
			oInnerTBody.rows[0].insertCell(1);
			if(newTable)
				oInnerTBody.rows[0].cells[1].className=this.classTabActive;
			else if(this.tabTable.rows.length==1)
				oInnerTBody.rows[0].cells[1].className=this.classTabInactiveLowest;			
			else
				oInnerTBody.rows[0].cells[1].className=this.classTabInactive;			
			oInnerTBody.rows[0].cells[1].setAttribute("align", "center");
			var oLabel=document.createElement("label");
			oLabel.innerHTML=tabName;
			oLabel.className=this.classTabName;
			var oImage1=document.createElement("img");
			oImage1.src=this.tabImage;
				
			oInnerTBody.rows[0].cells[1].appendChild(oImage1);
			oInnerTBody.rows[0].cells[1].appendChild(oLabel);
			
			
			oInnerTBody.rows[0].insertCell(2);
			if(newTable)
				oInnerTBody.rows[0].cells[2].className=this.tabRightActive;
			else
				oInnerTBody.rows[0].cells[2].className=this.tabRightInactive;			
			
			if(newTable)
			{
				//oTBody.rows[0].cells[0].setAttribute("colSpan",colsNeeded);
				oTBody.rows[0].cells[0].setAttribute("width",colsNeeded+"px");
				oTBody.rows[0].cells[0].colsneeded=colsNeeded;
		        oTBody.rows[0].cells[0].onclick=this.OnClick;
		        oTBody.rows[0].cells[0].rowIndex=0;
		        oTBody.rows[0].cells[0].colIndex=0;
		        oTBody.rows[0].cells[0].tabContent=tabContent;
		        oTBody.rows[0].cells[0].tabName=tabName;
				oTBody.rows[0].cells[0].appendChild(oInnerTable);
				//oTBody.rows[0].cells[1].setAttribute("colSpan", colsRemaining);
				colsRemaining=colsRemaining-colsNeeded;
				oTBody.rows[0].cells[1].setAttribute("width", colsRemaining+"px");
			}

			else
			{				
				//this.tabTable.tBodies[0].rows[0].cells[index].setAttribute("colSpan",colsNeeded);
				this.tabTable.tBodies[0].rows[0].cells[index].setAttribute("width",colsNeeded+"px");
				this.tabTable.tBodies[0].rows[0].cells[index].colsneeded=colsNeeded;
		        this.tabTable.tBodies[0].rows[0].cells[index].onclick=this.OnClick;
		        this.tabTable.tBodies[0].rows[0].cells[index].rowIndex=0;
		        this.tabTable.tBodies[0].rows[0].cells[index].colIndex=index;
		        this.tabTable.tBodies[0].rows[0].cells[index].tabContent=tabContent;
		        this.tabTable.tBodies[0].rows[0].cells[index].tabName=tabName;
				this.tabTable.tBodies[0].rows[0].cells[index].appendChild(oInnerTable);

				if(this.tabTable.tBodies[0].rows.length==1)
				{
					colsRemaining=colsRemaining-colsNeeded;
					if(colsRemaining<=10)
					{
						//this.tabTable.tBodies[0].rows[0].cells[this.tabTable.tBodies[0].rows[0].cells.length-2].setAttribute("colSpan",this.tabTable.tBodies[0].rows[0].cells[this.tabTable.tBodies[0].rows[0].cells.length-2].colSpan+colsRemaining);
						this.tabTable.tBodies[0].rows[0].cells[this.tabTable.tBodies[0].rows[0].cells.length-2].setAttribute("width", parseInt(this.tabTable.tBodies[0].rows[0].cells[this.tabTable.tBodies[0].rows[0].cells.length-2].width.substring(0, this.tabTable.tBodies[0].rows[0].cells[this.tabTable.tBodies[0].rows[0].cells.length-2].width.length-1))+colsRemaining+"px");
					    this.tabTable.tBodies[0].rows[0].deleteCell(this.tabTable.tBodies[0].rows[0].cells.length-1);   
					}
					else
					{
					    //this.tabTable.tBodies[0].rows[0].cells[index+1].setAttribute("colSpan",colsRemaining);
					    this.tabTable.tBodies[0].rows[0].cells[index+1].setAttribute("width",colsRemaining+"px");
					}
				}
				else
				{
					function ColsCollector(columnsneeded, index)
					{
						this.columnsneeded=columnsneeded;
						this.index=index;
					}
					
					function sortCols(colsColl)
					{
						var temp;
						for(var count=0;count<colsColl.length;count++)
						{
							for(var countSub=colsColl.length-1;countSub>=count+1;countSub--)
							{
								if(parseInt(colsColl[countSub-1].columnsneeded)<parseInt(colsColl[countSub].columnsneeded))
								{
									temp=colsColl[countSub-1];
									colsColl[countSub-1]=colsColl[countSub];
									colsColl[countSub]=temp;
								}
							}
						}					
					}
					
					var colsCollectorObj=[];
					for(var count=0;count<this.tabTable.tBodies[0].rows[0].cells.length;count++)
						colsCollectorObj[count]=new ColsCollector(this.tabTable.tBodies[0].rows[0].cells[count].colsneeded, count);
					sortCols(colsCollectorObj);
					var remaining=this.columns;
					var colCount=this.tabTable.tBodies[0].rows[0].cells.length;
					for(var count=0;count<this.tabTable.tBodies[0].rows[0].cells.length;count++)
					{
						var columnSpan=Math.ceil(remaining/colCount);
						if(columnSpan<colsCollectorObj[count].columnsneeded)
							columnSpan=colsCollectorObj[count].columnsneeded;
						remaining=remaining-columnSpan;
						colCount--;
						//this.tabTable.tBodies[0].rows[0].cells[colsCollectorObj[count].index].setAttribute("colSpan", columnSpan);
						this.tabTable.tBodies[0].rows[0].cells[colsCollectorObj[count].index].setAttribute("width", columnSpan+"px");
					}
					colsCollector=null;	
				}
			}
		}
		
		MultiTab.prototype.hideTab=function(rowIndex, colIndex)
		{
			this.tabTable.tBodies[0].rows[rowIndex].cells[colIndex].childNodes[0].rows[0].cells[0].className=this.tabLeftInactive;
			this.tabTable.tBodies[0].rows[rowIndex].cells[colIndex].childNodes[0].rows[0].cells[1].className=this.classTabInactiveLowest;
			this.tabTable.tBodies[0].rows[rowIndex].cells[colIndex].childNodes[0].rows[0].cells[2].className=this.tabRightInactive;
		}
		
		MultiTab.prototype.showTab=function(rowIndex, colIndex)
		{
			this.tabTable.tBodies[0].rows[rowIndex].cells[colIndex].childNodes[0].rows[0].cells[0].className=this.tabLeftActive;
			this.tabTable.tBodies[0].rows[rowIndex].cells[colIndex].childNodes[0].rows[0].cells[1].className=this.classTabActive;
			this.tabTable.tBodies[0].rows[rowIndex].cells[colIndex].childNodes[0].rows[0].cells[2].className=this.tabRightActive;
		}
		
		MultiTab.prototype.arrangeRows=function(newRowIndex)
		{
			if(newRowIndex!=this.tabTable.tBodies[0].rows.length-1)
			{
				var index=this.tabTable.tBodies[0].rows.length;
				this.tabTable.tBodies[0].insertRow(index);
				for(var count=0;count<this.tabTable.tBodies[0].rows[newRowIndex].cells.length;count++)
				{
					this.tabTable.tBodies[0].rows[index].insertCell(count);
					//this.tabTable.tBodies[0].rows[index].cells[count].setAttribute("colSpan", this.tabTable.tBodies[0].rows[newRowIndex].cells[count].colSpan);
					this.tabTable.tBodies[0].rows[index].cells[count].setAttribute("width", this.tabTable.tBodies[0].rows[newRowIndex].cells[count].width+"px");
					this.tabTable.tBodies[0].rows[index].cells[count].colsneeded=this.tabTable.tBodies[0].rows[newRowIndex].cells[count].colsneeded;
					this.tabTable.tBodies[0].rows[index].cells[count].className=this.tabTable.tBodies[0].rows[newRowIndex].cells[count].className;
		            this.tabTable.tBodies[0].rows[index].cells[count].onclick=this.OnClick;
		            this.tabTable.tBodies[0].rows[index].cells[count].rowIndex=this.tabTable.tBodies[0].rows.length-1;
		            this.tabTable.tBodies[0].rows[index].cells[count].colIndex=this.tabTable.tBodies[0].rows[newRowIndex].cells[count].colIndex;
		            this.tabTable.tBodies[0].rows[index].cells[count].tabContent=this.tabTable.tBodies[0].rows[newRowIndex].cells[count].tabContent;
		            this.tabTable.tBodies[0].rows[index].cells[count].tabName=this.tabTable.tBodies[0].rows[newRowIndex].cells[count].tabName;
					this.tabTable.tBodies[0].rows[index].cells[count].appendChild(this.tabTable.tBodies[0].rows[newRowIndex].cells[count].childNodes[0]);
				}
				this.tabTable.tBodies[0].deleteRow(newRowIndex);
				for(var count=newRowIndex;count<this.tabTable.tBodies[0].rows.length;count++)
				{
					if(count!=this.tabTable.tBodies[0].rows.length-1)
					{
						for(var cellCount=0;cellCount<this.tabTable.tBodies[0].rows[count].cells.length;cellCount++)
						{
							this.tabTable.tBodies[0].rows[count].cells[cellCount].childNodes[0].rows[0].cells[1].className=this.classTabInactive;
							this.tabTable.tBodies[0].rows[count].cells[cellCount].rowIndex=parseInt(this.tabTable.tBodies[0].rows[count].cells[cellCount].rowIndex)-1;
						}
					}
					else
					{
						for(var cellCount=0;cellCount<this.tabTable.tBodies[0].rows[count].cells.length;cellCount++)
						{
							if(this.tabTable.tBodies[0].rows[count].cells[cellCount].childNodes[0].rows[0].cells[1].className!=this.classTabActive)
							{
								this.tabTable.tBodies[0].rows[count].cells[cellCount].childNodes[0].rows[0].cells[1].className=this.classTabInactiveLowest;
							}
							else
							{
							    this.activeTabRowIndex=count;
							    this.activeTabColIndex=cellCount;
							}
							this.tabTable.tBodies[0].rows[count].cells[cellCount].rowIndex=this.tabTable.tBodies[0].rows.length-1;
						}
					}
				}
				for(var count=0;count<this.tabTable.tBodies[0].rows.length;count++)
				{
				    if(count!=0)
                        this.tabTable.tBodies[0].rows[count].className=this.classTabBackground;
                    else
                        this.tabTable.tBodies[0].rows[count].className="";
                }				
			}
		}
		
		MultiTab.prototype.OnClick=function(oEvent)
		{
			var self=multiTabObj;
			self.hideTab(self.activeTabRowIndex, self.activeTabColIndex);
			self.showTab(this.rowIndex, this.colIndex);
			self.activeTabRowIndex=this.rowIndex;
			self.activeTabColIndex=this.colIndex;
			self.arrangeRows(this.rowIndex);
			var content=document.getElementById(self.activeTabContent);
			content.style.display="none";
			var newContent=document.getElementById(this.tabContent);
			newContent.style.display="block";
			self.activeTabContent=this.tabContent;
			if(self.OnClickHandler)
			{
				self.OnClickHandler(this.rowIndex, this.colIndex, this.tabName);
			}
		}
		
		MultiTab.prototype.ActivateTab=function(tabName)
		{
		    var shouldBreak=false;
		    for(var row=0;row<this.tabTable.tBodies[0].rows.length;row++)
		    {
		        for(var col=0;col<this.tabTable.tBodies[0].rows[row].cells.length;col++)
		        {
				    if(isIE(navigator.userAgent))
				    {
    		            if(this.tabTable.tBodies[0].rows[row].cells[col].childNodes[0].rows[0].cells[1].childNodes[1].innerText==tabName)
    		            {
	    	                shouldBreak=true;
	    	                break;
	    	            }
	    	        }
				    if(isFirefox(navigator.userAgent))
				    {
    		            if(this.tabTable.tBodies[0].rows[row].cells[col].childNodes[0].rows[0].cells[1].childNodes[1].text==tabName)
    		            {
	    	                shouldBreak=true;
	    	                break;
	    	            }
                    } 	        
		        }
		        if(shouldBreak)
		            break;
		    }
		    if(row==this.tabTable.tBodies[0].rows.length)
		        return;
			var self=multiTabObj;
			self.hideTab(self.activeTabRowIndex, self.activeTabColIndex);
			self.showTab(row, col);
			self.activeTabRowIndex=row;
			self.activeTabColIndex=col;
			self.arrangeRows(row);
			var content=document.getElementById(self.activeTabContent);
			content.style.display="none";
			var newContent=document.getElementById(this.tabTable.tBodies[0].rows[row].cells[col].tabContent);
			newContent.style.display="block";
			self.activeTabContent=this.tabTable.tBodies[0].rows[row].cells[col].tabContent;
			if(self.OnClickHandler)
			{
				if(isIE(navigator.userAgent))
				{
					self.OnClickHandler(row, col, this.tabTable.tBodies[0].rows[row].cells[col].tabName);				
				}
				if(isFirefox(navigator.userAgent))
				{
					self.OnClickHandler(row, col, this.tabTable.tBodies[0].rows[row].cells[col].tabName);
				}
			}		    
		}
	}
	MultiTab._initialized=true;
}