
function load()
{
  if (GBrowserIsCompatible()) {
    geocoder = new GClientGeocoder();
    map = new GMap2(document.getElementById('map'));

setCountry();
    map.setMapType(G_PHYSICAL_MAP);

    var latlng = new GLatLng(0,0);

    map.setCenter(latlng, 1);


map.setUIToDefault();
    addressInputInit();
    searchMethodInit();


  }

}

function addressInputInit()
{
  if (document.getElementById('addressInput').value == '' || document.getElementById('addressInput').value==translate('address_box_hint')) {
    document.getElementById('addressInput').value=translate('address_box_hint');
    document.getElementById('addressInput').style.color='gray';
  }
}

function searchMethodInit()
{

  if (document.getElementById('radius_radio').checked && document.getElementById('addressInput').value != '' && document.getElementById('addressInput').value != translate('address_box_hint')) {
    enableRadiusSearchMethod();
  } else {
    enableCountrySearchMethod();
  }
}


function enableRadiusSearchMethod()
{
  document.getElementById('radius_radio').checked=true;
  document.getElementById('radius_search').style.color='black';
  document.getElementById('radiusSelect').disabled=false;
  document.getElementById('addressInput').disabled=false;
  document.getElementById('locationSearchButton').disabled=false;
  document.getElementById('country_search').style.color='#5e5e5e';
  document.getElementById('countryInput').disabled=true;

  if (document.getElementById('addressInput').value != '' && document.getElementById('addressInput').value != translate('address_box_hint')) {
    searchLocations() ;
  }
  //  else enableCountrySearchMethod();

}

function enableCountrySearchMethod()
{
  document.getElementById('country_radio').checked=true;
  document.getElementById('radius_search').style.color='#5e5e5e';
  document.getElementById('radiusSelect').disabled=true;
  document.getElementById('addressInput').disabled=true;
  document.getElementById('locationSearchButton').disabled=true;
  document.getElementById('country_search').style.color='black';
  document.getElementById('countryInput').disabled=false;

  searchLocationsNear('',document.getElementById('countryInput').value);
}

function setCountry(){
	domainArr = window.location.hostname.replace('www.','').replace('.co.','.').replace('.com.','.').split('.');
	domainExt = domainArr[1].toUpperCase();
	//alert(domainExt);
	document.getElementById('countryInput').value = domainExt;
	searchLocationsNear('',document.getElementById('countryInput').value);
}

function searchLocations()
{
  var address = document.getElementById('addressInput').value;
  geocoder.getLatLng(address, function(latlng) {
    if (!latlng) {
      alert(address + ' ' + translate('not_found'));
    } else {
      searchLocationsNear(latlng,'');
    }
  }
  );
}

