// -------------------------------------------------------------------
// TabStrip Control 
//
// Provide a control for creating/opening/removing dynamically tabs
//
// Author: Gregor Cieslak
// Date:   01.12.2005
//
//
//
//
//
// -------------------------------------------------------------------


var tabStripId			= 'dynsubtabs';
var tabsContent			= tabStripId + "_content";
var imageDirectory;
var currentTabIndex		= 0;
var currentSubTabIndex	= 0;
var openendTabHistory = new Array();
heightOccupied = 70;


// -------------------------------------------------
// Ctor
// -------------------------------------------------
function DynamicTabStrip()
{ 
	document.write("<div id=dynamicNav style='width:98%;'>");
	document.write("<div id=" + tabStripId + " style='display:block'></div></div>");
	document.write("<div id=" + tabStripId + "_content></div>");
	
	var div					= document.getElementById( tabStripId + "_content" );
	var frame = CreateIFrame(99999, "../Introduction.aspx");
	div.appendChild ( frame );
	CheckNoTabs();
	
	window.onresize = RefreshIFrameHeights;
		
}


function CheckNoTabs()
{
	
	var div					= document.getElementById( tabStripId );
	var tabs				= div.getElementsByTagName("div");
	var frame				= document.getElementById(  tabStripId + "_iFrameWindow_" + 99999 );

	if ( tabs.length == 0 )
	{
		frame.style.display = "block";
	}
	else
	{
	
		frame.style.display = "none";
	}

}

// -------------------------------------------------
// Saves the current state of tabs to given url via ajax
// -------------------------------------------------
function SaveStateTo( url )
{

	// Serialize tab's
	var div			= document.getElementById( tabStripId );
	var tabs		= div.getElementsByTagName("div");
	
	var state = "set=";
	
	
	for ( x = 0; x < tabs.length; x++ )
	{	
		// Get current tab and its id
		var tab = tabs[ x ];
		
		// ID ; FID; ClassName, URL; Caption
		state += tab.id + ";" + tab.fId + ";" + tab.className + ";" + tab.url + ";" +  tab.caption;
		
		state += "|";
	}
	
	
	
	var xmlHttpReq = false;
	var self = this;
	
	// Mozilla/Safari
	if (window.XMLHttpRequest)
	{
		self.xmlHttpReq = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) 
	{
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}

	self.xmlHttpReq.open('POST', url, true);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	self.xmlHttpReq.send( state );
   

}



// -------------------------------------------------
// Creates a tab with given caption and url at given level
// -------------------------------------------------
function CreateTab( caption, url, autoOpen, fId )
{
	
	
	if ( caption.length > 15 )
		caption = caption.substring(0, 15);
		
		
	// Get tab container
	var div			= document.getElementById( tabStripId );
	var tabs		= div.getElementsByTagName("div");
	var tabId		= 0;

	
	// Make sure we dont exceeded maximum allowed sub tabs
	if ( CheckTabExceededLimit( tabs.length ) == true ) 
		return;
	
	// Iterate thru all tabs and get the highest tab id
	for ( x = 0; x < tabs.length; x++ )
	{	
		// Get current tab and its id
		var tempTab = tabs[ x ];
		var currentId = parseInt ( tempTab.id.replace( tabStripId +  "_tab_","") );
		
		// See if tab id is highest..
		if ( currentId > tabId )
			tabId = currentId;
		
	}
	
	
	// Get new id by incrementing the last found id
	tabId++;
	
	
	// Create a new tab
	var newTab			= document.createElement("div");
	newTab.className	= "item off";
	newTab.id			= tabStripId + "_tab_" + tabId;
	newTab.fId			= fId;
	newTab.style.display = "block";
	newTab.caption		= caption;
	newTab.setAttribute("url", url);
	

	// Create a hyperlink
	var hyperLink		= document.createElement("a");
	hyperLink.href		= "javascript:OpenTab(" + tabId + ")";
	hyperLink.style.borderRightWidth = "0px";
	hyperLink.style.paddingRight	= "0px";
	hyperLink.appendChild( document.createTextNode(caption) );
	newTab.appendChild( hyperLink );
	
	
	var closeImage		= document.createElement("img");
	var closeLink		= document.createElement("a");
	
	// Close Image
	closeImage.src			= imageDirectory + "/tab-close.gif";
	closeImage.border		= "0";
	
	// Close Link
	closeLink.href  = "javascript:CloseTab(" + tabId + ")";
	closeLink.appendChild( closeImage );
	closeLink.style.borderLeftWidth = "0px";
	closeLink.style.paddingLeft		= "5px";

	
	newTab.appendChild( hyperLink );
	newTab.appendChild ( closeLink );
	div.appendChild ( newTab );
	if ( autoOpen == 1 )
	{
		OpenTab( tabId );
	}
		
}


