﻿var pointLatSpan = 51.922090773886175;
var pointLngSpan = -0.4804372787475586;
var pointPos = new GLatLng(pointLatSpan,pointLngSpan);

var pointMapCenterLatSpan = 51.92420810668249;
var pointMapCenterLngSpan = -0.47781944274902344;
var pointMapCenterPos = new GLatLng(pointMapCenterLatSpan,pointMapCenterLngSpan);

var pointRouteLatSpan = 51.922090773886175;
var pointRouteLngSpan = -0.4804372787475586;
var pointRoutePos = new GLatLng(pointRouteLatSpan,pointRouteLngSpan);

var startMapType =  G_NORMAL_MAP;
var smallLogoUrl = '/en/images_logo/logo_rdax_120x88.gif';
var markerIconGifUrl = '/en/z_system_images/elster-dot.gif';
var pointAddresseHTML = 'Elster Metering Limited<BR>130 Camford Way <BR>Sundon Park <BR>Luton <BR>Bedfordshire LU3 3AN';
var labelGetDirectionsHTML = '<b>Get directions</b> ';
var labelToHere = 'To here';
var labelFromHere = 'From here';
var labelAnchorBack = 'Back';
var labelStartAddress = 'Start address';
var labelEndAddress = 'End Address';
var labelButtonGo = 'Go';
var languageId = 'en';
var pointShortName = 'Elster Metering Ltd.';
var directionTitle = 'Route';

var startZoom = 15;
var startZoomString = "14";
if ( 140 != 0 ) startZoom = (startZoomString * 1);

var blueIcon = new GIcon(G_DEFAULT_ICON);
blueIcon.image = "/en/z_system_images/elster-dot.png";
blueIcon.iconSize = new GSize(20, 32);
markerOptions = { draggable: false ,icon:blueIcon};    

var map;
var gdir;
var geocoder = null;
var addressMarker;
var pointMarker = null;
var controls = new Array();
var gm2_sv0_style = 'display:block';
var gm2_sv1_style = 'display:none';
var gm2_sv2_style = 'display:none';
var gm2_sv3_style = 'display:none';
var pointHtml = gm2_get_pointHtml(gm2_sv0_style,gm2_sv1_style,gm2_sv2_style,gm2_sv3_style);
var gm2_route_formularFrom = null;
var gm2_route_formularTo = null;
var last_search_typ = 0;
 
