$(document).ready(function(){

	$('a.showinfobox').click( function(){
	
		$('div#infobox').slideUp()
	
		$('div#infobox').html( $('div#contactinfo'+$(this).attr('id')).html() );
		
		$('div#infobox').slideDown()

		$('.closeinfobox').click( function(){
	
			$('div#infobox').slideUp()
	
		});
		
	});

	if ($('div.contactinfo').length>0){
		
		if ( $('div.contactinfo').length == 1 ){

			showonemarker()		
			
		} else {
			
			showmultiplemarker();
		
		}
				
	}

});

function showonemarker() {

	var clubname = $('div.contactinfo').find("div.clubname").text();
	var clubid = $('div.contactinfo').attr('id');
	var latlng = $('div.contactinfo').find("div.google").text();
	var lat = jQuery.trim(latlng.split(',')[0]);
	var lng = jQuery.trim(latlng.split(',')[1]);

	var myPosition = new google.maps.LatLng(lat,lng);

	var myOptions = {
	  zoom: 18,
	  center: myPosition,
	  navigationControl: true,
	  mapTypeControl: true,
	  streetViewControl: false,
	  mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	  
	var map = new google.maps.Map(document.getElementById("map"), myOptions);
   
	var marker = new google.maps.Marker({ 
	      map: map,
	      position: myPosition,
	      title: clubname
	});

	google.maps.event.addListener(marker, 'click', function() {
		showinfobox( clubid )
	});	

}

function showmultiplemarker() { 

	var myOptions = {
	  navigationControl: true,
	  mapTypeControl: true,
	  streetViewControl: false,
	  mapTypeId: google.maps.MapTypeId.ROADMAP	
	}
	
	var map = new google.maps.Map(document.getElementById("map"), myOptions);

	var markerBounds = new google.maps.LatLngBounds();

	$('div.contactinfo').each(function(index) {
	
		var clubname = $(this).find("div.clubname").text();
		var clubid = $(this).attr('id');
		var latlng = $(this).find("div.google").text();
		
		if(latlng!=''){
		
			var lat = jQuery.trim(latlng.split(',')[0]);
			var lng = jQuery.trim(latlng.split(',')[1]);
			
			var myPosition = new google.maps.LatLng(lat,lng);

			var pattern = /(\-?\d{1,3}\.\d+)([, ]+)([\+\-]*\d{1,3}\.\d+)\b/;
			var isValidLatLng = pattern.test(myPosition);
			
			if(isValidLatLng) {
		
				var marker = new google.maps.Marker({ 
				      map: map,
				      position: myPosition,
				      title: clubname
				});

				google.maps.event.addListener(marker, 'click', function() {
					
					showinfobox( clubid )
					
				});

				markerBounds.extend(marker.position);
				map.fitBounds(markerBounds);
			
			}
		
		}
	    
	});

}

function showinfobox(clubid){

	$('div#infobox').slideUp()
	
	$('div#infobox').html( $('div#'+clubid).html() );
		
	$('div#infobox').slideDown()

	$('.closeinfobox').click( function(){
	
		$('div#infobox').slideUp()
	
	});
	
}


function showmultiplemarkerbygeocoder() { 

	var markerBounds = new google.maps.LatLngBounds();

	var geocoder = new google.maps.Geocoder();

	$('div.contactinfo').each(function(index) {
	
		var clubname = $(this).find("div.clubname").text()
		var clubaddress = $(this).find("div.address").text() + ', ' + $(this).find("div.postcity").text()
		var clubid = $(this).attr('id')
		
		geocoder.geocode( { 'address': clubaddress}, function(results, status) {
		  if (status == google.maps.GeocoderStatus.OK) {

			var marker = new google.maps.Marker({ 
			      map: map,
			      position: results[0].geometry.location,
			      title: clubname
			});

			google.maps.event.addListener(marker, 'click', function() {
			
				showinfobox( clubid )
			
		    });

			markerBounds.extend(marker.position);
			map.fitBounds(markerBounds);
			
			if(index==0){ map.setZoom(16) }; //hvis der kun er en på kortet
	    
		  } else {
//		    alert("Geocode was not successful for the following reason: " + status);
		  }
		});
		
	});

}
