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'); }
	},
	'.click' : function(element) {
		element.onclick = function() { if ($(this).attr('href')) document.location = $(this).attr('href'); }
	},
	
	'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;
		}
	},
	'.popup' : function(element) {
		element.onclick = function(){
			url = $(this).attr('href'); url += url.indexOf('?') > 0 ? '&layout=plain' : '?layout=plain';
			// are we already in a lightbox?
			console.log(document.body);
			if (document.body.id == 'plain')
				document.location.href = url; // yes, just redirect then
			else
				$.fancybox(url, {type: 'iframe', maxWidth: 900, width: 900, height: '95%'}); // no, show lightbox
			return false;
		}
	}
};

Behaviour.register(myrules);

