
var GoogleMap = {
	
	currentLocationElement:null,
	currentLocation:null,
	currentBubble:null,
	map:null,
	bounds:null,
	directionsDisplay: new google.maps.DirectionsRenderer(),
	directionsService:new google.maps.DirectionsService(),
	initialize:function(endpoint, load_ajax)
	{
		var latlng = new google.maps.LatLng(google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude);
		var map_options = {
		  zoom: 8,
		  center: latlng,
		  mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		map = new google.maps.Map(document.getElementById("map_canvas"), map_options);
		bounds = new google.maps.LatLngBounds();
		
		GoogleMap.directionsDisplay.setMap(map);
  		GoogleMap.directionsDisplay.setPanel(document.getElementById("directions_list"));
		
		/*
		$('search_form').addEvent('submit',function(e){
			e.stop();
			window.location = '/address/'+$('address').value.replace(/ /g,'+');
		});
		*/
		
		if(load_ajax)
		{
			var currentLocation = google.loader.ClientLocation.address.city+', '+google.loader.ClientLocation.address.region;
			var req = new Request.HTML({url:endpoint, 
				evalScripts:true,
				update:$('map-locations').getParent(),
				onSuccess: function(a,b,c,d) {
					
					$('address').value = currentLocation;
				},
	
				onFailure: function() {
					//console.log('request failed');
				}
			}).get({'address': currentLocation, 'action':'get_location_html' });
		}
		
		var address_default_value = $('address').value;
		$('address').addEvents({
		
			
			'focus':function(){
				this.value=""
			},
			'blur':function(){
				if(this.value == "")
				{
					this.value = address_default_value;
				}
			}
		
		});
	
	},
	/**/
	showDirections:function(){
		var start = $('address').value;
		var request = {
			origin:start, 
			destination:GoogleMap.currentBubble.marker_.getPosition(),
			travelMode: google.maps.DirectionsTravelMode.DRIVING
		};
		directionsService.route(request, function(response, status) {
			if (status == google.maps.DirectionsStatus.OK) {
				GoogleMap.directionsDisplay.setDirections(response);
				GoogleMap.currentBubble.hide();
			}
		});
	},
	
	addMarker:function(lat,lng,title,img,info,index)
	{
	
		 var image = new google.maps.MarkerImage(img,
		  // This marker is 20 pixels wide by 32 pixels tall.
		  new google.maps.Size(27, 30),
		  // The origin for this image is 0,0.
		  new google.maps.Point(0,index*30),
		  // The anchor for this image is the base of the flagpole at 0,32.
		  new google.maps.Point(13, 26));
		  
		var shadow = new google.maps.MarkerImage(NS_LOCATION_FINDER_PLUGIN_URL+'/images/map_marker_shadow.png',
		  // The shadow image is larger in the horizontal dimension
		  // while the position and offset are the same as for the main image.
		  new google.maps.Size(35, 29),
		  new google.maps.Point(0,0),
		  new google.maps.Point(13, 26));
	
	
		var marker = new google.maps.Marker({
			map: map, 
			position: new google.maps.LatLng(lat, lng),
			title:title,
			icon:image,
			shadow:shadow
		});
		bounds.extend(marker.position);
		
		var list_item = $$('#map-locations li')[index];
		list_item.addEvent('click',function(e){
				google.maps.event.trigger(marker, 'click');
		});
		
		var bubble = new InfoBubble(marker, map, title, info, list_item);

		google.maps.event.addListener(marker, 'click', function() {
			// infowindow.open(map,marker);
			//bubble = new InfoBubble(marker, map, title, info);
			if(GoogleMap.currentBubble)GoogleMap.currentBubble.hide();
			map.setCenter(marker.getPosition());
			bubble.show();
			GoogleMap.currentBubble = bubble;
			//GoogleMap.currentLocation = marker.getPosition();
		});
		
		 map.fitBounds(bounds);
	/**/	
	}
	
	
};


function InfoBubble(marker, map, title, info, list_item) {

  this.marker_ = marker;
  this.map_ = map;
  this.title_ = title;
  this.info_ = info;
  this.list_item_ = list_item;
  this.div_ = null;
  this.setMap(map);
}

InfoBubble.prototype = new google.maps.OverlayView();
InfoBubble.prototype.onAdd = function() {


  var div = document.createElement('div');
  div.id = 'gmap_bubble';
  
  var title_div = document.createElement('div');
  title_div.className = 'title';
  title_div.innerHTML = this.title_;
  
  var close_button = document.createElement("img");
  close_button.src = NS_LOCATION_FINDER_PLUGIN_URL+'/images/close_button.png';
  close_button.className = 'close_button'; 
  close_button.onclick = function(){
    if(GoogleMap.currentBubble)GoogleMap.currentBubble.hide();
  }


  title_div.appendChild(close_button);
  div.appendChild(title_div);
  
  var info_div = document.createElement('div');
  info_div.className = 'info';
  info_div.innerHTML = this.info_;
  div.appendChild(info_div);


  this.div_ = div;


  var panes = this.getPanes();
  panes.floatPane.appendChild(div);
}
InfoBubble.prototype.draw = function() {

  var bubbleProjection = this.getProjection();

  var div = this.div_;
  var marker = this.marker_;

  div.style.width = '200px'; 
  div.style.height = '120px';
  offsetX = 99;
  offsetY = 135;
  div.style.top = (bubbleProjection.fromLatLngToDivPixel(marker.getPosition()).y - offsetY) + 'px';
  div.style.left = (bubbleProjection.fromLatLngToDivPixel(marker.getPosition()).x - offsetX) + 'px';
}

InfoBubble.prototype.hide = function() {
   if (this.div_) {
    this.div_.style.visibility = "hidden";
	this.list_item_.removeClass('selected');
  }
}
InfoBubble.prototype.show = function() {
   if (this.div_) {
    this.div_.style.visibility = "visible";
	this.list_item_.addClass('selected');
  }
}

InfoBubble.prototype.remove = function() {
   if (currentMarker.div_) {
   	if (currentMarker.div_.parentNode && currentMarker.div_.parentNode.removeChild) {
		currentMarker.div_.parentNode.removeChild(currentMarker.div_);
	}
  }
}

var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var map;

function initialize() {
  directionsDisplay = new google.maps.DirectionsRenderer();
 var latlng = new google.maps.LatLng(google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude);
  var myOptions = {
    zoom:7,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    center: latlng
  }
  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  directionsDisplay.setMap(map);
  directionsDisplay.setPanel(document.getElementById("directions_list"));
}
  
function calcRoute() {
  var start = document.getElementById("start").value;
  var end = document.getElementById("end").value;
  var request = {
    origin:start, 
    destination:end,
    travelMode: google.maps.DirectionsTravelMode.DRIVING
  };
  directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
    }
  });
}



