function init() {
  if ($('map')) {
    $('calcRoute').show();
    GLoad();
    window.onunload = GUnload;
  }
  
  if ($('clientMap')) {
    GLoadClients();
    window.onunload = GUnload;
  }

  if ($('puzzle')) {
    var path = '/images/partner';
    $('puzzle_img').setStyle({'float':'left'}).insert({after:new Element('div',{id:'infoTxt',style:'float: left; width: 260px; padding-left: 10px;'})});
    $('puzzle').select('area').each(function(area){
      if ($(area.title)) {
        var p = $(area.title).next('p');
        $(area.title).remove();
        $('infoTxt').insert({bottom:p.hide()});
        $(area).observe('mouseover',function(){$('puzzle_img').src=path+'-'+this.img+'.jpg';$(this.p).show();}.bind({img:area.alt.toLowerCase(),p:p}));
        $(area).observe('mouseout',function(){$('puzzle_img').src=path+'.jpg';$(this).hide();}.bind(p));
      } else {
        $(area).observe('mouseover',function(){$('puzzle_img').src=path+'-'+this+'.jpg';}.bind(area.alt.toLowerCase()));
        $(area).observe('mouseout',function(){$('puzzle_img').src=path+'.jpg';});
      }
    });
  }


}


function showCountry(id, title) {
  if ($('customers-'+id)) {
    myLightWindow.activateWindow({
        href: '#customers-'+id,
        title: title,
        type: 'inline',
      width: 600,
      height: 400,
      onBlur: function () {$('flash').show();}
    });
    $('flash').hide();
    new ProtoFlow($('lightwindow_contents').down('div'),{slider:true,captions:true,useReflection:true,enableOnClickScroll:true});
  } else {
    if (language=='de') {
      alert('Fuer '+title+' wurden noch keine Kunden hinterlegt!');
    } else alert('No customers for '+title+' now!');
  }
}

function scrollDown(b) {
  if (b==1) {
    gScrollBar.setSizes();
    gScrollBar.scrollTo(250);
  } else scrollDown.defer(1);
}

function createMarker(point,html,estyle) {
    var marker = new GMarker(point);
    // ========== Open a *new* EWindow instead of a Google Info Window ==========
    var ew = new EWindow(map, estyle);      
    map.addOverlay(ew);
    ew.openOnMarker(marker,html);
    return marker;
}

function createNewClient(map,img,lat,lon) {
    // Create our "tiny" marker icon
    var blueIcon = new GIcon(G_DEFAULT_ICON);
    blueIcon.image = img;
    blueIcon.iconSize = new GSize(50, 33);
    blueIcon.iconAnchor = new GPoint(6, 20);
    blueIcon.infoWindowAnchor = new GPoint(5, 1);
    
    map.setCenter(new GLatLng(52.724647, 8.285009), 5);
    map.setMapType(G_HYBRID_MAP);
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    
    var point = new GLatLng(lat,lon);
    var marker = new GMarker(point,{ icon:blueIcon });
    /*var ew = new EWindow(map, E_STYLE_1);
    map.addOverlay(ew);
    ew.openOnMarker(marker,'<img src="/images/clients/big/eon-bayern.jpg">');*/
    map.addOverlay(marker);
}

function GLoadClients() {
  if (GBrowserIsCompatible()) {
      
      var map = new GMap2(document.getElementById("clientMap"));
      
      createNewClient(map,"/images/clients/big/eon-bayern.jpg",48.07807894349862, 11.513671875);
      createNewClient(map,"/images/clients/big/exxon.jpg",52.3755991766591, 9.732513427734375);
      createNewClient(map,"/images/clients/big/daldrup.jpg",51.7905891, 7.6059268);
      createNewClient(map,"/images/clients/big/eon-rgb.jpg",51.4580686, 7.0147614);
      createNewClient(map,"/images/clients/big/erdwerk.jpg",48.1391265, 11.5801863);
      createNewClient(map,"/images/clients/big/eum.jpg",50.31658, 11.913471);
      createNewClient(map,"/images/clients/big/fritz.jpg",48.4907823, 9.3996776);
      createNewClient(map,"/images/clients/big/geomechanik.jpg",46.726362, 14.094063);
      createNewClient(map,"/images/clients/big/itag.jpg",52.6219201, 10.0785804);
      createNewClient(map,"/images/clients/big/kazduco.jpg",48.019573, 66.923684);
      createNewClient(map,"/images/clients/big/leaders.jpg",24.476431, 54.37051);
      createNewClient(map,"/images/clients/big/ochs.jpg",49.45052, 11.08048);
      createNewClient(map,"/images/clients/big/omv.jpg",48.2092062, 16.3727778);
      createNewClient(map,"/images/clients/big/wintershall.jpg",51.318928, 9.49601);
      createNewClient(map,"/images/clients/big/world-oil-tools.jpg",53.9332706, -116.5765035);
  }
}

