// JavaScript Document

function TabNavigationProvider(aName, aTab)
{
  this.name =aName;
  window[aName]=this;
  this.naviListener=new Array();
  this.dist=30;
  this.lastTabActiviated = false;
  this.backButtonUsageCounter = 0;
  this.width=120;
  this.rightPos=null;
  this.leftButtonName = '_l_'+aName;
  this.rightButtonName = '_r_'+aName;
  this.reloadButtonName = '_b_'+aName;
  this.homeButtonName = '_h_'+aName;
  this.tab=aTab;
  this.buttons=new Array();
  this.tab.addTabStateListener(this);
}
/**
 * Setzt den Zaehler fuer die Verwendung des Back Button zurueck
 * und setzt den Forward Button auf false.
 * @author Markus Hanses
 */
TabNavigationProvider.prototype.resetBackCounter=function()
{
	this.backButtonUsageCounter = 0;
	this.buttons['forward'].setEnabled(false);
}

TabNavigationProvider.prototype.tabStateChanged=function(anEvent)
{
  var theNewTab = anEvent.newTab;
  var backState = this.buttons['back'].getState;
  var forwardState = this.buttons['forward'].getState;  
  
  if (theNewTab == 0)
  {
    this.buttons['back'].setEnabled(false);
	var forwardStateTemp = this.backButtonUsageCounter > 0 ? true : false;
	this.buttons['forward'].setEnabled(forwardStateTemp);
  }
  else if (theNewTab > 0)
  {
    this.buttons['back'].setEnabled(true);
	var forwardStateTemp = this.backButtonUsageCounter > 0 ? true : false;
	this.buttons['forward'].setEnabled(forwardStateTemp);
  }
  if (theNewTab == (this.tab.getTabCount()-1))
  {
    this.buttons['forward'].setEnabled(false);
	this.lastTabActivated = true;
  }
  else if ((theNewTab < (this.tab.getTabCount()-1)
  			&& this.lastTabActivated)
			|| this.backButtonUsageCounter > 0)
  {
    this.buttons['forward'].setEnabled(true);
  }
}

TabNavigationProvider.prototype.createNaviElement=function()
{
  var theElement = new DIVElement();
  theElement.addAttribute('class', 'tab_navi_parent');
  theElement.addStyleElement('position','absolute');
  theElement.addStyleElement('width',this.width+'px');
  theElement.addElement(this.createLeftElement());
  theElement.addElement(this.createRightElement());
  //theElement.addElement(this.createReloadElement());
  theElement.addElement(this.createHomeElement());
  return theElement;
}

TabNavigationProvider.prototype.createLeftElement=function()
{
  var theBackButton = new IconButton(this.leftButtonName, 
		'img/topnav_pfeil_links_weiss.gif',
		'img/topnav_pfeil_links_grau.gif',
		'img/topnav_pfeil_links_grau.gif');
  this.buttons['back']=theBackButton;  
  theBackButton.setPosition(0,0);
  theBackButton.setNaviText('zur&#252;ck');
  theBackButton.setEnabled(false);  
  theBackButton.setStyleClass('tabnavi_element');
  return theBackButton.getElement();
}

TabNavigationProvider.prototype.createRightElement=function()
{
  var theForwardButton = new IconButton(this.rightButtonName, 
  		'img/topnav_pfeil_rechts_weiss.gif',
		'img/topnav_pfeil_rechts_grau.gif',
		'img/topnav_pfeil_rechts_grau.gif');
	theForwardButton.setNaviText('vor');
  this.buttons['forward']=theForwardButton;
  var theBackButton = this.buttons['back'];
  theForwardButton.addActionListener(new RightAction(this, theForwardButton, theBackButton));  
  theBackButton.addActionListener(new LeftAction(this, theBackButton, theForwardButton));
  theForwardButton.setPosition(0,this.dist * 5);
  theForwardButton.setStyleClass('tabnavi_element');
  theForwardButton.setEnabled(false);
  return theForwardButton.getElement();
}

TabNavigationProvider.prototype.createReloadElement=function()
{
  var theRelButton = new IconButton(this.reloadButtonName, 
  		'img/topnav_reload_weiss.gif',
		'img/topnav_reload_grau.gif',
		'img/topnav_reload_grau.gif');
  theRelButton.addActionListener(new ReloadAction(this));
  theRelButton.setPosition(0,(this.dist*10));
  theRelButton.setNaviText('neu laden');
  theRelButton.setStyleClass('tabnavi_element');
  this.buttons['reload'] = theRelButton;
  return theRelButton.getElement();
}

