// JavaScript Document
// JavaScript Document
function DiagramMap()
{

  this.name='diagram_name_undefined';
  this.cellYCount=20;
  this.cellXCount=31;
  this.offsetY=2;
  this.offsetX=2;
  this.width=350;
  this.xScaleWidth=300;
  this.height=150;
  this.maxValue=0;
  this.scaleHeight=120; // defines the height of hte scale

  this.yScaleWidth=30; // defines the number of scale signs

  this.values=new Array();
  this.scale=150;
  this.background='blue';
  this.selectedBackground='yellow';
  this.rowModel=null;//new DiagramRowModel(this.cellYCount);
  this.columnModel=new DiagramColumnModel();
  this.columnModel.setCount(this.cellXCount);
  this.xScaleRenderer=null;
  this.columnRenderer=new DiagramColumnRenderer();
  this.columnRenderer.setDiagram(this);
  this.yScaleRenderer=new PriceYScaleRenderer();
  this.setXScaleRenderer(new DateXScaleRenderer());
  this.selectionModel=new DiagramSelectionModel(this, this.columnModel);

  this.contentMarginTop=40; //Distance to diagram from border top
  this.contentMarginLeft=0; //Distance to diagram from border left
  this.headerMarginTop = 5; //Distance header to border
  this.headerMarginLeft = 0; // Distance header to border


  this.tableHeader='';
  this.xScaleProvider= new DateProvider();
  this.selectionListener=new Array();
  /**Column background setting*/

  this.mouseOverBackground='url(img/dbalken_grau.gif)';
  this.defaultBackground='url(img/dbalken_hblau.gif)';
  this.selectedBackground ='url(img/dbalken_gruen.gif)';
  this.preSelectedBackround='url(img/dbalken_gelb.gif)';

  this.selectFilters=new Array();
  return this;
}

DiagramMap.prototype.addSelectFilter=function(aFilter)
{
  this.selectFilters[this.selectFilters.length]=aFilter;
}


DiagramMap.prototype.passSelectFilters=function(aValue)
{
  var theValue = true;
  for (var i=0; i<this.selectFilters.length && theValue;i++)
  {
    theValue = this.selectFilters[i].passFilter(this, aValue);
  }
  return theValue;
}


DiagramMap.prototype.addSelectionListener=function(aListener)
{

  if (aListener)
    this.selectionListener[this.selectionListener.length]=aListener;
  else
   alert("Could not identify Listener "+aListener);
}

DiagramMap.prototype.notifySelectionListener=function(aSelectionEvent)
{
  var theLength=this.selectionListener.length;
  for(var i=0;i<theLength;i++)
  {
    this.selectionListener[i].selectionPerformed(aSelectionEvent);
  }
}

DiagramMap.prototype.preSelect=function(aSelectUserObject)
{
  var theId = this.createColumnId(aSelectUserObject);
  //alert("Preselecting "+theId);
  var theElement = document.getElementById(theId);
  if (theElement)
  {
    var theCol = this.columnModel.getColumnById(theId);
    this.diagramMouseClicked(theId, theCol);
    theElement.style.background=this.preSelectedBackround;
  }
}

DiagramMap.prototype.selectElement=function(anElementId)
{
  var theElement = document.getElementById(anElementId);
  if (theElement)
  {
    var theCol = this.columnModel.getColumnById(anElementId);


    this.diagramMouseClicked(anElementId, theCol);
    //theElement.style.background=this.preSelectedBackround;
  }
}

DiagramMap.prototype.changeValueObject=function(anElementId, aNewValueObject, aNewSize)
{
  var theElement = document.getElementById(anElementId);
  if (theElement)
  {
    var theCol = this.columnModel.getColumnById(anElementId);
    var theValue = this.columnModel.getValue(theCol);
    if (!theValue)
    {
      alert("No offer found for col "+theCol);
    }
    theValue.setUserObject(aNewValueObject);
    theValue.setSize(aNewSize);
    this.columnRenderer.changeColumn(anElementId, theValue);
    //theElement.style.background=this.preSelectedBackround;
  }
}

