var map = null;
var geocoder = null;
var mapmngr = null;
var baseIcon = null;
var currentMarker = null;
var visablezoomlevel;

function createMarker(point, html, active) {
        // Set up our GMarkerOptions object
//          markerOptions = { icon:baseIcon };
//          markerOptions = { title: html };
//        markerOptions = {};
		if(active){
			markerOptions = { icon:baseIcon, clickable:true };		
		}
		else{
			markerOptions = { icon:baseIcon, clickable:false };
		}
        var marker = new GMarker(point, markerOptions);
  		mapmngr.addMarker(marker,visablezoomlevel);
  		if (active){
			GEvent.addListener(marker, "click", 
							function(){
								eventMarkerClick(marker, point, html);
							} );
		}
        return marker;
      }

function eventMarkerClick(marker, point, html)
{
	if (typeof MyOverlay !== 'undefined') {
					if (currentMarker) {
								closeOverlay();
		}
		if (!marker.overlay) {
			// just recording this for use in the closeOverlay function
			marker.overlay = new MyOverlay(marker, html);
		}
		currentMarker = marker;
		map.panTo(new GLatLng(marker.getPoint().lat(), marker.getPoint().lng()));
		map.addOverlay(marker.overlay);
//				marker.hide();
	}
	else
		marker.openInfoWindowHtml(html);
}

function eventMapZoomEnd()
{
//	alert('zoomend\nZoom level ' + map.getZoom());
	window.status = 'zoomlevel: ' + map.getZoom() + ' |  visablezoomlevel: ' + visablezoomlevel;
	closeOverlay(); 
}
				
function eventMapMoveEnd()
{
	var bounds = map.getBounds();
	var southWest = bounds.getSouthWest();
	var northEast = bounds.getNorthEast();
	var bounderies = 'SW: ' + southWest.lat() + ', ' + southWest.lng() + '\nNE: ' + northEast.lat() + ', ' + northEast.lng();
//			alert('moveend\n' + bounderies);
}


function closeOverlay() {
	if (currentMarker) {
		map.removeOverlay(currentMarker.overlay);
//				currentMarker.show();
	}
}

function SetCenter(address, zoom) {
	if (geocoder) {
		geocoder.getLatLng( address, 
								function(point) {
									if (point) map.setCenter(point, zoom);
								}
				  			);
	}
}

//Overlay for dealer
var MyOverlay = function(marker, html) {
					this.marker = marker;
					this.html = html;
					}

MyOverlay.prototype = new GOverlay();
MyOverlay.prototype.initialize = function(map) {
					var div = document.createElement("div");
					div.className = 'map_infowindow';
					div.innerHTML = this.html;

					// offsets based on popup div dimensions
					offsetX = -96;
					offsetY = 26;
					
					//offsetX = 92;
					//offsetY = 26;


					div.style.top = (map.fromLatLngToDivPixel(this.marker.getPoint()).y - offsetY) + 'px';
					div.style.left = (map.fromLatLngToDivPixel(this.marker.getPoint()).x - offsetX) + 'px';

					div.onclick = closeOverlay;

					this._map = map;
					this._div = div;

					map.getPane(G_MAP_FLOAT_PANE).appendChild(div);  
					}
MyOverlay.prototype.remove = function() {
								this._div.parentNode.removeChild(this._div);
								}
MyOverlay.prototype.redraw = function() {
								//haven't had need for this yet
								}
								
// Overlay for debug purpose
var Rectangle = function Rectangle(bounds, opt_weight, opt_color) {
	this.bounds_ = bounds;
	this.weight_ = opt_weight || 1;
	this.color_ = opt_color || "#000000";
}
Rectangle.prototype = new GOverlay();

// Creates the DIV representing this rectangle.
Rectangle.prototype.initialize = function(map) {
	  // Create the DIV representing our rectangle
	var div = document.createElement("div");
	div.style.border = this.weight_ + "px solid " + this.color_;
	div.style.position = "absolute";
	
	// Our rectangle is flat against the map, so we add our selves to the
	// MAP_PANE pane, which is at the same z-index as the map itself (i.e.,
	// below the marker shadows)
	map.getPane(G_MAP_MAP_PANE).appendChild(div);
	
	this.map_ = map;
	this.div_ = div;
}

// Remove the main DIV from the map pane
Rectangle.prototype.remove = function() {
	this.div_.parentNode.removeChild(this.div_);
}

// Copy our data to a new Rectangle
Rectangle.prototype.copy = function() {
	return new Rectangle(this.bounds_, this.weight_, this.color_,
					   this.backgroundColor_, this.opacity_);
}

// Redraw the rectangle based on the current projection and zoom level
Rectangle.prototype.redraw = function(force) {
	// We only need to redraw if the coordinate system has changed
	if (!force) return;

	// Calculate the DIV coordinates of two opposite corners of our bounds to
	// get the size and position of our rectangle
	var c1 = this.map_.fromLatLngToDivPixel(this.bounds_.getSouthWest());
	var c2 = this.map_.fromLatLngToDivPixel(this.bounds_.getNorthEast());

	// Now position our DIV based on the DIV coordinates of our bounds
	this.div_.style.width = Math.abs(c2.x - c1.x) + "px";
	this.div_.style.height = Math.abs(c2.y - c1.y) + "px";
	this.div_.style.left = (Math.min(c2.x, c1.x) - this.weight_) + "px";
	this.div_.style.top = (Math.min(c2.y, c1.y) - this.weight_) + "px";
}								


