var gTabs  = new Array()
var dTab   = 1 // default tab

// default tab colors
var gActCol = '#cbd9e6'
var gTabCol = '#5c81a3'
var angleGif = 'images/angle.gif'

/* Neue Variablen */
var gStyle;

var gColWidth = 113; //Defaultwert.
var gTabRowCnt = 0;
var gTabCnt = 0;
var gMaxColLen = 18; //Defaultwert; Wird in newTab() je nach Reiterbreite gesetzt.
var gActColumn;

var gTabColPosInd = new Array();
var colLimiter = new Array()

// ==============================================================================

function set_tab_style(style){
	switch(style){
		case 'ci':
			gActCol = '#cbd9e6'
			gTabCol = '#5c81a3'
			break
		
	}
  
  gStyle = style;
}

// ==============================================================================



function tabs() { 


	var tB ='';
	var len = gMaxColLen + 1;
	for( var i=1; i<len; i++ ) {
		tB += '<td width="' + gColWidth + '" height="1" bgcolor="#99aec1">';
		tB += '<img src="images/dot.gif" width="' + gColWidth + '" height="1">';
		tB += '</td>';
		if( i < gMaxColLen ) {
		  tB += '<td width="7" height="1" bgcolor="#99aec1"><img src="images/dot.gif" width="7" height="1"></td>';
		}
	}
	tB += wrtTabRows();
	document.write(tB);

  
}

// ========================================

function wrtTabRows() {

  var tB = "";
  for( var k=gTabRowCnt; k>0; k-- ) {
    tB += "<tr>";
    var rowKey = "R" + k;
    var nextRow = gTabs[rowKey];
    //var len = nextRow.length;
    //for( var m=0; m<len; m++ ) {
    for( m in nextRow ) {
      tB += nextRow[m].mWrtTab();
    }
    
    var n = gMaxColLen - nextRow.length;
    if( n > 0 ) {
      for( var n; n>0; n-- ) {    
        tB += '<td width="' + gColWidth + '" height="1" bgcolor="#99aec1">';
        tB += '<img src="images/dot.gif" width="' + gColWidth + '" height="1">';
        tB += '</td>';
        if( n > 1 ) {
          tB += '<td width="7" height="1" bgcolor="#99aec1">';
          tB += '<img src="images/dot.gif" width="7" height="1">';
          tB += '</td>';
        }
      }
    
    }
    
    tB += "</tr>"; 
  }
  return tB;
  
}

// ==============================================================================

function setTabColPosToIndex( whText, whRow, whCol, whID ) {

whCol=1;
  var rowKey = "R" + whRow;
  if( typeof(gTabColPosInd[rowKey]) == "undefined" ) {
    gTabColPosInd[rowKey] = new Array();
    gActColumn = 0;
  }

  gActColumn += whCol;
  gTabColPosInd[rowKey]["CP" + gActColumn] = whID;
  
  return gActColumn;
}

// ========================================

function getCompanionTabIndex( row, myColPos ) {

  var ind = gTabColPosInd["R" + row]["CP" + myColPos];
  if( typeof(ind) == "undefined" ) {
    ind = -1;
  }

  return ind;
  
}

// ========================================

function getCompanionTabStatus( myRow, compInd ) {

  var rowKey = "R" + (myRow+1) ;
  var active = gTabs[rowKey][compInd].isActive;
  return active;
  
}

// ==============================================================================

/* for older versions without 'mainnavigation'-Parameter  */
if( typeof(mainnavigation) == "undefined" ) {
  mainnavigation = true;
}


// The following max cols are defined for framesets without left main navigation. 
function newTab( cols, lnk, txt, row ){
	//Update
	if(typeof(cols) == 'number') {
		if (cols == 1){
			gColWidth = 113;
			gMaxColLen = mainnavigation ? 6 : 7;
		}
		if (cols == 2){
			gColWidth = 233;
			gMaxColLen = mainnavigation ? 3 : 3;
		}
		if (cols == 3){
			gColWidth = 33;
			gMaxColLen = mainnavigation ? 15 : 17;
		}
		if (cols == 4){
			gColWidth = 53;
			gMaxColLen = mainnavigation ? 12 : 14;
		}
	}
	// Column Limiter
	if(typeof colLimiter[row] == 'undefined'){
		colLimiter[row] = 0
	}
	colLimiter[row] ++
	if(colLimiter[row] > gMaxColLen) return

  /* for older versions without 'row'-Parameter  */
  if( typeof(row) == "undefined" ) {
    row = 1;
  }

  var rowKey = "R" + row;
  if( typeof(gTabs[rowKey]) == "undefined" ) {
    gTabs[rowKey] = new Array();
    gTabRowCnt++;
  }
  
  tabArr = gTabs[rowKey];
  gTabCnt++;

  tabArr[tabArr.length] = new mkTabObj( tabArr.length, gTabCnt, txt, lnk, row, cols );
}

