$(function() {

	$('#guilty ul a').hover(function() {
		if ($('.bubble', this).length == 0) {
			var bubble = $('<div />').addClass('bubble').appendTo(this);
			var li = $(this).closest('li');
			var img = $('<div />')
				.addClass('img')
				.css('backgroundImage', 'url(' + li.attr('imgsrc') + ')')
				.appendTo(bubble);
			li.addClass('ready');
		}
		$('.bubble', this).fadeIn(200);
	}, function() {
		$('.bubble', this).fadeOut(200);	
	});
	
	$('.select-deco select').change(function() {
		$('.value', $(this).parent()).text($(this.options[this.selectedIndex]).text());
	});
	
	$('#commitment').submit(function() {
		var form = this;
		var validate = [
			['_fname', 'etunimi', /.+/],
			['_lname', 'sukunimi', /.+/],
			['_ybirth', 'syntymävuosi', /^[0-9]{4}$/],
			['_piiri', 'kunta', /.+/],
			['_mailadr', 'sähköpostiosoite', /@/]
		];
		for (var i = 0; i < validate.length; i++) {
			var f = $('[name=' + validate[i][0] + ']', form);
			f.val($.trim(f.val()));
			if (! f.val().match(validate[i][2])) {
				alert('Tarkista ' + validate[i][1] + '.');
				f.focus();
				return false;
			} else {
				$('<input />')
					.attr('type', 'hidden')
					.attr('name', validate[i][0].substring(1))
					.attr('value', f.val())
					.appendTo(form);
			}
		}
		$('input[type=text]', form).val('');
		$('.select-deco .value', form).text('');
		return true;
	});
	
	$('#about-link').click(function() {
		$('#tietoa').toggle();
	}).each(function() {
		if (window.location.hash == "#tietoa") $(this).click();
	});
	
	$('a[href^=http://]').each(function() {
		$(this).attr('target', '_blank');
	});

});

