var point_markers = [];

function start_maps(ids, points) {
    var map = new GMap(document.getElementById("map"));
    map.addControl(new GSmallMapControl());

    var icon = new GIcon;
    icon.image = "http://www.priorityhealth.com/images/tools/providerdirectory/marker.gif";
    icon.shadow = "http://www.priorityhealth.com/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 GPoint(-84.585162, 43.897994);
    map.centerAndZoom(point, 6);
    var maxLon = -10000000;
    var minLon = 10000000;
    var maxLat = 0;
    var minLat = 10000000;
    var total_lon = 0;
    var total_lat = 0;
    var count = 0;
    for (var i = 0; i< ids.length; i++) {
        var id = ids[i];
        var lat = points[id]['lat'];
        var lon = points[id]['lon'];
        if (lon > maxLon) {
            maxLon = lon;
        }
        if (lon < minLon) {
            minLon = lon;
        }
        if (lat > maxLat) {
            maxLat = lat;
        }   
        if (lat < minLat) {
            minLat = lat;
        }
        total_lon = total_lon + lon;
        total_lat = total_lat + lat;
        count++;

        var html = points[id]['html'];
        var marker = createInfoMarker(new GPoint(lon, lat), icon, html)
        point_markers[id] = marker;
        map.addOverlay(marker);
    }
    alert('hello');
    map.centerAndZoom(new GPoint(total_lon/count, total_lat/count), 1);
    //now add letters on top of the balloons
    //addLetters();
    //addMouseOver();
}

//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 = document.getElementById("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;
}
