// JavaScript Document

/**
 * Navigation Handler for handling calling flightrequestor instances in context of the selection
 *
 */  

function NavigationHandler(aDepartureFlightRequestor, aDestinationRequestor, aTopRequestor)
{
  this.depRequestor = aDepartureFlightRequestor;
  this.destRequestor = aDestinationRequestor;
  this.topRequestor = aTopRequestor;
  this.tabPane=null;
  this.topNo=0;
  this.diagramNo=1;
  this.initialTab=0;
  this.tabResetted=false;
  this.inputIsComplete=false;  
}

NavigationHandler.prototype.update=function()
{
	this.checkInput();	
  	var theNumber = this.evaluateUpdate();  	
  if (this.inputIsComplete)
  {   	
	// Slider Reaktivierung
	var sliderCounter = window.A_SLIDERS.length;
	for (var i = 0; i < sliderCounter; i++) 
	{
		window.A_SLIDERS[i].setSliderActive();
	}
	
    this.callDiagramUpdate();
    if(this.tabPane)
    {
      this.tabPane.selectTab(this.diagramNo);
    }
  }
  else if (theNumber == 1)
  {
    this.callTopListUpdate();
  }
  else if (this.inputIsComplete && theNumber == 3)
  {
    this.callTopListUpdate();
    this.callDiagramUpdate();
    if(this.tabPane)
    {
      this.tabPane.selectTab(this.diagramNo);
    }
  }
  else 
  {
    return;
  }
  this.tabResetted=false;
}

/**
 * Testet, ob die Eingaben fuer eine Abfrage ausreichend sind.
 * @author Markus Hanses
 */
NavigationHandler.prototype.checkInput=function()
{
  	var theFromIata = this.depRequestor.get('fromIataCode');
  	var theToIata = this.depRequestor.get('toIataCode');
 	var startDate = this.depRequestor.get('startDate');
  	var endDate = this.depRequestor.get('endDate');
	var departureLocation = this.depRequestor.get('departureLocation'); 
	var destinationLocation = this.depRequestor.get('destinationLocation');
  	var departureInputOK = startDate != null
		&& startDate.length > 0 
		&& endDate != null	
		&& endDate.length > 0
		&& ((theFromIata != null 
				&& theFromIata.length > 0
				&& theToIata != null 
				&& theToIata.length > 0)
			|| (destinationLocation != null 
				&& destinationLocation.length > 0
				&& departureLocation != null
				&& departureLocation.length > 0)) 
		? true : false;
						
	theFromIata = this.destRequestor.get('fromIataCode');
  	theToIata = this.destRequestor.get('toIataCode');
 	startDate = this.destRequestor.get('startDate');
  	endDate = this.destRequestor.get('endDate'); 
	departureLocation = this.destRequestor.get('departureLocation'); 
	destinationLocation = this.destRequestor.get('destinationLocation');
  	var destinationInputOK = startDate != null 
		&& startDate.length > 0 
		&& endDate != null	
		&& endDate.length > 0
		&& ((theFromIata != null 
				&& theFromIata.length > 0
				&& theToIata != null 
				&& theToIata.length > 0)
			|| (destinationLocation != null 
				&& destinationLocation.length > 0
				&& departureLocation != null
				&& departureLocation.length > 0)) 
		? true : false;

	this.inputIsComplete = departureInputOK && destinationInputOK ? true : false;
}

/**
 * Checks what is the current tab and whether the user has changed to the topNo Tab.
 */
NavigationHandler.prototype.evaluateUpdate=function()
{
  var theSelectedTab = -1;
  if (this.tabPane)
  {
    theSelectedTab=this.tabPane.getSelectedTab();
    
  }
  if (this.inputIsComplete && !this.tabResetted)
  {
    return 2; 
  }
  else if (theSelectedTab==this.diagramNo)
  {
    return 2;
  }
  else if (theSelectedTab==this.topNo)
  {
    return 1;
  }
  return 0; 
}