function gm2_showVar0(){
   pointMarker.closeInfoWindow();
   gm2_sv0_style = 'display:block';
   gm2_sv1_style = 'display:none';
   gm2_sv2_style = 'display:none';
   gm2_sv3_style = 'display:none';
   pointMarker.openInfoWindowHtml(gm2_get_pointHtml(gm2_sv0_style,gm2_sv1_style,gm2_sv2_style,gm2_sv3_style));
}
function gm2_showVar1(){
   pointMarker.closeInfoWindow();
   gm2_sv0_style = 'display:none';
   gm2_sv1_style = 'display:block';
   gm2_sv2_style = 'display:none';
   gm2_sv3_style = 'display:none';
   pointMarker.openInfoWindowHtml(gm2_get_pointHtml(gm2_sv0_style,gm2_sv1_style,gm2_sv2_style,gm2_sv3_style));  
}
function gm2_showVar2(){
   pointMarker.closeInfoWindow();
   gm2_sv0_style = 'display:none';
   gm2_sv1_style = 'display:none';
   gm2_sv2_style = 'display:block';
   gm2_sv3_style = 'display:none';
   pointMarker.openInfoWindowHtml(gm2_get_pointHtml(gm2_sv0_style,gm2_sv1_style,gm2_sv2_style,gm2_sv3_style));  
}
function gm2_get_pointHtml(sv0_style,sv1_style,sv2_style,sv3_style) {
  var pointHtml = '';
  pointHtml    = '<div id="gm2_infowindow">';
  pointHtml    = pointHtml + "<img src=\"" + smallLogoUrl + "\"><div id=\"text\">"+pointAddresseHTML+"</div>";
  
  pointHtml    = pointHtml + '<p class="clr">&nbsp;</p><div id="routesearchbox">';
  
  pointHtml    = pointHtml + '<div id="search_menu" style="' + sv0_style + '">';
  pointHtml    = pointHtml + labelGetDirectionsHTML +'<a href="javascript:void(0)" onclick="gm2_showVar1()">' + labelToHere + '</a> - <a href="javascript:void(0)" onclick="gm2_showVar2()">' + labelFromHere + '</a>';
  pointHtml    = pointHtml + '</div>'; //search_menu
  pointHtml    = pointHtml + '<div id="search_var1" style="' + sv1_style + '">';
  pointHtml    = pointHtml + '<form name="gm2_route_formular" action="" onsubmit="last_search_typ = 1;gm2_saveValues(this.from.value, this.to.value);gm2_setDirections(this.from.value, this.to.value, \'' + languageId + '\'); return false;" method="post">';
  pointHtml    = pointHtml + labelGetDirectionsHTML + labelToHere + ' - <a href="javascript:void(0)" onclick="gm2_showVar2()">' + labelFromHere + '</a><br /><span class="fieldlabel">' + labelStartAddress + '</span><br />';
  //pointHtml    = pointHtml + '<input name="from" type="text" class="fieldsearch" value="'+gm2_getFromFormValue()+'" /><input type="submit" value="' + labelButtonGo + '" />';
  pointHtml    = pointHtml + '<table><tr><td><input name="from" type="text" class="fieldsearch" value="'+gm2_getFromFormValue()+'" /></td><td class="button"><input type="submit" value="' + labelButtonGo + '" /></td></tr></table>';
  pointHtml    = pointHtml + '<input name="to" type="hidden" value="' + pointShortName + ' @'+pointRouteLatSpan+','+pointRouteLngSpan+'" />';
  pointHtml    = pointHtml + '<input name="hl" type="hidden" value="' + languageId + '" />';
  pointHtml    = pointHtml + '</form>';
  pointHtml    = pointHtml + '<a href="javascript:void(0)" class="wizardpop" onclick="gm2_showVar0()">&laquo; ' + labelAnchorBack + '</a>';
  pointHtml    = pointHtml + '</div>'; //search_v1
  pointHtml    = pointHtml + '<div id="search_var2" style="' + sv2_style + '">';
  pointHtml    = pointHtml + '<form name="gm2_route_formular" action="#" onsubmit="last_search_typ = 2;gm2_saveValues(this.from.value, this.to.value); gm2_setDirections(this.from.value, this.to.value, \'' + languageId + '\'); return false"  method="post">';
  pointHtml    = pointHtml + labelGetDirectionsHTML + '<a href="javascript:void(0)" onclick="gm2_showVar1()">' + labelToHere + '</a> - ' + labelFromHere + '<br /><span class="fieldlabel">' + labelEndAddress + '</span><br />';
  //pointHtml    = pointHtml + '<input name="to" type="text" class="fieldsearch" value="'+gm2_getToFormValue()+'" /><input type="submit" value="' + labelButtonGo + '" />';
  pointHtml    = pointHtml + '<table><tr><td><input name="to" type="text" class="fieldsearch" value="'+gm2_getToFormValue()+'" /></td><td class="button"><input type="submit" value="' + labelButtonGo + '" /></td></tr></table>';
  pointHtml    = pointHtml + '<input name="from" type="hidden" value="' + pointShortName + ' @'+pointRouteLatSpan+','+pointRouteLngSpan+'" />';
  pointHtml    = pointHtml + '<input name="hl" type="hidden" value="' + languageId +'" />';
  pointHtml    = pointHtml + '</form>';
  pointHtml    = pointHtml + '<a href="javascript:void(0)" class="wizardpop" onclick="gm2_showVar0()">&laquo; ' + labelAnchorBack + '</a>';
  pointHtml    = pointHtml + '</div>'; //search_v2
  
  pointHtml    = pointHtml + '</div>'; //routesearchbox
  pointHtml    = pointHtml + '</div>'; //Infowindow
  
  return pointHtml;
}

