
$(function() {
	
	$('#Breadcrumbs ul li:not(:last)').each(function() {
		$( '<span>\xBB</span>' ).appendTo(this);
	});
	$('#Submenu li.visiting').each(function() {
		$( '<span>\xBB</span>' ).prependTo(this);
	});
	$('#Images a.prettyPhoto').prettyPhoto();
	$('#Images img.gallery:even').addClass('left');
	
	$('form.query').submit(function() {
		
		$(this).find('.error').removeClass('error');
		var notices = $(this).find('.warning');
		
		var validate = $(this).validate();
		
		if ( ! validate.success ) {
			$.each(validate.errors, function() {
				notices = notices.not( $('#errorNotice_' + this.id) );
			});
		}
		
		notices.remove();
		
		if ( validate.success ) {
			
			var values = $(this).serializeArray();
			for ( var i in values ) {
				var id = $(this).find(':input[name=' + values[i].name + ']').attr('id');
				var label = $(this).find('label[for=' + id + ']');
				if (label.length) {
					values[i].name = label.text();
				}
			}
			
			var form = this;
			$.post($(this).attr('action'), values, function(resp) {
				if (resp.success) {
					form.reset();
					location.href = '/kundklubb/registrera/slutford';
				} else {
					alert('An error accured! Please try again.');
				}
			}, 'json');
			
		} else {
			$.each(validate.errors, function() {
				$(this.input).addClass('error');
				var notice = $('#errorNotice_' + this.id);
				if ( notice.length ) {
					notice.html( errorMessage(this) );
				} else {
					$(document.createElement('div'))
						.hide()
						.attr('id', 'errorNotice_' + this.id)
						.addClass('warning')
						.html( errorMessage(this) )
						.insertAfter(this.input)
						.width($(this.input).width())
						.slideDown();
				}
			});
		}
		
		return false;
		
	}).each(function() {
		$(this).find(':input[value=""]:not(:hidden):first').focus();
		
		var form = this;
		$(this).find('.validate-require:input').each(function() {
			var id = $(this).attr('id');
			$(form).find('label[for=' + id + ']').addClass('require-mark');
		});
	});
	
});

function renderMap( id, lat, lon ) {
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById( id ));
		var point = new GLatLng(lat, lon);
		map.setCenter(point, 15);
		map.addOverlay(new GMarker(point));
		map.setUIToDefault();
	}
}