DiagramMap.prototype.setXStartValue=function(aValue)
{
  this.xScaleProvider.setStartValue(aValue);
}


DiagramMap.prototype.setTableHeader=function(aHtml)
{
  this.tableHeader=aHtml;
  //  ("Table header: "+this.tableHeader);
  var theHElement=document.getElementById('diagram_table_header_'+this.name);
  if (theHElement)
  {
    theHElement.innerHtml=this.tableHeader;
  }
}

DiagramMap.prototype.setName=function(aName)
{
  this.name=aName;
  window[this.name]=this;
}

DiagramMap.prototype.getName=function()
{
  return this.name;
}

DiagramMap.prototype.setMaxValue=function(aMax)
{
  this.maxValue=aMax;
}

DiagramMap.prototype.calculateScale=function(aMax, aRefLength)
{
	if (aMax == null)
	{
		alert("Diagram: Max value is null!");
		return 1;
	}
	else
	{
	 //alert("Scale calc: max value="+aMax+" scaleY="+aRefLength);
  }
	var theLength = Math.abs(aMax);
	//alert("Scale calc: max.abs ="+theLength+" scaleY="+aRefLength);
	var theScale = 1.0;
	if (theLength != 0)
	{
	 theScale = aRefLength/theLength;
	}
  //alert("calculate scale: MaxPrice:"+aMaxPrice+" RefLength:"+aRefLength+" result:"+theScale);
	return theScale;
}

DiagramMap.prototype.setDimension=function(aWidth, aHeight, aScaleHeight, aScaleCount, aXScaleWidth)
{
  this.width=aWidth - (this.contentMarginLeft+this.headerMarginLeft);
  this.xScaleWidth=this.width;
  this.height=aHeight;
  if (aScaleHeight)
  {
    this.scaleHeight = aScaleHeight;
  }
  if (aScaleCount)
  {
    this.columnRenderer.number = aScaleCount;
  }
  this.columnRenderer.setDiagramHeight(this.scaleHeight);

  this.yScaleRenderer.setHeight(this.scaleHeight);
  //this.xScaleRenderer.setDiagramWidth(this.width);
//  this.scaleHeight=aScaleHeight;
}

DiagramMap.prototype.setXScaleRenderer=function(aScaleRenderer)
{
  this.xScaleRenderer=aScaleRenderer;
  this.xScaleRenderer.setNumber(this.cellXCount);

  this.xScaleRenderer.setTable(this);
}

DiagramMap.prototype.setYScaleRenderer=function(aScaleRenderer)
{
  this.yScaleRenderer=aScaleRenderer;
}

DiagramMap.prototype.setXCount=function(aCount)
{
  this.cellXCount=aCount;
  this.xScaleRenderer.setNumber(this.cellXCount);
  this.columnModel.setCount(this.cellXCount);
}

DiagramMap.prototype.setYCount=function(aCount)
{
  this.cellYCount=aCount;
}

DiagramMap.prototype.selectColum=function(aCol)
{
  this.columnModel.select(aCol)
}

/**
 * Sets the scale factor for the ValueObjects size.
 * Should be a float number.
 */
DiagramMap.prototype.setScale=function(aScale)
{
  this.scale=aScale;
}

DiagramMap.prototype.computeCellHeight=function()
{
  return Math.round(this.scaleHeight/this.cellYCount);// Math.abs(this.height);
}

DiagramMap.prototype.computeCellWidth=function()
{
  return Math.round(this.xScaleWidth/this.cellXCount);
}

DiagramMap.prototype.addValue=function(aValueObject)
{
  this.setValue(this.values.length, aValueObject);
}

DiagramMap.prototype.setValue=function(aPos, aValueObject)
{
  this.values[aPos]=aValueObject;
  var theId = this.createColumnId(aValueObject);
  this.columnModel.addValueObject(aPos, aValueObject, theId);
}

// Event propagation:

/**
 * Mouse listener - reacts when the mouse enters a columns
 */

