function TabbedPane(aName, aParentElement) 
{
	this.name=aName;
	this.parentElement=aParentElement;
	this.elements='';
	this.tabList = new Array();
	this.currentTab=-1;
	this.renderer = new TabRenderer(this);
	this.viewsize=null;
	window[name]=this;
	this.viewsize=null;
	this.headerNavi=null;
	this.tabStateListener=new Array();
	return this;
}

TabbedPane.prototype.getSelectedTab=function(aTabNo)
{
  return this.currentTab;
}



TabbedPane.prototype.selectTab=function(aTabNo)
{
  if (aTabNo != this.currentTab)
  {
    var theCurrentTab = this.currentTab;
    this.setVisible(aTabNo);
    this.tabStateChanged(theCurrentTab, aTabNo);
	if (aTabNo == 1)
	{
		// Slider Reaktivierung
		var sliderCounter = window.A_SLIDERS.length;
		for (var i = 0; i < sliderCounter; i++) 
		{
			window.A_SLIDERS[i].setSliderActive();
		}
	} else {
		// Slider Reaktivierung
		var sliderCounter = window.A_SLIDERS.length;
		for (var i = 0; i < sliderCounter; i++) 
		{
			window.A_SLIDERS[i].setSliderInactive();
		}
	}
  }
}

TabbedPane.prototype.nextTab=function()
{
  var theTabNo = this.currentTab+1;
  if (theTabNo >=0 && theTabNo<this.getTabCount()){
    this.selectTab(theTabNo);
	if (theTabNo == 1)
	{
		// Slider Reaktivierung
		var sliderCounter = window.A_SLIDERS.length;
		for (var i = 0; i < sliderCounter; i++) 
		{
			window.A_SLIDERS[i].setSliderActive();
		}
	} else {
		// Slider Reaktivierung
		var sliderCounter = window.A_SLIDERS.length;
		for (var i = 0; i < sliderCounter; i++) 
		{
			window.A_SLIDERS[i].setSliderInactive();
		}
	}
  }
}

TabbedPane.prototype.prevTab=function()
{
  var theTabNo = this.currentTab-1;
  if (theTabNo >=0 && theTabNo<this.getTabCount()){
    this.selectTab(theTabNo);
	if (theTabNo == 1)
	{
		// Slider Reaktivierung
		var sliderCounter = window.A_SLIDERS.length;
		for (var i = 0; i < sliderCounter; i++) 
		{
			window.A_SLIDERS[i].setSliderActive();
		}
	} else {
		// Slider Reaktivierung
		var sliderCounter = window.A_SLIDERS.length;
		for (var i = 0; i < sliderCounter; i++) 
		{
			window.A_SLIDERS[i].setSliderInactive();
		}
	}
  }
}

TabbedPane.prototype.tabStateChanged=function(aOld, aNew)
{
  if (aOld!=aNew)
  {
    var theEvent = new TabChangeEvent(this, aOld, aNew);
    for(var i=0; i<this.tabStateListener.length;i++)
    {
      this.tabStateListener[i].tabStateChanged(theEvent);
    }
  }
}

TabbedPane.prototype.addTabStateListener=function(aListener)
{
  this.tabStateListener[this.tabStateListener.length]=aListener;
}

TabbedPane.prototype.setHeaderNaviRenderer=function(aRenderer)
{
  this.headerNavi=aRenderer;
  this.renderer.setNavigationProvider(aRenderer);
}
/**
 * Setzt den Zaehler fuer den Back Button im TabNavigationProvider zurueck.
 * @author Markus Hanses
 */
TabbedPane.prototype.resetBackCounterInTabNavigationProvider=function()
{
	this.headerNavi.resetBackCounter();
}

TabbedPane.prototype.setViewportSize=function(aWidth, aHeight)
{
  this.viewsize=new Point(aWidth, aHeight);
}

TabbedPane.prototype.toTabPaneString=function()
{
  document.write("TabbedPane: "+this.name+" count:"+this.getTabCount());
}

TabbedPane.prototype.setTabRenderer=function(aRenderer)
{
  this.renderer = aRenderer;
}

