
// Form Behaviors

Event.addBehavior({       
  // '.ad_escaped' : function(){
  //   ad = unescape(this.innerHTML);
  //   ifrm = this.up().down('iframe');
  //   ifrm = (ifrm.contentWindow) ? ifrm.contentWindow : (ifrm.contentDocument.document) ? ifrm.contentDocument.document : ifrm.contentDocument;
  //   ifrm.document.open();
  //   ifrm.document.write(ad);
  //   ifrm.document.close();    
  // },
  
  // Using 'form input.initial_focus' does not work in IE (weird)
  '.initial_focus' : function(){
    this.focus();
    this.up().addClassName('focus');
  },
  
  'form input:focus': function(e) {
    this.up().addClassName('focus');
  },
  
  'form select:focus': function(e) {
    this.up().addClassName('focus');
  },
  
  'form input:blur': function(e) {
    this.up().removeClassName('focus');
  },
  
  'form select:blur': function(e) {
    this.up().removeClassName('focus');
  },


  // Use 'prompt' class to make value disappear on focus / reappear if empty
  
  '.prompt:focus' : function(e) {
    if(!this.form.hasClassName('editing')){
      if (this.value == this.defaultValue ){ 
        this.value = '';
      };
    };
  },
  
  '.prompt:blur' : function(e) {
    if(!this.form.hasClassName('editing')){
      if (this.value == '' ){ 
        this.value = this.defaultValue;
      }
    };
  },
  
  
  // Turn forms into AJAX  
  'form.ajaxable:submit' : function(e) {
    new Ajax.Request(this.action, {asynchronous:true, evalScripts:true, method:this.method, parameters:Form.serialize(this)}); 
    return false;    
  },


  // Turn link into AJAX  
  'a.ajaxable:click' : function(e) {
    new Ajax.Request(this.href, {asynchronous:true, evalScripts:true, method:'get'}); 
    return false;    
  },


  // Makes sure a form only gets submitted once
  'form.submit_once:submit' : function() {
   if (this.submitted) {
     // alert('You can only submit this form once. \nPlease wait for a second...');
     return false;
   }
   else {
     this.submitted = true;
   };
  }
  
});



// Time in Words

Event.addBehavior({ 
  'span.time_in_words, p.time_in_words' : function(){
   this.innerHTML = get_local_time_for_date(this.title);
  }
});


// Common check all behavior.  Use as follows:
//
//   Event.addBehavior({
//     'check_all_link': CheckAll('div input[type=checkbox]')
//   })
var CheckAll = Behavior.create ({
  checkboxes : null,
  initialize : function(check_box_location){
    this.checkboxes = check_box_location;
  },
  onclick : function(e) {
    checked = this.element.checked;
    $$(this.checkboxes).each(
      function(o){
        o.checked = checked;
      }
    )
  }
})


// Used on Profile edit_basic page

function mark_for_destroy(e) {
  element = $(e).up('.has_many_element');
  element.down('.should_destroy').value = 1;
  element.hide();
}