DiagramMap.prototype.diagramMouseIn=function(anElementId, aModelId)
{
  var theElement = document.getElementById(anElementId);
  if (theElement)
  {
    if (!this.selectionModel.isSelected(aModelId))
      theElement.style.background=this.mouseOverBackground;
  }
  var theValue = this.columnModel.getValue(aModelId);
  var theText='undefined';
  if (theValue)
  {
    theText=theValue.getUserObject().getOfferId();
    var theEvent = new DiagramSelectionEvent();
    theEvent.source=this;
    theEvent.type=theEvent.MOUSE_OVER;
    theEvent.valueObject=theValue.getUserObject();
    theEvent.selectedId=anElementId;
    this.notifySelectionListener(theEvent);
  }

  return;
}

/**
 * Mouse listener - reacts when the mouse leaves a columns
 */
DiagramMap.prototype.diagramMouseOut=function(anElementId, aModelId)
{
 if (!this.selectionModel.isSelected(aModelId))
 {
  var theElement = document.getElementById(anElementId);
  if (theElement)
  {
    theElement.style.background=this.defaultBackground;
  }
 }
  //return nd('200');
  return;
}

/**
 * Mouse listener - reacts when the mouse does click on a columns
 */



/**
 * Mouse listener - reacts when the mouse does click on a columns
 */

DiagramMap.prototype.diagramMouseClicked=function(anElementId, aModelId)
{

  var theValue=this.columnModel.getValue(aModelId);
  if (theValue)
  {
    var theEvent = new DiagramSelectionEvent();
    theEvent.source=this;
    theEvent.type=theEvent.SELECTION_DONE;
    theEvent.valueObject=theValue.getUserObject();
    theEvent.selectedId=anElementId;
    if(this.passSelectFilters(theEvent.valueObject))
    {
      this.selectionModel.select(anElementId, aModelId);
      this.notifySelectionListener(theEvent);
    }
  }
  else
  {
    this.selectionModel.select(anElementId, aModelId);
    //alert("Change Selection: No value found for "+aModelId+' '+anElementId);
  }
  return nd('200');
}


DiagramMap.prototype.createRootElement=function()
{
  var theElement=new DIVElement();
  theElement.addAttribute('class','bar_diagram_root');
  theElement.addStyle('position:absolute');
  theElement.addStyle('top:0px');
  theElement.addStyle('left:'+0+'px');
  theElement.addStyle('height:'+(this.height+this.contentMarginTop+this.headerMarginTop)+'px');
  theElement.addStyle('width:'+(this.width+this.contentMarginLeft+this.headerMarginLeft)+'px');
  return theElement;
}

DiagramMap.prototype.createContentElement=function()
{
  var theElement=new DIVElement();
  theElement.addAttribute('class','bar_diagram_content');
  theElement.addStyle('position:absolute');
  theElement.addStyleElement('top',(this.contentMarginTop+this.headerMarginTop)+'px');
  theElement.addStyle('left:'+this.contentMarginLeft+'px');
  theElement.addStyle('height:'+this.scaleHeight+'px');
  theElement.addStyle('width:'+this.yScaleWidth+'px');
  theElement.addStyle('z-index:10');
  return theElement;
}




DiagramMap.prototype.createYScaleElement=function(aLeft)
{
  if(!aLeft)
  {
    aLeft=0;
  }
  var theElement=new DIVElement();
  theElement.addAttribute('class','bar_diagram_y_scale');
  theElement.addStyle('position:absolute');
  theElement.addStyle('top:'+(this.contentMarginTop+this.headerMarginTop)+'px');
  theElement.addStyle('left:'+aLeft+'px');
  theElement.addStyleElement('z-index',17);
  theElement.addStyle('height:'+this.scaleHeight+'px');
  theElement.addStyle('width:'+this.yScaleWidth+'px');
  return theElement;
}

