/**
 * 	Google Maps v3 for D3mls
 * 	@author D3
 *	@version 2.0
 *	@date 2011-6-16
 *	@requires jQuery and Google Maps API
 */
d3mls_main.setupJQueryUI();

var d3mls_maps = 
{
	initialLocation :null,
	divId : null,
	divId2 : null,
	map : null,
	marker : null,
	map2 : null,
	pano : null,
	zoom: 16,
	details : null,
	detailLatlng : null,

	/**
	 *	Init maps
	 */
	initialize : function (divId,divId2,geolocate,details) 
	{		
		this.details = details;
		this.divId = divId;
		this.divId2 = divId2;
		
		var detailLatlng = new google.maps.LatLng(details.latitude,details.longitude);
		this.detailLatlng = detailLatlng;
		var opts = {
			zoom: this.zoom,
			center: detailLatlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		//set up main map
		this.map = new google.maps.Map(document.getElementById(divId), opts);
		
		var marker = new google.maps.Marker({
		  position: detailLatlng, 
		  map: this.map, 
		  title:details.name,
		  draggable:false
		}); 
		
		marker.setAnimation(google.maps.Animation.BOUNCE);
		this.marker = marker;
		
		//set marker and link to popup dialog
		if (divId2 != null)
		{
			google.maps.event.addListener(marker, 'click', d3mls_maps._popup);
			$("#pushpinLink").bind('click', function(e){
				e.preventDefault();
				d3mls_maps._popup();
			});		
		}
		
		//geolocate
		if (geolocate)
			this.geolocate();
		else 
			$("#geolocateLink").bind('click', function(e){
				e.preventDefault();
				d3mls_maps.geolocate();
			});			
	},
	
	geolocate : function()
	{
		this.initialLocation = new google.maps.LatLng(38.326027, -75.113933);
		d3mls_main.loadProgressWheel(this.divId,"/images/quicksearch-loader.gif");
		this.initialLocation = new google.maps.LatLng(38.326027, -75.113933);
		this._geolocate();
		//hide progresswheel at the end of drawPolyline()
	},
		
	_geolocate : function()
	{
		var useragent = navigator.userAgent;
		var browserSupportFlag = new Boolean();

		if (useragent.indexOf('iPhone') != -1 || useragent.indexOf('Android') != -1 ) {
			this.map.style.width = '100%';
			this.map.style.height = '100%';
		} /*else {
			this.map.style.width = '600px';
			this.map.style.height = '200px';
		}*/

		// W3C Geolocation
		if(navigator.geolocation) {
			browserSupportFlag = true;
			navigator.geolocation.getCurrentPosition(function(position) {
				d3mls_maps.initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
				drawPolyline();
			}, function() {
				handleNoGeolocation(browserSupportFlag);
			});
		// Google Gears geolocation
		} else if (google.gears) {
			browserSupportFlag = true;
			var geo = google.gears.factory.create('beta.geolocation');
			geo.getCurrentPosition(function(position) {
				d3mls_maps.initialLocation = new google.maps.LatLng(position.latitude,position.longitude);
				drawPolyline();
			}, function() {
				handleNoGeoLocation(browserSupportFlag);
			});
		// no browser geolocation
		} else {
			browserSupportFlag = false;
			handleNoGeolocation(browserSupportFlag);
		}

		function handleNoGeolocation(errorFlag)
		{
			if (errorFlag == true) 
				alert("Geolocation service failed.");
			else
				alert("Your browser doesn't support geolocation. We've placed you near our office.");
		}
		//draw polyline
		function drawPolyline()
		{
			if (d3mls_maps.initialLocation != null)
			{
				//place geolocated marker
				var initialMarker = new google.maps.Marker({
				  position: d3mls_maps.initialLocation, 
				  map: d3mls_maps.map, 
				  title:"My Location",
				  draggable:true
				}); 					
				initialMarker.setAnimation(google.maps.Animation.BOUNCE);
				
				//draw polyline between geolocation and property
				var polylineCoordinates = [
					d3mls_maps.initialLocation,
					d3mls_maps.detailLatlng
				];
				var polylinePath = new google.maps.Polyline({
					path: polylineCoordinates,
					strokeColor: "#FF0000",
					strokeOpacity: 1.0,
					strokeWeight: 2
				});
				polylinePath.setMap(d3mls_maps.map);
				
				//reset boundary
				var latLngBounds = new google.maps.LatLngBounds(d3mls_maps.detailLatlng);
				latLngBounds.extend(initialMarker.getPosition());
				d3mls_maps.map.setCenter(latLngBounds.getCenter());
				d3mls_maps.map.fitBounds(latLngBounds);
				d3mls_main.hideProgressWheel(d3mls_maps.divId);		
			}			
		}
	},
	
	/**
	 *	Popup dialog with map, panorama, directions
	 */
	_popup : function()
	{	/*
		 * Load map
		 */
		var detailLatlng = new google.maps.LatLng(d3mls_maps.details.latitude,d3mls_maps.details.longitude);
		var opts = {
			zoom: d3mls_maps.zoom,
			center: detailLatlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			streetViewControl :true
		};
		
		d3mls_maps.map2 = new google.maps.Map(document.getElementById(d3mls_maps.divId2), opts);
		var marker = new google.maps.Marker({
		  position: detailLatlng, 
		  map: d3mls_maps.map2, 
		  title:d3mls_maps.details.name
		}); 
				
		/*
		 * Setup street view
		 */
		var panoramaOptions = {
		  position: detailLatlng,
		  pov: {
			heading: 34,
			pitch: 10,
			zoom: 1
		  },
		  visible:false
		};
		d3mls_maps.pano = new google.maps.StreetViewPanorama(document.getElementById("pano"),panoramaOptions);
		d3mls_maps.map2.setStreetView(d3mls_maps.pano);//set pano to respond to pegman

		var sv = new google.maps.StreetViewService();
	    sv.getPanoramaByLocation(detailLatlng, 50, function(data, status){

			if (status == google.maps.StreetViewStatus.OK) {
				d3mls_maps.pano.setVisible(true);
			}
		});

		/*
		 * Setup directions
		 */
		var directionsService = new google.maps.DirectionsService();
		var directionsDisplay = new google.maps.DirectionsRenderer();	
		directionsDisplay.setMap(d3mls_maps.map2);
		directionsDisplay.setPanel(document.getElementById("directionsPanel"));
		
		$("#getDirectionsButton").bind('click',function(){
			var start = $("#getDirectionsInputAddress").val();
			if (start != null)
			{
				var request = {
					origin:start, 
					destination:detailLatlng,
					travelMode: google.maps.DirectionsTravelMode.DRIVING
				};
				directionsService.route(request, function(response, status) {
					if (status == google.maps.DirectionsStatus.OK) {
						directionsDisplay.setDirections(response);
						$("#directionsPanel").show();
					}
				});
			}
		});
		
		/*
		 * Setup tabs
		 **/
		$("#d3mlsMapsTabs").tabs();
		//check resize when map tab selected
		$('#d3mlsMapsTabs').bind('tabsshow', function(event, ui) {
			switch(ui.panel.id)
			{
				case "d3mlsMapsMap" :
					google.maps.event.trigger(d3mls_maps.map2, 'resize');
					d3mls_maps.map2.setZoom(d3mls_maps.zoom);				
					d3mls_maps.map2.setCenter(detailLatlng, 15);
					break;
				case "d3mlsMapsStreetView" :
					google.maps.event.trigger(d3mls_maps.pano, 'resize');
					break;
			}
		});		
		/*
		 * Setup dialog, respond to resize
		 */
		$("#d3mlsMapsDialog").dialog({
			title:d3mls_maps.details.name,
			minWidth:parseInt($("#d3mlsMapsDialog").css("width")),
			open : function()
			{
				google.maps.event.trigger(d3mls_maps.map2, 'resize');
				d3mls_maps.map2.setZoom(d3mls_maps.zoom);
				google.maps.event.trigger(d3mls_maps.pano, 'resize');
			},
			close : function(){
				//d3mls_maps.map2.clearOverlays();
				//$(d3mls_maps.tabsObj).tabs('select', 0); // on close, switch to first tab so that the dialog map will redraw
			},
			resize : function()
			{
				var dialogWidth = $("#d3mlsMapsDialog").width();
				var dialogHeight = $("#d3mlsMapsDialog").height();
				var tabsWidth = dialogWidth - 20;
				var tabsHeight = dialogHeight - 40;
				var tabPanelWidth = tabsWidth - 20;
				var tabPanelHeight = tabsHeight - 40;
				var tabPanelItemWidth = tabPanelWidth - 20;
				var tabPanelItemHeight = tabPanelHeight - 40;
				
				$("#d3mlsMapsTabs").css({'width': tabsWidth, 'height': tabsHeight});
				$(".d3mlsMapsTab").css({'width': tabPanelWidth, 'height': tabPanelHeight});
				$(".d3mlsMapsTabContent").css({'width': tabPanelItemWidth, 'height': tabPanelItemHeight});
				google.maps.event.trigger(d3mls_maps.map2, 'resize');
				d3mls_maps.map2.setZoom(d3mls_maps.zoom);
				google.maps.event.trigger(d3mls_maps.pano, 'resize');
			}
		});		
		
	}

	
}