function searchLocationsNear(center,countryAbbrev)
{
  var radius = document.getElementById('radiusSelect').value;

  var filter =
  '&all_locations_radio=' + document.getElementById('all_locations_radio').checked
  + '&headquarters_radio=' + document.getElementById('headquarters_radio').checked
  + '&importer_radio=' + document.getElementById('importer_radio').checked
  + '&wholesaler_radio=' + document.getElementById('wholesaler_radio').checked
  + '&dealer_radio=' + document.getElementById('dealer_radio').checked
  + '&applicator_radio=' + document.getElementById('applicator_radio').checked;

  if (center != '') {
    var searchUrl = '/scripts/phpsqlsearch_genxml.php?lat=' + center.lat() + '&lng=' + center.lng() + '&radius=' + radius + filter;
  }
  if (countryAbbrev != '') {
    var searchUrl = '/scripts/phpsqlsearch_genxml.php?countryAbbrev=' + countryAbbrev + filter;
  }

  // document.write(searchUrl);
  GDownloadUrl(searchUrl, function(data) {
    var xml = GXml.parse(data);
    var markers = xml.documentElement.getElementsByTagName('marker');
    map.clearOverlays();

    var sidebar = document.getElementById('sidebar');
    sidebar.innerHTML = '';
    if (markers.length == 0) {
      sidebar.innerHTML = translate('no_results_found')+'.';
      map.setCenter(new GLatLng(52, 10), 3);
      return;
    }

    var bounds = new GLatLngBounds();
    for (var i = 0; i < markers.length; i++) {

    /*
      var name = markers[i].getAttribute('name');
      var description = markers[i].getAttribute('description');

      var address = markers[i].getAttribute('address');
      var postalcode = markers[i].getAttribute('postalcode');
      var city = markers[i].getAttribute('city');
      var country = markers[i].getAttribute('country');

      var website = markers[i].getAttribute('website');
      var email = markers[i].getAttribute('email');
      var phone = markers[i].getAttribute('phone');
      var fax = markers[i].getAttribute('fax');

      var headquarters_check = markers[i].getAttribute('headquarters_check');
      var importer_check = markers[i].getAttribute('importer_check');
      var wholesaler_check = markers[i].getAttribute('wholesaler_check');
      var dealer_check = markers[i].getAttribute('dealer_check');
      var applicator_check = markers[i].getAttribute('applicator_check');
      */

    //  var iconCode = ''+headquarters_check+importer_check+wholesaler_check+dealer_check+applicator_check+'';
      var icon = '';

      if (markers[i].getAttribute('headquarters_check') == 1) {
        icon = 'headquarters';
      } else if (markers[i].getAttribute('importer_check') == 1) {
        icon = 'importer';
      } else if (markers[i].getAttribute('wholesaler_check') == 1) {
        icon = 'wholesaler';
      } else if (markers[i].getAttribute('dealer_check') == 1) {
        icon = 'dealer';
      } else if (markers[i].getAttribute('applicator_check') == 1) {
        icon = 'applicator';
      }

      var typeArray = new Array();
      var j = 0;

     // if (headquarters_check == 1) {
     //   typeArray[j] = translate('headquarters');
     //   j++;
     // }
    //  if (importer_check == 1) {
    //    typeArray[j] = translate('importer');
    //    j++;
    //  }
      if (markers[i].getAttribute('wholesaler_check') == 1) {
        typeArray[j] = translate('wholesaler');
        j++;
      }
      if (markers[i].getAttribute('dealer_check') == 1) {
        typeArray[j] = translate('dealer');
        j++;
      }
      if (markers[i].getAttribute('applicator_check') == 1) {
        typeArray[j] = translate('applicator');
        j++;
      }

      var type = typeArray.join(', ');
      type = type.substring(0,1).toUpperCase() + type.substring(1);
      if (typeArray.length > 1) {
        type = type.replace(type.substr(type.lastIndexOf(', ')),' '+translate('and')+' '+type.substr(type.lastIndexOf(', ')+2));
      }

      var distance = parseFloat(markers[i].getAttribute('distance'));
      var point = new GLatLng(parseFloat(markers[i].getAttribute('lat')),
      parseFloat(markers[i].getAttribute('lng')));

      var marker = createMarker(point, icon, type, markers[i]);
      map.addOverlay(marker);

      var sidebarEntry = createSidebarEntry(marker, icon, type, distance, markers[i]);
      sidebar.appendChild(sidebarEntry);
      bounds.extend(point);
    }
    map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
  }
  );
}

function formatOpeningHours(startHour, startMinute,stopHour,stopMinute){

  if (startMinute.length == 1) startMinute += 0;
  if (stopMinute.length == 1) stopMinute += 0;
  return startHour + ':' + startMinute + ' - ' + stopHour + ':' + stopMinute;
}

