GoogleMap = null;
SmallIconZoomLimit = 6;
CurrentlyLoadedMarkerPoints = Array();

Trr.Map = function(wrapperId, defaultLatitude, defaultLongitude, defaultZoom) {
	if (!GBrowserIsCompatible()) {
		return;
	}

	this.DefaultLongitude = defaultLongitude;
	this.DefaultLatitude = defaultLatitude;
	this.DefaultZoom = defaultZoom;
	this.GoogleMap = new GMap2(document.getElementById(wrapperId));
	this.GoogleMap.setUIToDefault();
	this.GoogleMap.disableScrollWheelZoom()
	this.GoogleMap.setCenter(new GLatLng(defaultLatitude, defaultLongitude), defaultZoom);
	
	GoogleMap = this.GoogleMap;

	// unloads the map
	$(window).unload(function() {
		GUnload();
	});
}

Trr.Map.prototype.ShowDefault = function()
{
	this.GoogleMap.setCenter(new GLatLng(map.DefaultLatitude, map.DefaultLongitude), map.DefaultZoom);
}

Trr.Map.prototype.AddMarker = function( latitude, longitude, title, clickFunctionReference, isHK )
{
    var point = new GLatLng( latitude, longitude );
    var icon = isHK ? Trr.Map.GetHKIcon() : Trr.Map.GetIcon();
    var marker = new GMarker( point, {icon:icon, title:title } );
    GEvent.bind( marker, "click", this, clickFunctionReference );
    this.GoogleMap.addOverlay( marker );
    CurrentlyLoadedMarkerPoints[ CurrentlyLoadedMarkerPoints.length ] = point;
}

/*
Trr.Map.GetMarkers = function() {
	var batch = [];
	//Loop through offices and create markers
	batch.push(new GMarker(new GLatLng(latitude, longitude), { icon: Trr.Map.NewGetIcon(this.GoogleMap.getZoom()) }));
	return batch;
}

Trr.Map.prototype.AddOfficeMarkers = function()
{
	mgr = new GMarkerManager(map);
	mgr.addMarkers(getWeatherMarkers(20), 3);
	mgr.addMarkers(getWeatherMarkers(200), 6);
	mgr.addMarkers(getWeatherMarkers(1000), 8);
	mgr.refresh();
}
*/
Trr.Map.prototype.GetOfficeMarker = function(latitude, longitude, title, clickFunctionReference, isHK, isSmall) {
	var point = new GLatLng(latitude, longitude);
	var size = isSmall ? 5 : 8;
	var marker = new GMarker(point, { icon: Trr.Map.NewGetIcon(size, isHK), title: title });
	GEvent.bind(marker, "click", this, clickFunctionReference);
	CurrentlyLoadedMarkerPoints[CurrentlyLoadedMarkerPoints.length] = point;
	return marker;
}

Trr.Map.NewGetIcon = function(zoomlevel, isHK) {
	var icon = new GIcon();
	var size = zoomlevel > SmallIconZoomLimit ? "_big" : "";

	if (isHK) {
		icon.iconSize = zoomlevel > SmallIconZoomLimit ? new GSize(23, 23) : new GSize(13, 13);
		// specifies where on the map the icon should be attached, relative to the image size
		// In this case we want the icon image to attach at the actual point, 5px from left and 14px from top
		icon.iconAnchor = zoomlevel > SmallIconZoomLimit ? new GPoint(27, 25) : new GPoint(13, 13);
		icon.image = "/UI/Img/SearchOffice/MapIconHK" + size + ".png";

		if (zoomlevel > SmallIconZoomLimit)
		{
			icon.shadow = "/UI/Img/SearchOffice/MapIconHKShadow_big.png";
			icon.shadowSize = new GSize(44, 25);
		}
	}
	else {
		// specifies where on the map the icon should be attached, relative to the image size
		// In this case we want the icon image to attach at the actual point, 5px from left and 14px from top
		icon.iconAnchor = zoomlevel > SmallIconZoomLimit ? new GPoint(11.5, 27) : new GPoint(5, 14);

		icon.image = "/UI/Img/SearchOffice/MapIconWithoutShadow" + size + ".png";
		icon.iconSize = zoomlevel > SmallIconZoomLimit ? new GSize(19, 27) : new GSize(11, 15);

		icon.shadow = "/UI/Img/SearchOffice/MapIconShadow" + size + ".png";
		icon.shadowSize = zoomlevel > SmallIconZoomLimit ? new GSize(40, 27) : new GSize(20, 15);
	}
	return icon;
}

Trr.Map.GetHKIcon = function()
{
    var icon = new GIcon();
    icon.iconSize = new GSize(13, 13);
    // specifies where on the map the icon should be attached, relative to the image size
    // In this case we want the icon image to attach at the actual point, 5px from left and 14px from top
    icon.iconAnchor = new GPoint(13, 13);
    icon.image = "/UI/Img/SearchOffice/MapIconHK.png";
    return icon;
}

Trr.Map.GetIcon = function()
{
    var icon = new GIcon();
    
    // specifies where on the map the icon should be attached, relative to the image size
    // In this case we want the icon image to attach at the actual point, 5px from left and 14px from top
    icon.iconAnchor = new GPoint(5, 14);
    
    icon.image = "/UI/Img/SearchOffice/MapIconWithoutShadow.png";
    icon.iconSize = new GSize(11, 15);
    
    icon.shadow = "/UI/Img/SearchOffice/MapIconShadow.png";
    icon.shadowSize = new GSize(20,15);
    
    return icon;
}

Trr.Map.prototype.ShowClosestOffices = function(query) {
	var thisObj = this;
	new GClientGeocoder().getLatLng(query + ", Sverige",
        function(point) {
        	if (point != null) {
        		thisObj.GoogleMap.setCenter(point, 9);
        		thisObj.ZoomOutUntilFirstMarkerIsVisible();
        	}
        	else {
        		thisObj.ShowDefault();
        	}
        }
    );
}

/**
 * Keep zooming out unit at least one marker is visible
 */
Trr.Map.prototype.ZoomOutUntilFirstMarkerIsVisible = function()
{
    while(true)
    {
        //Get current bounds
        var bounds = this.GoogleMap.getBounds();
    
        //Check if any markers exists within current bounds
        for( i = 0; i < CurrentlyLoadedMarkerPoints.length; i++ )
        {   
            if( bounds.containsLatLng( CurrentlyLoadedMarkerPoints[ i ] ) )
            {
                return;
            }
        }
        
        this.GoogleMap.zoomOut();
        
        //Stop zooming out when the minimum zoom level has been reached
        if( this.GoogleMap.getZoom() == 2 )
        {
            return;
        }
    }
}
