/*
 * Url preview script 
 * powered by jQuery (http://www.jquery.com)
 * 
 * written by Alen Grakalic (http://cssglobe.com)
 * 
 * for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery
 *
 */
 
this.screenshotPreview = function(){	
	/* CONFIG */
		
		xOffset = 10;
		yOffset = 30;
		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
		
	/* END CONFIG */
	$("a.sales-person").hover(function(e){
		$("body").append("<p id='profile'><img src='"+ $(this).attr("data-photo") +"' alt='"+ $(this).html() +"' /></p>");								 
		$("#profile")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");						
    },
	function(){
		$("#profile").remove();
    });	
	$("a.sales-person").mousemove(function(e){
		$("#profile")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});
	$("a.sales-person").click(function(e){
		e.preventDefault();
	});
};



jQuery(function() {
	screenshotPreview();
	
	/**
	 * If we're on the directions page, show Google Map
	 */
	if($("body").hasClass("page-template-maps-page-php")){
		// 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.541204, -0.358568); // center map
		zoom_level = 15; // zoom on map
		
		marker_position = new google.maps.LatLng(51.541204, -0.358568); // marker point
		latlngstr = "51.542555, -0.356789"; // for directions
		marker_title = "Head Office"; // title for hover
		
		bubble_content = 'The Fresh Olive Company'; // 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.');
			}
		});
	}
});