NavigationHandler.prototype.callDiagramUpdate=function()
{
  this.depRequestor.callFlights();
  this.destRequestor.callFlights();
}

NavigationHandler.prototype.callTopListUpdate=function()
{
  this.topRequestor.callFlights();
}


NavigationHandler.prototype.setTabPane=function(aTabPane, aTopDestTabNo, aDiagramTabNo)
{
  this.initialTab=aTabPane.getSelectedTab();
  this.tabPane = aTabPane;
  this.tabPane.addTabStateListener(this);
  this.topNo=aTopDestTabNo;
  this.diagramNo= aDiagramTabNo;
}

/**
 * Interface implementation of the TabChangeListener. The Change Listener 
 * will be called when the tab will change  
 */
NavigationHandler.prototype.tabStateChanged=function(anEvent)
{
  var theOld = anEvent.oldTab;
  var theNew = anEvent.newTab;
  if (theOld != this.topNo && theNew==this.topNo)
  {
    this.tabResetted=true;
  }
  else
  {
    this.tabResetted=false;
  }
}
/**
 * Sollte nicht mehr verwendet werden.
 * Wurde durch checkInput() ersetzt.
 */
NavigationHandler.prototype.areAllLocationsSet=function()
{
  var theDepLocation = this.depRequestor.get('departureLocation');
  var theDestLocation = this.destRequestor.get('departureLocation')
  
  if (theDepLocation != null && theDepLocation.length>0) 
    
  { 
    if(theDestLocation != null && theDestLocation.length>0)
    { 
      return true;
    }
  }
  return false;
}


/**
 * DepartureCompletionListener for autocompletionfield
 *
 */  

function DepartureCompletionListener(aDepartureFlightRequestor, aDestinationRequestor, aTopRequestor, aNaviHandler)
{
  this.depRequestor = aDepartureFlightRequestor;
  this.destRequestor = aDestinationRequestor;
  this.topRequestor = aTopRequestor;
  this.naviHandler=aNaviHandler;
  this.locationMap=new Array();
}

DepartureCompletionListener.prototype.completionChanged=function(aCompletionEvent)
{ 
   var theLocation = aCompletionEvent.completion;
   this.depRequestor.setDepartureLocation(theLocation.name);
   this.depRequestor.setFromIataCode(theLocation.code);
   
   this.destRequestor.setDestinationLocation(theLocation.name);
   this.destRequestor.setToIataCode(theLocation.code);
   
   this.topRequestor.setDepartureLocation(theLocation.name);
   this.topRequestor.setFromIataCode(theLocation.code);
   
   this.naviHandler.update();

}

DepartureCompletionListener.prototype.setLocationMap=function(aArray)
{
  this.locationMap=aArray;
}

DepartureCompletionListener.prototype.getIataCode=function(aLocation)
{
  var theValue = this.locationMap[aLocation];
  if (!theValue)
  {
    theValue ='';
  }
  return theValue;
}


/**
 * DestinationCompletionListener for autocompletionfield
 *
 */  

function DestinationCompletionListener(aDestRequestor, aDepRequestor, aTopRequestor, aNaviHandler)
{
  this.destRequestor = aDestRequestor;
  this.depRequestor = aDepRequestor;
  this.topRequestor = aTopRequestor;
  this.naviHandler = aNaviHandler;
  this.locationMap=new Array();    
}

DestinationCompletionListener.prototype.setLocationMap=function(aArray)
{
  this.locationMap=aArray;
}

DestinationCompletionListener.prototype.completionChanged=function(aCompletionEvent)
{ 
   var theLocation = aCompletionEvent.completion;   
   this.destRequestor.setDepartureLocation(theLocation.name);
   this.destRequestor.setFromIataCode(theLocation.code);
   
   this.topRequestor.setDestinationLocation(theLocation.name);
   this.topRequestor.setToIataCode(theLocation.code);
   
   this.depRequestor.setDestinationLocation(theLocation.name);
   this.depRequestor.setToIataCode(theLocation.code);
  
   this.naviHandler.update();   
}