DiagramMap.prototype.createHeader=function()
{
  var theElement=new DIVElement();
  theElement.addAttribute('class','bar_diagram_header');
  theElement.addStyle('position:relative');
  theElement.addStyle('top:'+this.headerMarginTop+'px');
  theElement.addStyle('left:'+this.headerMarginLeft+'px');
  theElement.addStyle('height:'+20+'px');
  theElement.addStyle('width:'+this.width+'px');
  theElement.addStyle('z-index:15');
  //theElement.addStyle('background-color:#E0EFEF');
  theElement.addAttribute('id','diagram_table_header_'+this.name);

  if (this.tableHeader != null)
  {
    if (!theElement.addContent)
     alert("No addContent Method");
    else
    {
      //alert("Content setting: TableHeader: "+this.tableHeader);
      theElement.addContent(this.tableHeader);
    }
  }
  return theElement;
}

/*
  Creates an unique id to be used to identify the column within the page
  @param aValue valueObject has method getName()
*/
DiagramMap.prototype.createColumnId=function(aValue)
{
  return 'diagram_col_'+aValue.getName()+'_'+this.name;
}

DiagramMap.prototype.createElement=function()
{
  var theRootElement = this.createRootElement();
  theRootElement.addElement(this.createHeader());
  var theScale=this.calculateScale(this.maxValue, this.scaleHeight);
  this.columnRenderer.setScale(theScale);
  this.columnRenderer.backgroundImg=this.defaultBackground;
  this.yScaleRenderer.setScale(theScale);
  this.yScaleRenderer.setTopPos(0);
  this.yScaleRenderer.setHeight(this.scaleHeight);
  this.yScaleRenderer.setWidth(this.yScaleWidth);


  this.xScaleRenderer.setYPos(this.scaleHeight);//+this.contentMarginTop+this.headerMarginTop);

  var theXScale=this.xScaleRenderer.renderAll();
  var theYScale=this.yScaleRenderer.renderAll();
  var theBorder =2;
  var theLeft=this.contentMarginLeft-this.yScaleWidth-theBorder;

  var theYScaleContainer=this.createYScaleElement(theLeft);
  theYScaleContainer.addElement(theYScale);

  theRootElement.addElement(theYScaleContainer);
  //theRootElement.addElement(theYScale);

  var theContainer=this.createContentElement();
  var theContent = this.columnRenderer.renderColumns(this.columnModel);
  theContainer.addElement(theContent);
  theContainer.addElement(theXScale);
  theRootElement.addElement(theContainer);
  return theRootElement;
}

DiagramMap.prototype.write=function()
{
  return this.createElement().write();
}


function ValueObject()
{
  this.name='undefined Value';
  this.value=null;
  this.userObject=null;
  this.size=0;
  this.id=null;
}


ValueObject.prototype.setName=function(aName)
{
  this.name=aName;
}

ValueObject.prototype.getName=function(aName)
{
  return this.name;
}

ValueObject.prototype.setSize=function(aSize)
{
  this.size=aSize;
}

ValueObject.prototype.getSize=function()
{
  return this.size;
}

ValueObject.prototype.setUserObject=function(aUserObject)
{
  this.userObject = aUserObject;
}

ValueObject.prototype.getUserObject=function()
{
  return this.userObject;
}


ValueObject.prototype.setValue=function(aValue)
{
  this.value=aValue;
}

ValueObject.prototype.getValue=function()
{
  if (this.value != null)
  {
    return this.value;
  }
  else
  return this.userObject;
}








// Diagramm Ro



function PriceYScaleRenderer()
{
  this.number = 4;
  this.startValue=0;
  this.maxValue=0;
  this.width=20;
  this.height=0;
  this.topPos=0;
  this.elementHeight=30;

  this.content= new Array();

  this.diagramTable = null;

  this.style=null;

  this.cal = new GregorianCalendar();
  this.scale=0;
  this.bottoScaleOffset=0;
  this.yPos=0;
  this.xPos=0;
}

PriceYScaleRenderer.prototype.setTopPos=function(aValue)
{
  this.topPos=aValue;
}

PriceYScaleRenderer.prototype.setWidth=function(aScale)
{
  this.width=aScale;
}

PriceYScaleRenderer.prototype.setHeight=function(aScale)
{
  this.height=aScale;
}

PriceYScaleRenderer.prototype.setScale=function(aScale)
{
  this.scale=aScale;
}

