﻿// Calculate boundary to contains all the markers around given center.
//  find anti-symmetry again mapCenter of lat and lng
function bestFitWithCenter(map, bounds, mapCenter) {
  var swLL = bounds.getSouthWest();
  var neLL = bounds.getNorthEast();
       
  //leave margin each side  
  var marginRatio = 0.0001;

  var minLat = Math.min(2*mapCenter.lat() - neLL.lat(), swLL.lat());                 
  var maxLat = Math.max(2*mapCenter.lat() - swLL.lat(),  neLL.lat());         
  var minLng = Math.min(2*mapCenter.lng() - neLL.lng(), swLL.lng());                
  var maxLng = Math.max(2*mapCenter.lng() - swLL.lng(), neLL.lng());
   
  //GLog.write("minLat="+minLat + ", minLng="+minLng);
  //GLog.write("maxLat="+maxLat + ", maxLng="+maxLng);
    
  var minLatLng = new GLatLng(minLat-marginRatio, minLng-marginRatio);
  var maxLatLng = new GLatLng(maxLat+marginRatio, maxLng+marginRatio);
  
  bounds.extend(maxLatLng);
  bounds.extend(minLatLng);      
  
  map.setZoom(map.getBoundsZoomLevel(bounds));
  map.setCenter(mapCenter);     
}

// Zoom out to display all the markers and set the map to the center
function bestFitBounds(map, bounds) {
	map.setCenter(bounds.getCenter());
	map.setZoom(map.getBoundsZoomLevel(bounds));
}

// Zoom out to display the given min/max latitude and longitude and set the map to the center
function bestFitMinMax(map, minLatitude, minLongitude, maxLatitude, maxLongitude) {
	var bounds = new GLatLngBounds(new GLatLng(minLatitude, minLongitude), new GLatLng(maxLatitude, maxLongitude));
	
	bestFitBounds(map, bounds);
}

var bubble;
var approot;
var gmap;

function openBubble()
{
		
	var info = '<div style="padding-top:3px; padding-right:20px">' +
								this.dealer.naam + ' <br /> ' + this.dealer.adres + ' <br /> ' + this.dealer.postcode + ' ' + this.dealer.plaats + '<br /> ' + this.dealer.land + '<br />' +
								'Tel: ' + this.dealer.telefoon + '<br />' + 'Fax: ' + this.dealer.fax + '<br />' + 'Email: <a href="mailto:' + this.dealer.email + '">' + this.dealer.email + '</a><br />' + 'Website: <a href="' + this.dealer.website + '" target="_blank">' + this.dealer.website + '</a><br />' +
	           '</div>';

	var html = '<div style="font-weight:bold;color:#0072B8;position:absolute;left:16px;top:12px;">' + this.dealer.naam + '</div>' +
						 '<div style="position:absolute;left:13px;top:40px;">' + info + '</div>' +
						 '<div style="position:absolute;right:17px; top:14px; width:13px; height:13px; cursor:pointer;" onclick="if(bubble)bubble.hide();"></div>';

	if (!bubble)
	    bubble = new EBubble(gmap, approot + "img/dealers/popup3.png", new GSize(419, 189), new GSize(419, 189), new GPoint(0, 0), new GPoint(196, 168), true);

	bubble.openOnMarker(this, html);
}