TabbedPane.prototype.getTab=function(aNumber) {
  if (this.getTabCount()> aNumber) {
    return this.tabList[aNumber];
  }
  return '';
}

TabbedPane.prototype.getTabCount=function() {
 return this.tabList.length;
}

TabbedPane.prototype.show=function()
{
  this.setVisible(this.currentTab);
  return this.elements;
}


TabbedPane.prototype.setVisible=function(aNumber){
 if (aNumber >=0 && aNumber<this.getTabCount()){
  var theCurrentTab = this.getTab(this.currentTab);
  if(theCurrentTab) {
   if (aNumber != theCurrentTab.number)
      theCurrentTab.setVisible(false);
  }
  else {
   alert ("No current tab available");
  }
  var theSelected = this.getTab(aNumber);
  this.currentTab=theSelected.number;
  theSelected.setVisible(true);
 }
 else alert(aNumber+" not available!");
}


TabbedPane.prototype.openTab=function() {
 if (this.viewsize)
 {
  this.renderer.setViewport(this.viewsize.getWidth(),this.viewsize.getHeight());
 }

 var theTabbedPane = this.renderer.renderPane(this);
 this.elements = theTabbedPane;
 this.parentElement.innerHTML = theTabbedPane;
}

TabbedPane.prototype.createTab=function(aContent, aHeader)
{
  var theTab = new Tab();
  var theTabContent = aContent;
  if (theTabContent == null)
    theTabContent = new TextContent('Seite '+this.getTabCount());
  theTab.setTabContent(theTabContent);
  if (aHeader == null)
    aHeader = new TextContent('No Header defined');
  theTab.setTabHeader(aHeader);
  this.addTab(theTab);
}

TabbedPane.prototype.addTab=function(aTab) {
  aTab.number = this.getTabCount();
  this.tabList[aTab.number]=aTab;
  if (this.currentTab<0){
      this.currentTab=0;
      aTab.isVisible=true;
  }
  else aTab.isVisible=false;
}

function Tab(){
 this.header;
 this.content;
 this.number;
 this.isVisible=false;
 this.setTabContent = setTabContent;
 this.getTabContent = getTabContent;
 this.setTabHeader = setHeader;
 this.getTabHeader = getHeader;
 this.toString = toTabString;
 this.setHeaderVisible=setHeaderVisible;
 this.setContentVisible=setContentVisible;
 this.setVisible = setTabVisible;
 this.headerStyle = null;
 this.contentStyle=null;
 this.id=null;
 this.headerId=null;
}

function setTabVisible(aBoolean)
{
  this.isVisible=aBoolean;
  this.setHeaderVisible(aBoolean);
  this.setContentVisible(aBoolean);
}

function setHeaderVisible(aBoolean){
  if (this.headerStyle == null && this.headerId != null)
  {
    this.headerStyle = document.getElementById(this.headerId).style;
  }
  if(aBoolean && this.headerStyle)
  {
    this.header.setVisible(true);
  }
  else if(!aBoolean && this.headerStyle)
  {
    //this.headerStyle.visibility='hidden'; header will remain visible
    this.header.setVisible(false);
  }
}


function setContentVisible(aBoolean){
  if (this.contentStyle == null && this.id != null)
  {
    this.contentStyle = document.getElementById(this.id).style;
  }
  else {}
  
  if(aBoolean && this.contentStyle)
  {
    this.contentStyle.visibility='visible';
    this.content.setVisible(true);
  }
  else if(!aBoolean && this.contentStyle)
  {
    this.contentStyle.visibility='hidden';
    this.content.setVisible(false);
  }
}

function getHeader()
{
  return this.header;
}
function setHeader(aHeader)
{
  this.header = aHeader;
}

function setTabContent(aContent)
{
  this.content=aContent;
}
function getTabContent()
{
  return this.content;
}
function toTabString()
{
  return "TAB no.:"+this.number+" visible:"+this.isVisible;
}


function TabRenderer(aTabPane) {
 this.tabbedPane=aTabPane;
 this.viewport=null;
 this.navigationProvider = null;
}

TabRenderer.prototype.setViewport=function(aWidth, aHeight)
{
  var theViewPort = new Viewport();
  theViewPort.setSize(aWidth, aHeight);
  this.viewport = theViewPort;
}

