var myrules = {
	'.list .even' : function(element) {
		element.onmouseover = function(){ $(this).addClass('highlight'); }
		element.onmouseout = function(){ $(this).removeClass('highlight'); }
	},
	'.list .odd' : function(element) {
		element.onmouseover = function(){ $(this).addClass('highlight'); }
		element.onmouseout = function(){ $(this).removeClass('highlight'); }
	},
	'a.popup' : function(element) {
		element.onclick = function(){
			var href = $(this).attr('href');
			window.open(href + (href.indexOf('?') >= 0 ? '&' : '?') + 'layout=mini', '_popup', 'height=500,resizable=yes,width=550,scrollbars=yes');
			return false;
		}
	},
	'.toggle img' : function(element) {
		element.onmouseover = function(){
			this.oldsrc = this.src;
			this.src = this.attributes.getNamedItem('toggle').value;
		}
		element.onmouseout = function() {
			if (!this.oldsrc)
				this.src = this.attributes.getNamedItem('src').value;
			else
				this.src = this.oldsrc;
		}
	},
	'.radio_list .row' : function(element) {
		element.onclick = function() { $('input:enabled', this).each(function() { this.checked = true; }); }
	},
	'.click tr' : function(element) {
		element.onclick = function() { if ($(this).attr('href')) document.location = $(this).attr('href'); }
	},
	
	'input' : function(element) {
		element.onfocus = function(){ $(this).addClass('focused'); }
		element.onblur = function(){ $(this).removeClass('focused'); }
	},
	'textarea' : function(element) {
		element.onfocus = function(){ $(this).addClass('focused'); }
		element.onblur = function(){ $(this).removeClass('focused'); }
	},
	'select' : function(element) {
		element.onfocus = function(){ $(this).addClass('focused'); }
		element.onblur = function(){ $(this).removeClass('focused'); }
	},
	'.button' : function(element) {
		//$(element).disableSelection();
		element.onfocus = function() { this.blur(); }
	},
	'#map' : function(el) {
		load();
	},
	'input.submit' : function(element) { element.style.display = 'none'; },
	'span.submit' : function(element) {
		element.onmouseover = function(){ $(this).addClass('hover').addClass('hover_submit'); }
		element.onmouseout = function(){ $(this).removeClass('hover').removeClass('hover_submit'); }
		// little tricky cos we need to look for name conflicts between form.submit method and form.submit element :(
		element.onclick = function() {
			if (this.busy) { return; }
			
			// optional validation
			if ($(this).attr('validate')) {
				var errors = window.validate(this);
				var error_html = '';
				//alert(errors);
				if (errors === undefined) {
					// no errors given so all ok
				}
				else if (errors === false) {
					alert('Validation failed');
					return false;
				}
				else if (errors && errors.length) {
					// is array?
					if (errors.constructor == Array().constructor) {
						for(i in errors) error_html += '<li>' + errors[i] + '</li>';
						error_html = '<b>Error:</b><ul>' + error_html  + '</ul>';
						document.location.hash = 'top';
					}
					else
						error_html = errors;
					// show error
					if ($('#errors').length == 0)
						alert(errors);
					else
						$('#errors').show().html(error_html);
					return false;
				}
				// anything other than 'true' assumed a falure
				else if (errors !== true) {
					return false;
				}
			}
			
			var name = $(this).attr('name');
			$(this).parents('form').find('input.submit[name='+name+']').val(name).click();
			$(this).addClass('busy');
			this.busy = true;
		}
	},
	'.buttons a' : function(element) {
		element.onmouseover = function(){ $(this).addClass('hover'); }
		element.onmouseout = function(){ $(this).removeClass('hover'); }
		if ($(element).attr('onclick')) { }
		else element.onclick = function() {
			if (this.busy) { return; }
			$(this).addClass('busy');
			this.busy = true;
			return true;
		}
	}
};

Behaviour.register(myrules);
