// JavaScript Document

function PopUpRenderer(aDiagram, aDiagramName)
{
  this.offers=new Array();  
  this.offerContainer=null;
  this.cellHeight=15;
  this.diagram = aDiagram;
  this.diagramName=aDiagramName;
  this.selected='';
}

PopUpRenderer.prototype.setSelected=function(aSelected)
{
  this.selected=aSelected;
}
/**
 * Set for example the cell height with this function elswhere
 */
PopUpRenderer.prototype.init=function(anInit)
{
  for (var theInit in anInit)
  {
    this[theInit]=anInit[theInit];
  }
}

PopUpRenderer.prototype.setOffers=function(aOfferContainer)
{
  this.offerContainer=aOfferContainer;
  if (aOfferContainer != null)
    this.offers=aOfferContainer.getOffers();
  else 
    this.offers=new Array();
}

PopUpRenderer.prototype.computeHeight=function()
{
	var addition = 0;
	if (navigator.appName.indexOf("Explorer") != -1)
	{
	    addition = 5;
	} else {}	
  	return (this.cellHeight *(this.offers.length)) + addition;
}


PopUpRenderer.prototype.createContentElement=function(aNo, anOffer)
{
  
  var theOffer = anOffer;
  var theElement = new ElementTag('div');
  theElement.addAttribute('class', 'offer_popup_e');  
  theElement.addStyleElement('position', 'absolute');
  theElement.addStyleElement('top', (aNo*this.cellHeight)+'px');
  theElement.addAttribute('onmouseover','document.body.style.cursor=\'pointer\'');
  theElement.addAttribute('onmouseout','document.body.style.cursor=\'default\';');
  theElement.addAttribute('onclick','window.'
        +this.diagram.windowName
        +'.diagramSelected(\''+theOffer.getOfferId()+'\',\''+this.diagramName+'\',\''+this.selected+'\');nd(400);');
  
  var theAElement = new ElementTag('div');
  theAElement.addAttribute('class','offer_popup_e_airline');
  
  var theP = new PElement();
  
  theP.addContent(this.formatAirline(theOffer.getAirline()));
  theAElement.addElement(theP);
  
  var theBElement = new ElementTag('div');
  theBElement.addAttribute('class','offer_popup_e_price');
  var theQ = new PElement();
  theQ.addContent(theOffer.getPrice());  
  theBElement.addElement(theQ);
  
  var theImgElement = new ElementTag('div');
  theImgElement.addAttribute('class','offer_popup_e_time_icon');
  var theClock = new ElementTag('img');
  
  theClock.addAttribute('src','img/uhr.gif');  
  theClock.addAttribute('alt','uhrzeit');
  theImgElement.addElement(theClock);
  
  var theDElement = new ElementTag('div');
  theDElement.addAttribute('class','offer_popup_e_time');
  var theR = new PElement();
  var theTime = theOffer.getTime();
  theR.addContent(theTime);  
  
  var theArrival = theOffer.getArrivalTime();
  
  theR.addContent(' - ');
  theR.addContent(theArrival);
  
  theDElement.addElement(theR);
  
  
  
  
  
  var theCElement = new ElementTag('div');
  theCElement.addAttribute('class','offer_popup_e_end');
  var theR = new PElement();
  theR.addContent('&#8364;');  
  theCElement.addElement(theR);
  
  theElement.addElement(theAElement);
  theElement.addElement(theBElement);
  theElement.addElement(theImgElement);
  theElement.addElement(theDElement);
  theElement.addElement(theCElement);
  return theElement;
}

PopUpRenderer.prototype.formatAirline=function(aAirline)
{
  var theAirline = aAirline;
  if (theAirline.length > 12)
  {
    theAirline = theAirline.substring(0,11)+'.';
  }
  return theAirline;
} 

PopUpRenderer.prototype.getParentElement=function()
{
  var theParent = new DIVElement();
  theParent.addAttribute('class', 'offer_popup_p');
  theParent.addStyleElement('position','absolute');
  theParent.addStyleElement('top',0+'px');
  theParent.addStyleElement('left',0+'px');
  theParent.addStyleElement('height',this.computeHeight()+'px');
  
  
  var theSorted = this.sort(this.offers);
  if (theSorted.length != this.offers.length)
    alert("Mismatch on sorted result = "+theSorted.length+" original: "+this.offers.length);
  for(var i=0; i<theSorted.length; i++)
  {
    var theOffer = theSorted[i];
    theParent.addElement(this.createContentElement(i, theOffer));
  }
  
  return theParent;
}

PopUpRenderer.prototype.sort=function(anOfferArray)
{
  var theResultArray = new Array();
  
  for(var i = 0;i<anOfferArray.length;i++)
  {
    var theNext = anOfferArray[i];
    var thePrice = Number(theNext.getPrice());
    
    var thePos = this.getIndex(theResultArray, thePrice);
    this.insert(theResultArray, theNext, thePos);
  }
  return theResultArray;
}

PopUpRenderer.prototype.getIndex=function(anArray, aPrice)
{
  for(var i = 0;i<anArray.length;i++)
  {
    var theNext = Number(anArray[i].getPrice());
    if (theNext>aPrice)
    {
      return i;
    }
  }
  return anArray.length;
}

PopUpRenderer.prototype.insert=function(anArray, anOffer, aPos)
{
  if(aPos == anArray.length)
  { 
    anArray[anArray.length] = anOffer;
    return;
  }
  var theNext = anOffer;
  var theIndex = aPos;
  var theShift = new Array();
  var theShifted;
  for(var i = 0;i<anArray.length;i++)
  {
    if (i == theIndex)
    {
      theShifted = anArray[i];
      anArray[i] = theNext;
      theIndex = i+1;
      theNext = theShifted;
    }
  }
  anArray[anArray.length] = theNext;
}



PopUpRenderer.prototype.write=function()
{
  var theParent = this.getParentElement();
  
  return theParent.write();
}

function PopUpListener(aDiagram, aDiagramName)
{
  this.diagram=aDiagram;
  this.renderer= new PopUpRenderer(aDiagram, aDiagramName);
}


PopUpListener.prototype.setMonthDiagram=function(aDiagram)
{
  this.diagram=aDiagram;
  
}

PopUpListener.prototype.selectionPerformed=function(aSelectionEvent)
{
  if (aSelectionEvent.type==aSelectionEvent.MOUSE_OVER)
  {
   var theOffer = aSelectionEvent.valueObject;
   var theSelectedId = aSelectionEvent.selectedId;
   var theDayOfferList = theOffer.getDayOfferList();
   if (theDayOfferList)
   {  
    this.renderer.setOffers(theDayOfferList);
    this.renderer.setSelected(theSelectedId);
    var theHtml = this.renderer.write();
    return overlib(theHtml, FULLHTML, STICKY);
   }
  }
  
}

