var map;

function initializeMap() {

	var myLatlng = new google.maps.LatLng(51.666993,-4.013845);
 
     var myOptions = {
        zoom: 15,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.HYBRID
     }

     map = new google.maps.Map(document.getElementById("googlemap"), myOptions);
  
     var marker = new google.maps.Marker({
         position: myLatlng, 
         map: map,
         title:"We are here",
         zIndex:7
     });
 
    google.maps.event.addListener(marker,"click", function() {
     var myHtml = "<b>We are here!</b><br />";
     var infowindow = new google.maps.InfoWindow({content:myHtml});
     infowindow.open(map,marker);
    });
 
}

function refreshimg(id) {
	$('#captcha_image_'+id).html('<a href="#" onclick="return refreshimg('+id+');" title="Click to refresh image"><img src="/images/captcha_'+id+'.jpg?' + Math.random() + '" width="206" height="32" alt="Captcha image" /></a>');
	return false;
}

$(document).ready(function(){
						   
			   
		initializeMap(); 
		$("#contact_form").validate({
			invalidHandler: function(e, validator) {
				if (validator.invalid.captcha_1 != undefined) refreshimg(1);
				$('li').removeClass('error');
				var errors = validator.numberOfInvalids();
				if (errors) {
					var message = errors == 1
						? 'You missed 1 field. It has been highlighted below'
						: 'You missed ' + errors + ' fields.  They have been highlighted below';
						$('#error_list').empty().text(message);
					$('#form_fill').addClass('errors');
				} else {
					$('#form_fill').removeClass('errors');
				}
			},

			rules: {
				captcha_1: {
					required: true,
					remote: '/misc/captcha/valid_captcha.php'
				},
				contactname: {
					required: true,
					minWords: 1
				},
				email: {
					required: true,
					email: true
				},
				postcode1: {
					required: true,
					minWords: 1
				},
				postcode2: {
					required: true,
					minWords: 1
				}
			},
			
			messages: {
				captcha_1: "&nbsp;",
				contactname: {
					required: "&nbsp;",
					minWords: "&nbsp;"
				},
				email: {
					required: "&nbsp;",
					minlength: "&nbsp;"
				},
				postcode1: {
					required: "&nbsp;",
					minWords: "&nbsp;"
				},
				postcode2: {
					required: "&nbsp;",
					minWords: "&nbsp;"
				}
			},
			
			errorPlacement: function(error, element) {
					$(element).parents('li').addClass('error');
			},
			
			onkeyup: false,
			
			submitHandler: function(form) {
				jQuery(form).ajaxSubmit({
					success: showResponse,
					type: 'post',
					dataType: 'script',
					timeout: 4000 
				});
			}
		});

	$('#contact_form input,#contact_form textarea').focus(function() { 
		$(this).parents('li').addClass('selected'); 
	}).blur(function(){ 
		$(this).parents('li').removeClass('selected'); 
	});
	
	
	
});

function showResponse(responseText, statusText)  { 
	if (statusText == 'success') {
		$('#response_container').empty().append(responseText);
		Cufon.replace('#response');
	} else {
    alert('status: ' + statusText + '\n\nresponseText: \n' + responseText); 
	}
} 



