/* Author: 

*/

$(function (){
	/*
	 * FONCTION DE PRÉCHARGEMENT DES IMAGES
	 * ON NE PRÉCHARGE QUE LES QUATRES IMAGES
	 * DE FONDS (UNE POUR CHAQUE MENU)
	 */
	$('aside#reservation').hide();
    $.preLoadGUI([
    	['img/fond/fond_home.png', 'img'],
    	['img/fond/Ch_suitej.jpg', 'img'],
    	['img/fond/Lo_golf.jpg', 'img'],
    	['img/fond/Re_incentives.jpg', 'img']
    ]);
    
    /*
     * DATE PICKER POUR LA SÉLECTION DES DATES
     */
	$(".datepicker").datepicker({
	    altFormat: "dd/mm/yy",
	    dateFormat: "dd/mm/y",
	    dayNamesMin: ["Di", "Lu", "Ma", "Me", "Je", "Ve", "Sa"],
	    dayNames: ["Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"],
	    dayNamesShort: ["Dim", "Lun", "Mar", "Mer", "Jeu", "Ven", "Sam"],
	    firstDay: 1,
	    monthNames: ["Janvier", "F\u00e9vrier", "Mars", "Avril", "Mai", "Juin", "Juillet", "Ao\u00fbt", "Septembre", "Octobre", "Novembre", "D\u00e9cembre"],
	    monthNamesShort: ["Jan", "F\u00e9v", "Mar", "Avr", "Mai", "Jui", "Jui", "Ao\u00fb", "Sep", "Oct", "Nov", "D\u00e9c"],
	    minDate: "+1d",
	    onClose: function (a) {
	       if( $(this).attr('id') == 'reservation_arrivee' && a != '' ) {
	       	date = $.datepicker.parseDate("dd/mm/y", a);
	    	date.setDate(date.getDate()+1);
	       $("#reservation_depart").datepicker("option","minDate",date); }
	    }
	});

	$(".ui-datepicker").css('zIndex', '99999989');

	/*
	 * MENU DÉROULANT
	 */
	$("ul.topnav li").hoverIntent({
		sensitivity: 10, // number = sensitivity threshold (must be 1 or higher)
		interval: 50,   // number = milliseconds for onMouseOver polling interval
		over: function() {$(this).find("ul.subnav").slideDown(400,'easeOutBack')},     // function = onMouseOver callback (required)
		timeout: 300,   // number = milliseconds delay before onMouseOut
		out:  function() {$(this).find("ul.subnav").slideUp(700,'easeOutBack')}       // function = onMouseOut callback (required)
	});

	/*
	 * FONCTION HASHAGE POUR LES #
	 * DANS LES LIENS ET LA NAVIGATION INTELLIGENTE
	 */
	$(window).hashchange(function() {
			if ( window.location.hash.substring(1) != '' && $('ul.topnav li[data-link='+window.location.hash.substring(1)+']').length >= 1 )
				$('ul.topnav li[data-link='+window.location.hash.substring(1)+']').menu_click();
	}); $(window).hashchange();
	
	/*
	 * FONCTIONS AU BIND DU CLICK
	 */
	$('h1#logo').click(function() { window.location.replace('index.php'); }) ;
	$('a.contact, a#contact_form_close').click(function() { $('div#contact_form').toggleDIV(); return false; });
	$('a#reservation_form_fermer').click(function() { $('div#reservation_form').toggleDIV(); return false; });


	/**************************************************
	 ************** FONCTIONS DES INPUTS **************
	 **************************************************/
	$('input.numeric').ForceNumericOnly();
	$('input.max').change(function() {
		$(this).parent().find('span.nd').is(':visible') &&
			( parseInt($(this).val()) <= parseInt($(this).attr('max')) ) &&
				$(this).parent().find('span.nd').fadeOut();
	});
	$('input.plurial').change( function() {
		if ( $.browser.msie ) {
			$(this).val()>1?$(this).parent().children(".pluriel").show().css('display', 'inline'):$(this).parent().children(".pluriel").hide().css('display', 'none');
		} else {
			$(this).val()>1?$(this).parent().children(".pluriel").fadeIn():$(this).parent().children(".pluriel").fadeOut();
		}
	});
	$('.checkboxes input').change(function() {
		$(this).is(":checked")&&($(".isChecked").removeClass("checked"),$(this).prev(".isChecked").addClass("checked"));
	});
	/**************************************************
	 **************************************************
	 **************************************************/

	$('span#ConditionsResa_click').hoverIntent({
		sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)
		interval: 0,   // number = milliseconds for onMouseOver polling interval
		over: function(event) { $('#conditionresa_div').show(); $('#chambres_div').hide(); },     // function = onMouseOver callback (required)
		timeout: 0,   // number = milliseconds delay before onMouseOut
		out:  function(event) { $('#chambres_div').fadeIn(); $('#conditionresa_div').fadeOut();}       // function = onMouseOut callback (required)
	});
		
	/**************************************************
	 ************ FORMULAIRE DE RESERVATION ***********
	 **************************************************/
	form_reservation = function () {
	    $("input#reservation_depart").val() == "" || $("input#reservation_depart").val() == "" ? $("aside#reservation").tilt() : ($("div#reservation_form").toggleDIV(), dep = $("input#reservation_depart").val(), arr = $("input#reservation_arrivee").val(), $("input#date_depart").val(dep), $("input#date_arrivee").val(arr), $.ajax({
	        url: "ajax/getFreeRooms.php",
	        dataType: "json",
	        data: {
	            d: dep,
	            a: arr
	        },
	        success: function (c) {
	            $.each(c, function (a, b) {
	                $("ul#roomscheck li").eq(a).find(".RoomCount").html(b);
	                $("ul#roomscheck li").eq(a).find("input").attr("max", parseInt(b, 10))
	            })
	        }
	    }));
	    return !1
	};
	$("form#reservation").submit(form_reservation);
	$("input#reservation_submit").click(form_reservation);

	///// FORMULAIRE //////
	$("form#reservation_form").submit(function(){var a=!1;$(this).find("input").each(function(){$(this).hasClass("required")&&$(this).val()==""&&($(this).tilt(100,10).css("borderColor","red"),a=!0)});var b=0;$("input.room").each(function(){b+=parseInt($(this).val());parseInt($(this).val())>parseInt($(this).attr("max"))&&($(this).parent().find("span.nd").show().tilt(),a=!0)});if(a===!0)return!1;if(b==0)return!1;$.ajax({type:"POST",url:"page_reservation.php",data:$("form#reservation_form").serialize(), success:function(a){console.log(a);$("#adresse_mail").html($("input#email").val());$("#reservation_form form").fadeOut(function(){$("#reservation_form #msg_ok").hide();$("#reservation_form").animate({marginTop:"+=145px",marginLeft:"+=160px",height:"120px",width:"300px"},700,"easeOutBack",function(){$("#reservation_form #msg_ok").fadeIn()})});return!1}});return!1});
	$("input#reservation_submit_fix").click(function() {
		$("form#reservation_form").trigger('submit');
	});
	
	
	
	$("form#reservation_form").find("input").focus(function(){$(this).css("borderColor","#badc22")}).blur(function(){$(this).css("borderColor","#d6d6d6")});

	$("form#reservation_form").find("input").keypress(function(){$(this).val()!=""&&$(this).css("borderColor","#d6d6d6")});
	
	
		
}); 













