var point_markers = [];

function start_maps(points) {
    var map = new GMap2($('map'));
    map.addControl(new GSmallMapControl());

    var icon = new GIcon;
    icon.image = "/images/tools/providerdirectory/marker.gif";
    icon.shadow = "/images/tools/providerdirectory/shadow50.png";
    icon.iconSize = new GSize(20,34);
    icon.shadowSize=new GSize(37,34);
    icon.iconAnchor=new GPoint(9,34);
    icon.infoWindowAnchor=new GPoint(9,2);
    icon.infoShadowAnchor=new GPoint(18,25);
    
    //random point to center and zoom, required to plot points, the actual center point is set later
    var point = new GLatLng(43.897994, -84.585162);
    map.setCenter(point, 11);
    var bounds = new GLatLngBounds;
    for (var i = 0; i< points.length; i++) {
        var id = points[i]['id'];
        var lat = points[i]['lat'];
        var lon = points[i]['lon'];

        var html = points[i]['html'];
        var num = i+1
        icon.image = "/images/tools/providerdirectory/red/r-"+(i+1)+'.png';

        var point = new GLatLng(lat, lon);
        var marker = createInfoMarker(point, icon, html)
        bounds.extend(point);

        point_markers[id] = marker;
        map.addOverlay(marker);
    }

    map.setZoom(map.getBoundsZoomLevel(bounds));
    map.setCenter(bounds.getCenter());
}

//This function creates the marker and assigns it a listener
function createInfoMarker(point, icon, html) {

    var marker = new GMarker(point, icon);

    GEvent.addListener(marker, "click",

        function() {
            marker.openInfoWindowHtml(html);
        }

    );

    return marker;
}

//This function shows the balloon for a marker, it is used outside of the map, it just creates a click which is handled by the marker listener
function showInfo (marker_num, id) {
    var map = $('map');
    var marker_images = map.childNodes[0].childNodes[0].childNodes[6];
    for (var i = 0; i < marker_images.length; i++) {
        var image = marker_images[i];
        if (i == marker_num-1) {
            image.style.zIndex = i+100;
        }
        else {
            image.style.zIndex = i;
        }
    }
    GEvent.trigger(point_markers[id], "click");
}

function letter_click() {
    var id = this.id
    id = id.replace(/^letter(\d+\_\d+)$/, "$1");
    GEvent.trigger(point_markers[id], "click");
}

function showHover(markerId) {
    var view_location_div = document.createElement("div");
    view_location_div.className = "popup";
    view_location_div.id = "view-location";
    view_location_div.innerHTML = "View&nbsp;Location";
    view_location_div.style.position = "absolute";
    var marker = document.getElementById(markerId);
    var container = marker.parentNode;
    var xPos = findPosX(marker);
    var yPos = findPosY(marker);
    xPos = xPos-24;
    yPos = yPos+30;
    view_location_div.style.left = xPos+"px";
    view_location_div.style.top = yPos+"px";
    view_location_div.style.display = "block";
    container.appendChild(view_location_div);
}

//adding mouseovers for some reason does not allow passing in anything so we get it from this.
function showMapHover() {
    var view_location_div = document.createElement("div");
    view_location_div.className = "popup";
    view_location_div.id = "view-location";
    view_location_div.innerHTML = "View&nbsp;Location";
    view_location_div.style.position = "";
    var top = this.style.top;
    top = top.replace(/^(\d+)px$/gm, "$1");
    top = parseInt(top)+30;
    top = top+"px";
    view_location_div.style.top = top;
    var left = this.style.left;
    left = left.replace(/^(\d+)px$/gm, "$1");
    left = parseInt(left)-24;
    left = left+"px";
    view_location_div.style.left = left;
    view_location_div.style.display = "block";
    var container = this.parentNode;
    container.appendChild(view_location_div);
}

function hideHover() {
    var view_location_div = document.getElementById("view-location");
    view_location_div.parentNode.removeChild(view_location_div);
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}