TabRenderer.prototype.setNavigationProvider=function(aNaviRenderer)
{
  this.navigationProvider=aNaviRenderer;
}


TabRenderer.prototype.renderPane=function (aTabPane) {
  var theTabbedPane  = '<div id="TabbedPane_"'+aTabPane.name+'">';
  if (this.viewport!=null)
  {
    var theViewport=this.viewport.write();
    theTabbedPane+=theViewport;
  }
  else
  {
    //alert("Viewport not set");
  }
  theTabbedPane+=this.createHeader(aTabPane);
  theTabbedPane+=this.createElements(aTabPane);
  theTabbedPane+='</div>';
  return theTabbedPane;
}

TabRenderer.prototype.getHeaderWidth=function()
{
  return 600;
}

TabRenderer.prototype.getHeaderHeight=function()
{
  return 0;
}

TabRenderer.prototype.getTabWidth=function()
{
  return 600;
}

TabRenderer.prototype.getTabHeight=function()
{
  return 450;
}


TabRenderer.prototype.createHeader=function(aTabbedPane)
{
 var theTabCount = aTabbedPane.getTabCount();
 var theContent='';
 var theNextTab;
 var theTabHeader;
 var theLeftHeaderPos = 0;
 var theParent=new DIVElement();
 theParent.addAttribute('class','tabheader_parent');
 theParent.addStyleElement('position', 'absolute');
 theParent.addStyleElement('width', this.getHeaderWidth()+'px');
 theParent.addStyleElement('height', this.getHeaderHeight()+'px');


 theParent.addStyleElement('z-index', '70');

 for (i=0;i<theTabCount;i++) {
   theNextTab=aTabbedPane.getTab(i);
   theNextTab.headerId='tabheader_'+i;
   theContent+='<div id="'+theNextTab.headerId+'" class="tabheader" style="position:absolute; top:0px; z-index:100;';
   theContent+=' left:'+theLeftHeaderPos+'px; visibility:visible;"';
   theContent+='" onClick="window.'+aTabbedPane.name+'.setVisible('+theNextTab.number;
   theContent+=')" onmouseover="document.body.style.cursor=\'pointer\'" onmouseout="document.body.style.cursor=\'default\'">';
   theTabHeader = theNextTab.getTabHeader();
   if(theTabHeader != null)
   {
    theLeftHeaderPos+=theTabHeader.width;
    if (theNextTab.isVisible)
      theTabHeader.visible = true;
    var theInput = theTabHeader.show();
    theContent=theContent+theInput;
   }
   else
    alert('header not set:'+theNextTab.headerId);
   theContent+='</div>';
  }
  theContent+=this.createNavigation();
  theParent.addContent(theContent);
  return theParent.write();
 }

TabRenderer.prototype.createNavigation=function()
{
  if (this.navigationProvider != null)
  {
    return this.navigationProvider.write();
  }
  return '';
}

TabRenderer.prototype.createElements=function(aTabbedPane){
 var theTabCount = aTabbedPane.getTabCount();
 var theContent='';
 var theNextTab;
 var theTabContent;
 var i;
 var theStyle;

 var theParent=new DIVElement();
 theParent.addAttribute('class','tab_parent');
 theParent.addStyleElement('position', 'absolute');
 theParent.addStyleElement('top', this.getHeaderHeight()+'px');
 theParent.addStyleElement('left', '0px');
 theParent.addStyleElement('width', this.getTabWidth()+'px');
 theParent.addStyleElement('height', this.getTabHeight()+'px');

 for (i=0;i<theTabCount;i++) {
   theNextTab=aTabbedPane.getTab(i);
   if (!theNextTab.isVisible) {
    theStyle='visibility:hidden;';
   }
   else {
    theStyle='visibility:visible;';
   }
   theNextTab.id='tab_'+theNextTab.number;
   theContent+='<div id="'+theNextTab.id+'" class="tabcontent" style="'+theStyle;   
   theContent+=' position:absolute; top:0px; left:0px;">';
   theTabContent = theNextTab.getTabContent();
   if(theTabContent != null)
   {
    var theInput = theTabContent.show();
    theContent=theContent+theInput;
   }
   else
    	alert('Content not set');
   theContent+='</div>';
  }
 theParent.addContent(theContent);
 return theParent.write();
}

