// JavaScript Document
function JsButton(aName)
{ 
  this.name=aName;
  this.id='_Bt_'+aName;
  this.listener=new Array();
  this.element=null;
  this.text='?';
  var theArray = window['jsButtonList'];
  if (!theArray)
  {
    theArray=new Array();
    window['jsButtonList']=theArray;
  }
  theArray[this.id]=this;
  this.height=30;
  this.width=40;
  this.userObject;
 
}

JsButton.prototype.setHeight=function(aHeight)
{
  this.height=aHeight;
}

JsButton.prototype.setWidth=function(aWidth)
{
  this.width=aWidth;
}

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

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

JsButton.prototype.addActionListener=function(aListener)
{
  this.listener[this.listener.length]=aListener;
}

JsButton.prototype.notifyListener=function(anAction)
{
  var theEvent = new ActionEvent(this, this.userObject, anAction);
  for (var i=0;i < this.listener.length; i++)
  {
    this.listener[i].actionPerformed(theEvent);
  }
} 

JsButton.prototype.createElement=function()
{
  var theParent = new DIVElement();
  theParent.addStyle('position:absolute');
  theParent.addStyle('top:0px');
  theParent.addStyle('left:0px');
  theParent.addStyleElement('width:',this.width+'px');
  theParent.addStyleElement('height',this.height+'px');  
  theParent.addStyle('border:2px; border-style:solid; border-color:red; padding:10px;');
  //SETTING FONT STYLE
  theParent.addStyle(this.getStyle());
  theParent.addStyle('background-color:#ffb500');
  theParent.addStyle('border:2px; border-style:solid; border-color:#2e5ba8; padding:10px;');
  theParent.addAttribute('id',this.id);
  theParent.addAttribute('class',this.name);
  this.addAction(theParent);  
  var thePElement=new PElement();
  thePElement.addContent(this.text);
  theParent.addElement(thePElement);
  
  return theParent;
}

JsButton.prototype.addAction=function(aElement)
{
    var theAction='return window[\'jsButtonList\'][\''+this.id+'\'].notifyListener(\'CLICKED\');';
    aElement.addAttribute('onmouseover','document.body.style.cursor=\'pointer\';');
    aElement.addAttribute('onmouseout','document.body.style.cursor=\'default\';');
    aElement.addAttribute('onclick',theAction);
}

JsButton.prototype.getElement=function()
{
  if(this.element==null)
  {
    this.element=this.createElement();
  }
  return this.element;
}


JsButton.prototype.write=function()
{
  return this.getElement().write();
}

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



function ActionEvent(aSource, aUserObject, aAction)
{
  this.source=aSource;
  this.userObject=aUserObject;
  this.action=aAction;
}



function IconButton(anId, anActiveImg, anInactiveImg, aPressedImg)
{
  this.DISABLED = 0;
  this.ENABLED  = 1;
  this.state=this.ENABLED;
  this.id=anId;
  this.element=null;
  this.active=new ElementTag('img');
  this.active.addAttribute('src', anActiveImg);
  this.active.addAttribute('alt', 'Click');
  
  this.disabled=new ElementTag('img');
  this.disabled.addAttribute('src', anInactiveImg);
  this.disabled.addAttribute('alt', 'Inaktiv');
  
  this.pressed=new ElementTag('img');
  this.pressed.addAttribute('src', aPressedImg);
  this.pressed.addAttribute('alt', '');
  this.writer=null;
  this.mouseOverText=null;
  
  this.listener = new Array();
  this.hightLightImage=null;
  
  this.top=0;
  this.left=0;
  //window[this.id] = this;
  this.userObject = null;
  this.styleClass = anId;
  var theArray = window['jsButtonList'];
  if (!theArray)
  {
    theArray=new Array();
    window['jsButtonList']=theArray;
  }
  theArray[this.id]=this;
  
  this.text=null;
  this.textElement=null;
  return this;
}

IconButton.prototype.setText=function(aText)
{
  this.text=aText;
  this.textElement = new DIVElement();
  this.textElement.addStyleElement('position', 'absolute'); 
  this.textElement.addStyleElement('top', '0px');
  this.textElement.addStyleElement('left', '0px');  
  var theContent = new PElement();
  theContent.addContent(this.text);
  this.textElement.addElement(theContent); 
}

IconButton.prototype.setNaviText=function(aText)
{
  this.text=aText;
  this.textElement = new DIVElement();
  this.textElement.addStyleElement('position', 'absolute'); 
  this.textElement.addStyleElement('top', '4px');
  this.textElement.addStyleElement('left', '40px');
  this.textElement.addStyleElement('width','70px');
  this.textElement.addAttribute("align","left");  
  var theContent = new SPANElement();
  theContent.addContent('<b>' + this.text + '</b>');
  theContent.addAttribute('class','lfTextEntry');
  this.textElement.addElement(theContent); 
}

