//trio_googlemap_api.js

    var map = null;
    var geocoder = new GClientGeocoder();
    // Create a search control
    var searchControl = new GSearchControl();
  
    // Add in a full set of searchers
    var localSearch = new GlocalSearch();

    var options = new GsearcherOptions();

    var app;
    var prefix_image_file = "";

    var icon_house = "images/house.png";
    var icon_shadow_house = "images/house-shadow.png";
    var icon_apartment = "images/apartment.png";
    var icon_shadow_apartment = "images/apartment-shadow.png";

    var icon_highlight = "images/highlight.png";
    var icon_shadow_highlight = "images/highlight-shadow.png";

    var icon_schools = "images/school.png";
    var icon_shadow_schools = "images/school-shadow.png";
    var icon_theatres = "images/theater.png";
    var icon_shadow_theatres = "images/theater-shadow.png";
    var icon_shopping_malls = "images/shopping.png";
    var icon_shadow_shopping_malls = "images/shopping-shadow.png";
    var icon_train_station = "images/train.png";
    var icon_shadow_train_station = "images/train-shadow.png";
    var icon_hotels = "images/hotel.png";
    var icon_shadow_hotels = "images/hotel-shadow.png";
    var icon_galleries = "images/gallery.png";
    var icon_shadow_galleries = "images/gallery-shadow.png";
    var icon_beauty_clinics = "images/beautyClinics.png";
    var icon_shadow_beauty_clinics = "images/beautyClinics-shadow.png";

    var icon_trio = "images/trio.png";
    var icon_shadow_trio = "images/trio-shadow.png";

    var main_marker;

   function App(){
      var divMap = document.getElementById("map");
      
      if(home == "" || home == null){
        if(divMap != null){
          if(divMap.style != null){
             divMap.style.display = 'none';
          }
        }
      }

      //Don't do anything when can't find the map
      if (divMap != null && GBrowserIsCompatible()) {

        map = new GMap2(divMap);
        //map.addControl(new GSmallMapControl());
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        //here we need to call c# method "GeoCoder.GetLatLng(address)" to return Latitude & longitude and use them below.
        //showAddress(home);
        
        showHome(home, homeInfoHtml);
        //map.setCenter(new GLatLng(-33.825887,151.199988), 14);

        options.setExpandMode(GSearchControl.EXPAND_MODE_OPEN);
        searchControl.addSearcher(localSearch, options);
        //searchControl.addSearcher(new GvideoSearch());
  
        // Set the Local Search center point
        localSearch.setCenterPoint(map);
  
        // tell the searcher to draw itself and tell it where to attach
        // Don't show search box
        searchControl.draw(document.getElementById("searchcontrol"));
        // bind a search control to the map, suppress result list
        //map.addControl(localSearch, new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10,20)));

        // tell the search control to call be on start/stop
        searchControl.setSearchStartingCallback(this, App.prototype.OnSearchStarting);
        //searchControl.setOnKeepCallback(this, App.prototype.OnKeep, "view on map");
        //searchControl.setSearchCompleteCallback(this, App.prototype.OnSchoolSearchComplete);
      }
      return;
    }

    function LoadInterests(searchControl){
      if (school)
      {
        searchControl.setSearchCompleteCallback(this, App.prototype.OnSchoolSearchComplete);
        searchControl.execute("school");
      }
      else if (theater)
      {
        searchControl.setSearchCompleteCallback(this, App.prototype.OnTheaterSearchComplete);
        searchControl.execute("category: Theatre");
      }
      else if (shop)	
      {
        searchControl.setSearchCompleteCallback(this, App.prototype.OnShopSearchComplete);
        searchControl.execute("category: Shopping Centre");
      }
      else if (hotel)
      {
        searchControl.setSearchCompleteCallback(this, App.prototype.OnHotelSearchComplete);
        searchControl.execute("category: Hotel");
      }
      else if (station)	
      {
        searchControl.setSearchCompleteCallback(this, App.prototype.OnStationSearchComplete);
        searchControl.execute("category: Train Station");
      }
      else if (gallery)	
      {
        searchControl.setSearchCompleteCallback(this, App.prototype.OnGallerySearchComplete);
        searchControl.execute("gallery");
      }
      else if (beauty)	
      {
        searchControl.setSearchCompleteCallback(this, App.prototype.OnBeautySearchComplete);
        searchControl.execute("category: Beauty Salon");
      }else{
         showTrio();
         main_marker.openInfoWindowHtml(homeInfoHtml);
      }
    }

    function createMarker(posn, title, icon_image, icon_image_shadow) {
      var icon = getIcon(icon_image,icon_image_shadow);
      var marker = new GMarker(posn, {title: title, icon: icon, draggable:false });
      return marker;
    }

    function addMarkerObjectToMap(app,markerObject){
      if(app.markerList == null){
        app.markerList = new Array();
      }
      app.markerList.push(markerObject);
    }

    function OnSearchCompleteCommon(searcher, icon_img, icon_shadow_img){
      // if we have local search results, put them on the map
      if ( searcher.results && searcher.results.length > 0) {
        for (var i = 0; i < searcher.results.length; i++) {
          var result = searcher.results[i];

          // if this is a local search result, then proceed...
          if (result.GsearchResultClass == GlocalSearch.RESULT_CLASS ) {
            var markerObject = new Object();
            markerObject.result = result;
            markerObject.latLng = new GLatLng(parseFloat(result.lat), parseFloat(result.lng));
       	    markerObject.gmarker = new GMarker(markerObject.latLng, {icon : getIcon(icon_img,icon_shadow_img) });
            var clickHandler = method_closure(this, App.prototype.OnMarkerClick, [markerObject]);
            GEvent.bind(markerObject.gmarker, "click", this, clickHandler);
            addMarkerObjectToMap(this,markerObject);
            map.addOverlay(markerObject.gmarker);
            result.__markerObject__ = markerObject;
          }
        }
      }
    }

    App.prototype.OnSchoolSearchComplete = function(sc, searcher) {
      OnSearchCompleteCommon(searcher,icon_schools,icon_shadow_schools);
      school = false;
      LoadInterests(sc);
    }

    App.prototype.OnTheaterSearchComplete = function(sc, searcher) {
        OnSearchCompleteCommon(searcher,icon_theatres,icon_shadow_theatres);
        theater = false;
        LoadInterests(sc);
    }

    App.prototype.OnShopSearchComplete = function(sc, searcher) {
        OnSearchCompleteCommon(searcher,icon_shopping_malls,icon_shadow_shopping_malls);
        shop = false;
        LoadInterests(sc);
    }

    App.prototype.OnHotelSearchComplete = function(sc, searcher) {
      OnSearchCompleteCommon(searcher,icon_hotels,icon_shadow_hotels);
      hotel = false;
      LoadInterests(sc);
    }

    App.prototype.OnStationSearchComplete = function(sc, searcher) {
      OnSearchCompleteCommon(searcher,icon_train_station,icon_shadow_train_station);
      station = false;
      LoadInterests(sc);
    }

    App.prototype.OnGallerySearchComplete = function(sc, searcher) {
      OnSearchCompleteCommon(searcher,icon_galleries,icon_shadow_galleries);
      gallery = false;
      LoadInterests(sc);
    }

    App.prototype.OnBeautySearchComplete = function(sc, searcher) {
      OnSearchCompleteCommon(searcher,icon_beauty_clinics,icon_shadow_beauty_clinics);
      beauty = false;
      LoadInterests(sc)
    }

    App.prototype.OnSearchStarting = function(sc, searcher, query) {
      map.closeInfoWindow();
      if(this.markerList) {
        for (var i=0; i < this.markerList.length; i++) {
	  var markerObject = this.markerList[i];
          //map.removeOverlay(markerObject.gmarker);
        }
      }
      else
      {
       this.markerList = new Array();
      }
    }