// global var for closeTab
//var avoidCloseTab = false;
//var closeTabWasAvoid = false;

// -------------------------------------------------
// Closes given tab
// -------------------------------------------------
function CloseTab( tabId )
{
	//alert( 'avoidCloseTab' + avoidCloseTab );
	//closeTabWasAvoid = avoidCloseTab;
	
	//if( avoidCloseTab == true )
	//{ 
		// Get tab container
		var div			= document.getElementById( tabStripId );
		
		// Get tab container
		var tabs		= div.getElementsByTagName("div");
		
		// Get correspondig tab
		var tab = document.getElementById( tabStripId + "_tab_" + tabId );

		
		// Remove iFrame ( if opened )
		var iFrame = document.getElementById( tabStripId +  "_iFrameWindow_" + tabId);
		
		if ( iFrame != null )
		{
			// Get the content container
			contentContainer = document.getElementById( tabsContent );
			contentContainer.removeChild ( iFrame );
			iFrame = null;
		}
		
		
		
		// Remove Tab	
		div.removeChild( tab );
		
		
		// Open previous tab
		var lastFoundIndex = -1;
		
		
			
		for ( x = 0; x < tabs.length; x++ )
		{	
			var tempTab = tabs[ x ];
			
			var foundIndex = parseInt ( tempTab.id.replace( tabStripId + "_tab_","") );
			
			//alert ( foundIndex );
			if ( foundIndex < tabId )
				lastFoundIndex = foundIndex;
		}
		
		// If no previous tab found, lookup for a next tab
			
		for ( x = 0; x < tabs.length; x++ )
		{	
			var tempTab = tabs[ x ];
			
			var foundIndex = parseInt ( tempTab.id.replace( tabStripId + "_tab_","") );
			
			//alert ( foundIndex );
			if ( foundIndex > lastFoundIndex )
				lastFoundIndex = foundIndex;
		}
		
		if ( lastFoundIndex != -1  )
		{
			OpenTab ( lastFoundIndex );
		}
		
		CheckNoTabs()
		HideWaitBox();
	//}
	
	
}

// -------------------------------------------------
// Opens tab at given index at given level
// -------------------------------------------------
function OpenTab ( tabId )
{	
	
	
	// Get target table and the first row
	var div = document.getElementById( tabStripId );
	
	
	// Get the content container
	contentContainer = document.getElementById( tabsContent );
	
	var selectedTab = document.getElementById( tabStripId + "_tab_" + tabId );
	var tabs = div.getElementsByTagName("div");
	
	for ( x = 0; x < tabs.length; x++ )
	{	
		var tab = tabs[ x ];
		var currentId = parseInt ( tab.id.replace( tabStripId +  "_tab_","") );
	
		
		if ( currentId == tabId)
			tab.className = "item on";
		else
			tab.className = "item off";
	}
	
	
	
	iFrame = document.getElementById( tabStripId + "_iFrameWindow_" + tabId );
	
	
	HideAllContents ();
	if ( iFrame == null || iFrame.refresh != null )
	{
		
		// Create an iframe
		iFrame = CreateIFrame( tabId, selectedTab.getAttribute("url"));
		// Add iframe to content container
		contentContainer.appendChild( iFrame );
	}

	
	
	currentTabIndex = tabId;
	iFrame.style.display			= 'block';
	contentContainer.style.display	= "block";
	//HideWaitBox();
	
	
}