IconButton.prototype.setHighLightImage=function(aSource)
{
  this.hightLightImage=new ElementTag('img');
  this.hightLightImage.addAttribute('src', aSource);
  this.hightLightImage.addAttribute('alt', '');
}

IconButton.prototype.setStyleClass=function(aClass)
{
  this.styleClass=aClass;
}

IconButton.prototype.setPosition=function(aTop, aLeft)
{
  this.top=aTop;
  this.left = aLeft;
}

IconButton.prototype.setUserObject=function(aObject)
{
  this.userObject = aObject;
}

IconButton.prototype.setEnabled=function(aValue)
{
  if (aValue)
  {
    this.state=this.ENABLED;
    this.replaceButton(this.active);
  }
  else
  {
    this.state=this.DISABLED;
    this.replaceButton(this.disabled);
  }
}
/**
 * Gibt den Status des Button aus. 
 * Ob er aktiviert ist oder nicht.
 * @author Markus Hanses
 */
IconButton.prototype.getState=function()
{
	var theState = this.state == this.ENABLED ? true : false;	
	return theState;
}

IconButton.prototype.onMouseOverPage=function()
{
 if (this.state==this.ENABLED)
 {
   document.body.style.cursor='pointer';
   if (this.hightLightImage != null)
   {
    this.replaceButton(this.hightLightImage);
   }
   if (this.mouseOverText != null)
     overlib(this.mouseOverText);
 }
 return;
}

IconButton.prototype.onMouseOutPage=function()
{
  
  document.body.style.cursor='default';
  if (this.state==this.ENABLED)
  {
   if (this.hightLightImage != null)
   {
    this.replaceButton(this.active);
   }
  }
  nd(20);
}

IconButton.prototype.onMouseDownPage=function()
{
 setTimeout('window[\'jsButtonList\'][\''+this.id+'\'].click();',200);
 if (this.state==this.ENABLED)
 {
    this.replaceButton(this.pressed);
 }
 return;
}

IconButton.prototype.onMouseUpPage=function()
{
 return;
}

/**
 * Replaces the button image
 */
IconButton.prototype.replaceButton=function(anIconElement)
{
  if (this.element == null)
  {
    this.element=document.getElementById(this.id);
  }
  
  if(anIconElement && this.element != null)
  {
    var theHtml = anIconElement.write();
    if (this.textElement != null)
    {
      theHtml+=this.textElement.write();
    }
    this.element.innerHTML=theHtml;
  }
  return;
}


/**
 * Called on click on the button.
 */
IconButton.prototype.click=function()
{
  nd(20);
  if (this.state==this.ENABLED)
  {
   this.replaceButton(this.active);
   this.notifyActionListener(new ActionEvent(this, this.userObject, this.CLICKED));
  }
}

/*
*
* Notifies the listener for the click action
*/
IconButton.prototype.notifyActionListener=function(anEvent)
{
  for (var i = 0; i<this.listener.length; i++)
  {
    this.listener[i].actionPerformed(anEvent);
  }
}
/**
 * Adds the specified action listener to the action listener list.
 */
IconButton.prototype.addActionListener=function(anActionListener)
{
  this.listener[this.listener.length]=anActionListener;
}


/**
 * Return the HTML builder object of this button element.
*/
IconButton.prototype.getElement=function()
{
  if (this.writer==null)
  {
    var theInElement = new DIVElement();
    theInElement.addAttribute('class',this.styleClass);
    theInElement.addAttribute('id',this.id);
    theInElement.addAttribute('onmouseover','javascript:window[\'jsButtonList\'][\''+this.id+'\'].onMouseOverPage();');
    theInElement.addAttribute('onmouseout','javascript:window[\'jsButtonList\'][\''+this.id+'\'].onMouseOutPage();');
    theInElement.addAttribute('onmousedown','window[\'jsButtonList\'][\''+this.id+'\'].onMouseDownPage()');
    theInElement.addAttribute('onmouseup','javascript:window[\'jsButtonList\'][\''+this.id+'\'].onMouseUpPage();');
    theInElement.addStyle('font-family:Arial,Helvetica, sans-serif; font-size:11px; font-weight:bold;');
    theInElement.addStyle('color:black');  
    theInElement.addStyle('position:absolute'); 
    theInElement.addStyleElement('top',this.top+'px'); 
    theInElement.addStyleElement('left',this.left+'px');	
    if (this.state==this.ENABLED)
    {
     theInElement.addElement(this.active);
     if (this.textElement != null)
     {
     }
    }
    else
    {
      theInElement.addElement(this.disabled);
    }
    if (this.textElement != null)
    {
      theInElement.addElement(this.textElement);
    }
    this.writer = theInElement;
  }
  return this.writer;
}