PriceYScaleRenderer.prototype.setElementHeight=function(aValue)
{
  this.elementHeight=aValue;
}

PriceYScaleRenderer.prototype.setYPos=function(aYPos)
{
  this.yPos=aYPos;
}

PriceYScaleRenderer.prototype.setXPos=function(aXPos)
{
  this.xPos=aXPos;
}

PriceYScaleRenderer.prototype.setNumber=function(aNumberOfScaleElements)
{
  this.number=aNumberOfScaleElements;
}

PriceYScaleRenderer.prototype.setTable=function(aTable)
{
  this.diagramTable=aTable;
}

PriceYScaleRenderer.prototype.setStart=function(aValue)
{
  this.startValue=aValue;
}

PriceYScaleRenderer.prototype.render=function(aScalePos, aWidth, aHeight)
{
  var thePElement = new PElement();
  var theValue = (this.height-(aScalePos*this.elementHeight))/this.scale;
  theValue = theValue.toFixed(0);

  thePElement.addContent(theValue);//todo addDate instead
  var theElement=new DIVElement();
  theElement.addAttribute('class','bar_diagram_y_scale');
  theElement.addStyle('position:absolute');
  theElement.addStyle('width:'+aWidth+'px');
  theElement.addStyle('left:'+(this.xPos)+'px');
  theElement.addStyle('height:'+this.elementHeight+'px');
  theElement.addStyle('top:'+(aScalePos*this.elementHeight)+'px');
  //theElement.addStyle('background-image:url('+this.backgroundImg+')');
  //theElement.addStyle(this.getStyle());
  theElement.addElement(thePElement);


  return theElement;
}

PriceYScaleRenderer.prototype.renderScale=function(aScalePos, aWidth, aHeight, aLeft)
{
  var theElement = new ElementTag('div');
  theElement.addAttribute('class','diagram_y_scale');
  theElement.addStyleElement('position','absolute');
  theElement.addStyleElement('top',(aScalePos*this.elementHeight)+'px');
  if (window.ActiveXObject)
    theElement.addStyleElement('left',(aLeft)+'px');
  else
    theElement.addStyleElement('left',aLeft+'px');
  theElement.addStyleElement('border-top-width','1px');
  theElement.addStyleElement('border-right-width','1px');
  theElement.addStyleElement('border-right-color','gray');
  theElement.addStyleElement('border-top-color','gray');
  theElement.addStyleElement('border-right-style','solid');
  theElement.addStyleElement('border-top-style','solid');
  theElement.addStyleElement('z-index', 17);
  theElement.addStyleElement('width', aWidth+'px');
  theElement.addStyleElement('height', (this.elementHeight)+'px');
 return theElement;
}

PriceYScaleRenderer.prototype.renderAll=function()
{

  var theWidth = Math.round(this.width);
  if (this.number >0)
    this.elementHeight= Math.round(this.height/this.number);

  var theElement = new DIVElement();
  theElement.addStyleElement('width',theWidth);
  theElement.addStyleElement('height', this.height);
  theElement.addStyleElement('position', 'absolute');
  theElement.addStyleElement('top', this.topPos+'px');
  theElement.addStyleElement('left', '0px');
  theElement.addStyleElement('z-index', 16);
  var theNoWidth=Math.round(1*(theWidth/4));
  var theScWidth=Math.round(1*(theWidth/4));

  for (var i=0;i<this.number;i++)
  {
    theValue = this.render(i, theNoWidth, this.elementHeight);
    theElement.addElement(theValue);
    theValue = this.renderScale(i, (4), this.elementHeight, theWidth-2);
    theElement.addElement(theValue);
  }

  return theElement;
}

PriceYScaleRenderer.prototype.getStyle=function()
{
  var theStyle='font-family:Arial,';
  theStyle+=' Helvetica, sans-serif; font-size:11px; font-weight:bold;';
  theStyle+=' color:red';
  return theStyle;
}


//_______________________________



