$(function() {
	var found;
	$('#navbar a').each(function() {
		if ($(this).attr('href') == document.location.pathname) {
			var width = $(this).parent().width();
			var height = $(this).parent().height();
			var left = $(this).parent().offset().left;
			var top = $(this).parent().offset().top;		
			//alert('width='+width+' , height='+height+' , left='+left+' , top='+top);
			var layer = '<div id="navact" style="position:absolute;left:'+left+'px;top:'+top+'px;width:'+width+'px;height:'+height+'px;background:#fff;opacity:0.5;filter:alpha(opacity=50);"></div>';
			$('#navact').remove();
			$('body').append(layer);
			found = true;
		} else {
			found = false;
		}
	});
	
	if (found == false) {
		$('#search ul.active').each(function() {
			var href = $(this).find('a').eq(0).attr('href');
			$('#navbar a').each(function() {
				if ($(this).attr('href') == href) {
					var width = $(this).parent().width();
					var height = $(this).parent().height();
					var left = $(this).parent().offset().left;
					var top = $(this).parent().offset().top;		
					var layer = '<div id="navact" style="position:absolute;left:'+left+'px;top:'+top+'px;width:'+width+'px;height:'+height+'px;background:#fff;opacity:0.5;filter:alpha(opacity=50);"></div>';
					$('#navact').remove();
					$('body').append(layer);
				}
			});
		});
	}

	if ($('#searchKeywords').val() == '') {
		$('#searchKeywords').val('Rechercher').css('color', '#999');
	}
	
	$('#searchKeywords').focus(function() {
		$(this).val('').css('color', '#000');
	});
	
	$('#searchKeywords').blur(function() {
		if ($(this).val() == '') $('#searchKeywords').val('Rechercher').css('color', '#999');
	});
	
    slideshow('#slideshow');
    $('#formContact input[type="submit"]').click(sendFormContact);
	
    $('#formContact .datepicker').eq(0).datepicker({
        minDate: 0,
		onSelect: function(dateText) {
			var MyDate = $(this).datepicker('getDate');
			MyDate.setDate(MyDate.getDate());
			if ($('#formContact .datepicker').eq(1).length > 0) {
				$('#formContact .datepicker').eq(1).datepicker('option', 'minDate', MyDate);
				if ($('#formContact .datepicker').eq(1).datepicker('getDate')) {
					var nbNuits = DateDiffDays($(this).datepicker('getDate'), $('#formContact .datepicker').eq(1).datepicker('getDate'));
					$('#nb_nuits').val(nbNuits);	
				}
			}
		}
    });
	
	if ($('#formContact .datepicker').eq(1).length > 0) {
		$('#formContact .datepicker').eq(1).datepicker({
			minDate: 0,
			onSelect: function(dateText) {
				var nbNuits = DateDiffDays($('#formContact .datepicker').eq(0).datepicker('getDate'), $(this).datepicker('getDate'));
				$('#nb_nuits').val(nbNuits);
			}
		});	
		$('#formContact .datepicker').eq(1).datepicker($.datepicker.regional[$('body').attr('lang')]);
	}
    
    $('.newBlank').click(function() {
        window.open($(this).attr('href'));
        return false;
    })
	$('#choixNbPers').dialog({
		title: 'Réserver un hébergement',
		autoOpen: false,
		modal: true,
		width: 400,
		buttons: {
			"Annuler": function() {
			    $(this).dialog('close');
			}
		}
	});
	$('.reserverHebergement').click(function() {
		$('#choixNbPers').dialog('open');
		return false;
	});
	
	$('#choixNbPers a').click(function() {
		$('#choixNbPers').dialog('close');
	});

	
	// Calendrier
	$('#calendrierMois li').click(function() {
		document.location.href = document.location.pathname + '?date=' + $(this).attr('date');
	});
	
	
//	var timer = 0;
	
	//$('#navbar li a').click(function() {
	//	var length = $('#menu'+$(this).attr('rel')+' li').length;
	//	if (length == 0) {
	//		document.location.href = $(this).attr('href');
	//		return false;
	//	}		
	////	if (timer) clearTimeout(timer);
	//	$('#search .menu').hide();
	//	$('#menu'+$(this).attr('rel')).show();
	////	timer = setTimeout('setMenuActive()', 10000);
	//	return false;
	//});
});

// Remettre le menu correspondant à la page en actif
//function setMenuActive() {
//	$('#search .menu').fadeOut('slow', function() {
//		$('#search .active').fadeIn();
//	});
//}

function slideshow(divSlideshow) {
    var current = $(divSlideshow).find('img').eq(0);
    var timer = null;
    var next = null;
    
    $.ajax({
        url: 'slideshow.html?ajax=true',
        dataType: 'json',
        success: function(data) {
            var length = data.length;
            for (var x = 0; x < length; x++) {
                var picture = data[x];
                if (current.attr('src') != '/img-photo-' + picture.ID + '.jpg') {									
                    current.after('<img style="display:none" class="slideshowElement" id="slideshowPicture' + picture.ID + '" src="/img-photo-' + picture.ID + '.jpg" alt="" width="637" height="184" />');					
					//current.parent().append('<img style="display:none" class="slideshowElement" id="slideshowPicture' + picture.ID + '" src="/img-photo-' + picture.ID + '.jpg" alt="" width="637" height="184" />');					
                    $('#slideshowPicture' + picture.ID).load(function() {
                        if (timer == null) {
                            timer = setInterval(function() {
                                var pictures = $(divSlideshow).find('img');
                                var picturesLength = pictures.length;
                                if (picturesLength > 1) {
                                    for (var y = 0; y < picturesLength; y++) {
                                        if (pictures.eq(y).is(':visible')) {
                                            current = pictures.eq(y);
                                            var next = pictures.eq(y + 1);
                                            if (next.length == 0) next = pictures.eq(0);
                                        }
                                    }
                                    if (next != undefined) next.fadeIn('slow');
                                    current.fadeOut('slow');
                                }
                            }, 4000);
                        }
                    });
                }
            }
        }
    });
}

function sendFormContact() {
    var fields = $('#formContact input.required[type!="radio"], #formContact textarea.required');
    var errmsg = '';
    for (var x = 0; x < fields.length; x++) {
        if (fields.eq(x).val() == '') {
            errmsg += "- Vous devez remplir le champ '"+fields.eq(x).attr('name')+"'\n";
        }
    }
    if (errmsg == '') {
        if ($('#email').val() != $('#email2').val()) {
            errmsg += "- Vous devez saisir la même adresse e-mail";
        }
    }
    if (errmsg != '') {
        alert(errmsg);
        return false;
    }
    return true;
}