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(); }
	},
	'.buttons 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 .button' : 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;
		}
	},
	
	'.fader' : function(el) {
		// params
		el.delay = parseInt($(el).attr('delay'));
		el.speed = $(el).attr('speed');
		if (el.speed != 'slow' && el.speed != 'normal' && el.speed != 'fast') el.speed = parseInt(el.speed);
		el.count = parseInt($(el).attr('count'));
		el.basehref = $(el).attr('basehref');
		el.start = parseInt($(el).attr('start'));
		el.images = [];
		for(var i=0; i<el.count; i++) el.images[i] = $(el).attr('image' + i);
			
		el.fade2next = function() {
			var next = el.start+1 == el.count ? 0 : el.start + 1;
			// preload next image
			imgPreload = new Image();
			imgPreload.onload=function() {
				$('.next_image', el).css('background-image', 'url(' + el.basehref + el.images[next] + ')').fadeIn(el.speed, el.finishedFading);
			}
			imgPreload.src = el.basehref + el.images[next];
			
			$(el).attr('start', next);
		}
		// finished fading
		el.finishedFading = function() {
			// swap images around
			var next = $('.next_image', el);
			var curr = $('.cur_image', el);
			curr.css('background-image', next.css('background-image'));	// set outer bg to inner bg
			next.hide();
			el.prepare4nextFade();
		}
		el.prepare4nextFade = function() {
			setTimeout(el.fade2next, el.delay);
		}
		el.prepare4nextFade();
	}
};

Behaviour.register(myrules);

