/* Dealer locator tool, jrh 08/11/2010 */

function loglink(id, type) {
	// Log a link using the clicklog_host.php file
	$.ajax( {
		type: "POST",
		async: false,
		url : "http://" + window.location.hostname + "/clicklog_host.php",
		data : "dealer=" + id + "&searchlog_id=" + searchlog_id + "&type=" + type,
		cache : false
	});
}

function calcRoute(src,dest,display,service) {
  var request = {
    origin:src, 
    destination:dest,
    travelMode: google.maps.DirectionsTravelMode.DRIVING
  };
  service.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      display.setDirections(response);
    }
  });
}

function initialize() {

  if (directions_mode) {
    var directionsDisplay = new google.maps.DirectionsRenderer();
    var directionsService = new google.maps.DirectionsService();
  }

  var bounds = new google.maps.LatLngBounds();
  var ipLoc = new google.maps.LatLng(ip_lat, ip_lon);
  bounds.extend(ipLoc); // Include our location in the bounding box
  var myOptions = {
    zoom: 5,
    center: ipLoc,
    disableDefaultUI: true,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    navigationControl: true,
    navigationControlOptions: {
      style: google.maps.NavigationControlStyle.SMALL
    }
  }

  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

  if (directions_mode) {
    directionsDisplay.setMap(map);
    directionsDisplay.setPanel(document.getElementById("directions"));
    calcRoute(src,dst,directionsDisplay,directionsService);
  }

  // Perhaps use a jquery foreach here and plot latlongs from an array
  // set previously in dealer_locator_header.php

  if (results.length > 0) {
    var markers = [];
    var openinfo = null;
    var infowindows = [];

    $.each(results, function(index, result) {
      var location = new google.maps.LatLng(result[1],result[2]);

      if (index < 8) {
        bounds.extend(location); // Extend the map to fit the bounds, just do this on results 1-3 though (for now!)
      }

      // Bubble text
      var bubble = '<div style="height: 130px; width: 240px; overflow: visible"><strong>' + result[3] + '</strong><br>'; // Company name
      if (result[4].length > 0) { bubble += result[4] + ', '; } // address 2
      bubble += result[5] + ', '; // state
      bubble += result[6] + '<br>'; // postcode
      if (result[7].length > 0) { bubble += '<strong>Phone:</strong> ' + result[7] + '<br>'; } // phone
      if (result[8].length > 0) { bubble += '<strong>Fax:</strong> ' + result[8] + '<br>'; } // fax
      if (result[9].length > 0) { bubble += '<strong>Web:</strong> <a href="' + result[9] + '" target="_blank" onclick="loglink(' + result[0] + ',\'W\')">' + result[9].substring(0,35)  + '</a><br>'; } // website
      bubble += '<a href="dealer_locator.php?src=' + ip_lat + ',' + ip_lon + '&dst=' + encodeURIComponent(result[10] + ', ' + result[4] + ', ' + result[5] + ', ' + result[6]) + '">Get directions</a>';

      infowindows[index] = new google.maps.InfoWindow({
	content: bubble
      });

      if($.browser.msie && $.browser.version=="6.0") {
        markers[index] = new google.maps.Marker({
  	  position: location, 
	  map: map, 
	  title:result[3],
          icon: '../images/dealer.gif'
        });
      } else {
        markers[index] = new google.maps.Marker({
	  position: location, 
	  map: map, 
	  title:result[3],
          icon: '../images/dealer.png'
        });
      }

      // Add listener to marker
      google.maps.event.addListener(markers[index], 'mouseover', function() {

	// Close last open infowindow
        if (openinfo != null) {
	  infowindows[openinfo].close(map,markers[openinfo]);
	}

	infowindows[index].open(map,markers[index]);
	openinfo = index;
      });

      // Add listener to result row on page
      $('#result-' + result[0]).mouseover(function() {
	// Close last open infowindow
        if (openinfo != null) {
	  infowindows[openinfo].close(map,markers[openinfo]);
	}

	infowindows[index].open(map,markers[index]);
	openinfo = index;
      });
    }); // each result
    
    map.fitBounds(bounds); // Fit/zoom the map to the bounds
  }
}
  
function loadScript() {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=initialize";
    document.body.appendChild(script);
}
  
window.onload = loadScript;
		

