﻿// JScript File

google.load("maps", "2.x");

var ArchGM =
{

    markers: new Array(),
    // default lat and long and zoom level to jump to on initialization
    DefaultZoomLevel :  5
,
    Bounds : null
,    
    DefaultLatitude:    54.500651
,
    DefaultLongitude:   -4.064941 //-2.298134
,
    
    //current map positions According to ArchGM map manipulation.
    CurrentLongitude: null
,
    CurrentLatitude: null
,    
    CurrentZoomLevel: null
,       
    map: null //The Google GMap2
,   
    ResultContainerOver: null
,
    ResultContainerOut: null
,
    
    // used to popup a message to the user.
  Popup: function(msg){alert(msg);}// <---- end of Popup
,  
  // used to load and initialize the map and it's settings
  load: function(){
        this.map = new google.maps.Map2(document.getElementById("map"));
        //this.Bounds = new google.maps.GLatLngBounds();
        this.setCentre();
        this.RenderMarkers();
        this.Managecontrols();
    } // ---- end of load:
    
,
  
  // set the centre of the map to the current long and lot values
    setCentre: function(){
        if(google.maps.BrowserIsCompatible()){
            // if the current long, lat and zoom levels are not set, set them to the default.
            if(this.CurrentLongitude === null){  this.CurrentLongitude = this.DefaultLongitude;     }
            if(this.CurrentLatitude === null){   this.CurrentLatitude = this.DefaultLatitude;       }
            if(this.CurrentZoomLevel === null){  this.CurrentZoomLevel = this.DefaultZoomLevel;     }
            this.map.setCenter(new google.maps.LatLng( this.CurrentLatitude, this.CurrentLongitude ) , this.CurrentZoomLevel);
        } // ---- end of is browser compatible.
    } // ---- end of SetCentre
    
,    
   
// mouse events
    MarkerMouseOver: function(markerId){
       
        var output = document.getElementById(this.markers[markerId].ResultContainer);
        output.className =this.ResultContainerOver;
     
    } //  end of MarkerMouseOver
,
    MarkerMouseOut: function(markerId){
        var output = document.getElementById(this.markers[markerId].ResultContainer);
        output.className =this.ResultContainerOut;
    //    if(output(markerId)){
    //    output(markerId).style.backgroundColor ="#ffffff";
    //    }
    }
,        
    MarkerMouseClick: function(marker){
    
        //var output = document.getElementsByName("output");
        marker.marker.openInfoWindowHtml(marker.marker.point,marker.msg);// output(markerId).innerHTML);
        
    }// end of MarkerMouseClick
,

    AddMarker: function(lng,lat,ResultContainer,msg){
    if(lng!=null){
        var marker = new objMarker();
        marker.longitude = lng;
        marker.latitude = lat;
        marker.msg=msg;
        marker.ResultContainer=ResultContainer;
        marker.markerNumber = this.markers.length;
        marker.point = new google.maps.LatLng(lng, lat);
        marker.marker = new google.maps.Marker(marker.point); 
      //  this.Bounds.extend(marker.point);
      // add the events to the marker
            google.maps.Event.addListener(marker.marker, "mouseover", function(){ArchGM.MarkerMouseOver(marker.markerNumber);});  
            google.maps.Event.addListener(marker.marker, "mouseout", function(){ArchGM.MarkerMouseOut(marker.markerNumber);});  
            google.maps.Event.addListener(marker.marker, "click", function(){ArchGM.MarkerMouseClick(marker);}); 
        this.markers.push(marker);}// end of lng not null
        
    //    google.maps.Event.addListener(markers[NewArrayNo], "mouseout", function(){ArchGM.MarkerMouseOut(NewArrayNo);});    
           
    //    this.map.addOverlay(marker);
    }// ----- end of add marker
,    
    // used to render the contents of the marker array onto the map.
    RenderMarkers: function(){
    
        for (var i=0; i < this.markers.length; i++) {
                       
            this.map.addOverlay(this.markers[i].marker); 
        }   // end of for loop
    } // end of Render Markers

,

fit: function(){
    var bounds = new GLatLngBounds();
    bounds.extend(this.markers[0].getPoint());
    var center = bounds.getCenter();
    this.map.closeInfoWindow();
    //if(marker.length>0){map.setZoom(map.getBoundsZoomLevel(bounds));}
    this.map.setCenter(center);
    //map.panDirection(0,0.2);
}
,
Managecontrols: function(){
///hide controls
    this.map.addControl(new GLargeMapControl());
    this.map.addControl(new GMapTypeControl());
//    google.maps.Event.addListener(this.map, "click", function(){this.map.closeInfoWindow()});
//    this.map.enableContinuousZoom();
//    this.map.enableDoubleClickZoom();
//    this.map.hideControls();
//    this.map.addListener(this.map, "mouseover", function(){
//        this.map.showControls();
//        });
//    this.map.addListener(this.map, "mouseout", function(){
//        this.map.hideControls(); 
//        });  

}

};// end of ArchGM

function objMarker(){
    longitude:          null;
    latitude:           null;
    msg:                null;
    marker:             null;
    infoWindowText:     null;
    ResultContainer:    null;
    marker:             null;
    point:              null;
    markerNumber:       null;
}
