$(document).ready(function() {

	// site feature
	$('#feature').siteFeature({
		autoPlay: true,
		autoPlayInterval: 3500
	});
	
	// smooth scroll
	$(".smooth-scroll").anchorAnimate({
		speed: 800
	});
	
	// enlarge search field on focus
	$("#search-field").focus(function() {
		$(this).animate({
			width: "250px",
		 }, 500 );
	}).blur(function() {
		$(this).animate({
			width: "150px"
		}, 500);
	});


	
	// display code group field on checked
	/*
	var code_group = $('.code-group-checked');
	if ( code_group.length == 1 ) {
		code_group.click( function() {
			if ( code_group.is(':checked') ) {
				$('#code-group-box').slideDown();
			}
		});
		code_group.blur( function() {
			if ( code_group.is(':checked') ) {
				return false;
			} else {
				$('#code-group-box').slideUp();
			}
		});
	}*/

	// display code group field on checked
	var code_group = $('.code-group-checked');
	if ( code_group.length == 1 ) {
		code_group.click( function() {
			if ( code_group.is(':checked') ) {
				$('#code-group-box').slideDown();
			}
		});
		code_group.change( function() {
			if ( code_group.attr('checked') != true ) {
				$('#code-group-box').slideUp();
			}
		});
	}
	
	// clear search field on focus
	$("#search-field").focus(function() {
	    if( this.value == this.defaultValue ) {
	    this.value = "";
	    }
	}).blur(function() {
	    if( !this.value.length ) {
	    this.value = this.defaultValue;
	    }
	});

	
	// check form fields
	var form = $('#checkout2');
	if ( form.length == 1 ) {
		form.each(function() {
			$('.required').blur(function() {
				var value = $(this).attr('value');
				if(!value) {
					$(this).addClass('empty').nextAll('small').fadeIn();
					$(this).next('span').removeClass('field-on').addClass('field-off');
				} else {
					$(this).removeClass('empty').nextAll('small').slideUp();
					$(this).next('span').removeClass('field-off').addClass('field-on');
				}
			});
		});
		form.submit(function() {
			var ok = true;
			$('.required').each(function() {
				var value = $(this).attr('value');
				if (!value) {
					$(this).addClass('empty').nextAll('small').fadeIn();
					$(this).next('span').removeClass('field-on').addClass('field-off');
					ok = false; 
				}
			});
			return ok;
		});
	}
	
	// change price
	var convention = $('#convention');
	if ( convention.length == 1 ) {
		$('.price-convention').hide();
		convention.click(
			function() {
				var checked = convention.is(':checked');
				if ( checked == true ) {
					$('.price-full').hide();
					$('.price-convention').show();
				} else {
					$('.price-convention').hide();
					$('.price-full').show();
				}
			});
	}
	
	// google map
	var memberpage = $('#member-page');
	if ( memberpage.length == 1 ) {
		var $name = $('h1').text();
		var $place = $('address').html();
		initialize( $name, $place);
	}
});
function initialize( name, place ) {
	geocoder = new google.maps.Geocoder();
	if (geocoder) {
		var address = document.getElementById("adresse").value;
		geocoder.geocode( { 'address': address}, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				
				var latlng = results[0].geometry.location;
				
				var mapOptions = {
					zoom: 7,
					center: latlng,
					mapTypeControl: false,
				    mapTypeControlOptions: {
				    	style: google.maps.MapTypeControlStyle.DROPDOWN_MENU,
				    	position: google.maps.ControlPosition.BOTTOM_RIGHT	
				    },
				    
					mapTypeId: google.maps.MapTypeId.ROADMAP
				}
				map = new google.maps.Map(document.getElementById("member-map"), mapOptions);

				var windowContent = '<div id="map-info-window"><span class="info-window-name">'+name+'</span></div>';
				
				var infowindow = new google.maps.InfoWindow({
					content: windowContent,
					maxWidth: 350
				});
				var marker = new google.maps.Marker({
					position: latlng, 
					map: map
				});
				infowindow.open(map,marker);

				google.maps.event.addListener(marker, 'click', function() {
					infowindow.open(map,marker);
				});
			} else {
				alert("Impossible de trouver l'adresse pour les raisons suivantes : " + status);
			}
		});
	}
}