function DateXScaleRenderer()
{
  this.number = null;
  this.startDate;
  this.content= new Array();
  this.diagramTable = null;
  this.style=null;
  this.cal = new GregorianCalendar();
  this.elementWidth=0;
  this.cellHeight=20;
  this.yPos=0;

}


DateXScaleRenderer.prototype.setCellHeight=function(aValue)
{
  this.cellHeight=aValue;
}

DateXScaleRenderer.prototype.setElementWidth=function(aWidth)
{
  this.elementWidth=aWidth;
}

DateXScaleRenderer.prototype.setYPos=function(aYPos)
{
  this.yPos=aYPos;
}


DateXScaleRenderer.prototype.setNumber=function(aNumberOfScaleElements)
{
  this.number=aNumberOfScaleElements;
}

DateXScaleRenderer.prototype.setTable=function(aTable)
{
  this.diagramTable=aTable;
}




DateXScaleRenderer.prototype.setStartDate=function(aDate)
{
  this.startDate=aDate;
}



DateXScaleRenderer.prototype.render=function(aScalePos, aWidth, aHeight)
{
  var thePElement = new PElement();
  thePElement.addContent(this.diagramTable.xScaleProvider.getXValue(aScalePos));//todo addDate instead
  var theElement=new DIVElement();
  theElement.addAttribute('class','bar_diagram_x_render');
  theElement.addStyle('position:absolute');
  theElement.addStyle('width:'+aWidth+'px');
  theElement.addStyle('left:'+(aScalePos*aWidth)+'px');
  //theElement.addStyle('height:'+aHeight+'px');
  theElement.addStyle('top:'+this.yPos+'px');
  //theElement.addStyle('background-image:url('+this.backgroundImg+')');
  //theElement.addStyle(this.getStyle());
  theElement.addElement(thePElement);
  return theElement;
}

DateXScaleRenderer.prototype.renderAll=function()
{

  var theWidth = Math.round(this.diagramTable.computeCellWidth());
  var theHeight=this.cellHeight;
  var theElement = new DIVElement();
  theElement.addStyleElement('position', 'absolute');
  theElement.addStyleElement('top', '0px');
  theElement.addStyleElement('left', '0px');
  for (var i=0;i<this.number;i++)
  {
    theValue = this.render(i, theWidth, theHeight);
    theElement.addElement(theValue);
  }

  return theElement;
}

DateXScaleRenderer.prototype.getStyle=function()
{
  var theStyle='font-family:Arial,';
  theStyle+=' Helvetica, sans-serif; font-size:11px; font-weight:bold;';
  theStyle+=' color:red';
  return theStyle;
}

/*

  function DiagramColumnRenderer()
*/

function DiagramColumnRenderer()
{
  this.offset=100;
  this.topOffset=250;
  this.width=30;
  this.backgroundImg='img/dbalken_hblau.gif';
  this.elementWidth=30;
  this.scale=1;
  this.diagram=null;
  this.popup='Under construction';
  this.mouseAction='alert(\'mouse clicked\');';
  this.barWidth=0;
  this.barPos=0;
}

DiagramColumnRenderer.prototype.setBarWidth=function(aWidth)
{
  this.barWidth=aWidth;
  var theValue=(this.elementWidth-this.barWidth)/2;
  this.barPos=Math.abs(Math.round(theValue));
}

DiagramColumnRenderer.prototype.setDiagram=function(aDiagram)
{
  this.diagram=aDiagram;
}

DiagramColumnRenderer.prototype.setScale=function(aScale)
{
  this.scale=aScale;
  //alert("Scale set to "+this.scale);
}

DiagramColumnRenderer.prototype.setElementWidth=function(aWidth)
{
  this.elementWidth=aWidth;
  if (this.barWidth == 0)
  {
    //calculating bar width to 70% of Element width
    var theWidth=Math.round(this.elementWidth*0.5);
    this.setBarWidth(theWidth);
  }
}


DiagramColumnRenderer.prototype.changeColumn=function(anElementId, aValueObject)
{
  var theValue = aValueObject;
  var theElement = document.getElementById(anElementId);
  if (theElement)
  {
    var theBarHeight=Math.round(theValue.getSize()*this.scale);
    theElement.style.top=(this.topOffset-theBarHeight)+'px';
    theElement.style.height= theBarHeight+'px';
  }
}

