/*
 * Copyright 2006 OST-SYSTEMS. All rights reserved.
 */

function HectorButton(button, text, height,
					 imageLeft, imageLeftDown, imageLeftWidth,
					 imageCenter, imageCenterDown,
					 imageRight, imageRightDown, imageRightWidth,
					 onclick) {
  this.enabled = true;
  this.imageDisabled = null;
  this.root = null;
  this.textElement = null;
  this.innerTextElement = null;
  this.imageElement = null;
  this._clicked = false;

  if (!button) {
    return;
  }
  
  this.imageElement = new Array(3);
  this.button = button;
  this.text = text;
  this.height = height;
  this.onclick = onclick;

  this.image = [imageLeft, imageCenter, imageRight];	
  this.imageDown = [imageLeftDown, imageCenterDown, imageRightDown];	
  this.imageWidth = [imageLeftWidth, imageRightWidth];	
  
  this._createButton();
  return this;  
}

HectorButton.prototype._setImages = function(_self, images) {
  _self.imageElement[0].src = images[0];
  _self.imageElement[2].src = images[2];
  //this.imageElement[1].setAttribute("background", images[1]);
  _self.imageElement[1].style.background = "url(" + images[1] + ") repeat-x";
}
  
HectorButton.prototype._createButton = function() {
  	var doc = this.button.ownerDocument;
  	
  	this.root = doc.createElement("div");      
  
  	this.button.appendChild(this.root);
  
  	var element = doc.createElement("table");
  	element.setAttribute("border", "0");
  	element.setAttribute("cellspacing", "0");
  	element.setAttribute("cellpadding", "0");
  	element.style.margin = "0px";
  	element.style.borderSpacing = "0px";
  	element.style.width = "auto";
  	this.root.appendChild(element);
  	var table = element;
  	
  	element = doc.createElement("tr");
  	table.appendChild(element);
  	var tr = element;
  	
  	var td = doc.createElement("td");	
  	td.style.padding = "0px";
  	tr.appendChild(td);

  	element = doc.createElement("img");
  	element.setAttribute("alt", "");
  	td.appendChild(element);
  	element.id = this.button.id + "Image0";
  	this.imageElement[0] = element;
  	
  	td = doc.createElement("td");	
  	td.style.padding = "0px";
  	tr.appendChild(td);
  	td.setAttribute("valign", "middle");
  	//td.setAttribute("height", this.height);
  	td.height = this.height;
  	
  	this.imageElement[1] = td;
  
  	element = doc.createElement("span");
  	var style = element.style;
  	element.innerHTML = this.text;
  	style = element.style;
  	//style.lineHeight = this.height + "px";
  	//style.height = this.height + "px";
  	style.overflow = "hidden";
  	style.whiteSpace = "nowrap";
  	style.position = "relative";
  	style.textShadow = "#404040 1px 1px 1px";
  	
  	td.appendChild(element);
  	this.textElement = element;
  	
  	td = doc.createElement("td");	
  	td.style.padding = "0px";
  	tr.appendChild(td);
  	
  	element = doc.createElement("img");
  	element.setAttribute("alt", "");
  	td.appendChild(element);
  	this.imageElement[2] = element;
  	this._setImages(this, this.image);
  	
  	var _self = this;
  	this.root.onmousedown = function(event) {
  	  if (_self.enabled) {
  	    _self._setImages(_self, _self.imageDown);
  	    _self.clicked = true;
  	  }
  		event.stopPropagation();
	  	event.preventDefault();
	  	
  	}
  	this.root.onmouseup = function(event) {
  		event.stopPropagation();
	  	event.preventDefault();
  	  if (_self.enabled) {
  	    _self._setImages(_self, _self.image);
    	  if (_self.clicked) {
    	    _self.clicked = false;
    	    if (_self.onclick) {
  	        _self.onclick(event);
    	    }  
  	    }
  	  }    
  	}
  	this.root.onmouseout = function(event) {
  	  if (_self.enabled) {
  	    _self._setImages(_self, _self.image);
	      _self.clicked = false;
  	  }  
  		event.stopPropagation();
	  	event.preventDefault();
  	}
  }


