
function fw_link(id, anchor)
{
  if (anchor)
  {
    this.elem = anchor;
  }
  else if (id)
  {
    this.elem = document.getElementById(id);
    if (! this.elem)
    {
      alert("unable to find link instance " + id);
    }
  }
}

fw_link.prototype.get_id = function()
{
  return this.elem.getAttribute("id");
}

fw_link.prototype.get_add_properties_to_url = function()
{
  var result = this.elem.getAttribute("iw_add_properties_to_url");
  return (result ? eval(result) : true);
}

fw_link.prototype.get_url = function()
{
  return this.elem.href;
}

fw_link.prototype.set_url = function(url)
{
  this.elem.href = url;
}

fw_link.prototype.set_label = function(label)
{
  this.elem.innerHTML = label;
}

fw_link.prototype.get_label = function()
{
  return this.elem.innerHTML;
}

fw_link.prototype.get_target = function()
{
  return this.elem.target;
}

fw_link.prototype.is_enabled = function()
{
  return eval(this.elem.getAttribute("iw_enabled"));
}

fw_link.prototype.is_visible = function()
{
  return this.elem.style.display != "none";
}

fw_link.prototype.set_css_class = function(css_class)
{
  this.elem.setAttribute(fwIsIE ? "className" : "class", css_class);
}

fw_link.prototype.get_css_class = function()
{
  return this.elem.getAttribute(fwIsIE ? "className" : "class")
}

fw_link.prototype.set_enabled = function(enabled)
{
  if (enabled == this.is_enabled())
  {
    return;
  }

  this.elem.setAttribute("iw_enabled", enabled);
  var newCss = this.elem.getAttribute(enabled ? "iw_css_enabled" : "iw_css_disabled");
  if ( !newCss )
  {
    var baseCss = this.get_css_class();
    baseCss = baseCss.replace( /-disabled$/, "");
    newCss = enabled ? baseCss : (baseCss + "-disabled");
  }
  this.set_css_class(newCss);
  this.elem.setAttribute("style", this.elem.getAttribute(enabled ? "iw_style_enabled" : "iw_style_disabled"));

  if(!enabled)
  {
    var currentHref = this.elem.getAttribute("href");
    if(currentHref)
  	  this.elem.setAttribute("iw_href", currentHref);
  	this.elem.removeAttribute("href");
  
    if (!this.elem.iw_onclick_when_enabled)
    {
      this.elem.iw_onclick_when_enabled = this.elem.onclick;
    }
    this.elem.onclick = function() { return false; };
  }
  else
  {
    if (this.elem.getAttribute("iw_href")) {
        this.elem.setAttribute("href", this.elem.getAttribute("iw_href"));
    }
  
    var onclick_backup = this.elem.iw_onclick_when_enabled;
    if (! onclick_backup)
    {
      // for the first time on mozilla, it is necessary to use getAttribute
      // since it has not yet been loaded into the JS DOM tree
      onclick_backup = this.elem.getAttribute("iw_onclick_when_enabled");
    }

    if (typeof onclick_backup == "string")
    {
      //need to convert it into a function object.
      onclick_backup = new Function("event", onclick_backup);
      this.elem.iw_onclick_when_enabled = onclick_backup;
    }
    this.elem.onclick = (onclick_backup 
                         ? onclick_backup 
                         : function() {  return true; });
  }
}

fw_link.prototype.set_visible = function(visible)
{
  if (visible == this.is_visible())
  {
    return;
  }
  
  this.elem.style.display = (visible ? "" : "none");
}

fw_link.prototype.set_image = function(imageSrc)
{
    if (imageSrc.indexOf("/") == 0)
    {
        imageSrc = fwContextPath + imageSrc;
    }

    this.elem.getElementsByTagName("IMG")[0].src = imageSrc;
}

fw_link.prototype.set_tooltip_desc = function(title)
{
    this.elem.getElementsByTagName("IMG")[0].title = title;
}

fw_link.prototype.get_flags = function()
{
  return this.elem.getAttribute("iw_flags");
}

fw_link.prototype.set_flags = function(flags)
{
  return this.elem.setAttribute("iw_flags",flags);
}


function fw_button(id)
{
  this.base = fw_link;
  this.base(id);
}

fw_button.prototype = new fw_link();

fw_button.prototype.toString = function()
{
  return "button " + this.get_id() + " is " + (this.is_enabled() ? "enabled" : "disabled");
}
