$(document).ready(function() {

    $('#home-slider').bxSlider({
        auto: true,
        controls: false,
        mode: 'fade'
    });
    
    // map vars
	var latlng, 
		options, 
		map, 
		marker, 
		directionDisplay, 
		directionsService,
		infowindow,
		location_marker,
		bubble_content,
		marker_position,
		zoom_level;

	// variables
	latlng = new google.maps.LatLng(51.76573, -0.979934); // center map
	zoom_level = 17; // zoom on map
	
	marker_position = new google.maps.LatLng(51.76573, -0.979934); // marker point
	latlngstr = "51.76573, -0.979934"; // for directions
	marker_title = "Head Office"; // title for hover
	
	bubble_content = 'stuff goes here'; // info window content



	// setup map magic	
	options = {
		zoom:zoom_level,
		center:latlng,
		mapTypeId:google.maps.MapTypeId.ROADMAP
	};
	map = new google.maps.Map(document.getElementById("google-map"), options);
	directionsService = new google.maps.DirectionsService();
	directionDisplay = new google.maps.DirectionsRenderer();
	directionDisplay.setMap(map);

	// markers
	marker = new google.maps.Marker({
		position: marker_position,
		map: map,
		title: marker_title
	});
	
	// bubble
	infowindow = new google.maps.InfoWindow({
		content: bubble_content,
		size: new google.maps.Size(50,50),
		maxWidth: 315
	});
	
	google.maps.event.addListener(marker, 'click', function() {
		infowindow.open(map,this);
		return false;
	});

	// normal directions from input
	$('.directions input.submit').live('click', function(e) {
		e.preventDefault();
		directionDisplay.setMap(map);
		directionDisplay.setPanel($('.directions .results').get(0));
		var start = $("#start").attr('value');
		var end = latlngstr;
		var request = {
			origin:start,
			destination:end,
			travelMode: google.maps.DirectionsTravelMode.DRIVING,
			unitSystem: google.maps.DirectionsUnitSystem.IMPERIAL
		};
		directionsService.route(request, function(response, status) {
			if (status == google.maps.DirectionsStatus.OK) {
				directionDisplay.setDirections(response);
			}
		});
	});

	// get geo location for directions
	$('.directions .geolocation').live('click', function(e) {
		e.preventDefault();
		$('.directions input#start').attr('value', '');
		setTimeout(function() { $(this).text('From my current location');
		}, 2000);
		directionDisplay.setMap(map);
		directionDisplay.setPanel($('.directions .results').get(0));
		if(navigator.geolocation) {
			navigator.geolocation.getCurrentPosition(function(position) {
				var start = position.coords.latitude + ', ' + position.coords.longitude;
				var end = latlngstr;
				var request = {
					origin:start,
					destination:end,
					travelMode: google.maps.DirectionsTravelMode.DRIVING,
					unitSystem: google.maps.DirectionsUnitSystem.IMPERIAL
				};
				directionsService.route(request, function(response, status) {
					if (status == google.maps.DirectionsStatus.OK) {
						directionDisplay.setDirections(response);
					}
				});
			});
		} else {
			alert('Geolocations service is not available for this browser.');
		}
	});
	
});
