 var map,coords;
 var getDirections = {};
 var placeMarker = {};

 $(document).ready(function(){

	/* Dev: Location Map */
	placeMarker = function(lat,lng){
		var point = new GLatLng(lat,lng);
		map.addOverlay(createMarker(point));
	};
	
	createMarker = function(point) {
		var baseIcon = new GIcon();
		baseIcon.shadow = "/_img/google-shadow.png";
		baseIcon.iconSize = new GSize(25,45);
		baseIcon.shadowSize = new GSize(12,45);
		baseIcon.iconAnchor = new GPoint(12,45);

		var icon = new GIcon(baseIcon);
		icon.image = "/_img/google-marker.png";
		var marker = new GMarker(point,icon);
		return marker;
	};

	if ($('#dev_map')) {
		coords = new GLatLng($('#map_lat').val(),$('#map_lng').val());
		map = new GMap2(document.getElementById('map'));
		map.addControl(new GSmallMapControl());
		map.enableScrollWheelZoom();
		map.enableDoubleClickZoom();
		map.addControl(new GMapTypeControl());
		map.setCenter(coords,parseInt($('#map_zoom').val()));
		placeMarker($('#map_lat').val(),$('#map_lng').val());
	}	

	/* Dev: Request Details */

	var requestValidate = function() {
		var err = 1;
		var name = $("#request_name").attr('value');
		var address = $("#request_address").attr('value');
		var postcode = $("#request_postcode").attr('value');
		var research = $("#request_research").attr('value');
		if (name != '' && address != '' && postcode != '' && research != '') {
			err = 0;
		}
		if (err == 0) {
			$("#request-form").hide();
			$("#request-form-complete").hide();
			$("#request-form-loading").show();
			return true;
		} else {
			alert("Please enter your name, address, postcode and which newspapers you read.");
			return false;
		}
	};

	var requestComplete = function() {
	 	$.get('/thanks-request-details.php');
		$("#request-form-loading").hide();
		$("#request-form-complete").show();
		setTimeout(requestRevert,9000);
	};
	
	var requestRevert = function() {
		$("#request-form-loading").hide();
		$("#request-form-complete").fadeOut('slow');
		$("#request-form").fadeIn('slow');
	};
	
	$('#form-request').ajaxForm({
		beforeSubmit:requestValidate,
	    success:requestComplete,
	    clearForm:true
	});

	/* Dev: Sub-nav */
	$('.dev-subnav a').click(function() {
		var t = $(this).attr('id').split("-");
		$('.dev-subnav a').removeClass('selected');
		$('.dev-subnav a#subnav-'+t[1]).addClass('selected');
		$('.tab').hide();
		$.scrollTo('#churchill',900);
		$('#tab-'+t[1]).fadeIn(500);
		map.checkResize();
		map.setCenter(coords,parseInt($('#map_zoom').val()));
		return false;
	});

	/* Dev: Images */
	$('.static-thumbs a').click(function() {
		var t = $(this).attr('id').split("-");
		$('#tab-'+t[0]+' .static-pic').hide();
		$('#tab-'+t[0]+' .static-caption').hide();
		$('#tab-'+t[0]+' #large-'+t[2]+'-'+t[3]).show();
		$('#tab-'+t[0]+' #caption-'+t[2]+'-'+t[3]).show();
	});

	/* Dev: Directions */
	$('#directions-form').submit(function() {
		if ($('#dev_map') && ($('#location').val() != '')) {
			var b = $(this).find('button').attr('id').split("-");
			window.open('/directions.php?id='+b[2]+'&address='+$('#location').val(),'directions'+b[2],'width=500,height=700,scrollbars=1');
		}
	});

 });