$(document).ready(function(){

	// added to reset value to blank on form fields
   	$("#location").focus(function(){
		if($(this).val() == "Type your postcode here")
			$(this).val("");
	});
	
	$("#location").blur(function(){
		if($(this).val() == "")
			$(this).val("Type your postcode here");
	})


	$("#location2").focus(function(){
		if($(this).val() == "Type your postcode here")
			$(this).val("");
	});

	if($("#location2").val() == "")
		$("#location2").val("Type your postcode here");
	
	$("#location2").blur(function(){
		if($(this).val() == "")
			$(this).val("Type your postcode here");
	});
						   
	$("ul#locatedDestinations").width(300);
	$("ul#locatedDestinations").after('<div id="map" style="background-color: rgb(229, 227, 223);"></div>');
	
	var geocoder = new google.maps.Geocoder();
	
	var postcode = $('#initial').val() + " United Kingdom";
	
	//console.log(postcode);
	
	var latLng = '';
	
	geocoder.geocode({'address':postcode}, function(results, status){
		if (status == google.maps.GeocoderStatus.OK)
		{
			if (results[0])
			{
				var latLng = new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng());
					
					var markers = new Array();
					var myOptions = {
						zoom: 9,
						scrollwheel: true,
						center: latLng,
						mapTypeId: google.maps.MapTypeId.ROADMAP
					};
					
					var map = new google.maps.Map(document.getElementById("map"), myOptions);
					
					$.ajax({
						type: "GET",
						url: "inc/gen_xml.php",
						data: "postcode="+$('#location').val(),
						dataType: "xml",
						success: function(xml){
							//parse xml
							//name
							var name = new Array();
//							address
//							var address1 = new Array();
//							var address2 = new Array();
//							var city = new Array();
//							var county = new Array();
//							postcode
//							var postcode = new Array();
//							var postcodeareas = new Array();
//							contact
//							var telephone = new Array();
//							var mobile = new Array();
//							var email = new Array();
//							geo
//							var latitude = new Array();
//							var longitude = new Array();
//							premium
//							var priority = new Array();
//							
//							var minLat = null;
//							var maxLat = null;
//							
//							var minLng = null;
//							var maxLng = null;
							
							//xml into arrays
							$('marker',xml).each(function(i){
								name[i] = $(this).attr("name");
//								address1[i] = $(this).attr("address1");
//								address2[i] = $(this).attr("address2");
//								city[i] = $(this).attr("city");
//								county[i] = $(this).attr("county");
//								postcode[i] = $(this).attr("postcode");
//								postcodeareas[i] = $(this).attr("postcode");
//								telephone[i] = $(this).attr("telephone");
//								mobile[i] = $(this).attr("mobile");
//								latitude[i] = $(this).attr("lat");
//								if(minLat == null || $(this).attr("lat") < minLat)
//								{
//									minLat = $(this).attr("lat");
//								}
//								if(maxLat == null || $(this).attr("lat") > maxLat)
//								{
//									maxLat = $(this).attr("lat");
//								}
//								longitude[i] = $(this).attr("lng");
//								if(minLng == null || $(this).attr("lng") < minLng)
//								{
//									minLng = $(this).attr("lng");
//								}
//								if(maxLng == null || $(this).attr("lng") > maxLng)
//								{
//									maxLng = $(this).attr("lng");
//								}

								var image = new google.maps.MarkerImage('images/markers/image.png',
					  		    	// This marker is 20 pixels wide by 32 pixels tall.
					  		      	new google.maps.Size(22, 52),
					  		     	// The origin for this image is 0,0.
					  		      	new google.maps.Point(0,0),
					  		      	// The anchor for this image is the base of the flagpole at 0,32.
					  		      	new google.maps.Point(11, 52));
						  		
						  		var shadow = new google.maps.MarkerImage('images/markers/shadow.png',
						  		    // The shadow image is larger in the horizontal dimension
						  		    // while the position and offset are the same as for the main image.
						  		    new google.maps.Size(48, 52	),
						  		    new google.maps.Point(0,0),
						  		    new google.maps.Point(11, 52));
						  		  var shape = {
						  		    coord: [11,0,12,1,13,2,14,3,15,4,16,5,16,6,17,7,17,8,18,9,18,10,19,11,19,12,20,13,20,14,20,15,21,16,21,17,21,18,21,19,21,20,21,21,21,22,21,23,21,24,21,25,21,26,21,27,21,28,21,29,21,30,21,31,21,32,21,33,21,34,21,35,21,36,20,37,20,38,20,39,19,40,19,41,18,42,18,43,17,44,17,45,16,46,15,47,14,48,14,49,13,50,12,51,10,51,9,50,8,49,7,48,6,47,5,46,5,45,4,44,3,43,3,42,2,41,2,40,2,39,1,38,1,37,1,36,0,35,0,34,0,33,0,32,0,31,0,30,0,29,0,28,0,27,0,26,0,25,0,24,0,23,0,22,0,21,0,20,0,19,0,18,0,17,0,16,0,15,1,14,1,13,1,12,2,11,2,10,3,9,3,8,4,7,4,6,5,5,6,4,7,3,7,2,8,1,9,0],
						  		      type: 'poly'
						  		  };
								
								
								var marker = new google.maps.Marker({
									title:$(this).attr("name"),
									map:map,position:new google.maps.LatLng($(this).attr("lat"),$(this).attr("lng")),
									icon: image,
									shadow: shadow,
									draggable:false});
							});			
						}	
				
					});		
			}
		}
	});
});
