$(document).ready(function() {
	MRConvertLinks();
});

var currentHREF = window.location.href.split('http://').pop().split('/');
if (currentHREF.length>1 && currentHREF[1].length>0 && currentHREF[1].indexOf('#') != 0 && currentHREF[1].indexOf('nc') != 0) {
	var hashPart = window.location.href.split('http://').pop().split('/').slice(1).join('/');
	if (hashPart.length) hashPart = '#/'+hashPart;
	window.location.href = 'http://'+window.location.href.split('http://').pop().split('/').shift()+hashPart;
}

var MRConvertLinks = function() {
	$('a[href]').each(function(i) {
		if (!$(this).attr('href')) return;
		if ($(this).attr('href').indexOf('http:') == 0) return;
		this.linkTarget = $(this).attr('href');
		var linkName = this.linkTarget.split('#/').pop().split('#').pop();
		while (linkName.indexOf('/') == 0) {linkName = linkName.slice(1)};
		$(this).attr('href', window.location.href.split('#').shift()+'#/'+linkName);
		$(this).click($.fn.loboGallery.checkHash);
	});
}

var sendMail = function(url) {
	var mailObject = {
		name: $('#mailForm  #mailFormName')[0].value,
		message: $('#mailForm  #mailFormMessage')[0].value,
		email: $('#mailForm  #mailFormMail')[0].value,
		absenden: 'absenden'
	}
	//console.log(mailObject);
	
	if (
	  $('#mailFormMessage').data("neverEmptyDefault") == $('#mailFormMessage').val() ||
	  $('#mailFormMail').data("neverEmptyDefault") == $('#mailFormMail').val() ||
	  $('#mailFormName').data("neverEmptyDefault") == $('#mailFormName').val()
	) {
		$('#mailFormStatusMessage').html('Es müssen alle Felder ausgefüllt werden.');
		$('#mailForm').fadeOut();
		$('#mailFormStatus').fadeOut(0);
		$('#mailFormStatus').fadeIn();
		return;
	}
	
	$.post(url+'?type=180', $("#mailForm").serialize(), function(data) {
		$('#mailForm').fadeOut();
		if (data == 'STATE:DONE') {
			$('#mailFormStatusMessage').html('Die Nachricht wurde verschickt.');
		} else {
			$('#mailFormStatusMessage').html('Es müssen alle Felder ausgefüllt werden.');
		}
		$('#mailFormStatus').fadeOut(0);
		$('#mailFormStatus').fadeIn();
	});
}

var mailFormBack =  function(url) {
	$('#mailForm').fadeIn();
	$('#mailFormStatus').fadeOut();
}
