// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function spin(id) {
	$(id).innerHTML = '<img src="/images/spinner.gif" alt="Loading"/>';
}

function addCloser(element) {
	var closer = document.createElement('a');
	closer.href="#";
	closer.innerHTML = 'Close';
	closer.className = 'closer';
	closer.style.display = 'inline'; // 'layout' quality required for opacity in IE (see http://wiki.script.aculo.us/scriptaculous/show/GivingElementsLayout)
	closer.onclick = function() {Element.remove(element);return false;};
	element.appendChild(closer);	
}

var application_rules = {
	'.help' : function(e) {
		
	},
	'.flash' : function(e) {
		addCloser(e);
	},
	'.text-field' : function(e) {
		e.originalValue = e.value;
		e.has_changed = function() {
			return this.originalValue != this.value;
		}
		e.testChanged = function() {
			if(this.has_changed())
				Element.addClassName(this, 'changed');
			else
				Element.removeClassName(this, 'changed');			
		}
		e.markChanged = function(event) {
			if (event.which == Event.KEY_ESC) {
				this.value = this.originalValue;
			}
			this.testChanged();
		}
		e.onfocus = function() {
			Element.addClassName(this, 'focus');
		}
		e.onblur = function() {
			Element.removeClassName(this, 'focus');
		}
		e.onkeyup = e.markChanged;
	},
	'.calendar-field' : function(e) {
		Calendar.setup(
			{
		      inputField  : e.id,         // ID of the input field
		      ifFormat    : "%m/%d/%Y",    // the date format
		      eventName      : "click",
				cache : true,
				showOthers: true
		    }
		  );
	}
}

Behaviour.register(application_rules);