$("#loading_gif").css("display","block");
//Set up Variables for Google Maps
var lat, lng;
var map;
var geocoder;
var markersArray = [];
var markerInfoBoxArray = [];
var routesArray = [];
var markers;
var ResultsCount = 0;
var directionsDisplay;
var directionsService
var contentString;
var MarkerContent;
console.log('MK1');
//Load in the Google Maps Api. Once Loaded call the Initialize Function
function loadScript() {
console.log('MK2');
markers = marker_library;//xmlDoc.documentElement.getElementsByTagName("marker");
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://maps.googleapis.com/maps/api/js?key=AIzaSyCFz8WzAzFQt3GP20TNal9WrbX8bV1_jU4&sensor=false&' +
'callback=loadInfoBoxScript';
document.body.appendChild(script);
}
function loadInfoBoxScript() {
console.log('MK3');
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = '/js/infobox.js';
document.body.appendChild(script);
checkInfoBoxLoaded();
}
checkInfoBoxLoaderTimer="";
function checkInfoBoxLoaded(){
console.log('MK4');
if (typeof(myInfoBoxTestingObject) !== 'undefined'){
clearInterval(checkInfoBoxLoaderTimer);
loadMarkClusterScript();
}
else{
checkInfoBoxLoaderTimer = setInterval(function(){checkInfoBoxLoaded()},1000);
}
}
function loadMarkClusterScript() {
console.log('MK5');
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = '/js/markerclusterer.js';
document.body.appendChild(script);
checkMarkerClusterLoaded();
}
checkMarkerClusterLoaderTimer="";
function checkMarkerClusterLoaded(){
console.log('MK6');
if (typeof(markerClusterTestObject) !== 'undefined'){
clearInterval(checkMarkerClusterLoaderTimer);
initialize();
}
else{
checkMarkerClusterLoaderTimer = setInterval(function(){checkMarkerClusterLoaded()},1000);
}
}
var markerCluster
//Initialize the Map and Display it by referencing the Div it is to be placed in.
function initialize() {
console.log('MK7');
directionsService = new google.maps.DirectionsService();
var mapOptions = {
center: new google.maps.LatLng(53.400, -8.1000),
zoom: 7,
mapTypeId: google.maps.MapTypeId.ROADMAP,
backgroundColor: '',
disableDoubleClickZoom: false
};
map = new google.maps.Map(document.getElementById("map"),mapOptions);
addMarkers(markers);
}
function addMarkers(markers){
console.log('MK8');
//Loop through the xml data and set up our local variables
for (var i = 0; i < markers.length; i++) {
var name = markers[i].getAttribute("name");
var address = markers[i].getAttribute("address");
var phone = markers[i].getAttribute("phone");
var fax = markers[i].getAttribute("fax");
var email = markers[i].getAttribute("email");
var web = markers[i].getAttribute("web");
var comment = markers[i].getAttribute("comment");
var brand_comment = markers[i].getAttribute("brand_comment");
var type = markers[i].getAttribute("type");
var lat = markers[i].getAttribute("lat");
var lng = markers[i].getAttribute("lng");
var sales = markers[i].getAttribute("sales");
var service = markers[i].getAttribute("service");
var parts = markers[i].getAttribute("parts");
if(sales == "True"){
var point = new google.maps.LatLng(parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng")));
createContent(name,address,phone,fax,email,web,comment,brand_comment,type,lat,lng,point,sales,service,parts,i);
}
}
ResultsCount = 0;
jQuery("#loading_gif").css("display","none");
jQuery("#map-search-box").css("display","block");
}
function filterMarkers(markers,searchType,search_text,search_by){
console.log('MK9');
map.setZoom(7);
var ib;
clearOverlays();
//clearClusters();
jQuery("#divSearchResults").html("");
for (var i = 0; i < markers.length; i++) {
var name = markers[i].getAttribute("name");
var address = markers[i].getAttribute("address");
var phone = markers[i].getAttribute("phone");
var fax = markers[i].getAttribute("fax");
var email = markers[i].getAttribute("email");
var web = markers[i].getAttribute("web");
var comment = markers[i].getAttribute("comment");
var brand_comment = markers[i].getAttribute("brand_comment");
var type = markers[i].getAttribute("type");
var lat = markers[i].getAttribute("lat");
var lng = markers[i].getAttribute("lng");
var point = new google.maps.LatLng(parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng")));
var sales = markers[i].getAttribute("sales");
var service = markers[i].getAttribute("service");
var parts = markers[i].getAttribute("parts");
switch(searchType)
{
case "sales":
if( search_by == "address" ){
if (address != ""){
var county = address.split(" ");
var countyElements = county.length - 1;
if ((sales == "True") && county[countyElements].toUpperCase().indexOf(search_text.toUpperCase()) !== -1 ){
createContent(name,address,phone,fax,email,web,comment,brand_comment,type,lat,lng,point,sales,service,parts,i);
}
}
}
else if( search_by == "name" ){
if ((sales == "True") && name.toUpperCase().indexOf(search_text.toUpperCase()) !== -1 ){
createContent(name,address,phone,fax,email,web,comment,brand_comment,type,lat,lng,point,sales,service,parts,i);
}
}
break;
case "services":
if( search_by == "address" ){
if (address != ""){
var county = address.split(" ");
var countyElements = county.length - 1;
if ((service == "True") && county[countyElements].toUpperCase().indexOf(search_text.toUpperCase()) !== -1 ){
createContent(name,address,phone,fax,email,web,comment,brand_comment,type,lat,lng,point,sales,service,parts,i);
}
}
}
else if( search_by == "name" ){
if ((service == "True") && name.toUpperCase().indexOf(search_text.toUpperCase()) !== -1 ){
createContent(name,address,phone,fax,email,web,comment,brand_comment,type,lat,lng,point,sales,service,parts,i);
}
}
break;
}
}
if (ResultsCount == 0){
jQuery('#divSearchResults').html("
- No Garages were found for this search - - Please try again -
");
}
ResultsCount = 0;
//var markerCluster = new MarkerClusterer(map, markersArray);
}
function createContent(name,address,phone,fax,email,web,comment,brand_comment,type,lat,lng,point,sales,service,parts,i){
console.log('MK10');
var services_listing="";
var myLatlng = new google.maps.LatLng(lat,lng);
var services = "
@";
}
var telephone="";
if (phone !="" && phone != "undefined"){
telephone = " tel.:"+phone;
}
var faxnumber="";
if(fax.length > 0){
faxnumber = " fax:"+fax;
}
if (sales == "True" && service == "True"){
var marker_icon = "http://dealer.fiat.ie/images/mm_service_sales1.png";
}else if(sales =="True" && service == "False"){
var marker_icon = "http://dealer.fiat.ie/images/mm_sales.png";
}else if(sales =="False" && service == "True"){
var marker_icon = "http://dealer.fiat.ie/images/mm_service.png";
}
else if(sales =="False" && service == "False" && parts == "True"){
var marker_icon = "http://dealer.fiat.ie/images/mm_service.png";
}
//var line_address = address.replace(/ /g,",");
var line_address = address;
var contentString = "
"+name+"
"+line_address+" "+telephone+faxnumber+"
"+services+"
"+mailto+"
";
ResultsCount = ResultsCount + 1;
var resultsContent = "
"+name+"
"+services_listing+"
"+line_address+"
";
var currentResults = jQuery("#divSearchResults").html();
jQuery("#divSearchResults").html(currentResults+resultsContent);
createMarkerandInfobox(marker_icon,name,myLatlng,contentString,i);
}
function createMarkerandInfobox(marker_icon,name,myLatlng,contentString,i){
var ib;
marker = new google.maps.Marker({
position:myLatlng,
map: map,
title: name,
animation: google.maps.Animation.DROP,
icon: marker_icon,
customInfo : i
});
if (contentString != ""){
contentString = contentString.replace('',"");
var myinfoboxOptions = {
content: contentString
,disableAutoPan: false
,maxWidth: 0
,pixelOffset: new google.maps.Size(-140, 0)
,zIndex: null
,draggable: true
,boxStyle: {
background: "#ffffff"
,opacity: 1
,width: "280px"
,padding:"0px;"
}
,closeBoxMargin: "10px 10px 2px 2px"
,closeBoxURL: "http://www.google.com/intl/en_us/mapfiles/close.gif"
,infoBoxClearance: new google.maps.Size(1, 1)
,isHidden: false
,pane: "floatPane"
,enableEventPropagation: false
};
ib = new InfoBox(myinfoboxOptions);
google.maps.event.addListener(marker, 'click', function() {
closeAllOpenInfoBoxes();
var myPosition = this.getPosition();
centerMap(myPosition,11);
ib.open(map, this);
var markerArrayPosition = (this).customInfo;
jQuery(".search_results_result").each(function(){
var SelectedDiv = jQuery(this);
if (jQuery(this).hasClass("selected")){
jQuery(this).removeClass("selected");
}
var i = jQuery(this).attr("rel");
if ( parseInt(i) == parseInt(markerArrayPosition)){
jQuery(this).addClass("selected");
jQuery("#divSearchResults").scrollTo( jQuery('.selected'), 800 );
}
});
});
google.maps.event.addListener(ib,'closeclick',function(){
jQuery("#divSearchResults .search_results_result.selected").removeClass("selected");
});
google.maps.event.addListener(ib,'domready',function(){
jQuery(".infoBox").on("click","button.find_directions_section",function(){
jQuery("#map-search-box").css("display","none");
jQuery("#map-search-directions").css("display","block");
var latlng = jQuery("span.directions_lat_lng",this).html();
var dealer = jQuery("span.directions_dealer",this).html();
jQuery("#frmDirectionsFiat").html(dealer);
jQuery("#frmDirectionsFiatLatLng").val(latlng);
MarkerContent = jQuery(".infoBox").html();
jQuery("#get_directions").trigger("click");
});
});
markerInfoBoxArray.push(ib);
}
markersArray.push(marker);
}
function calcRoute(MarkerContent){
jQuery('#divDirectionsResults').html("");
var route;
var marker_start_icon = "http://dealer.fiat.ie/images/market_a.png";
var marker_end_icon = "http://dealer.fiat.ie/images/mm_service_sales.png";
var start = jQuery("#frmDirectionsYou").val();
var end = jQuery("#frmDirectionsFiatLatLng").val();
//directionsDisplay = new google.maps.DirectionsRenderer({suppressMarkers: true});
directionsDisplay = new google.maps.DirectionsRenderer({suppressMarkers:true});
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById("divDirectionsResults"));
routesArray = [];
var polyline = {
strokeColor:'#96172E'
}
var request = {
origin:start,
destination:end,
provideRouteAlternatives: true,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
for (var i = 0, len = response.routes.length; i < len+1; i++) {
route = new google.maps.DirectionsRenderer({
suppressMarkers: true,
clickable: true,
polylineOptions:polyline,
map: map,
directions: response,
routeIndex: i,
clickable: true
});
directionsDisplay.setDirections(response);
routesArray.push(route);
}
contentString = "";
var leg = response.routes[0].legs[0];
createMarkerandInfobox(marker_start_icon,name,leg.start_location,contentString);
createMarkerandInfobox(marker_end_icon,name,leg.end_location,MarkerContent);
}
else if (status == "ZERO_RESULTS") {
jQuery('#divDirectionsResults').html("
- No directions found between your location and the selected garage -
");
}
else if (status == "NOT_FOUND") {
jQuery('#divDirectionsResults').html("
- Your Location can not be found by Google -- Please enter a different location -
");
}
else{
console.log("Google Response - "+status);
}
});
}
function centerMap(myPosition,zoom){
map.setCenter(myPosition);
map.setZoom(zoom);
}
//Add in our Markers to the Map
function clearOverlays() {
for (var i = 0; i < markersArray.length; i++ ) {
markersArray[i].setMap(null);
}
}
function closeAllOpenInfoBoxes(){
for (var i = 0; i < markerInfoBoxArray.length; i++) {
markerInfoBoxArray[i].setMap(null);
}
}
function clearDirections(){
for (var i = 0; i < routesArray.length; i++){
routesArray[i].setMap(null);
directionsDisplay.setMap(null);
}
}
function clearClusters(){
//markerCluster.clearMarkers();
}
var CurrentLocation = "";
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
console.log("navigator.geolocation = True");
}
else
{
x.innerHTML="Geolocation is not supported by this browser.";
console.log("navigator.geolocation = False");
}
}
function showPosition(position)
{
console.log("ShowPosition");
CurrentLocation = CreateLatLngObject(position.coords.latitude,position.coords.longitude);
codeLatLng(CurrentLocation);
}
function CreateLatLngObject(Latitude, Longitude) {
var latlng = new google.maps.LatLng(parseFloat(Latitude), parseFloat(Longitude));
return latlng;
}
var geocoder;
function codeLatLng(latlng) {
console.log("CodeLatLng");
geocoder = new google.maps.Geocoder();
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
var address = results[1].formatted_address;
address = address.split(",");
var area = address[0];
var county = address[1];
var country = address[2];
jQuery("#frmDirectionsYou").val(address);
jQuery("#get_directions").trigger("click");
}
}
});
}