﻿// Added by suggesion of SwansonRussell
var requireIcon = false;
// End Add


var map = null;
var geocoder = null;
var distance = 5;

//var markerScript = document.getElementById('GeoCodeScript').src.replace('geocode.js', 'dealer_markers.aspx')
var markerScript = "http://locator.johnsonoutdoors.com/dealer_markers.aspx";

var Markers = new Array()
var Ma = -1

/* Setup the map properties */
function initialize() {
    map = new GMap2(document.getElementById("map_canvas"));
    // Add controls for moving and map type
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    geocoder = new GClientGeocoder();
    geocoder.getLatLng('Mali',
		function(point) {
		    map.setCenter(point, 2)
		})
}

/* Displays map of the area typed in the search textbox */
function showAddress(address, g_dist) {
    distance = g_dist
    geocoder.getLatLng(address,
		function(point) {
		    if (!point)
		        alert(address + " not found.  Please try your search using a different description.")
		    else {
		        map.setCenter(point, GetZoom(distance))
		        AddMarkers(point['y'], point['x'])
		    }
		}
	)
}

/* Returns the proper Google Zoom level based on distance of search */
function GetZoom(dist) {
    switch (dist) {
        case '5':
            return 12
            break
        case '10':
            return 11
            break
        case '25':
            return 10
            break
        case '50':
            return 9
            break
        case '100':
            return 8
            break
        case '250':
            return 7
            break
        default:
            return 11
    }
}

/* Function to add dealer markers to the map */
function AddMarker(map, Lat, Long, IconURL, IconSize, ShadowSize, Desc) {

    // Added by suggestion of SwansonRussel
    if (requireIcon && IconURL == '') return;
    // End Add

    // Build a point object with the Lat/Long of the dealer
    var point = new GLatLng(Lat, Long);

    // Create a new icon object based on the default icon
    var TriIcon = new GIcon(G_DEFAULT_ICON);
    if (IconURL.length)
        TriIcon.image = IconURL;
    else
        if (typeof BaseIconURL != 'undefined') TriIcon.image = BaseIconURL

    // Create a marker object based on the point data, use the appropriate icon
    var marker = new GMarker(point, { icon: TriIcon });
    Ma++
    Markers[Ma] = marker

    // rebuild the info div to work around google height bug
    var x = document.createElement('div')
    x.innerHTML = Desc.replace(/<div class="MapClear">$/, "");
    var div = document.createElement('div')
    //alert(x.childNodes.length);
    var last = false;
    while (x.childNodes.length > 0) {
        var n = x.childNodes[0];
        var is_p = false;
        if (n.nodeName != null && (n.nodeName == "P" || n.nodeName == "DIV")) {
            if (x.childNodes.length > 1) {
                n.style.display = "inline";
            }
            is_p = true;
        }
        var cl = n.className;
        if (cl != null && (cl == "City" || cl == "State" || cl == "PostalCode")) {
            last = true;
            div.appendChild(n);
        } else {
            if (last && is_p && x.childNodes.length > 1) {
                div.appendChild(document.createElement('br'));
                last = false;
            }
            div.appendChild(n);
            if (is_p && x.childNodes.length > 1) {
                div.appendChild(document.createElement('br'));
            }
        }
    }
    marker.info_div = div;

    // Store the description text in an attribute
    marker.alt = Desc;
    // Add the marker to the map
    map.addOverlay(marker);
    // Add a "click" event to the maker to show the details bubble
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindow(marker.info_div);
        SetClass2(this.getPoint());
    });
	
	/*GEvent.addListener(marker, "infowindowopen", function() {
		var iw = map.getInfoWindow();
		var elem = document.getElementById('infoContain');
		elem.innerHTML = "";
		elem.innerHTML = Desc;
		var pElems = elem.getElementsByTagName("p");
		
	});*/

    // Build the dealer list if the div exists	
    DealerList = document.getElementById('dealer_list')
    if (DealerList != null) {
        DealerList.style.display = 'block';
        var NewDealer = document.createElement('div')
        NewDealer.className = 'DealerListing'
        // Add an onclick event to trigger the popup in the map
        NewDealer.onclick = function() { SetClass(this); DoPopup(eval('new GLatLng' + point)) }
        NewDealer.innerHTML = Desc
        NewDealer.id = "Listing_" + Ma
        NewDealer.title = point
        DealerList.appendChild(NewDealer)
    }
}

/* Finds a map marker by Point and opens the InfoWindow */
function DoPopup(marker) {
    for (i = 0; i < Markers.length; i++)
        if (Markers[i].getPoint().equals(marker)) {
            //Markers[i].openInfoWindowHtml(Markers[i].alt)
            Markers[i].openInfoWindow(Markers[i].info_div)
        }
}

/* Changes the class of the Dealer listing that is currently selected */
function SetClass(SelectedDiv) {
    var DealerList = SelectedDiv.parentNode
    for (i = 0; i < DealerList.childNodes.length; i++) {
        DealerList.childNodes[i].className = 'DealerListing'
    }
    SelectedDiv.className = 'DealerListingSelected'
}

/* Changes the class of the Dealer listing associated with a clicked InfoWindow */
function SetClass2(newPoint) {

    if (document.getElementById('dealer_list') == null) return false
    var DealerList = document.getElementById('dealer_list')

    for (i = 0; i < DealerList.childNodes.length; i++) {
        DealerList.childNodes[i].className = (newPoint == DealerList.childNodes[i].title) ? 'DealerListingSelected' : 'DealerListing'
        //        if (newPoint == DealerList.childNodes[i].title) DealerList.childNodes[i].scrollIntoView()
        if (newPoint == DealerList.childNodes[i].title) DealerList.scrollTop = DealerList.childNodes[i].offsetTop;
    }
}



/*	This function embeds the JO dealer locator javascript into the document 
and loads the dealers within the region.  */
function AddMarkers(myLat, myLong) {
    map.clearOverlays()
    DealerList = document.getElementById('dealer_list')
    if (DealerList != null) {
        DealerList.innerHTML = ''
    }
    var html_doc = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', markerScript + '?lat=' + myLat + '&long=' + myLong + '&distance=' + distance + '&group=' + Group);
    html_doc.appendChild(js);
}