function gm2_initialize() {
  if (GBrowserIsCompatible()) {      
    map = new GMap2(document.getElementById("map_canvas"));
    map.setCenter(pointMapCenterPos, startZoom);
    map.setMapType(startMapType);
    gm2_addControl();
    gm2_showPointMarker(map);
    
    //Route
    gdir = new GDirections(map, document.getElementById("directions"));
    GEvent.addListener(gdir, "load", gm2_onGDirectionsLoad);
    GEvent.addListener(gdir, "error", gm2_handleErrors);
  }
}
function gm2_addControl() {
  controls[controls.length] = new GSmallMapControl();
  map.addControl(controls[controls.length-1]);
  controls[controls.length] = new GMapTypeControl();
  map.addControl(controls[controls.length-1]);
}
function gm2_showPointMarker(map){
  pointMarker = new GMarker(new GLatLng(pointLatSpan,pointLngSpan),markerOptions);
  map.addOverlay(pointMarker);
  GEvent.addListener(pointMarker, "click", function() {
      pointMarker.openInfoWindowHtml(pointHtml);
      if (last_search_typ == 1) gm2_showVar1();
      if (last_search_typ == 2) gm2_showVar2();
  });
  pointMarker.openInfoWindowHtml(pointHtml);
}
    
//Routeplaner
function gm2_setDirections(fromAddress, toAddress, locale) {
  pointMarker.closeInfoWindow();
  gdir.load("from: " + fromAddress + " to: " + toAddress,
            { "locale": locale });
}
function gm2_addPolyLine(){
  var polyline = new GPolyline([
            new GLatLng(43.580227642275176, 3.9383411407470703),
            new GLatLng(43.57932607494175, 3.939467668533325),
            new GLatLng(43.57909290877903, 3.9399290084838867),
            new GLatLng(43.57906181988913, 3.9408624172210693)
        ], "#0000ff", 5);
        map.addOverlay(polyline);          
}
function gm2_handleErrors(){
   document.getElementById('directions').style.display = 'none';
   gm2_route_formularFrom = null;
   gm2_route_formularTo = null;
if (gdir.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: " + gdir.getStatus().code);
else if (gdir.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: " + gdir.getStatus().code);
else if (gdir.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: " + gdir.getStatus().code);
//   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
  
else if (gdir.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: " + gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
  alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
 
else alert("An unknown error occurred.");
}
function gm2_onGDirectionsLoad(){
  var html = '<div id="route_results" class="gm2_block_header">' + directionTitle + '</div>';
      html = html + '<div id="to_point_marker"><table><tr><td class="cell1"><a href="javascript:void(0);" onclick="gm2_goToPointMarker();"><img src="' + markerIconGifUrl + '" border="0" alt=""/></a></td><td class="cell2"><a href="javascript:void(0);" onclick="gm2_goToPointMarker();">' + pointShortName + '</a></td></tr></table></div>'   
  document.getElementById('directions').innerHTML = html;
  document.getElementById('directions').style.display = 'block';
  
  gm2_addPolyLine();
  
}
function gm2_goToPointMarker(){
    map.setCenter(pointMapCenterPos, 15);
    pointMarker.openInfoWindowHtml(pointHtml);
}
//Fuer Drucken-Function
function openwindow(t_url, sbars, width, height, bnav) {
    var wx, wy;
    if (bnav == 1) {
        bnav = "menubar=yes,toolbar=yes,";
    } else {
        bnav = "";
    }
    if (sbars == 1) {
        scrollb = "scrollbars=yes,";
    } else {
        scrollb = "";
    }
    if (screen.availWidth >= width) {
        wx = (screen.availWidth - width) / 2;
    } else {
        wx = 0;
    }
    if (screen.availHeight >= height) {
        wy = (screen.availHeight - height) / 2;
    } else {
        wy = 0;
    }
    return window.open(t_url, '', scrollb + bnav + 'width=' + width + ',height=' + height + ',left=' + wx + ',top=' + wy + ', resizable=yes');
}        
function gm2_printPage(){
    if ((gm2_route_formularFrom != null) && (gm2_route_formularTo != null)) {
        openwindow("http://maps.google.com/maps?f=d&saddr="+gm2_route_formularFrom+"&daddr="+gm2_route_formularTo+"&hl=" + languageId + "&geocode=&mra=ls&sll=&sspn=1.271431,2.471924&ie=UTF8&z=8&pw=2", 1, 800, 800, 0);
    } else {
        alert("A directions request could not be successfully parsed.");
    }
}
function gm2_saveValues(from,to){
  gm2_route_formularFrom = from;
  gm2_route_formularTo = to;
}
function gm2_getFromFormValue(){
    if (gm2_route_formularFrom == null) return '';
    if (last_search_typ == 1) return gm2_route_formularFrom.replace('"', '' );
    return '';
}
function gm2_getToFormValue(){
    if (gm2_route_formularTo == null) return '';
    if (last_search_typ == 2) return gm2_route_formularTo.replace('"', '' );
    return '';
}
