// 
// Add items to the list in a function like this:
// function loadPointsOfInterest() 
// {
//    submitPointOfInterest("Bethel AK", "60.7922222", "-161.7558333", "blue", "description");
//    submitPointOfInterest("Another AK", "60.7922222", "-161.7558333", "blue", "description");
// }
//
// Call this page with ?admin on the end of the url to get the GeoCoder to display
//
// If you want the items listed in a table, then add <td id='poi_listx'> entries in an html table row
// for as many columns as you want, where x is the number of the column, starting at 1.

var map = null;         // the google map object
var icon_blue = null;	// icon for items on map
var icon_green = null;	// icon for items on map
var icon_red = null;    // icon for items on map
var icon_shadow = null; // icon for shadows
var iw = null;          // shared infowindow for marker

var x = new Array();    // array of points of interest to plot
var xi = 0;             // current index in the array of points of interest to plot

var opts;               // select dropdown listbox object - points of interest
var col = 0;            // current column for listing points of interest in a table

// -----------------------------------------------------------------------
// obtains handles to the objects and queues up the points of interest to plot
// -----------------------------------------------------------------------
function load_poi() {
    geocoder = new google.maps.Geocoder();

	// setup our markers
    icon_blue = new google.maps.MarkerImage("assets/images/mm_20_blue.png");
    icon_red = new google.maps.MarkerImage("assets/images/mm_20_red.png");
    icon_green = new google.maps.MarkerImage("assets/images/mm_20_green.png");
    icon_shadow = new google.maps.MarkerImage("assets/images/mm_20_shadow.png");

	opts = document.getElementById("opts");

	var latlng = new google.maps.LatLng(60.55457, -151.2597);  //kenai
	var myOptions = {
	    zoom: 3,
	    center: latlng,
	    mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	map = new google.maps.Map(document.getElementById("map"), myOptions);

	iw = new google.maps.InfoWindow();

	// build list of plot points
	loadPointsOfInterest();
	plotPointsOfInterest();
}

// -----------------------------------------------------------------------
// add the point of interest to the queue to plot
//	a - address or notes for info window
//	n - name or title for info window
//	la, lo - lat/long
//	color - color of marker
// -----------------------------------------------------------------------
function submitPointOfInterest(n, la, lo, color, a)
{
	if (lo == '' || la == '' || lo == null || la == null)
	{
		// skip - no valid coords
	}
	else
	{
		var o = new Object();
		o.a = a;
		o.n = n;
		o.lo = lo;
		o.la = la;
		o.color = color;
		x[xi++] = o;
	}	
}

// -----------------------------------------------------------------------
// plot the points of interest on the map and add to the selection dropdown
// -----------------------------------------------------------------------
function showPointOfInterest(notes, goto, title, lo, la, color)
{
	var point;
	point = new google.maps.LatLng(la, lo, true);
	if (point)
	{
        var mo = new Object();
        if (color == 'blue')
            mo.icon = icon_blue;
        else if (color == 'green')
            mo.icon = icon_green;
        else
            mo.icon = icon_red;
        mo.map = map;
        mo.position = point;
        mo.shadow = icon_shadow;
        mo.title = title;

        var marker = new google.maps.Marker(mo);
		if (marker)
		{
			// put the marker in the dropdown
			var z = new Option(title, title, false, goto);
			z.point = point;
			z.notes = notes;
			z.marker = marker;
			if (opts != null) {

			    var j = opts.length;
			    opts[j] = z;
			}

			// add code for marker to open infowindow when clicked
            google.maps.event.addListener(marker, "click", function() {
            iw.content = '<div class="gmaps_poi"><div class="gmaps_poi_hdr">' + z.text + '</div>' + z.notes + '</div>';
                iw.open(map, marker);
            });
		}

		// add to poi_list if the container exists
if (col == 0) // ministry home entry
{
   var mh = document.getElementById('ministryhome');
		    var lia = document.createElement("A");
		    lia.href = "javascript:optSelect('" + title + "');";
		    var lit = document.createTextNode(title);
		    lia.appendChild(lit);
    mh.appendChild(lia);
col = 999;

}
else
{
		col = col + 1;
		var v = document.getElementById("poi_list" + col);
		// if not found and col <> 1 then maybe we're past the end and need to wrap around to the first column
		if (v == null && col != 1) {
		    col = 1;
		    v = document.getElementById("poi_list" + col);
		}
		if (v != null) {
		    var br = document.createElement("BR");
		    var lia = document.createElement("A");
		    lia.href = "javascript:optSelect('" + title + "');";
		    var lit = document.createTextNode(title);
		    lia.appendChild(lit);
		    v.appendChild(lia);
		    v.appendChild(br);
		    //v.insertBefore(li, null);
		}
}
	}
}

// -----------------------------------------------------------------------
// show the point of interest in the queue
// -----------------------------------------------------------------------
function plotPointsOfInterest()
{
    //while (--xi > -1)
    //  showPointOfInterest(x[xi].a, false, x[xi].n, x[xi].lo, x[xi].la, x[xi].color);
    for (var j = 0; j < xi; j++)
		showPointOfInterest (x[j].a, false, x[j].n, x[j].lo, x[j].la, x[j].color);
}

// -----------------------------------------------------------------------
// select point of interest based on name in selection list
// -----------------------------------------------------------------------
function optSelect(title) 
{
    if (opts != null) 
    {
        for (var j = 0; j < opts.options.length; j++) {
            if (opts.options[j].text == title) {
                opts.selectedIndex = j;
                optChanged();
                break;
            }
        }
    }
}

// -----------------------------------------------------------------------
//  show the name and notes and location of the selected point of interest
// -----------------------------------------------------------------------
function optChanged ()
{
	var z;
	if (opts != null)
	{
		z = opts.options[opts.selectedIndex];
		if (z != null) {
		    iw.content = '<div class="gmaps_poi"><div class="gmaps_poi_hdr">' + z.text + '</div>' + z.notes + '</div>';
		    iw.open(map, z.marker)
		}
	}
}

// -----------------------------------------------------------------------
// extend the onload event to call our load also
// -----------------------------------------------------------------------
var oldonload = window.onload;
if(typeof window.onload != "function")
	window.onload = load_poi;
else
	window.onload = function() {
		oldonload();
		load_poi();
	}

// -----------------------------------------------------------------------
// extend the unload event to unload our stuff
// -----------------------------------------------------------------------
var oldunload = window.unload;
if(typeof window.unload != "function")
	window.unload = unload_poi;
else
	window.unload = function() {
		oldunload();
		unload_poi();
	}

function unload_poi()
{
    google.maps.Unload();
}

// -----------------------------------------------------------------------
// geocode location provided and display output to paste into poi list
// -----------------------------------------------------------------------

function lookupLatLong(address) {
    if (geocoder) {
        geocoder.geocode({ 'address': address }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var b = document.getElementById("lookupResults");
                if (b != null) {
                    var rt = document.createTextNode('\tsubmitPointOfInterest("' + address + '", "' + results[0].geometry.location.lat() + '", "' + results[0].geometry.location.lng() + '", "blue", "description");');
                    b.insertBefore(rt, null);
                    var br = document.createElement("BR");
                    b.insertBefore(br, null);
                }
            } else {
                alert("Geocode was not successful for the following reason: " + status);
            }
        });
    }
}