TabNavigationProvider.prototype.createHomeElement=function()
{
  var theRelButton = new IconButton(this.homeButtonName, 
  		'img/topnav_home_weiss.gif',
		'img/topnav_home_grau.gif',
		'img/topnav_home_grau.gif');
	theRelButton.setNaviText('&#220;bersicht');
  theRelButton.addActionListener(new HomeAction(this));
  theRelButton.setPosition(0,(this.dist*15));
  theRelButton.setStyleClass('tabnavi_element');
  return theRelButton.getElement();
}



TabNavigationProvider.prototype.write=function()
{
  var theElement = this.createNaviElement();
  return theElement.write();
}


TabNavigationProvider.prototype.addListener=function(aListener)
{
  this.naviListener[this.naviListener.length]=aListener;
}

TabNavigationProvider.prototype.notifyListener=function(anEvent)
{
  for (var i=0;i<this.naviListener.length;i++)
    this.naviListener[i].actionPerformed(anEvent);
}


TabNavigationProvider.prototype.reload=function()
{
  var theEvent = new NavigationEvent(this, 'RELOAD');
  this.notifyListener(theEvent);
}

TabNavigationProvider.prototype.forward=function()
{
  	var theEvent = new NavigationEvent(this, 'FORTH');
	this.backButtonUsageCounter = this.backButtonUsageCounter > 0 
		? this.backButtonUsageCounter-1 : this.backButtonUsageCounter;
  	this.notifyListener(theEvent);	
}

TabNavigationProvider.prototype.back=function()
{
  	var theEvent = new NavigationEvent(this, 'BACK');
	this.backButtonUsageCounter++;
  	this.notifyListener(theEvent);  	
}


TabNavigationProvider.prototype.home=function()
{
  var theEvent = new NavigationEvent(this, 'HOME');
  this.notifyListener(theEvent);

}



function NavigationEvent(aSource, anAction)
{
  this.RELOAD='RELOAD';
  this.FORTH='FORTH';
  this.BACK='BACK';
  this.HOME='HOME';
  this.action=anAction;
  this.source=aSource;
}

NavigationEvent.prototype.getSource=function()
{
  return this.source;
}

NavigationEvent.prototype.getAction=function()
{
  return this.action;
}


function TabNaviListener(aTab, aTopRequestor, aDepRequestor, aDestRequestor)
{
  this.tab=aTab;
  this.topRequestor=aTopRequestor;
  this.depReq=aDepRequestor;
  this.destReq=aDestRequestor;
}

TabNaviListener.prototype.actionPerformed=function(anEvent)
{
  if (anEvent.getAction()==anEvent.RELOAD)
  {  	
    this.topRequestor.callFlights();
	if (this.tab.getSelectedTab()) 
	{
		this.depReq.callFlights(); 
		this.destReq.callFlights();	
	}	
  }
  else if (anEvent.getAction()==anEvent.FORTH)
  {
    this.tab.nextTab();
  }
  else if (anEvent.getAction()==anEvent.BACK)
  {
    this.tab.prevTab();
  }
  else if (anEvent.getAction()==anEvent.HOME)
  {  	
	this.topRequestor.resetRequestor();
  	this.depReq.resetRequestor();
  	this.destReq.resetRequestor();
	reset();
	this.topRequestor.callFlights();
	this.tab.selectTab(0);
	this.tab.resetBackCounterInTabNavigationProvider();	
  }
}

//- -------------------------- Button - ActionListener definitions -----------------------------------
function LeftAction (aNavi, aLeft, aForward)
{
  this.navi=aNavi;
  this.button=aLeft;
  this.forward = aForward;
}

LeftAction.prototype.actionPerformed=function(anEvent)
{
  this.navi.back();
  
  if (this.navi.tab.getSelectedTab()==0)
  {
    this.button.setEnabled(false);
  }
  if ((this.navi.tab.getTabCount()-1) > this.navi.tab.getSelectedTab())
  {
    this.forward.setEnabled(true);
  }
}

LeftAction.prototype.setForwardButton=function(aButton)
{
  this.forward=aButton;
}

function RightAction (aNavi, aRight, aLeft)
{
  this.navi=aNavi;
  this.button=aRight;
  this.left=aLeft;
}

RightAction.prototype.actionPerformed=function(anEvent)
{
  this.navi.forward();
  if (this.navi.tab.getSelectedTab()==(this.navi.tab.getTabCount()-1))
  {
    this.button.setEnabled(false);
  }
  if (this.navi.tab.getSelectedTab()>0)
  {
    this.left.setEnabled(true);
  }
}


function ReloadAction (aNavi)
{
  this.navi=aNavi;
}

ReloadAction.prototype.actionPerformed=function(anEvent)
{
  this.navi.reload();
}

function HomeAction (aNavi)
{
  this.navi=aNavi;
}

HomeAction.prototype.actionPerformed=function(anEvent)
{
  this.navi.home();
}

