jQuery(document).ready(function() {
	singleTabs();
	addComment();
	newsLetter(jQuery('#newsform'));
	jQuery("a[rel='flickrthings']").fancybox({
		'titleShow'			: true,
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'cyclic'			: true
	});
});

function singleTabs() {
	if ( self.document.location.hash.substring(1).match('comment') ) {
		jQuery('#_avis').show(0);
		jQuery('#tab_2 li a[href="#_avis"]').parent().addClass('selected');
	} else if ( self.document.location.hash.substring(1).match('reservations') ) {
		jQuery('#_reservations').show(0);
		jQuery('#tab_2 li a[href="#_reservations"]').parent().addClass('selected');
	} else {
		jQuery('.jTabs:first').show(0);
		jQuery('#tab_2 li:first-child').addClass('selected');
	}
	
	jQuery('#tab_2 li:first').addClass('firstTab');
	
	jQuery('#tab_2 li a').click(function(e){
		e.preventDefault();
		jQuery('#tab_2 li').removeClass('selected');
		jQuery('.jTabs').hide(0);
		jQuery(jQuery(e.currentTarget).attr('href')).show(0);
		jQuery(e.currentTarget).parent().addClass('selected');
	});
	jQuery('.reserve_2 a').click(function(e){
		e.preventDefault();
		jQuery('#tab_2 li').removeClass('selected');
		jQuery('.jTabs').hide(0);
		jQuery(jQuery(e.currentTarget).attr('href')).show(0);
		jQuery('#tab_2 li a[href="' + jQuery(e.currentTarget).attr('href') + '"]').parent().addClass('selected');
	});
}

function addComment() {
	jQuery('a.avis').click(function(e){
		//e.preventDefault();
		jQuery('#respond').show(0);
	});
	
	if ( self.document.location.hash.substring(1).match('comment-form') ) {
		jQuery('#respond').show(0);
	}
	
	if ( jQuery('select[name="select-author"]').val() == 1 ) {
		jQuery('.reply-author').show(0).find('input[name="author"]').addClass('required');
	} else {
		jQuery('.reply-author').hide(0).find('input[name="author"]').removeClass('required');
	}
	
	jQuery('select[name="select-author"]').change( function() {
		if ( jQuery(this).val() == 1 ) {
			jQuery('.reply-author').slideDown(200).find('input[name="author"]').addClass('required');
		} else {
			jQuery('.reply-author').slideUp(200).find('input[name="author"]').removeClass('required');
		}
	});
	
	jQuery('#commentform').submit( function() {
		jQuery(this).find('.input-error').remove();
		var els = jQuery(this).find('.required');
		var i = 0;
		
		els.each( function() {
			if ( $(this).val() == '' ) {
				$(this).after('<div class="input-error">Ce champs est obligatoire</div>');
			} else if ( $(this).hasClass('email') && !checkMail($(this).val()) ) {
				$(this).after('<div class="input-error">L\'adresse mail est invalide</div>');
			} else {
				i++;
			}
			errorInput($(this));
		});
		
		if ( i === els.length ) {
			return true;
		}
		return false;
	});
}

function checkMail(mail) {
	var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(mail)) {
		return true;
	}
	return false;
}

function errorInput(el) {
	var n = el.nextAll('.input-error:first');
	if ( n.length ) {
		n.parent().css({ position: 'relative' });
		n.css({
			left: n.prev().position().left
		}).mouseenter(function(){
			$(this).fadeOut(200, function(){ $(this).remove(); });
		});
	}
}

function newsLetter(form) {
	form.submit( function() {
		jQuery.ajax({
			type: 'POST',
			url: blogUrl + '/_subscribe.php',
			data: form.serialize(),
			async: true,
			success: function(post) {
				form.find('.nl_msg').remove();
				form.append('<div class="nl_msg">' + post + '</div>');
			}
		});
		return false;
	});
}