function CreateIFrame( tabId, src)
{
	// Create an iframe
	iFrame = document.createElement("iframe");
	
	
	//var windowHeight		= window.outerHeight;
	var windowHeight		= document.body.clientHeight;
	
	/*var frameHeight			= '85%';
	
	if ( windowHeight < 1000 ) frameHeight			= '91%';
	if ( windowHeight < 520 ) frameHeight			= '88%';
	if ( windowHeight < 350 ) frameHeight			= '350';
	*/

    ShowWaitBox();
	iFrame.id				=  tabStripId + "_iFrameWindow_" + tabId;
	iFrame.src				= src;
	iFrame.width = '100%';
	iFrame.height			= windowHeight - heightOccupied;
	//iFrame.height			= '95%';
	iFrame.style.display	= 'block';
	iFrame.style.border	    = '0';
	iFrame.frameBorder		= '0';
	iFrame.marginWidth		= '0';
	iFrame.marginHeight		= '0';
	iFrame.scrolling		= 'auto';
	iFrame.name				= 'iFrameWindow';
	
	RefreshIFrameHeights();
	return iFrame;

}


function RefreshIFrameHeights()
{
	
	var windowHeight		= document.body.clientHeight;
	var contentContainer = document.getElementById( tabsContent );
	frameWindows = 	contentContainer.getElementsByTagName("iframe");
	
	for ( x = 0; x < frameWindows.length; x ++ )
	{	
		
		frameWindows[ x ].height			= windowHeight - 70;
	}

}

function CheckTabExceededLimit( openedTabs )
{

	
	var width	= window.screen.width;
	var heigth	= window.screen.height;
	var maxSubTabs = 6;
	
	
	if ( width <= 1280 ) maxSubTabs = 13;
	if ( width <= 1024 ) maxSubTabs = 9;
	if ( width <= 800 ) maxSubTabs = 7;
	
	//alert ("Allowed tabs: " + maxSubTabs + " for width:" + width );
	
	if ( openedTabs > maxSubTabs )
	{
		alert("You have opended to many tabs. Please close a tab to open another one.");
		return true;
	}
	
	return false;


}

function RefreshTabs ( fId )
{

	// Get tab container
	var div			= document.getElementById( tabStripId );
	
	// Get tab container
	var tabs		= div.getElementsByTagName("div");
	
	
	
	for ( x = 0; x < tabs.length; x++ )
	{	
		var tab = tabs[ x ];
		
		if ( tab.fId == fId )
		{
			var currentId = parseInt ( tab.id.replace( tabStripId +  "_tab_","") );
			iFrame = document.getElementById( tabStripId + "_iFrameWindow_" + tabId );
			iFrame.refresh = "true";
		
		}
	}

}

function RefreshTabsByUrl ( url )
{

	// Get div
	var div			= document.getElementById( tabStripId );
	
	// Get tab container
	var tabs		= div.getElementsByTagName("div");
	
	for ( x = 0; x < tabs.length; x++ )
	{	
		var tab = tabs[ x ];
		var currentId = parseInt ( tab.id.replace( tabStripId +  "_tab_","") );
		
		iFrame = document.getElementById( tabStripId + "_iFrameWindow_" + currentId );
		
		if ( iFrame.src.indexOf( url ) != -1 )
		{
			iFrame.refresh = "true";
			return;
		}
		
	}

}


function HideAllContents()
{
	// Hide all frame windows
	frameWindows = 	contentContainer.getElementsByTagName("iframe");
	
	for ( x = 0; x < frameWindows.length; x ++ )
	{	
		frameWindows[ x ].style.display = "none";
	}

}

// ----------------------------------------------
// Renames current tab
// ----------------------------------------------
function RenameCurrentTab( newCaption )
{
	
	// Get target table and the first row
	var tab = document.getElementById( tabStripId + "_tab_" + currentTabIndex );
	tab.childNodes[0].removeChild( tab.childNodes[0].childNodes[0]);
	tab.childNodes[0].appendChild( document.createTextNode ( newCaption ));
} 


// ----------------------------------------------
// Closes the current open tab
// ----------------------------------------------
function CloseCurrentTab()
{
	 CloseTab( currentTabIndex );
}