DestinationCompletionListener.prototype.getIataCode=function(aLocation)
{
  var theValue = this.locationMap[aLocation];
  if (!theValue)
  {
    theValue ='';
  }
  return theValue;
}

/**
 * StartFlightHandler for listening to calendar events 
 *
 */  

function StartFlightHandler(aDepartureFlightRequestor, aDestinationFlightRequestor,aTopListRequestor, aNaviHandler)
{
  this.depRequestor=aDepartureFlightRequestor;
  this.destRequestor=aDestinationFlightRequestor;
  this.topListRequestor=aTopListRequestor;
  this.cal=new GregorianCalendar();
  this.naviHandler=aNaviHandler;
}

StartFlightHandler.prototype.dateSelected=function(anEvent)
{
  if (anEvent.action == anEvent.SELECT)
  {
    var theCal = anEvent.date;
    this.requestReturnFlight(this.cal.print(theCal));
  }
}

StartFlightHandler.prototype.requestReturnFlight=function(aCal)
{
  this.depRequestor.setStartDate(aCal);
  this.topListRequestor.setStartDate(aCal);
  this.naviHandler.update();
}


/**
 * ReturnFlightHandler for listening to calendar events 
 *
 */  

//----------------------------------- ReturnFlightHander ----------------------------------------
function ReturnFlightHandler(aDepartureFlightRequestor, aDestinationFlightRequestor,aTopListRequestor, aNaviHandler)
{
  this.depRequestor=aDepartureFlightRequestor;
  this.destRequestor=aDestinationFlightRequestor;
  this.topListRequestor=aTopListRequestor;
  this.cal=new GregorianCalendar();
  this.naviHandler = aNaviHandler;
}



ReturnFlightHandler.prototype.dateSelected=function(anEvent)
{
  var theCal = anEvent.date;
  if (anEvent.action == anEvent.SELECT)
  {
    this.requestReturnFlight(this.cal.print(theCal));
  }
}

ReturnFlightHandler.prototype.requestReturnFlight=function(aCal)
{
  this.destRequestor.setStartDate(aCal);
  this.depRequestor.setEndDate(aCal);
  this.topListRequestor.setEndDate(aCal);
  
  this.destLocation=this.depRequestor.getDestinationLocation();
  this.depLocation=this.depRequestor.getDepartureLocation();

  this.naviHandler.update();
}

/**
 * Listener fuer den Uhrzeit Regler des Hinflugs.
 * @author Markus Hanses
 * @param {Object} aDepRequestor
 * @param {Object} aTopListReq
 * @param {Object} aNaviHandler
 */
function DepartureTimeSliderListener(aDepRequestor, aTopListReq, aNaviHandler)
{
  this.lastStartPos=null;
  this.lastEndPos=null;
  this.depRequestor = aDepRequestor;
  this.topRequestor = aTopListReq;
  this.naviHandler = aNaviHandler;
}
/**
 * Registriert eine Veraenderung des Reglers.
 * @author Markus Hanses
 * @param {Object} aSliderEvent
 */
DepartureTimeSliderListener.prototype.sliderPosChanged=function(aSliderEvent)
{  
  var theNewStartPosition = aSliderEvent.newStartValue;
  var theNewEndPosition = aSliderEvent.newEndValue;
  if (theNewStartPosition != this.lastStartPos
  		|| theNewEndPosition != this.lastEndPos)
  {
    this.lastStartPos = theNewStartPosition;
	this.lastEndPos = theNewEndPosition;
    this.depRequestor.setStartTime(theNewStartPosition);
	this.depRequestor.setEndTime(theNewEndPosition);
    //this.topRequestor.setEndTime(theNewEndPosition);        
	this.depRequestor.callFlights();	
  }  
}
/**
 * Listener fuer den Regler der Uhrzeit fuer den Rueckflug. 
 * @author Markus Hanses
 * @param {Object} aDepRequestor
 * @param {Object} aTopListReq
 * @param {Object} aNaviHandler
 */