function initNewMarkers(data, status)
{
	if ((!data) && (status == -1)) return; // Timeout occurred

	eval('var dealersData = ' + data);

	approot = dealersData.approot;

	gmap = dealersData.gmap;
	var dealers = dealersData.dealers;
	
	gmap.clearOverlays();

	if (dealers)
	{
		var icon = new GIcon();
		icon.image = approot + "img/dealers/marker.png";
		icon.iconSize = new GSize(40, 60);
		icon.iconAnchor = new GPoint(20, 60);
		icon.infoWindowAnchor = new GPoint(13, -2);
		icon.shadow = approot + "img/dealers/marker_shadow.png";
		icon.shadowSize = new GSize(60, 60);
		

		for (var i = 0; i < dealers.length; i++)
		{
		    var dealer = dealers[i];
		    var marker = new GMarker(new GLatLng(dealer.lat, dealer.lng), { title: dealer.naam, icon: icon });
		    marker.dealer = dealer;
			
      GEvent.addListener(marker, "click", openBubble);
        
			gmap.addOverlay(marker);
		}
	}
	else
	{
	    if (dealersData.count > 0)
		{
			var icon = new GIcon();
			icon.image = approot + "img/dealers/count.png";
			icon.iconSize = new GSize(67, 67);
			icon.iconAnchor = new GPoint(30, 30);
			icon.infoWindowAnchor = new GPoint(30, 30);

			var html = '<div style="font-size:12px;color:black;font-weight:bold;width:50px;height:20px;text-align:center;line-height:20px;">' + dealersData.count + '</div>';
			var marker = new LabeledMarker(gmap.getCenter(), { title: dealersData.msg, labelText: html, labelOffset: new GSize(-25, -10), icon: icon });
			gmap.addOverlay(marker);
		}
		else
		{
			var icon = new GIcon();
			icon.image = approot + "img/dealers/nodealers.png";
			icon.iconSize = new GSize(188, 93);
			icon.iconAnchor = new GPoint(94, 46);
			icon.infoWindowAnchor = new GPoint(94, 46);
		
			var marker = new LabeledMarker(gmap.getCenter(), {title:'',labelText:'',labelOffset:new GSize(0,0),icon:icon});
			gmap.addOverlay(marker);
		}
	}
}



/*********************************************************************************/

/*
* LabeledMarker Class
*
* Copyright 2007 Mike Purvis (http://uwmike.com)
* 
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* 
*       http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This class extends the Maps API's standard GMarker class with the ability
* to support markers with textual labels. Please see articles here:
*
*       http://googlemapsbook.com/2007/01/22/extending-gmarker/
*       http://googlemapsbook.com/2007/03/06/clickable-labeledmarker/
*/



// Constructor
function LabeledMarker(latlng, options){
    this.latlng = latlng;
    this.labelText = options.labelText || "";
    this.labelClass = options.labelClass || "markerLabel";
    this.labelOffset = options.labelOffset || new GSize(0, 0);
    
    this.clickable = options.clickable || true;
    
    if (options.draggable) {
    	// This version of LabeledMarker doesn't support dragging.
    	options.draggable = false;
    }
    
    GMarker.apply(this, arguments);
}

// It's a limitation of JavaScript inheritance that we can't conveniently
// extend GMarker without having to run its constructor. In order for the
// constructor to run, it requires some dummy GLatLng.
LabeledMarker.prototype = new GMarker(new GLatLng(0, 0));


// Creates the text div that goes over the marker.
LabeledMarker.prototype.initialize = function(map) {
	// Do the GMarker constructor first.
	GMarker.prototype.initialize.apply(this, arguments);
	
	var div = document.createElement("div");
	div.className = this.labelClass;
	div.innerHTML = this.labelText;
	div.style.position = "absolute";
	map.getPane(G_MAP_MARKER_PANE).appendChild(div);

	if (this.clickable) {
		// Pass through events fired on the text div to the marker.
		var eventPassthrus = ['click', 'dblclick', 'mousedown', 'mouseup', 'mouseover', 'mouseout'];
		for(var i = 0; i < eventPassthrus.length; i++) {
			var name = eventPassthrus[i];
			GEvent.addDomListener(div, name, newEventPassthru(this, name));
		}

		// Mouseover behaviour for the cursor.
		div.style.cursor = "pointer";
	}
	
	this.map = map;
	this.div = div;
}

function newEventPassthru(obj, event) {
	return function() { 
		GEvent.trigger(obj, event);
	};
}

// Redraw the rectangle based on the current projection and zoom level
LabeledMarker.prototype.redraw = function(force) {
	GMarker.prototype.redraw.apply(this, arguments);
	
	// We only need to do anything 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 p = this.map.fromLatLngToDivPixel(this.latlng);
	var z = GOverlay.getZIndex(this.latlng.lat());
	
	// Now position our DIV based on the DIV coordinates of our bounds
	this.div.style.left = (p.x + this.labelOffset.width) + "px";
	this.div.style.top = (p.y + this.labelOffset.height) + "px";
	this.div.style.zIndex = z + 1; // in front of the marker
}

// Remove the main DIV from the map pane, destroy event handlers
LabeledMarker.prototype.remove = function() {
	GEvent.clearInstanceListeners(this.div);
	this.div.parentNode.removeChild(this.div);
	this.div = null;
	GMarker.prototype.remove.apply(this, arguments);
}


/*********************************************************************************/