//---------------------------------- Header Content Handler: HeaderImageContent -------------------------

function HeaderImageContent(aText, aOpenImgUrl, aClosedImgUrl)
{

  this.text = aText;
  this.id=aText+'_'+aOpenImgUrl+'_'+aClosedImgUrl;
  this.openurl=aOpenImgUrl;
  this.closedurl=aClosedImgUrl;
  this.closedImg='<img src="'+this.closedurl+'" alt="'+this.text+'"/>';
  this.openImg='<img src="'+this.openurl+'" alt="'+this.text+'"/>';
  this.show = showHeaderImage;
  this.width=100;
  this.setText=setText;
  this.toString = toImageContent;
  this.setVisible = setHeaderImgVisible;
  this.visible = false;
}


function setHeaderImgVisible(aBoolean)
{
  this.visible=aBoolean;
  var theImgContainer = document.getElementById(this.id);
  if(theImgContainer)
  {
    if(aBoolean)
    {
      theImgContainer.innerHTML=this.openImg;
    }
    else
    {
      theImgContainer.innerHTML=this.closedImg;
    }
  }

}
function showHeaderImage()
{
  if (this.visible)
    return '<div id="'+this.id+'">'+this.openImg+'</div>';
  else
    return '<div id="'+this.id+'">'+this.closedImg+'</div>';
}



//---------------------------------- Example Content Handler: TextContent -------------------------

function TextContent(aText)
{
  this.text = aText;
  this.show = showText;
  this.width=100;
  this.setText=setText;
  this.toString = toTextContent;
  this.setVisible = setTextVisible;

}

function setText(aText)
{
  this.text = aText;
}

function setTextVisible(aBoolean)
{
  //doing nothing... text is just text an no document element!
}

function showText()
{
  return this.text;
}

function toTextContent()
{
  return "TextContent: text="+this.text;
}


//---------------------------------- Example Content Handler: ImageContent -------------------------


function ImageContent(aText, anURL)
{
  this.text = aText;
  this.id=aText+'_'+anURL;
  this.url=anURL;
  this.show = showImage;
  this.width=100;
  this.setText=setText;
  this.toString = toImageContent;
  this.setVisible = setImgVisible;
}

function setImgVisible(aBoolean)
{
  //nothing to do...
}

function toImageContent()
{
  return "ImageContent"+this.text+" Image:"+this.url;
}

function showImage()
{
  return '<div id="'+this.id+'"><img src="'+this.url+'" alt="'+this.text+'"/></div>';
}


//---------------------------------- Example Content Handler: FlightContent -------------------------


function FlightContent(aText)
{
  this.text = aText;
  this.id='__'+aText;
  this.show = initDiv;
  this.width=550;
  this.setText=setText;
  this.toString = toFlightContent;
  this.setVisible = setFlightContentVisible;
  this.flightContentContainer= null;
  this.setFlightContent=setFlightContent;
}

function setFlightContent(aContent)
{
  if (this.flightContentContainer == null)
  {
    this.flightContentContainer = document.getElementById(this.id);
  }
  this.flightContentContainer.innerHTML=aContent;
}

function setFlightContentVisible(aBool)
{
  if(this.flightContainer !=null)
  {
  	if (!aBool)
  	this.flightContainer.style.visibility='hidden';
	else
	this.flightContainer.style.visibility='visible';
  }
}

function initDiv()
{
  return '<div id="'+this.id+'" style="width:"'+this.width+'px; border:10px solid red; padding:10px; font-size:3em;"></div>';
}

function toFlightContent()
{
  return "FlightContent: "+this.id;
}


function TabChangeEvent(aTabpane, aOld, aNew)
{
  this.tab=aTabpane;
  this.oldTab=aOld;
  this.newTab=aNew;
}


function TabChangeListener()
{

}

TabChangeListener.prototype.tabStateChanged=function(aEvent)
{
  nd(300);
}