DiagramColumnRenderer.prototype.setDiagramHeight=function(aHeight)
{
  this.topOffset=aHeight;
}

DiagramColumnRenderer.prototype.renderColumn=function(aCol, aModel)
{
  var theValue = aModel.getValue(aCol);
  var theId=aModel.getId(aCol);
  //alert("ID already set "+theId);

  var theParent=new DIVElement();
   theParent.addAttribute('class','bar_diagram_col_parent');
   theParent.addStyle('position:absolute');
   theParent.addStyle('left:'+(aCol*this.elementWidth)+'px');
   //theParent.addStyle('background-image:url('+this.backgroundImg+')');
   theParent.addStyle('top:'+0+'px');
   theParent.addStyle('height:'+this.topOffset+'px');
   theParent.addStyle('width:'+this.elementWidth+'px');
   //theParent.addStyle('background-color:rgb(51,0,102)');
  if (theValue != null)
  {
    var theElement = new DIVElement();
    theElement.addAttribute('class','bar_diagram_col_element');
    theElement.addStyle('position:absolute');
    theElement.addStyle('left:'+this.barPos+'px');

    var theBarHeight=Math.round(theValue.getSize()*this.scale);
    theElement.addStyle('top:'+(this.topOffset-theBarHeight)+'px');
    theElement.addStyle('height:'+theBarHeight+'px');
    theElement.addStyle('width:'+this.barWidth+'px');
    theElement.addStyleElement('background-image', this.diagram.defaultBackground);

    theElement.addAttribute('id',theId);
    var theMouseInAction='return window.'+this.diagram.name+'.diagramMouseIn(\''+theId+'\',\''+aCol+'\')';
    //overlib(\''+this.popup+'\')';
    theElement.addAttribute('onmouseover','document.body.style.cursor=\'pointer\';'+theMouseInAction);
    var theMouseOutAction='return window.'+this.diagram.name+'.diagramMouseOut(\''+theId+'\',\''+aCol+'\')';
    //overlib(\''+this.popup+'\')';
    theElement.addAttribute('onmouseout','document.body.style.cursor=\'default\';'+theMouseOutAction);

    var theMouseClickAction='return window.'+this.diagram.name+'.diagramMouseClicked(\''+theId+'\',\''+aCol+'\')';
    theElement.addAttribute('onclick',theMouseClickAction);
    //alert(theElement.write());
    var thePEl= new PElement();
    thePEl.addContent(" ");
    theElement.addElement(thePEl);
    theParent.addElement(theElement);
    //alert("Parent"+theParent.write());
  }
  else
  {

  }

  return theParent;
}


DiagramColumnRenderer.prototype.renderColumns=function(aColModel)
{
  //alert("Render column topOffset "+this.topOffset);
  this.setElementWidth(Math.round(this.diagram.computeCellWidth()));
  var theContainer= new DIVElement();
  theContainer.addAttribute('class','bar_diagram_col_container');
  theContainer.addStyle('position:absolute');
  theContainer.addStyle('left:0px');
  theContainer.addStyle('top:0px');

  //theContainer.addElement(theOverlib);
  var theLength=aColModel.getCount();
  for (var i=0;i<theLength; i++)
  {
    var theElement = this.renderColumn(i, aColModel);
    theContainer.addElement(theElement);
  }
  return theContainer;
}



function DiagramColumnModel()
{
  this.backgroundStyle='background-color:#E0E0E0';
  this.rows=new Array();
  this.columValues=new Array();
  this.ids=new Array();
  this.values=new Array();
  this.bgcolor='#FF0000';
  this.offset=100;
  this.count=0;
}

DiagramColumnModel.prototype.setCount=function(aValue)
{
  this.count=aValue;
}

DiagramColumnModel.prototype.getCount=function()
{
  return this.count;
}

DiagramColumnModel.prototype.getBgColor=function(aCol)
{
  return this.bgcolor;
}