function DestinationTimeSliderListener(aDepRequestor, aTopListReq, aNaviHandler)
{
  this.lastStartPos=null;
  this.lastEndPos=null;
  this.depRequestor = aDepRequestor;
  this.topRequestor = aTopListReq;
  this.naviHandler = aNaviHandler;
}
/**
 * Reagiert auf eine Veraenderung des Reglers.
 * @author Markus Hanses
 * @param {Object} aSliderEvent
 */
DestinationTimeSliderListener.prototype.sliderPosChanged=function(aSliderEvent)
{  
  var theNewStartPosition = aSliderEvent.newStartValue;
  var theNewEndPosition = aSliderEvent.newEndValue;
  if (theNewStartPosition != this.lastStartPos
  		|| theNewEndPosition != this.lastEndPos)
  {
    this.lastStartPos = theNewStartPosition;
	this.lastEndPos = theNewEndPosition;
    this.depRequestor.setStartTime(theNewStartPosition);
	this.depRequestor.setEndTime(theNewEndPosition);
    //this.topRequestor.setEndTime(theNewEndPosition);        
	this.depRequestor.callFlights();	
  }  
}

/**
 * Listener fuer den Preis den Fluges vom Abflughafen zum Zielflughafen
 * @author Markus Hanses
 * @param {Object} aDepRequestor
 * @param {Object} aTopListReq
 * @param {Object} aNaviHandler
 */
function DeparturePriceSliderListener(aDepRequestor, aTopListReq, aNaviHandler)
{
  this.lastPos=null;
  this.depRequestor = aDepRequestor;
  this.topRequestor = aTopListReq;
  this.naviHandler = aNaviHandler;
}
/**
 * Die Position des Reglers hat sich veraendert
 * @author Markus Hanses
 * @param {Object} aSliderEvent
 */
DeparturePriceSliderListener.prototype.sliderPosChanged=function(aSliderEvent)
{  
  var theNewPosition = aSliderEvent.newValue;
  if (theNewPosition != this.lastPos)
  {
    this.lastPos = theNewPosition;
    this.depRequestor.setMaxPrice(theNewPosition);
    this.topRequestor.setMaxPrice(theNewPosition);      
	this.depRequestor.callFlights();
	
  }
  
}
/**
 * Listener fuer den Preis des Rueckffluges.
 * @author Markus Hanses
 * @param {Object} aDestRequestor
 * @param {Object} aTopListReq
 * @param {Object} aNaviHandler
 */
function DestinationPriceSliderListener(aDestRequestor, aTopListReq, aNaviHandler)
{
  this.lastPos=null;
  this.destRequestor = aDestRequestor;
  this.topRequestor = aTopListReq;
  this.naviHandler = aNaviHandler;
}
/**
 * Die Position des Reglers hat sich veraendert.
 * @author Markus Hanses
 * @param {Object} aSliderEvent
 */
DestinationPriceSliderListener.prototype.sliderPosChanged=function(aSliderEvent)
{  
  var theNewPosition = aSliderEvent.newValue;
  if (theNewPosition != this.lastPos)
  {
    this.lastPos = theNewPosition;
    this.destRequestor.setMaxPrice(theNewPosition);
    this.topRequestor.setMaxPrice(theNewPosition);    
    //this.naviHandler.update();    
	this.destRequestor.callFlights();
  }
  
}

/**
 * Listener fuer die Toleranz der Tage des Abflugdatums des Hinflugs.
 * @author Markus Hanses
 * @param {Object} aDepRequestor
 * @param {Object} aTopListReq
 * @param {Object} aNaviHandler
 */
