Event.observe(window, 'load', function() {
  // highlights rows on mouseover, does NOT work in ie
	$$('table.recordset tr').each(function(element) {
		Event.observe(element, 'mouseover', function(e) {
			this.addClassName("ruled");
		});
		
		Event.observe(element, 'mouseout', function(e) {
			this.removeClassName("ruled");
		});
	});

	et = new ElementToggler({collapsibleElement: 'table', clickableElementTag: 'th', collapsibleElementClass: 'collapsing'});
	et.loadToggleInformation();

  Ajax.Responders.register({
    onCreate: function() {
      if($('notification') && Ajax.activeRequestCount > 0) {
        Effect.Appear('notification',{duration: .5, queue: 'end'}); } },
    onComplete: function() {
      if($('notification') && Ajax.activeRequestCount == 0) {
        Effect.Fade('notification',{duration: .5, queue: 'end'}); } }
  });
  
  Element.prototype.triggerEvent = function(eventName) {
    if (document.createEvent) {
      var evt = document.createEvent('HTMLEvents');
      evt.initEvent(eventName, true, true);
      return this.dispatchEvent(evt);
    }
    
    if (this.triggerEvent) { return this.triggerEvent('on' + eventName); }
  };
});

function toggle_all_check_boxes_in(dom_id, checked) {
	$(dom_id).getElementsBySelector('input[type=checkbox]').each(function(checkbox) {
		checkbox.checked = checked;
	});
}

function transfer(from, to) {
	from = $(from);
	to = $(to);
	
	from.getElementsBySelector("option").each(function(option) {
		if(option.selected) {
			option.selected = false;
			option.remove();
			to.appendChild(option);
		}
	});
}

function select_options(element) {
	$(element).getElementsBySelector("option").each(function(option) {
		option.selected = true;
	});
}

function select_option(val, element) {
	for($i = 0; $i < $(element).length; $i++) {
		if ($(element).options[$i].value == val) {
			$(element).selectedIndex = $i;
		}
	}
}

function set_notice(notice) {
  $('notice').update(notice).show();
}

function showProgressBar() {
  $('upload-button').hide();
  $('upload-progress').show(); 
  return true; 
}

/* A pretty little hack to make uploads not hang in Safari. Just call this
 * immediately before the upload is submitted. This does an Ajax call to
 * the server, which returns an empty document with the "Connection: close"
 * header, telling Safari to close the active connection. A hack, but
 * effective.
 */

function closeKeepAlive() {
  if (/AppleWebKit|MSIE/.test(navigator.userAgent)) {
    new Ajax.Request("/utilities/close_keepalive", { asynchronous:false });
  }
}

function preSubmit(result) {
  if (result == true) {
    showProgressBar();
    closeKeepAlive();
  }
}
