﻿/* ---------------------------------------------------------------------

SusQtech Event GMap Plotter
Written by SusQtech
www.susqtech.com

* requires: 
	jQuery 1.3.2 or later
	Google Map Web Part
--------------------------------------------------------------------- */
var gmarkers = [];
var htmls = [];
var to_htmls = [];
var from_htmls = [];
var i = 0;

function PlotEventMapPins() {

    $(document).ready(function() {
            var map = new GMap2(document.getElementById("EventsMap"));
            var shown = false;

            // get event from event detail page and mark it
            $('.NATPEventDetail').each(function() {
                var name = $(this).children('.NATPEventHotelName').text();
                var address = $(this).children('.NATPEventHotelAddress').text();
                var city = $(this).children('.NATPEventLocation').children('.NATPEventHotelCity').text();
                var state = $(this).children('.NATPEventLocation').children('.NATPEventHotelState').text();
                var zip = $(this).children('.NATPEventLocation').children('.NATPEventHotelZip').text();

                var lat = $(this).children('.NATPEventLocation').children('.NATPLatitude').text();
                var lng = $(this).children('.NATPEventLocation').children('.NATPLongitude').text();
                showmap(map, lat, lng, name, address, city, state, zip);
                shown = true;
            });

            // get each event location from event page and mark them
            $('.NATPEventDetails').each(function() {
                var name = $(this).children('.NATPHotelName').text();
                var address = $(this).children('.NATPHotelAddress').text();
                var city = $(this).children('.NATPHotelCity').text();
                var zip = $(this).children('.NATPHotelZip').text();
                var state = $(this).children('.NATPHotelState').text();
                var eventDetailUrl = $(this).children('.NATPHotelDetailUrl').text();
                var lat = $(this).children('.NATPLatitude').text();
                var lng = $(this).children('.NATPLongitude').text();
                
               	getAddress(map,lat, lng, name, address, city, state, zip, eventDetailUrl, false);
            });

        if (!shown) {
            //show map
            $("#map").show();
            showmap(map);
        }
    });

    function showmap(map, lat, lng, name, address, city, state, zip) {		
    	if(lat == null || lng == null) {
    		// default to us view
    		map.setCenter(new GLatLng(38,-97), 3);						
    	}
    	else {
    	    //getAddress(map,search,name, true);
    	    addMarkerPageLevel(map, lat, lng, name, address, city, state, zip, true)
    	}
    	map.addControl(new GSmallMapControl());
    	map.addControl(new GMapTypeControl());
    }
    
    function addMarker(map, lat, lng, name, address, city, state, zip, eventDetailUrl, setCenter) {
    	if(setCenter == true) 
    		map.setCenter(new GLatLng(lat, lng), 10);			
    		
    	var point = new GLatLng(lat, lng);
    	mOpts = { title:name };
    	var marker = new GMarker(point,mOpts);

    	var html = "<div class=\"bubble\">";
    	html += formatWindow(name, address, city, state, zip, eventDetailUrl, point);

    	// The info window version with the "to here" form open
    	to_htmls[i] = html + '<br>Directions: <b>To here<\/b> - <a href="javascript:fromhere(' + i + ')">From here<\/a>' +
           '<br>Start address:<form action="http://maps.google.com/maps" method="get" target="_blank">' +
           '<input type="text" SIZE=20 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br>' +
           '<INPUT value="Get Directions" TYPE="SUBMIT">' +
           '<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() +
           '"/>';

    	// The info window version with the "to here" form open
    	from_htmls[i] = html + '<br>Directions: <a href="javascript:tohere(' + i + ')">To here<\/a> - <b>From here<\/b>' +
           '<br>End address:<form action="http://maps.google.com/maps" method="get"" target="_blank">' +
           '<input type="text" SIZE=20 MAXLENGTH=40 name="daddr" id="daddr" value="" /><br>' +
           '<INPUT value="Get Directions" TYPE="SUBMIT">' +
           '<input type="hidden" name="saddr" value="' + point.lat() + ',' + point.lng() +
           '"/>';
    	// The inactive version of the direction info

    	html += '<br>Directions: <a href="javascript:tohere(' + i + ')">To here<\/a> - <a href="javascript:fromhere(' + i + ')">From here<\/a>';

    	html += "</div>";

    	//marker.title = name;
    	GEvent.addListener(marker, "click", function() {
    	    map.setCenter(new GLatLng(lat, lng), 10);
    	    marker.openInfoWindowHtml(html);
    	});

    	GEvent.addListener(marker, 'infowindowclose', function() {
    	    map.setCenter(new GLatLng(38, -97), 3);
    	});
    	gmarkers[i] = marker;
    	htmls[i] = html;
    	i++;

    	map.addOverlay(marker);
 }

 function addMarkerPageLevel(map, lat, lng, name, address, city, state, zip, setCenter) {
     if (setCenter == true)
         map.setCenter(new GLatLng(lat, lng), 10);
     var point = new GLatLng(lat, lng);
     mOpts = { title: name };
     var marker = new GMarker(point, mOpts);

     var html = "<div class=\"bubble\">";
     html += formatWindowPageLevel(name, address, city, state, zip);
     // The info window version with the "to here" form open
     to_htmls[i] = html + '<br>Directions: <b>To here<\/b> - <a href="javascript:fromhere(' + i + ')">From here<\/a>' +
           '<br>Start address:<form action="http://maps.google.com/maps" method="get" target="_blank">' +
           '<input type="text" SIZE=20 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br>' +
           '<INPUT value="Get Directions" TYPE="SUBMIT">' +
           '<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() +
           '"/>';

     // The info window version with the "to here" form open
     from_htmls[i] = html + '<br>Directions: <a href="javascript:tohere(' + i + ')">To here<\/a> - <b>From here<\/b>' +
           '<br>End address:<form action="http://maps.google.com/maps" method="get"" target="_blank">' +
           '<input type="text" SIZE=20 MAXLENGTH=40 name="daddr" id="daddr" value="" /><br>' +
           '<INPUT value="Get Directions" TYPE="SUBMIT">' +
           '<input type="hidden" name="saddr" value="' + point.lat() + ',' + point.lng() +
           '"/>';
     // The inactive version of the direction info

     html += '<br>Directions: <a href="javascript:tohere(' + i + ')">To here<\/a> - <a href="javascript:fromhere(' + i + ')">From here<\/a>';
     html += "</div>";

     //marker.title = name;
     GEvent.addListener(marker, "click", function() {         
         marker.openInfoWindowHtml(html);
     });

     gmarkers[i] = marker;
     htmls[i] = html;
     i++;

     map.addOverlay(marker);

 }

    function formatWindow(name, address, city, state, zip, eventDetailUrl, point) {
        var html = "<h1>" + name + "</h1>";
        html += "<p />";
        html += address + "<br />";
        html += city + ", " + state + ", " + zip + "<br />";
        html += "<a href='" + eventDetailUrl + "'>Details</a>";
        return html;
    }

    function formatWindowPageLevel(name, address, city, state, zip) {
        var html = "<div class=\"bubble\">";
        html += "<h1>" + name + "</h1>";
        html += "<p />";
        html += address + "<br />";
        html += city + ", " + state + ", " + zip + "<br />";
        html += "</div>";
        return html;
    }
    /*function getAddress(map,address,city,state,zip,name,setCenter) {
        // ====== Create a Client Geocoder ======
            var geo = new GClientGeocoder();
            var search = address + " " + city + ", " + state + " " + zip;
            geo.getLocations(search, function(result) {
                // If that was successful
                if (result.Status.code == G_GEO_SUCCESS) {
                    // Lets assume that the first marker is the one we want
                    var p = result.Placemark[0].Point.coordinates;
                    var lat = p[1];
                    var lng = p[0];
                    addMarker(map, lat, lng, name, setCenter);
                }
                else {
		            search = city + ", " + state + " " + zip;                	
                    geo.getLocations(search, function(result) {
                        // If that was successful
                        if (result.Status.code == G_GEO_SUCCESS) {
                            // Lets assume that the first marker is the one we want
                            var p = result.Placemark[0].Point.coordinates;
                            var lat = p[1];
                            var lng = p[0];
                            addMarker(map, lat, lng, name, setCenter);

                        }
                        else {
        		            search = zip;                	
                            geo.getLocations(search, function(result) {
                                // If that was successful
                                if (result.Status.code == G_GEO_SUCCESS) {
                                    // Lets assume that the first marker is the one we want
                                    var p = result.Placemark[0].Point.coordinates;
                                    var lat = p[1];
                                    var lng = p[0];
                                    addMarker(map, lat, lng, name, setCenter);
                                }
                                else {
                                   	console.log("address not found: "+address + " " + city + ", " + state + " " + zip);
                                }
                            });
                           	
                        }
                    });
                }
            });
    }*/

    function getAddress(map, lat, lng, name, address, city, state, zip, eventDetailUrl, setCenter) {
                addMarker(map, lat, lng, name, address, city, state, zip, eventDetailUrl, setCenter);
    }

    function getAddressPageLevel(map, lat, lng, name, address, city, state, zip, setCenter) {

        addMarkerPageLevel(map, lat, lng, name, address, city, state, zip, setCenter);
    }
}

// functions that open the directions forms
function tohere(i) {
    gmarkers[i].openInfoWindowHtml(to_htmls[i]);
}
function fromhere(i) {
    gmarkers[i].openInfoWindowHtml(from_htmls[i]);
}


