// Copyright (c) 2003-2007 Mira Software, Inc. All rights reserved.

/* Demo code
<script src="placeholder.js" type="text/javascript"></script>

<input id="search" type="text">
<script type="text/javascript">
new inputPlaceholder('search', 'search keywords...');
</script>
*/

function inputPlaceholder(id, value)
  {
  var thisCopy = this;
  
  this.inputElement = document.getElementById(id);
  this.value = value;
  this.valueOriginal = (this.inputElement.value == '' || this.inputElement.value == value);

  this.addEvent = function(element, eventType, functor)
    {
    if (element.addEventListener)
      element.addEventListener(eventType, functor, false);
    else if (element.attachEvent)
      element.attachEvent('on' + eventType, functor);
    }

  this.onFocus = function()
    {
    if (this.valueOriginal && this.inputElement.value == this.value)
      this.inputElement.value = '';
    }

  this.onBlur = function()
    {
    if (this.inputElement.value == '' || this.inputElement.value == this.value)
      this.inputElement.value = this.value;
    else
      this.valueOriginal = false;
    }

  this.addEvent(this.inputElement, 'focus',   function() {return thisCopy.onFocus()});
  this.addEvent(this.inputElement, 'blur',    function() {return thisCopy.onBlur()});

  if (this.inputElement.value == '')
    this.onBlur();

  return this;
  }


