  var map;
  var totalLocations = 0;
  var markers_array = new Array();
  var MapBounds = new GLatLngBounds();
  // ====== Array for decoding the failure codes ======
  var reasons=[];
  reasons[G_GEO_SUCCESS]            = "Success";
  reasons[G_GEO_MISSING_ADDRESS]    = "Missing Address: The address was either missing or had no value.";
  reasons[G_GEO_UNKNOWN_ADDRESS]    = "Unknown Address:  No corresponding geographic location could be found for the specified address.";
  reasons[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
  reasons[G_GEO_BAD_KEY]            = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
  reasons[G_GEO_TOO_MANY_QUERIES]   = "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
  reasons[G_GEO_SERVER_ERROR]       = "Server error: The geocoding request could not be successfully processed.";
  reasons[403]                      = "Error 403: Probably an incorrect error caused by a bug in the handling of invalid JSON.";
  var listings = new Array();
  var gdir, directions_overlay, directions_locations;
  var directions_status = false;
  var default_center = new GLatLng(37.09024, -95.712891);

  var defalut_search_value = "Zip or City";

  function onloadInit(){
    $("#search_input").val(defalut_search_value);
    $("#search_input").focus(function() {
      if( this.value == defalut_search_value ) {
        this.value = "";
      }
    }).blur(function() {
      if( !this.value.length ) {
        this.value = defalut_search_value;
      }
    });
    $("#street_view").dialog({
      autoOpen: false,
      bgiframe: true,
      height: 400,
      width: 410,
      modal: true,
      resizable: false,
      buttons: {
        Cancel: function() {
          $(this).dialog('close');
        }
      }
    });
    initialize();
  }

  function initialize() {
    if(GBrowserIsCompatible()) {
      map = new GMap2(document.getElementById("map_canvas"));
      map.addControl(new GLargeMapControl());
      map.addControl(new GMapTypeControl());
      map.setCenter(default_center, 3);
      map.addControl(new MoreControl());
      gdir = new GDirections();
      GEvent.addListener(gdir, "error", handleErrors);
      GEvent.addListener(gdir, "load", function(){ 
        var poly = gdir.getPolyline();
        if(directions_overlay){
          map.removeOverlay(directions_overlay);
        }
        directions_overlay = poly;
        map.addOverlay(poly);
        customPanel(map,"map",gdir,document.getElementById("directions"));
      });
    }
    $("#directions").hide();
    $("#main").css("visibility","visible");
  }

  function listingClass(src_xml){
    this.id = $(src_xml).attr("id");
    this.title = $(src_xml).attr("title");
    this.address = $(src_xml).attr("address");
    this.lat = parseFloat($(src_xml).attr("lat"));
    this.lng = parseFloat($(src_xml).attr("lng"));
    this.phone = $(src_xml).attr("phone");
  }


  function trim(string){
    return string.replace(/(^\s+)|(\s+$)/g, "");
  }

  function showDirInput(id,state,lat,lng){
    if(state){
      $("#dir_"+id).html("<h4>Start adress:<br><input type='text' id='dir_start_"+id+"' style='width:150px;'> <a href='javascript:void(0)' onclick='getStartAddress("+id+",\""+lat+"\",\""+lng+"\")'>GO</a></h4>");
    } else {
      $("#dir_"+id).html("<h4><a href='javascript:void(0)' onclick='showDirInput("+id+",true,\""+lat+"\",\""+lng+"\")'>Get Directions</a></h4>");
    }
  }

  function getStartAddress(id,lat,lng){
    setDirections($("#dir_start_"+id).val(),lat+","+lng);
  }

  function focusLocation(mid,zoom){
    var current_marker = getMarkerObj(mid);
    if(zoom){
      map.setCenter(current_marker.getPoint(),zoom);
    } else {
      map.setCenter(current_marker.getPoint());
    }
    current_marker.openInfoWindowTabsHtml(current_marker.infoTabs);
  }

  function getMarkerObj(id){
    for(var i=0;i<markers_array.length;i++){
      if(markers_array[i].id==id){
        return markers_array[i];
        break;
      }
    } 
    return null;
  }

  var panoClient = new GStreetviewClient();

  function createLocationMarker(latlng, location, i) {
    var marker = new GMarker(latlng);
    marker.id = i;
    marker.latlng = latlng;
    var myHtml = "<div class='location-item'>"+
                 "  <h2>"+location.title+"</h2>"+
                 "  <h4>"+location.address+"</h4>"+
                 "  <h4>"+location.phone+"</h4>"+
                 "  <h4><a href='javascript:void(0)' onclick='showStreetView(\""+location.lat+"\",\""+location.lng+"\")'>Street View</a></h4>"+
                 "  <div id='dir_"+marker.id+"'><br><br></div>";
    myHtml +=    "</div>";
    var infoTabs = [new GInfoWindowTab("Info", myHtml)];
    marker.infoTabs = infoTabs;
    GEvent.addListener(marker,"click", function() {
      marker.openInfoWindowTabsHtml(infoTabs, {maxWidth: 250});
      //var fenwayPOV = {yaw:370.64659986187695,pitch:-20};
    });
    GEvent.addListener(marker,"infowindowopen", function() {
      showDirInput(marker.id,false,marker.latlng.lat(),marker.latlng.lng());
    });
    return marker;
  }

  function createStartMarker(latlng) {
    var icon = new GIcon(G_DEFAULT_ICON);
    icon.image = "http://maps.google.com/mapfiles/dd-start.png";
    var marker = new GMarker(latlng,{ icon:icon });
    return marker;
  }

  function getStore(){
    switch($("#stores").val()){
      case "":
        return;
      break;
      case "all":
        outputLocation();
      break;
      default:
        focusLocation($("#stores").val(),16);
      break;
    }
    
  }

  function doSearch(){
    $.ajax({
      type: "GET",
      url: "data.php?search="+$("#search_input").val(),
      dataType: "xml",
      success: outputLocations
    });
  }

  function showStreetView(lat, lng){
    var point = new GLatLng(lat,lng);
    $("#street_view").dialog("open");
    var panoramaOptions = { latlng:point };
    var myPano = new GStreetviewPanorama(document.getElementById("street_view_div"), panoramaOptions);
  }

  function outputLocations(xd){
    map.clearOverlays();
    if(directions_overlay){
      $("#directions").hide();
      map.removeOverlay(directions_overlay);
      gdir.clear();
      $("#directions").html("");
    }
    markers_array = new Array();
    totalLocations = $("location",xd).length;
    MapBounds = new GLatLngBounds();
    var content = "<div class='ui-widget ui-widget-content ui-corner-all location-item'><input type='text' id='search_input' value='"+$("#search_input").val()+"'> <a href='javascript:void(0)' onclick='doSearch()'>SEARCH</a></div>";
    var counter = 0;
    if(totalLocations>0){
      var marker_count = 0;
      $("location",xd).each(function(){
        var listing = new listingClass($(this));
        var point = new GLatLng(listing.lat,listing.lng);
        var marker=new createLocationMarker(point, listing, marker_count);
        map.addOverlay(marker,0,17);
        MapBounds.extend(point);
        markers_array.push(marker);
        marker_count++;
        content += "<div class='ui-widget ui-widget-content ui-corner-all location-item'>"+
                   "<a href='javascript:void(0)' onclick='focusLocation("+counter+")'>"+listing.title+"</a><br>"+
                   "<h4>"+listing.address+"</h4>"+
                   "<h4>"+listing.phone+"</h4>";
        content += "</div>";
        counter++;
      });
      map.setZoom(map.getBoundsZoomLevel(MapBounds)-1);
      var clat = (MapBounds.getNorthEast().lat() + MapBounds.getSouthWest().lat()) / 2;
      var clng = (MapBounds.getNorthEast().lng() + MapBounds.getSouthWest().lng()) / 2;
      map.setCenter(new GLatLng(clat,clng));
    } else {
      content += "<div class='ui-state-highlight ui-corner-all' style='margin-top:10px;padding:0 .7em;margin-bottom:10px;'>"+
                 "  <div><p style='padding:5px 0px 6px 0px;'>No listings found! Try again.</p></div>"+
                 "</div>";
    }
    $("#results_list").html(content);
  }

  function setDirections(from,to){
    if(directions_overlay){
      map.removeOverlay(directions_overlay);
      gdir.clear();
      $("#directions").html("");
    }
    directions_locations = "from: " + from + " to: " + to;
    //gdir.load(direction,{"locale":"en_US",getPolyline:true,getSteps:true});
    gdir.load(directions_locations,{getPolyline:true,getSteps:true});
  }

  function handleErrors(){
    var error = "";
    if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
      error = "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)
      error = "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)
      error = "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_GEO_BAD_KEY)
      error = "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)
      error = "A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code;
    else error = "An unknown error occurred.";
    $("#directions").show();
    $("#directions").html("Driving directions error!<br>"+error);
  }

  function customPanel(map,mapname,dirn,div) {
    var html = "";
    function waypoint(point, type, address) {
      var target = '"' + mapname+".showMapBlowup(new GLatLng("+point.toUrlValue(6)+"))"  +'"';
    }
    function routeDistance(dist) {
      html += "<div style='padding-bottom:0.3em;'><span class='tipArial'><b>"+dist+"</b></span></div>";
    }      
    function detail(point, num, description, dist) {
      html += "<div class='directionItem' onclick='showDirectionDetails(new GLatLng("+point.toUrlValue(6)+"))'>"+description+" ("+dist+")</div>";
    }
    function copyright(text) {
      //html += '<div style="font-size: 0.86em;">' + text + "</div>";
    }
    for (var i=0; i<dirn.getNumRoutes(); i++) {
      if (i==0) {
        var type="play";
      } else {
        var type="pause";
      }
      var route = dirn.getRoute(i);
      var geocode = route.getStartGeocode();
      var point = route.getStep(0).getLatLng();
      waypoint(point, type, geocode.address);
      routeDistance(route.getDistance().html+" (about "+route.getDuration().html+")");
      for (var j=0; j<route.getNumSteps(); j++) {
        var step = route.getStep(j);
        if(j==0){
          var marker = new createStartMarker(step.getLatLng());
          map.addOverlay(marker);
        }
        detail(step.getLatLng(), j+1, step.getDescriptionHtml(), step.getDistance().html);
      }
    }
    var geocode = route.getEndGeocode();
    var point = route.getEndLatLng();
    waypoint(point, "stop", geocode.address);
    copyright(dirn.getCopyrightsHtml());
    div.innerHTML = html;
    $("#directions").show();
  }

  function MoreControl(){};
  MoreControl.prototype = new GControl();
  MoreControl.prototype.initialize = function(map) {
    var container = document.createElement("div");
    var savepos = document.createElement("div");
    savepos.id= "directions";
    savepos.title= "Show/Hide regions layers on the map";
    savepos.className= "directoins_div";
    container.appendChild(savepos);
    map.getContainer().appendChild(container);
    return container;
  }
  MoreControl.prototype.getDefaultPosition = function() {
    return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 32));
  }

