var snelsite = {
	externallinks : function()
	{
		$('a[rel="external"]').attr('target', '_blank');
	},

	galleries : function()
	{
		$('div.gallery-thumbs.clrfix a:has(img.gthumb)').click(function(event) {
			$(this).parents('div.content')
				.find('p.selected-thumb-desc')
					.text($(this).find('img')[0].alt)
					.end()
				.find('p.current_picture img')
					.attr('src', this.href.replace(/thumb_/, ''))
					.end();

			event.preventDefault();
		});
	},

	forum : function()
	{
		$('#bbcode-switch').append('<a href="#">(Bekijk)</a>').find('a').click(function(event) {
			$(this).text(($(this).text() == '(Bekijk)' ? '(Verberg)' : '(Bekijk)'));
			$('#bbcode-explanation').toggle();
			event.preventDefault();
		});
		$('#bbcode-explanation').toggle();
	},

	validate_form : function()
	{
		$('#userform').submit(function(event) {
			var err = [];

			// required: input[text], textarea, select
			$(this).find(':input.required[value=""]').each($.proxy(function (index, el) {
				err.push(
					'Verplicht veld niet ingevuld: ' +
					$(el).parent(':has(span.fieldtitle)').find('span.fieldtitle').text().replace(/\s*\*\s*/, '')
				);
			}, this));

			// required: radio, checkbox
			var req = [];
			$(this).find('span.fieldtitle')
				.parent(':has(input:radio, input:checkbox)')
				.not(':has(input:checked)')
				.each($.proxy(function (index, el) {
					err.push(
						'Verplicht veld niet ingevuld: ' +
						$(el).find('span.fieldtitle').text().replace(/\s*\*\s*/, '')
					);
				}, this));

			// email
			$(this).find('input.email[value!=""]').each($.proxy(function (index, el) {
				if (!$(el).val().match(/^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,6}$/i)) {
					err.push(
						'Ongeldige waarde ingevoerd bij: ' +
						$(el).parent(':has(span.fieldtitle)').find('span.fieldtitle').text().replace(/\s*\*\s*/, '') +
						'<br /><small>Dit veld moet een geldig e-mailadres zijn</small>'
					);
				}
			}, this));

			// text_only
			$(this).find('input.text_only[value!=""]').each($.proxy(function (index, el) {
				if (!$(el).val().match(/^[+a-z,.! -]+$/i)) {
					err.push(
						'Ongeldige waarde ingevoerd bij: ' +
						$(el).parent(':has(span.fieldtitle)').find('span.fieldtitle').text().replace(/\s*\*\s*/, '') +
						'<br /><small>Dit veld mag alleen tekst bevatten</small>'
					);
				}
			}, this));

			// digits_only
			$(this).find('input.digits_only[value!=""]').each($.proxy(function (index, el) {
				if (!$(el).val().match(/^[0-9]+$/)) {
					err.push(
						'Ongeldige waarde ingevoerd bij: ' +
						$(el).parent(':has(span.fieldtitle)').find('span.fieldtitle').text().replace(/\s*\*\s*/, '') +
						'<br /><small>Dit veld mag alleen cijfers bevatten</small>'
					);
				}
			}, this));

			if (err.length) {
				$('#form-errors').remove();
				$('#form-errorbox').append(
					'<ul class="content warn" id="form-errors">' +
					'<li><p>' +
					err.join('</p></li><li><p>') +
					'</p></li>' +
					'</ul>'
				);
				window.scrollTo(0,-4000);
				event.preventDefault();
			}
		});
	},

	init : function()
	{
		snelsite.externallinks();
		snelsite.galleries();
		snelsite.validate_form();
	}
}

$(document).ready(snelsite.init);