DiagramColumnModel.prototype.getBackgroundStyle=function(aCol)
{
  if (this.columValues[aCol])
  {
    return this.columValues[aCol];
  }
  return this.backgroundStyle;
}



DiagramColumnModel.prototype.addValueObject=function(aColNo, aValueObject, anId)
{
  //alert("added aCol="+aColNo+" anObject="+aValueObject+" anId:"+anId);

  aValueObject.id = anId;
  this.values[aColNo]=aValueObject;
  this.ids[anId]=aColNo;
}

DiagramColumnModel.prototype.getValueById=function(anId)
{
  var theColNo = this.ids[anId];
  if (theColNo != null)
    return this.values[theColNo];
  return null;
}

DiagramColumnModel.prototype.getColumnById=function(anId)
{
  var theColNo = this.ids[anId];
  //alert("Found col no "+theColNo+" for id "+anId);
  return theColNo;
}


DiagramColumnModel.prototype.getId=function(aCol)
{
  if (this.values[aCol] != null)
    return this.values[aCol].id;
  return '_undefined';
}

DiagramColumnModel.prototype.getValue=function(aColNo)
{
  if (this.values[aColNo] == null)
  {
    //alert('Col '+aColNo+' has no value' );
    return null;

  }
  else
  {
    return this.values[aColNo];
  }
}

function DiagramSelectionModel(aDiagram, aColModel)
{
  this.selected=new Array();
  this.selectionList=new Array();
  this.elements=new Array();
  this.singleSelectionId;
  this.selectionClasses=new Array();
  this.diagram = aDiagram;
  this.columnModel=aColModel;
}

DiagramSelectionModel.prototype.setSelected=function(anId, anElement, aValueObject,aBoolean)
{
  if (aBoolean && anId!=null)
  {
    this.singleSelectionId=anId;
    this.selected[anId]=aValueObject;
    this.elements[anId]=anElement;
  }
  else if (!aBoolean)
  {
    this.selected[anId]=null;
    this.elements[anId]=null;
    singleSelectionId=null;
  }
}


DiagramSelectionModel.prototype.getSelected=function(aValue)
{
  if (aValue==null)
    return null;
  return this.selected[aValue];
}

DiagramSelectionModel.prototype.getFirstSelectedId=function()
{
  return this.singleSelectionId;
}

DiagramSelectionModel.prototype.getFirstSelectedElement=function()
{
  if (this.singleSelectionId)
    return this.elements[this.singleSelectionId];
  return null;
}

DiagramSelectionModel.prototype.isSelected=function(anId)
{
  if(anId!=null && this.selected[anId])
  {
    return true;
  }
  return false;
}

DiagramSelectionModel.prototype.select=function(anElementId, aModelId)
{
  var theElement = document.getElementById(anElementId);

  if (theElement)
  {
    var theSelected = this.getFirstSelectedId();
    if(theSelected)
    {
      var theUnselect = this.getFirstSelectedElement();
      this.setSelected(theSelected, null, null,false);
      theUnselect.style.background=this.diagram.defaultBackground;
    }
    var theValue=this.columnModel.getValue(aModelId);
    this.setSelected(aModelId, theElement, theValue ,true);
    theElement.style.background=this.diagram.selectedBackground;
  }
}



function DateProvider()
{
  this.startDate;
  this.cal=new GregorianCalendar();
}

DateProvider.prototype.setStartValue=function(aValue)
{
  this.startDate=aValue;
}

DateProvider.prototype.getXValue=function(aPos, aCurrentLenth)
{
  var theNew = this.cal.shift(aPos, this.startDate);
  return theNew.getDate();
}


function DiagramSelectionEvent()
{
  this.SELECTION_DONE=1;
  this.MOUSE_OVER=2;
  this.MOUSE_OUT=3;
  this.MOUSE_CLICKED=1;

  this.source=null;
  this.valueObject=null;
  this.selectedId=null;
}

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

DiagramSelectionEvent.prototype.getValueObject=function()
{
  return this.valueObject;
}

DiagramSelectionEvent.prototype.getSelectedId=function()
{
  return this.selectedId;
}