function GLoad() {
  if (GBrowserIsCompatible()) {
    gMap = new GMap2(document.getElementById("map"));
    gDirection = new GDirections(gMap, document.getElementById("route"));
    GEvent.addListener(gDirection, "error", handleErrors);
    GEvent.addListener(gDirection, "load",  scrollDown);
    gMap.setCenter(new GLatLng(52.724647, 8.285009), 13);
    gMap.setMapType(G_HYBRID_MAP);
    gMap.addControl(new GSmallMapControl());
    gMap.addControl(new GMapTypeControl());
    gMarke = new GMarker(new GLatLng(52.721297603714255, 8.267032206058502));
    var html = '<img src="/images/logo.jpg" width="223" height="40" alt="GOT - German Oil Tools" /><br /><strong style="color: #000; border-top: 1px solid #000; display: block">GOT - German Oil Tools GmbH</strong>Vechtaer Marsch 5<br />D-49377 Vechta';
    GEvent.addListener(gMarke, "click", function(){gMarke.openInfoWindowHtml(html)});
    gMap.addOverlay(gMarke);
    gMarke.openInfoWindowHtml(html);
  }
}

function GRoute_beta() {
  gDirection.load("from: "+$F('from')+" to:DE, Vechta, Vechtaer Marsch 5", {"locale": $('locale')?$F('locale'):"de"});
  return false;
}

function handleErrors(){
  switch(language) {
    case 'de':
      if (gDirection.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
        alert("Die von Ihnen eingegebene Adresse wurde nicht gefunden. Versuchen Sie es z.B. mit dem Ort und der anliegenden Autobahn.");
      else if (gDirection.getStatus().code == G_GEO_SERVER_ERROR)
        alert("Es ist ein Fehler bei Google Maps aufgetreten. Bitte nehmen Sie Kontakt über das Kontaktformular mit uns auf, wir geben Ihnen gerne Auskunft.");
      else if (gDirection.getStatus().code == G_GEO_MISSING_QUERY)
        alert("Bitte tragen Sie Ihre Adresse ein.");
      else if (gDirection.getStatus().code == G_GEO_BAD_KEY)
        alert("Es ist ein Fehler bei Google Maps aufgetreten. Bitte nehmen Sie Kontakt über das Kontaktformular mit uns auf, wir geben Ihnen gerne Auskunft.");
      else if (gDirection.getStatus().code == G_GEO_BAD_REQUEST)
        alert("Es ist ein Fehler bei Google Maps aufgetreten. Bitte nehmen Sie Kontakt über das Kontaktformular mit uns auf, wir geben Ihnen gerne Auskunft.");
      else alert("Es ist ein Fehler bei Google Maps aufgetreten. Bitte nehmen Sie Kontakt über das Kontaktformular mit uns auf, wir geben Ihnen gerne Auskunft.");
      break;
    default:
      if (gDirection.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
        alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gDirection.getStatus().code);
      else if (gDirection.getStatus().code == G_GEO_SERVER_ERROR)
        alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gDirection.getStatus().code);
      else if (gDirection.getStatus().code == G_GEO_MISSING_QUERY)
        alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gDirection.getStatus().code);
      else if (gDirection.getStatus().code == G_GEO_BAD_KEY)
        alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gDirection.getStatus().code);
      else if (gDirection.getStatus().code == G_GEO_BAD_REQUEST)
        alert("A directions request could not be successfully parsed.\n Error code: " + gDirection.getStatus().code);
      else alert("An unknown error occurred.");
  }
}

function openHistory(part) {
  Modalbox.show($('tpl_'+part).innerHTML, {title: ' <img src="/images/logo.jpg" alt="GOT - German Oil Tools" />', width: 400});
  return false;
}

var language = 'de';
var gScrollBar = null;
var gMap = null;
var gDirection = null;
var gMarke = null;

window.onload = init;