function createMarker(point, iconCode, type, markers)
{
  var marker = new GMarker(point, customIcons[iconCode]);
  var html = '<table class="balloonContent">';

  html += '<tr><th colspan="4" class="title">' + markers.getAttribute('name') + '</th></tr>';
  html += '<tr><td colspan="4">';
  if (markers.getAttribute('headquarters_check') == 1) html += '<i>'+translate('Headquarters')+' in '+ markers.getAttribute('city') +'</i><br/>';
  if (markers.getAttribute('importer_check') == 1) html += '<i>'+translate('Importer')+' '+translate('for')+' '+ countryName(markers.getAttribute('country').toUpperCase()) +'</i><br/>';
  if (type != '') html += '' + type + ' in ' + markers.getAttribute('city');
  else if (markers.getAttribute('headquarters_check') != 1) html += '' + translate('located_in') + ' ' + markers.getAttribute('city');
  if (markers.getAttribute('description')) html += '<br/>'+markers.getAttribute('description').replace('. ','.<br/>') +'<br/>'
  html += '</td></tr>';

  // contactgegevens
  html += '<tr><th colspan="4">'+translate('Contact_information')+'</th></tr>'
  + '<tr style="vertical-align:bottom"><td colspan="2">'
  + markers.getAttribute('address')
  + '<br/>' + markers.getAttribute('postalcode') + ' ' + markers.getAttribute('city') + ' (' + markers.getAttribute('country').toUpperCase() + ')'
  + '<br/><a href="http://' + markers.getAttribute('website') + '" target="_blank">'+markers.getAttribute('website')+'</a>';
  if (markers.getAttribute('headquarters_check') == 1) html += '<br/>&nbsp;';
  html += '</td><td colspan="2">';
  if(markers.getAttribute('phone') != '') html += 'tel.: ' + markers.getAttribute('phone');
  if(markers.getAttribute('phone2') != '') html += ' / ' + markers.getAttribute('phone2');
  if(markers.getAttribute('fax') != '') html += '<br/>fax.: ' + markers.getAttribute('fax');
  if(markers.getAttribute('email') != '') html += '<br/><a href="mailto:' + markers.getAttribute('email') + '">' + markers.getAttribute('email') + '</a>';
  if (markers.getAttribute('headquarters_check') == 1) html += '<br/><a href="contact.html">Contactformulier</a>';
  html += '</td></tr>';

  // routebeschrijving
  html += '<tr><th colspan="4">'+translate('Directions')+'</th></tr>'
  html += '<tr style="vertical-align:bottom"><td colspan="4">';
  html += '<form action="http://maps.google.com/maps" target="_blank">';
  html += '<input type="hidden" name="f" value="d" />';
  html += '<input type="hidden" name="source" value="s_d" />';
  html += '<label for="destination">'+translate('vertrekpunt')+': </label>';
  html += '<input name="saddr" id="destination" value="" length="30" />';
  html += '<input type="hidden" name="daddr" id="destination" value="'+ markers.getAttribute('address') + ' ' + markers.getAttribute('postalcode') + ' ' + markers.getAttribute('city') + ', ' + markers.getAttribute('country') + '" />';
  html += '<input type="submit" value="'+translate('get_directions')+'" />';
  html += '</form>';
  if (markers.getAttribute('headquarters_check') == 1) html += '<br/><strong>Bereikbaarheid via het OV:</strong><br/>Neem bus 48 vanaf Amsterdam CS of Station Sloterdijk.<br/>Voor vertrektijden, zie <a href="http://www.gvb.nl" target="_blank">GVB</a> of <a href="http://www.9292ov.nl" target="_blank">9292ov</a>.<br/><a target="_blank" title="tierrafino ov routebeschrijving" href="/pdf/tierrafino_ov_routebeschrijving.pdf">Routebeschrijving vanaf bushalte Oostzaanstraat</a> (pdf).';
  html += '</td></tr>';


  // openingstijden
  if (markers.getAttribute('opening_hours_monday_check') == 1
 || markers.getAttribute('opening_hours_tuesday_check') == 1
 || markers.getAttribute('opening_hours_wednesday_check') == 1
 || markers.getAttribute('opening_hours_thursday_check') == 1
 || markers.getAttribute('opening_hours_friday_check') == 1
 || markers.getAttribute('opening_hours_saturday_check') == 1
 || markers.getAttribute('opening_hours_sunday_check') == 1
) {
    html += '<tr><th colspan="4">'+translate('Opening_hours')+'</th></tr>';
    html += '<tr class="opening_hours_tr"><td>';


    if (markers.getAttribute('opening_hours_monday_check') == 1) {
        html += translate('monday') + ':<br/>';
    }

    if (markers.getAttribute('opening_hours_tuesday_check') == 1) {
        html += translate('tuesday') + ':<br/>';
    }

    if (markers.getAttribute('opening_hours_wednesday_check') == 1) {
        html += translate('wednesday') + ':';
    }

    html += '</td><td>';

    if (markers.getAttribute('opening_hours_monday_check') == 1) {
        html += formatOpeningHours(markers.getAttribute('opening_hours_monday_start_hour'), markers.getAttribute('opening_hours_monday_start_minute'), markers.getAttribute('opening_hours_monday_stop_hour'),
        markers.getAttribute('opening_hours_monday_stop_minute'))
        + '<br/>';
    }

    if (markers.getAttribute('opening_hours_tuesday_check') == 1) {
        html += formatOpeningHours(markers.getAttribute('opening_hours_tuesday_start_hour'), markers.getAttribute('opening_hours_tuesday_start_minute'), markers.getAttribute('opening_hours_tuesday_stop_hour'),
        markers.getAttribute('opening_hours_tuesday_stop_minute'))
        + '<br/>';
    }

    if (markers.getAttribute('opening_hours_wednesday_check') == 1) {
        html += formatOpeningHours(markers.getAttribute('opening_hours_wednesday_start_hour'), markers.getAttribute('opening_hours_wednesday_start_minute'), markers.getAttribute('opening_hours_wednesday_stop_hour'),
        markers.getAttribute('opening_hours_wednesday_stop_minute'));
    }

    html += '</td><td style="padding-left:18px; _padding-left:0;">';

    if (markers.getAttribute('opening_hours_thursday_check') == 1) {
        html += translate('thursday') + ':<br/>';
    }


    if (markers.getAttribute('opening_hours_friday_check') == 1) {
        html += translate('friday') + ':<br/>';
    }


    if (markers.getAttribute('opening_hours_saturday_check') == 1) {
        html += translate('saturday') + ':<br/>';
    }


    if (markers.getAttribute('opening_hours_sunday_check') == 1) {
        html += translate('sunday') + ':';
    }

    html += '</td><td>';

    if (markers.getAttribute('opening_hours_thursday_check') == 1) {
        html += formatOpeningHours(markers.getAttribute('opening_hours_thursday_start_hour'), markers.getAttribute('opening_hours_thursday_start_minute'), markers.getAttribute('opening_hours_thursday_stop_hour'),
        markers.getAttribute('opening_hours_thursday_stop_minute'))
        + '<br/>';
    }


    if (markers.getAttribute('opening_hours_friday_check') == 1) {
        html += formatOpeningHours(markers.getAttribute('opening_hours_friday_start_hour'), markers.getAttribute('opening_hours_friday_start_minute'), markers.getAttribute('opening_hours_friday_stop_hour'),
        markers.getAttribute('opening_hours_friday_stop_minute'))
        + '<br/>';
    }


    if (markers.getAttribute('opening_hours_saturday_check') == 1) {
        html += formatOpeningHours(markers.getAttribute('opening_hours_saturday_start_hour'), markers.getAttribute('opening_hours_saturday_start_minute'), markers.getAttribute('opening_hours_saturday_stop_hour'),
        markers.getAttribute('opening_hours_saturday_stop_minute'))
        + '<br/>';
    }


    if (markers.getAttribute('opening_hours_sunday_check') == 1) {
        html += formatOpeningHours(markers.getAttribute('opening_hours_sunday_start_hour'), markers.getAttribute('opening_hours_sunday_start_minute'), markers.getAttribute('opening_hours_sunday_stop_hour'),
        markers.getAttribute('opening_hours_sunday_stop_minute'));
    }
    html += '</td></tr>';
}


  html += '</table>';

  GEvent.addListener(marker, 'click', function() {
    marker.openInfoWindowHtml(html);
  }
  );
  return marker;
}

