var $j = jQuery.noConflict();

$j(document).ready(function(){
	/**
	 * CONTACT FORM
	 */
	if ($j('form[name=contact]').length == 1) {
		$j('input[name=lastname]').simpleAntiSpam('contact');
		$j('input[name=email]').simpleAntiSpam('contact');
		$j('input[name=subject]').simpleAntiSpam('contact');
		$j('textarea[name=message]').simpleAntiSpam('contact');
	}
	
	/**
	 * NEWSLETTER FORM
	 */
	if ($j('form[name=newsletterForm]').length == 1) {
		$j('input[name=newsletter_email]').deleteDefaultValueOnFocus();
		$j('input[name=newsletter_email]').simpleAntiSpam('newsletter');
	}
	
	/**
	 * CGV
	 */
	if ($j('form[name=step4]').length == 1) {
		$j('input[name=reservation_cgv]').click(function(){
			if ($j(':checked[name=reservation_cgv]').length) {
				$j('input[name=validButton]').attr('disabled', '');
			}
			else {
				$j('input[name=validButton]').attr('disabled', 'disabled');
			}
		});
	}
	
	/**
	 * LEFT ADVERTISEMENT
	 */
	if ($j('div.box_pub_gauche_inter').length == 1) {
		$j('div.box_pub_gauche_inter').cycle({
			fx: 'scrollLeft',
			speed:    750,
			timeout:  5000
		});
	}
	
	/**
	 * BOTTOM ADVERTISEMENT
	 */
	if ($j('div.box_pub_droite_home').length == 1) {
		$j('div.box_pub_droite_home').cycle({
			fx: 'scrollDown',
			speed:    750,
			timeout:  5000
		});
	}
	
	/**
	 * CALENDRIER RESERVATION
	 */ 
	if ($j('#reservation_date_start').length > 0 &&  $j('#reservation_date_return').length > 0) {
		
		var dates = $j( "#reservation_date_start, #reservation_date_return" ).datepicker({
			dateFormat: 'dd/mm/yy',
			minDate: 0, 
			maxDate: "+"+maxIntervalReservationInMonth+"M",
			onSelect: function( selectedDate ) {
				
				/* Bloquage des dates antérieures à la date de départ pour pour la date de retours*/
				var option = this.id == "reservation_date_start" ? "minDate" : "maxDate", instance = $j( this ).data( "datepicker" ), date = $j.datepicker.parseDate( instance.settings.dateFormat || $j.datepicker._defaults.dateFormat, selectedDate, instance.settings );
				dates.not( this ).datepicker( "option", option, date );
				
				/* Chargement des liste horaires en fonction des dates et de l'heure courrante */
				var type = 'reservation';
				if(this.id == 'reservation_date_return'){
					type = 'return';
				}
				getListHoursReservation(selectedDate, type);
			}
		}, $j.datepicker.regional["fr"]);
		
		/**
		 * Valeurs initiale des liste horaires
		 */
		if($j('#reservation_date_start').val()){
			getListHoursReservation($j('#reservation_date_start').val(), 'reservation');
		}
		if($j('#reservation_date_return').val()){
			getListHoursReservation($j('#reservation_date_return').val(), 'return');		
		}
	}
	
});

window.addEvent('domready', function() {
	/**
	 * MENU MATIC
	 */
	var myMenu = new MenuMatic({'matchWidthMode' : true, 'stretchMainMenu' : true, 'opacity':100});
});


function getListHoursReservation(date, type){
	
	var listId 	= 'reservation_time_start';
	if(type == 'return'){	
		listId	= 'reservation_time_return'
	}
	
	$j.ajax({
	  url: '/ajax/hours-reservation',
	  dataType: 'json',
	  contentType: "application/x-www-form-urlencoded;charset=ISO-8859-15",
	  data: {'date':date, 'type':type},
	  success: function(data){
		
		/* Chargement des valeurs de la liste */
		$j('#'+listId).attr('options').length = 0;
		if(data.list_hours.length>0){
			for(i=0;i<data.list_hours.length;i++){
				$j('#'+listId).attr('options')[i] = new Option(data.list_hours[i], data.list_hours[i]);
			}
		}else{
			displayAlertDateReservation();
		}
		
	  }
	});
}

function displayAlertDateReservation(){
	
	$j('#dialog_date_error').dialog({
		resizable:false,
		draggable:false,
		title:$j('#dialog_date_error .title').html(),
		modal:true,
		buttons:{
			"Ok": function() { $j(this).dialog("close"); }
		}
	});	
}
