var map = null;
var gMap;
window.onload = function()
{
    if (typeof GUnload == 'undefined' || typeof mapConfig == 'undefined')
    {
        return;
    }

    window.onunload = GUnload;
    map = new mapScript( mapConfig );
    if (!map.ok)
    {
        return;
    }
    map.initGMap();
}

function mapScript( config )
{


    this.ok = false;
    this.config = config;

    if (!this.config)
    {
        return;
    }

    this.box = document.getElementById( this.config.boxId );
    if (
        (!this.box)
        ||
        (!GBrowserIsCompatible())
    )
    {
        return;
    }

    this.map = null;
    this.spotsDataUrlBase = null;
    this.thumbsLayer = null;
    this.ok = true;
}

mapScript.prototype.initGMap = function()
{
    this.map = new GMap2( this.box );
	gMap = this.map;
    var center = new GLatLng(56.968221051706436 , 24.61142310333253);
    with (this.map)
    {
        addControl(new GLargeMapControl());

        enableScrollWheelZoom();
        enableDoubleClickZoom();
        enableContinuousZoom();
        setCenter( center , 7 );
    }

    // this.map.openInfoWindowHtml( center );
    // this.map.closeInfoWindow(  );
    var self = this;
    GEvent.addListener(this.map, "moveend", function() { self.requestSpots(); } );
    this.requestSpots();

}

mapScript.prototype.getSpotsDataUrlBase = function()
{
    if (!this.spotsDataUrlBase)
    {
        this.spotsDataUrlBase = this.config.spotsDataUrl.concat('?task=getSpotsJson');
    }
    return this.spotsDataUrlBase;
}



mapScript.prototype.requestSpots = function()
{
    var bounds = this.map.getBounds();
    var sw = bounds.getSouthWest();
    var ne = bounds.getNorthEast();
    var spotsDataUrl = this.getSpotsDataUrlBase();

    spotsDataUrl = spotsDataUrl.concat(
    '&minLat=', sw.lat(),
    '&maxLat=', ne.lat(),
    '&minLng=', sw.lng(),
    '&maxLng=', ne.lng(),
    '&rand=', Math.random()
    );

    var self = this;
    loadXmlHttp ( spotsDataUrl,
    function(xmlhttp, params)
    {
        var spotsData = eval(xmlhttp.responseText);
        self.loadSpots(spotsData);
    },
    null, 'GET' , null, true);
}

mapScript.prototype.loadSpots = function(spotsData)
{
    var sameSpots = false;
    if (
    (this.thumbsLayer)
    &&
    (this.thumbsLayer.spotsData)
    &&
    (spotsData.length == this.thumbsLayer.spotsData.length)
    )
    {
        sameSpots = true;
        for (var i = 0; i < spotsData.length; i++)
        {
            if (!sameSpots)
            {
                continue;
            }
            if (spotsData[i].id != this.thumbsLayer.spotsData[i].id)
            {
                sameSpots = false;
            }
        }
    }

    if (!sameSpots)
    {
        if (this.thumbsLayer)
        {
            this.map.removeOverlay( this.thumbsLayer );
        }

        this.thumbsLayer = new thumbsLayer( spotsData, this );
        this.map.addOverlay(this.thumbsLayer);

    }
}


function thumbsLayer( spotsData, mapScript )
{
    this.spotsData = spotsData;
    this.mapScript = mapScript;
}

if (typeof GOverlay != 'undefined')
{

    thumbsLayer.prototype = new GOverlay();

    thumbsLayer.prototype.initialize = function( map )
    {
        this.map    = map;
		this.markers = [];

        for (var i = 0, spot; spot = this.spotsData[i]; i++)
        {
            spot.point = new GLatLng( parseFloat ( spot.lat ) , parseFloat( spot.lng ) );

			var spotIcon = new GIcon();
			spotIcon.image = spot.iconUrl;
			spotIcon.iconSize = new GSize(spot.iconWidth, spot.iconHeight);
			spotIcon.iconAnchor = new GPoint(spot.iconWidth / 2, spot.iconHeight / 2);
			spotIcon.infoWindowAnchor = new GPoint(0, -2);

			var options = {
				icon: spotIcon
			};

			var marker = new GMarker(spot.point, options);
			this.map.addOverlay(marker);
			this.markers.push(marker);

            spot.marker = marker;

			marker.spot = spot;

		     GEvent.addListener(marker, 'mouseover', function()
			 {
		        this.openExtInfoWindow(
		          map,
		          "spotWindow",
		          this.spot.infoHtml,
		          {beakOffset: 3}
		        );
		      });

			 GEvent.addListener(marker, "click", function(  )
	        {
				location.href = this.spot.itemUrl;
	        });
        }
    }

    thumbsLayer.prototype.redraw = function(force) {
		return;
   }
    thumbsLayer.prototype.copy = function() {
		return this;
   }
    thumbsLayer.prototype.remove = function() {
		for(var i = 0, marker; marker = this.markers[i]; ++i)
		{
			this.map.removeOverlay(marker);
		}
		return;
   }

}

function showMap()
{
    var flash = document.getElementById('introFlashBox');
    if (!flash)
    {
        return;
    }
    flash.parentNode.removeChild(flash);
}
