﻿/// <reference path="googlemaps-intellisense.js" />
/// <reference path="Dealer.js" />

function AbsDealerMap( ) {    
}

// 'private' variables
AbsDealerMap.prototype._dealerMarkers = [];
AbsDealerMap.prototype._dealers = [];
AbsDealerMap.prototype._mapInstance;
AbsDealerMap.prototype._containerID;
AbsDealerMap.prototype._centerPoint;
AbsDealerMap.prototype._units = "miles";
AbsDealerMap.prototype._bounds = new GLatLngBounds();

// methods
AbsDealerMap.prototype.initializeMap = function( containerID, latitude, longitude, units, dealers ) {
    this._containerID = containerID;
    this._centerPoint = new GLatLng( latitude, longitude );
    this._units = units;
    this._dealers = dealers;
     var mapDiv = document.getElementById(  this._containerID );
     this._mapInstance = new GMap2( mapDiv ); 
     this._mapInstance.setCenter(this._centerPoint );
     this.addOverlays();
     this._mapInstance.addControl( new GSmallMapControl() );         
     
     var zoomLevel = this.calculateBounds();
     this._mapInstance.setCenter( this._centerPoint, zoomLevel );
}

AbsDealerMap.prototype.setZoom = function( zoomlevel ) {
    this._mapInstance.setCenter( this._centerPoint, zoomlevel );
}

AbsDealerMap.prototype.addOverlays = function() {  
    for ( var i = 0; i < this._dealers.length; i++ ) {
        this._dealerMarkers[i] = this.createMarker( this._dealers[i], i );
        this._mapInstance.addOverlay( this._dealerMarkers[i] );
     }
}

AbsDealerMap.prototype.selectMarker = function( marker, dealer ) {
    var tabs = dealer.GetInfoWindowTabs();
    marker.openInfoWindowTabsHtml( tabs );
    var mapDiv = document.getElementById( "minimap" );
    
    if ( mapDiv )
        var miniMap = new MiniMap( mapDiv, dealer );
}

AbsDealerMap.prototype.selectMarkerByDealerID = function( dealerID ) {
       
    
    for ( var i = 0; i < this._dealerMarkers.length; i++ ) {
    
        if ( this._dealers[i].DealerID == dealerID ) {
            this.selectMarker( this._dealerMarkers[i], dealers[i] );
            return;
        }
    }

}

AbsDealerMap.prototype.createMarker = function( dealer, index ) {
    var dealerIcon = dealer.GetIcon();
    
    var dealerOptions = { icon:dealerIcon, title:dealer.CompanyName };
        
    var dMarker = new GMarker( dealer.GetGLocation(), dealerOptions );
      
    
    GEvent.addListener( dMarker, "click", function( externalName ) { 
        // hack: name of variable on web page ***MUST*** be mapWrapper
        mapWrapper.selectMarker(dMarker, dealer);       
        } );
     
     GEvent.addListener( dMarker, "mouseover", function() {
        dMarker.openInfoWindowHtml( "<span class='infoWindowHeader'>" + dealer.CompanyName + "</span><br/>Click the icon to see more details..." ); 
     } );
   
    return dMarker;
}

AbsDealerMap.prototype.calculateBounds = function() {
   this._bounds = new GLatLngBounds();  
  
    for ( var i = 0; i < this._dealerMarkers.length; i++ ) {        
        this._bounds.extend(  this._dealerMarkers[i].getLatLng() );        
    }
    
    return this._mapInstance.getBoundsZoomLevel( this._bounds ) - 1;
}

// MiniMap Class
function MiniMap( container, dealer ) {
   
    this.Map = new GMap2( container );   

    this.Map.setCenter( dealer.GetGLocation(), 14 );
    var dealerIcon = dealer.GetIcon();
    
    this.Map.addOverlay( new GMarker( dealer.GetGLocation(), { icon:dealerIcon, title:dealer.CompanyName } ) );
    
    this.Map.disableContinuousZoom();
    this.Map.disableDoubleClickZoom();
    this.Map.disableDragging();
   this.Map.disableGoogleBar();
   this.Map.disableInfoWindow();
    
    var CopyrightDiv = container.firstChild.nextSibling;
	var CopyrightImg = container.firstChild.nextSibling.nextSibling;
	CopyrightDiv.style.display = "none"; 
	CopyrightImg.style.display = "none";
};

MiniMap.prototype.GetContainer = function() { return this.Map.getContainer(); };