// ==============================================================================
// ==============================================================================

function mkTabObj( id, num, whTxt, whLnk, whRow, whCol ) {

  // Properties:
  this.id = id;     // index in gTabs
  this.num = num;   // Nummer in Query
  
  this.pText = whTxt;
  this.pLink = whLnk + '?tab=' + this.num;
  this.pRow = whRow;
  this.pCols = whCol;
  this.pColPos;

	var act = location.search.substr(5);
	act = act != '' ? act : dTab;
	this.isActive = this.num == act ? true : false;
  
  this.pWidth = gColWidth 	
// old definition + (this.pCols-1)*(gColWidth+7);
  
  this.pHeight = this.pRow > 1 ? 16 : this.isActive ? 18 : 16;
  this.pColor = this.isActive ? gActCol : gTabCol;
  
  // Functions:  
  this.mWrtTab = M_wrtTab;
  this.mWrtCorner = M_wrtCorner;

  // Initialisation:
  this.pColPos = setTabColPosToIndex( this.pText, this.pRow, this.pCols, this.id );

}

// ========================================

function M_wrtTab() {

	var out = '<td colspan="2" height="18" valign="top">';
	
	out += '<table cellpadding="0" cellspacing="0" border="0">';
	out += '<tr>';

	/* --- Abstand vor Text + Link --- */
	out += '<td width="6" bgcolor="' + this.pColor + '" class="menu">&nbsp;</td>';
	out += '<td bgcolor="' + this.pColor + '" width="' + (this.pWidth - 13) + '" height="' + this.pHeight + '">';
	out += '<a href="' + this.pLink + '" class="menu">' + this.pText + '</a>'; 
	out += '</td>'; 
  
  
  /* --- obere, rechte Ecke --- */
  var cornerGif = angleGif;
  if( this.pRow < gTabRowCnt ) {
    //var compTabInd = getCompanionTabIndex( this.pRow+1, this.id + this.pCols );
    var compTabInd = getCompanionTabIndex( this.pRow+1, this.pColPos );  
    if( compTabInd != -1 ) {
      var gifNum = 0;
//alert("R1: " + this.pText + " --> " + gTabs["R" + (this.pRow+1)][compTabInd].pText);
      if( this.isActive ) {
        gifNum = 1;
      }
      else {
        if( getCompanionTabStatus( this.pRow, compTabInd ) ) {
          gifNum = 2;
        }
      }
      cornerGif = 'images/' + gStyle + '_' + gifNum + '_active.gif';        
    }  
  }
  out += this.mWrtCorner( cornerGif );
  
  
  /* --- Abstand zum naechsten Tab + Row-Ende --- */
  if( this.pColPos != gMaxColLen ) {
    out += '<td bgcolor="#99aec1" width="7"><img src="images/dot.gif" width="7"></td>';
  }
  out += '</tr>';
  
  
  /* --- Abstand-TR zu unterem Tab --- */
  if( this.pRow > 1 ) {
    var color = this.pColor;
    var rowOffsetGif;
    //var compTabInd = getCompanionTabIndex( this.pRow-1, this.id + this.pCols );  
    var compTabInd = getCompanionTabIndex( this.pRow-1, this.pColPos );
    if( compTabInd != -1 ) {
//alert("R2: " + this.pText + " --> " + gTabs["R" + (this.pRow-1)][compTabInd].pText);
      rowOffsetGif = 'images/' + gStyle + '_2_inactive_small.gif';
      if( this.isActive ) {
        rowOffsetGif = 'images/' + gStyle + '_2_active_small.gif';
      }
    }
    else {
      color = "#FFFFFF";
      rowOffsetGif = 'images/dot.gif';
    }
    out += '<tr>';
    out += '<td width="' + (this.pWidth-7) + '" height="2" bgcolor="#99aec1" colspan="2">';    
    out += '<img src="images/dot.gif">';
    out += '</td>';    
    out += '<td bgcolor="' + color + '" valign="top">';    
    out += '<img src="' + rowOffsetGif + '" width="7" height="2" border="0">';
    out += '</td>';
    if( this.pColPos != gMaxColLen ) {
      out += '<td bgcolor="#99aec1" width="7"><img src="images/dot.gif"></td>';
    }
    out += '</tr>';    
  }
  
  
  /*---  Tab Ende --- */
  out += '</table>';
  out += '</td>';
  
  return out;
}

// ========================================

function M_wrtCorner( whGif ) {
  var res = '';
  res += '<td bgcolor="' + this.pColor + '" width="7" height="7" valign="top">';
  res += '<img src="' + whGif + '">';
  res += '</td>';
  
  return res;
}

// ==============================================================================
// ==============================================================================