function DepartureDayToleranceSliderListener(aDepRequestor, aTopListReq, aNaviHandler)
{
  this.lastPos=null;
  this.depRequestor = aDepRequestor;  
  this.topRequestor = aTopListReq;
  this.naviHandler=aNaviHandler;
}

/**
 * Erfasst die Veraenderung der Reglerposition.
 * @author Markus Hanses
 * @param {Object} aSliderEvent
 */
DepartureDayToleranceSliderListener.prototype.sliderPosChanged=function(aSliderEvent)
{
   var theNewPosition = aSliderEvent.newValue;
   
  if (theNewPosition != this.lastPos)
  {
   	this.lastPos = theNewPosition;   
   	this.depRequestor.setDayTolerance(theNewPosition);
   	this.topRequestor.setDayTolerance(theNewPosition);
	this.depRequestor.callFlights();   
  }
}
/**
 * Listener fuer die Toleranz der Tage des Rueckflugdatums.
 * @author Markus Hanses
 * @param {Object} aDestRequestor
 * @param {Object} aTopListReq
 * @param {Object} aNaviHandler
 */
function DestinationDayToleranceSliderListener(aDestRequestor, aTopListReq, aNaviHandler)
{
  this.lastPos=null;
  this.destRequestor = aDestRequestor;
  this.topRequestor = aTopListReq;
  this.naviHandler=aNaviHandler;
}

/**
 * Erfasst die Veraenderung der Reglerposition
 * @author Markus Hanses
 * @param {Object} aSliderEvent
 */
DestinationDayToleranceSliderListener.prototype.sliderPosChanged=function(aSliderEvent)
{
   var theNewPosition = aSliderEvent.newValue;
   
  if (theNewPosition != this.lastPos)
  {
   this.lastPos = theNewPosition;
   this.destRequestor.setDayTolerance(theNewPosition);
   this.topRequestor.setDayTolerance(theNewPosition);
   this.destRequestor.callFlights();
  }
}

//-------------------------- DiagramDateShiftListener ---------------------


function DiagramDateShiftListener(aStartCalendar, aReturnCalendar, aDayToleranceSlider)
{
  this.startCal=aStartCalendar;
  this.returnCal=aReturnCalendar;
  this.dayToleranceSlider = aDayToleranceSlider;
}


/**
 *  RETURN_DATE_SHIFTED = 16;
    START_DATE_SHIFTED = 32;
    OFFER_SELECTION_DONE = 2;
 */ 
DiagramDateShiftListener.prototype.diagramActionPerformed=function(aDiagramEvent)
{
  var theDiagram = aDiagramEvent.getSource();
  var theType =aDiagramEvent.getType();
  
  if (theType==aDiagramEvent.RETURN_DATE_SHIFTED)
  {
    this.returnCal.selectDate(aDiagramEvent.getNewDate());
  }
  else if (theType==aDiagramEvent.START_DATE_SHIFTED)
  {
    this.startCal.selectDate(aDiagramEvent.getNewDate());
  }  
}


function CalendarSyncListener(aRecognition)
{
 this.master=null;
 this.slave=null;
 this.action=aRecognition;
 this.cal = new GregorianCalendar();
}

CalendarSyncListener.prototype.setMasterCalendar=function(aMaster)
{
  this.master = aMaster;
}

CalendarSyncListener.prototype.setSlaveCalendar=function(aSlave)
{
  this.slave = aSlave;
}

CalendarSyncListener.prototype.dateSelected=function(anEvent)
{
  if (anEvent.action == this.action)
  {
    var theDate = anEvent.date;
    if(this.action==anEvent.SHIFT_NEXT && this.cal.before(this.slave.startDate, theDate))
      this.slave.selectDate(theDate);
    else if (this.cal.after(this.slave.startDate, theDate))
      this.slave.selectDate(theDate);
  }
  //else
    //alert ("Sync Handler: Date selection/shift prev month not recognized: "+anEvent.action);
}

