
$(document).ready(function(){
	
	initialize();
	
	$('#mapit_dialog').dialog({
		autoOpen: false,
		width: 600,
		height: 350
	});
	
	// Dialog Link
	$('.mapit').click(function(){
		var address = $(this).next();
		$('#mapit_dialog').dialog('option', 'title', address.val());
		$('#mapit_dialog').dialog('open');
		codeAddress(address.val());
		return false;
	});

	
	$('#contact_dialog').dialog({
		autoOpen: false,
		width: 500,
		height: 280
	});
	
	$('.contact a').click(function(){
		$('#contact_dialog').dialog('open');
	});
	
	if($('form.contactagent',"#contact_dialog").is("*")){
		$('form.contactagent',"#contact_dialog").ajaxForm({
			beforeSubmit: checkemail,
			success: function(msg){
				$('form.contactagent',"#contact_dialog").replaceWith(msg);
			},
			error: function(){
				$('form.contactagent',"#contact_dialog").replaceWith('<p class="error center">An Error Occurred, please try again.</p>');
			}
		});
	}
	
	if($('form.contactagent','#contactagent-vis').is("*")){
		$('form.contactagent','#contactagent-vis').ajaxForm({
			beforeSubmit: checkemail,
			success: function(msg){
				$('form.contactagent','#contactagent-vis').replaceWith(msg);
			},
			error: function(){
				$('form.contactagent','#contactagent-vis').replaceWith('<p class="error center">An Error Occurred, please try again.</p>');
			}
		});
	}

});

var geocoder;
var map;
function initialize() {
	geocoder = new google.maps.Geocoder();
	var latlng = new google.maps.LatLng(-34.397, 150.644);
	var myOptions = {
    	zoom: 11,
    	center: latlng,
    	mapTypeId: google.maps.MapTypeId.ROADMAP
  	}
  	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}

function codeAddress(address) {
	geocoder.geocode( { address: address}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK && results.length) {
			// You should always check that a result was returned, as it is
			// possible to return an empty results object.
			if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
				//center(results[0].geometry.location.lat(), results[0].geometry.location.lng())
				//map.setCenter(results[0].geometry.location);
				//console.log(results[0].geometry.location.lat());
				//console.log(results[0].geometry.location.lng());
			     var myOptions = {
			    	zoom: 11,
			    	center: results[0].geometry.location,
			    	mapTypeId: google.maps.MapTypeId.ROADMAP
			  	}
				map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
				var marker = new google.maps.Marker({
					position: results[0].geometry.location,
					map: map
				});
			}
		} else {
			console.log("Geocode was unsuccessful due to: " + status);
		}
	});
}

function center(lat, lon) {
    map.setCenter(new google.maps.LatLng(lat, lon));
}

function checkemail(formData, item) {
	$(".error").remove();
	var email = $('.emailaddress', item);
	var filter = /^[a-z0-9._-]+@([a-z0-9_-]+.)+[a-z]{2,6}$/i;
	if (email.val() == '' || !filter.test(email.val())) {
		$(item).prepend('<p class="error center">Please provide a valid email address</p>');
		email.focus();
		return false;
	}
	return true;
}