var myrules = {
	'.list .even' : function(element) {
		element.addEventListener('mouseover', function(){ $(this).addClass('highlight'); }, false);
		element.addEventListener('mouseout', function(){ $(this).removeClass('highlight'); }, false);
	},
	'.list .odd' : function(element) {
		element.addEventListener('mouseover', function(){ $(this).addClass('highlight'); }, false);
		element.addEventListener('mouseout', function(){ $(this).removeClass('highlight'); }, false);
	},
	'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();
	},
	'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.addEventListener('mouseover', function(){ $(this).addClass('hover'); }, false);
		element.addEventListener('mouseout', function(){ $(this).removeClass('hover'); }, false);
		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'));
		if (!el.delay) {
			el.delay_from = parseInt($(el).attr('delay_from'));
			el.delay_to = parseInt($(el).attr('delay_to'));
		}
		el.delay_initial = parseInt($(el).attr('delay_initial')); if (!el.delay_initial) el.delay_initial = 0;
		el.speed = $(el).attr('speed');
		if (el.speed != 'slow' && el.speed != 'normal' && el.speed != 'fast') el.speed = parseInt(el.speed);
		el.fader_children = $(el).children();
		el.count = el.fader_children.length;
		el.start = el.count - 1;
		
		el.fade2next = function() {
			// 1: fade out everything except bottom (first) one
			if (el.start > 0)
				el.fader_children.eq(el.start).fadeOut(el.speed, el.prepare4nextFade);
			
			// 2: at bottom (first) one, so fade first back in
			else
				el.fader_children.eq(el.count - 1).fadeIn(el.speed, function() {
					el.prepare4nextFade();
					el.fader_children.slice(0, el.count - 1).show();
				});
			
			$(el).attr('start', el.start > 0 ? el.start - 1 : el.count-1);
		}
		el.prepare4nextFade = function() {
			setTimeout(el.fade2next, el.delay 
				? el.delay 
				: Math.floor(el.delay_from + (Math.random() * (el.delay_to - el.delay_from)))
			);
		}
		
		if (el.delay_initial > 0) setTimeout(el.fade2next, el.delay_initial);
		else el.prepare4nextFade();
	},
	'a.iframed' : function(el) {
		el.onclick = function() {
			$('#iframe_overlay, #iframe_wrapper').remove();
			$('body').append("<div id='iframe_overlay'></div><div id='iframe_wrapper'><div id='iframe_outer'><a id='iframe_close'></a><iframe id='iframe_frame' scrolling='auto' frameborder='0' src='" + $(el).attr('href') + "'></iframe></div></div>");
			$('#iframe_close').click(function() { $('#iframe_overlay, #iframe_wrapper').remove(); });
			return false;
		}
	}
};

Behaviour.register(myrules);
$(function() { $('input.submit').hide();});