HectorButton.prototype.remove = function() {
  this.button.removeChild(this.root);	
}

HectorButton.prototype.setDisabledImages = function(imageLeftDisabled, imageCenterDisabled, imageRightDisabled) {
  this.imageDisabled = [imageLeftDisabled, imageCenterDisabled, imageRightDisabled];
}

HectorButton.prototype.setEnabled = function(enabled) {
  this.enabled = enabled;
  if (enabled) {
    this._setImages(this, this.image);
  }
  else if (this.imageDisabled) {
    this._setImages(this, this.imageDisabled);
  }
}

HectorButton.prototype._setPressed = function(pressed) {
  if (this.enabled) {
    if (pressed) { 
      this._setImages(this, this.imageDown);
    }
    else {
  	  this._setImages(this, this.image);      
    }  
  }
}

var _hectorPath = window.hectorImagePath;
if (!_hectorPath) {
  _hectorPath = location.protocol + "//" + location.host + location.pathname;
  var _hectorLastSlash = _hectorPath.lastIndexOf("/");
  if (_hectorLastSlash >= 0) {
    _hectorPath = _hectorPath.substring(0, _hectorLastSlash) + "/scripts/HectorClasses/Images/";
  }
}  

function HectorGlassButton(button, text, onclick) {
  this.imageElement = new Array(3);
  this.button = button;
  this.text = text;
  this.height = 23;
  this.onclick = onclick;

  var _hectorPath = getHectorPath() + "scripts/HectorClasses/Images/";  

  var buttonPrefix = _hectorPath + "glassbutton2"
  this.glassButtonImage = [buttonPrefix + 'left.png', buttonPrefix + 'middle.png', buttonPrefix + 'right.png'];
  this.glassButtonImageDown = [buttonPrefix + 'leftclicked.png', buttonPrefix + 'middleclicked.png', buttonPrefix + 'rightclicked.png'];

  this.image = this.glassButtonImage;	
  this.imageDown = this.glassButtonImageDown;
  this.imageWidth = [10, 10];
  
  this._createButton();
  
  this.textElement.style.color = 'white';
  this.textElement.style.fontFamily = 'sans-serif';
  this.textElement.style.fontSize = '12px';
  this.textElement.style.fontWeight = 'bold';
  this.textElement.style.height = this.height + "px";  
}

HectorGlassButton.prototype = new HectorButton(null);

HectorGlassButton.prototype.setEnabled = function(enabled)
{
	this.enabled = enabled;
	if (enabled)
	{
		this.textElement.style.color = "white";
	}
	else
	{
		this.textElement.style.color = "rgb(150,150,150)";
	}
}

function createGenericButton (div, title, onaction, minwidth) {
  while (div.firstChild != null) {
    div.removeChild(div.lastChild);
  }
  var button = new HectorGlassButton(div, title, onaction);
  div._button = button;

  var _hectorPath = getHectorPath() + "scripts/HectorClasses/Images/";  

  var buttonPrefix = _hectorPath + "glassbutton2";
  var _hectorGenericButtonImage = 
    [buttonPrefix + "left.png", buttonPrefix + "middle.png", 
     buttonPrefix + "right.png"];
  var _hectorGenericButtonImageDown = 
    [buttonPrefix + "leftclicked.png", buttonPrefix + "middleclicked.png", 
     buttonPrefix + "rightclicked.png"];

  button.image = _hectorGenericButtonImage;
  button.imageDown = _hectorGenericButtonImageDown;
  button._setImages(button, button.image);
  if (minwidth) {
    button.button.style.minWidth = minwidth + "px";
  }  
  return button;
}  

function genericButtonSetEnabled (div, enabled) {
  if (div._button != null) {
    div._button.setEnabled(enabled);
  }
}
        