//    App.prototype.OnKeep = function(result) {
//      if (result.__markerObject__) {
//        markerObject = result.__markerObject__;
//        this.OnMarkerClick(markerObject);
//      }
//    }
    //The openInfoHtml styling done here
    App.prototype.OnMarkerClick = function(markerObject) {
      map.closeInfoWindow();
      
      var htmlNode ="<div class=gs-localResult gs-result>";
      htmlNode +=  "<div class=gs-title><a target=_blank class=gs-title href=";
      htmlNode += markerObject.result.url;
      htmlNode += ">";
      htmlNode += markerObject.result.title;
      htmlNode += "</a></div><div class=gs-address>";

      if (markerObject.result.streetAddress)
      {
      	htmlNode += "<div class=gs-street>" + markerObject.result.streetAddress + "</div>";
      }
      
      if (markerObject.result.city){
      	htmlNode += "<div class=gs-city>" + markerObject.result.city + ", </div>";
      }
      
      if (markerObject.result.region){
      	htmlNode += "<div class=gs-region>" + markerObject.result.region + "</div>";
      }

      if (markerObject.result.country)
      {
      	htmlNode += "<div class=gs-country>" + markerObject.result.country + "</div>";
      }

      htmlNode += "</div>";
      
      if (markerObject.result.phoneNumbers)
      {
      	htmlNode += "<div class=gs-phone>" + markerObject.result.phoneNumbers[0].number	+ "</div>";
      }
      
      if (markerObject.result.ddUrl)      
      {
      	htmlNode += "<div class=gs-directions><a class=gs-directions target=_blank href=";
      	htmlNode += markerObject.result.ddUrl;
      	htmlNode += ">directions</a></div>"
      }
      htmlNode += "</div>";
      //markerObject.gmarker.setInfoWindow(htmlNode);
      markerObject.gmarker.openInfoWindow(htmlNode);

    }


    function method_closure(object, method, opt_argArray) {
      return function() {
        return method.apply(object, opt_argArray);
      }
    }

     function getIcon(imgFile, shadowFile) {
        var icon = new GIcon();
        icon.image = prefix_image_file + imgFile;
        icon.iconAnchor = new GPoint(16, 16);
        icon.infoWindowAnchor = new GPoint(16, 0);
        icon.iconSize = new GSize(32, 32);
        icon.shadow = prefix_image_file + shadowFile;
        icon.shadowSize = new GSize(59, 32);
        return icon;
    }

    function showAddress(address) {
      searchAndShowAddress(address, icon_highlight, icon_shadow_highlight, address, address, false);
    }

    function showTrio()
    {
      var address = "Corner of Booth Street and Pyrmont Bridge Road, Camperdown Australia";
      var html_title = "<img src='" + prefix_image_file +  "images/logo_white_sm.gif'><br/><strong>TRIO Apartments Sydney</strong><br/>";
      html_title += "Corner of Booth Street and Pyrmont Bridge Road <br/>Camperdown";
      searchAndShowAddress(address, icon_trio, icon_shadow_trio,html_title, "TRIO Apartments Sydney", false);
    }

    function searchAndShowAddress(address, icon, icon_shadow, html_title, address_title,  showInfo) {
      if (address == null || address == "") return;
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              //alert(address + " not found");
            } else {
              //var market = new GMarker(point);
              var marker = new GMarker(point, {icon : getIcon(icon,icon_shadow), title: address_title });
              GEvent.addListener(marker, "click",
                function() {
                  marker.openInfoWindowHtml(html_title);
                }
              );
              map.addOverlay(marker);
              if(showInfo){
                marker.openInfoWindowHtml(html_title);
              }
            }
          }
        );
      }
    }

    function showHome(address, infoHtml) {
      if(address == null || address == "") return;
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              //alert(address + " not found");
            } else {
              map.setCenter(point, 14);
              //var marker = new GMarker(point,{title: address});

              var icon_img, icon_shadow_img, homeTypeLower;
              //alert("homeType: " + homeType);
              homeTypeLower = homeType.toLowerCase();

              if(homeTypeLower == "apartment"){
                icon_img = icon_apartment;
                icon_shadow_img = icon_shadow_apartment;
              }else if(homeTypeLower == "home"){
                icon_img = icon_house;
                icon_shadow_img = icon_shadow_house;
              }else{
                icon_img = icon_house;
                icon_shadow_img = icon_shadow_house;
              }

              var marker = new GMarker(point, {icon : getIcon(icon_img,icon_shadow_img), title: address });
                GEvent.addListener(marker, "click",
                function() {
                  marker.openInfoWindowHtml(infoHtml );
                }
              );
              
              main_marker =  marker;

              map.addOverlay(marker);
             // marker.openInfoWindowHtml(address);
            }

            //Show highlights
            showHighlights();

            // execute an inital search
            LoadInterests(searchControl);
            
            //Show trio
            //showTrio();
            //setTimeout("showTrio()",0);
          }
        );
      }      
    }