jQuery.fn.formval = function(opts) {
	function formval_error(field,error) {
		$(field).addClass('error');
		$(field).hint(error);
	}
	function formval_noerror(field) {
		$(field).removeClass('error');
		$(field).nohint();
	}
	
	function formval_validate(field,val) {
		formval_noerror(field); // Error verwijderen
		
		if (val.regex) {
			var m = field.value.match(val.regex);
			if (!m) {
				formval_error(field,val.error);
			}
		}
	}
	
	function formval_work(form,field) {
		var vals=null;
		
		var tmp = $(form).find('.vals');

		if (tmp.length>0) {
			vals=eval('('+$(tmp[0]).text()+')');
		}

		for (var a in vals) {
			if (!field || field==a) {
				$(form).find('SELECT[name='+a+']').each(function() { formval_validate(this,vals[a]); } );
				$(form).find('INPUT[name='+a+']').each(function() { formval_validate(this,vals[a]); } );
			}
		}
		
		if ($(form).find('.error').length>0) {
			$(form).find('INPUT[type=submit name^=save]').attr('disabled',true);
			return false;
		}	else
			$(form).find('INPUT[type=submit name^=save]').removeAttr('disabled');
		return true;
	}

	function formval_submit(event) {
		if (event.target.name.match(/^save/) && !formval_work(this))
			event.preventDefault();
	}
	
	function formval_blur(event) {
		formval_work(this.form,this.name);
	}
	
	this
		.submit(formval_submit)
		.find('input[type=text],input[type=password],select')
		.blur(formval_blur)
		;	

	return this;
};

jQuery.fn.formbeauty = function() {
	this
		.find('INPUT,TEXTAREA,SELECT')
		.addClass('beauty')
		.focus(function() {
			$(this).addClass('focused');
			})
		.blur(function() {
			$(this).removeClass('focused');
			})
		;

	return this;
};

jQuery.fn.menu = function() {
	this
		.find('li')
		.each(function() {
				var a = $(this).find('a')[0];
				var txt = $(this).find('a').text();
				var img = $('<img alt="" />')[0];
				img.src='/gfx/button.php?txt='+encodeURIComponent(txt)+(a.className=='a' ? '' : '&off');
				img.style.width='200px';
				img.style.height='50px';
				$(this).find('a').text('').append(img);
			})
		;
};

jQuery.fn.nohint = function() {
	this.prev('.hint').remove(); // oude hint verwijderen
};

jQuery.fn.hint = function(hint) {
	function hint_in() {
		$(this).prev('.hint').fadeTo(250,1);
	}
	function hint_out() {
		$(this).prev('.hint').fadeTo(150,0);
	}
	
	this.each(function() {
			var o = $(this).offset();
			var div = $('<div class="hint">'+hint+'</div>')
				.css('left',o.left+$(this).width()+5)
				.css('top',o.top)
				;
			$(this).prev('.hint').remove(); // oude hint verwijderen
			this.parentNode.insertBefore(div[0],this);
			$(this)
				.unbind('focus',hint_in)
				.unbind('blur',hint_out)
				.focus(hint_in)
				.blur(hint_out)
				;
		});
};

jQuery.fn.listing = function() {
	this.find('tbody tr')
		.click(function(event) {
				if (event.target.nodeName!='INPUT') {
					var cb = $(this).find('input[type=checkbox]');
					if (cb.length>0) {
						$(this).closest('body').loading();
						window.location=window.location+'/'+cb[0].value;
					}
				}
			})
		.hover(
			function() {
				$(this).addClass('over');
			}, function() {
				$(this).removeClass('over');
			})
		;
};

/**
*/
jQuery.fn.loading = function() {
	this.each(function() {
		var o = $(this).offset();
		$('<div><img src="/gfx/loading.gif" /></div>')
			.css('position','absolute')
			.css('background','white')
			.css('textAlign','center')
			.fadeTo(0,0.75)
			.css('top',o.top)
			.css('left',o.left)
			.css('height',$(this).height())
			.css('width',$(this).width())
			.appendTo(this)
			.find('img').each(function() { var y = ($(this.parentNode).height()-$(this).height())/2;$(this).css('marginTop',y); });
		});
	return this;
};

function init(root) {
	$(root).find('form').formbeauty({}).formval({});
	$(root).find('form[rel=ajax]').submit(function(event) {
		$(this).find('input[type=submit]').attr('enabled',false);
		event.preventDefault();
		var con = $(this).closest('.container')[0];
		var data = 'ajax='+con.id+'&'+$(this).serialize();
		$(con).loading();
		$.post(window.location.href,data,function(data) {
			$(con).html(data);
			init(con);
			});
		});
	$(root).find('.delete').find('img')
		.css('cursor','pointer')
		.click(function(event) {
				var m = this.id.match(/([a-z]+)_([0-9]+)/);
				if (m!=null && confirm('Weet je het zeker?')) {
					var con = $(this).closest('.container')[0];
					$(con).loading();
					$.post(window.location.href,'ajax='+m[1]+'&delete='+m[2],function(data) {
						$(con).html(data);
						init(con);
						});
					}
			})
		.closest('LI').hover(function() {
			$(this).find('.delete').css('visibility','visible');
		}, function() {
			$(this).find('.delete').css('visibility','hidden');
		});
	$(root).find('.unhideform')
		.css('cursor','pointer')
		.css('color','blue')
		.click(function() {
			$(this).hide();
			$(this).next('form').show();
			})
		.next('form').hide()
		;
}

jQuery.fn.visivals = function(visivals) {
	for (var n in visivals) {
		var f = $(this).find('#'+n);
		$(f).data('visivals',visivals[n]);
	}
	this.find('SELECT')
		.change(function() {
	
		var v = $(this).val();
		var id = this.id;
	
		$(this).closest('FORM').find('INPUT, TEXTAREA, SELECT').each(function() {

			var visi = $(this).data('visivals');
			if (visi && visi[id]) {
				if (jQuery.inArray(v,visi[id])>=0) {
					$(this).closest('div').show();
				}	else {
					$(this).closest('div').hide();
					$(this).val('').change();
				}
			}
		
			});
	
		})
		.change()
		;
	return this;
};

$(document).ready(function() {
		init($('body')[0]);
		$(this).find('.menu').menu();
		$('table').listing();
		$('a').click(function(event) {
				if (this.href.match(/(lionhead|(members|staff).haarlemjamborette.nl)/)==null) {
					this.target='_blank';
				}
			});
	});