function createSidebarEntry(marker, icon, type, distance, markers)
{
  var div = document.createElement('div');
  var html = '<b>' + markers.getAttribute('name') + '</b>';
  if (distance >= 0) html += ' (' + distance.toFixed(1) + ' km)';

  if (markers.getAttribute('headquarters_check') == 1) html += '<br/><i>'+translate('Headquarters')+' in '+ markers.getAttribute('city') +'</i>';
  if (markers.getAttribute('importer_check') == 1) html += '<br/><i>'+translate('Importer')+' '+translate('for')+' '+ countryName(markers.getAttribute('country').toUpperCase()) +'</i>';
  if (type != '') html += '<br/>' + type + ' in ' + markers.getAttribute('city');
  else if (markers.getAttribute('headquarters_check') != 1) html += '<br/>' + translate('located_in') + ' ' + markers.getAttribute('city');


  div.innerHTML = html;
  div.style.cursor = 'pointer';
  div.style.marginBottom = '3px';
  div.style.padding = '2px';
  GEvent.addDomListener(div, 'click', function() {
    GEvent.trigger(marker, 'click');
  }
  );
  GEvent.addDomListener(div, 'mouseover', function() {
    div.style.backgroundColor = '#eee';
  }
  );
  GEvent.addDomListener(div, 'mouseout', function() {
    div.style.backgroundColor = '';
  }
  );
  return div;
}
