$(document).ready(function() {
	
	//check if #header exists (added by bart)
	if($('#header').length > 0){
		$('#header').before('<div id="nav">')
		.cycle({
			fx:     'fade', 
			pager: '#nav',
			before: loadText
		});
	}
	
	
	$(".searchzipcode").click(function(){
		if($("#DealerPostalcode").val()!=''){
			clearAndSetCenter();
			
			//fetch vars from inputs
			var address = $("#DealerPostalcode").val();
			radius = $('#DealerRadius').val();
			//create geocoder object
			geocoder = new google.maps.Geocoder();
			//geocode address
			geocoder.geocode( { 'address': address}, function(results, status){
				if(status == google.maps.GeocoderStatus.OK){
					//setting vars for ajax call
					latlon = results[0].geometry.location;
					dataString = 'data[Dealer][lat]='+results[0].geometry.location.lat()+'&data[Dealer][lng]='+results[0].geometry.location.lng()+'&data[Dealer][radius]='+radius;
					action = webpath+'dealers/findinradius';
					$.ajax({
						type: "POST",
						url: action,
						data: dataString,
						success: function(msg){
							$("#markerselector").html(msg);
						}
					});
				}
				else{
					$("#markerselector").html('Postcode onjuist.');	
				}
			});	
			
		}
	});
	
	$(".searchcity").click(function(){
		if($("#DealerCity").val()!=''){
			clearAndSetCenter();
			
			var address = $("#DealerCity").val();
				
			dataString = 'data[Dealer][city]='+address;
			action = webpath+'dealers/findinradius';
			
			$.ajax({
				type: "POST",
				url: action,
				data: dataString,
				success: function(msg) {
					$("#markerselector").html(msg);		      
				}
			});
		}
	});
	
});


function clearAndSetCenter(){
	centlatlng = new google.maps.LatLng(52.132633, 5.291266);
	map.setCenter(centlatlng);
	map.setZoom(7);
	googleClearMarkers();
}

function googleInit(){
	//declare infowindow object	for googlemaps
	infowindow = new google.maps.InfoWindow();
}

function googleGeoCode(address){
	geocoder = new google.maps.Geocoder();
	geocoder.geocode( { 'address': address}, function(results, status){
		if(status == google.maps.GeocoderStatus.OK){
			return results[0].geometry.location;
		}
		else{
			return 'error';
		}
	});
}

var markersArray = [];
function googlePlaceMarker(name,lat,lon,popuptext){
	
	
	var marker = new google.maps.Marker({
        map: map, 
        position: new google.maps.LatLng(lat,lon),
        title: name,
       	icon: webpath+'img/pages/mapmarker.png'
    });
    
    /*
    var infowindow = new google.maps.InfoWindow({
        content: popuptext,
        maxWidth: 300
    });
    */

    google.maps.event.addListener(marker, 'click', function() {
    	infowindow.setContent(popuptext); 
		infowindow.open(map,marker);
	});
	
	markersArray.push(marker);
}


function googleClearMarkers() {
	if(markersArray){
		for(i in markersArray){
			markersArray[i].setMap(null);
			
			//markersArray[i].infowindow.close();
		}
		markersArray.length = 0;
		infowindow.close();
	}
}


function googleCreateBranchMap(){
	googleLatLon = new google.maps.LatLng(52.132633, 5.291266);
	
	myOptions = {
		zoom: 7,
		center: googleLatLon,
		mapTypeId: google.maps.MapTypeId.TERRAIN,
		mapTypeControlOptions: {
		   mapTypeIds: []
		}

	};
	
	map = new google.maps.Map(document.getElementById("branche_map"),myOptions);
	
	google.maps.event.addListener(map, 'click', function() {
        infowindow.close();
    });
}

function googleZoomInToMarker(i){
	map.setZoom(14);
	google.maps.event.trigger(markersArray[i], 'click');
}



function loadText() {
	$('#infoblock #info').hide().html(this.title).fadeIn('slow');
}




