document.observe('dom:loaded',function(){
  ClickClear.init();
  //List = null;
})

// ===== Click Clear =====
// Author: Nick Merwin (nickmerwin@gmail.com)
// Version: 1.2
// Last Updated: 09.16.08
// License: MIT
// Requirements: Prototype >1.6
// Usage: 
// * give input elements a class "click_clear"
// * give password fields a name that includes "password" (i.e. "user_password") 
//    and set type="text"
// * set alt attribute to default text for text field inputs and title attribute for textareas
// * call ClickClear.init() after DOM loaded
// * IE7 can't convert password field back to text field
var ClickClear = {
  inputs: {},
  init:function(el){
    $$(el ? el+' .click_clear' : '.click_clear').each((function(input){
      if(input.value == "" || input.value == null) input.value = this.defaulty(input);
      this.observe(input);
      
      //alert('its: ' + input.id + ' / ' + input.name + ' : ' + input.up('form').id + ' / ' + input.up('form').name);
      
      try{input.up('form').observe('submit',(function(e){
        Event.element(e).select(".click_clear").each((function(input){this.check(input)}).bind(this));
        return true;
      }).bind(this))}catch(e){}
    }).bind(this));
  }, 
  check:function(input){if(input.value == this.inputs[this.getId(input)]) input.value = ''},
  clone:function(input,type){
    this.cloning = true;
    var clone = input.cloneNode(false);
    try{clone.type = type}catch(e){}
    input.parentNode.replaceChild(clone, input);
    this.cloning = false;
    return this.observe(clone);
  },
  observe:function(input){
    var id = this.getId(input);
    this.inputs[id] = this.defaulty(input.observe('focus',(function(e){
      if(this.cloning) return;
      var input = Event.element(e);
      if(input.value == this.inputs[id]) {
        input.value = "";
        if(/password/.test(id)) input = this.clone(input, "password");
        setTimeout(function(){input.focus()},10);
        return input;
      }
    }).bind(this)).observe('blur',(function(e){
      if(this.cloning) return;
      var input = Event.element(e);
      if(input.value == "") {
        input.value = this.inputs[id];
        if(/password/.test(id)) this.clone(input, "text");
      }
    }).bind(this)));
    return input;
  },
  defaulty:function(input){return input.nodeName == "TEXTAREA" ? input.title : input.alt},
  getId:function(input){return input.id || input.name},
  cleary:function(){$$('.click_clear').each((function(input){this.check(input)}).bind(this))},
  reset:function(input){$(input).value = ''; $(input).blur()}
}
// ===== end ClickClear =====