var map = null;
var gMap;

window.onload = function()
{
    if (typeof GUnload == 'undefined')
    {
        return;
    }

    window.onunload = GUnload;

    if (typeof mapConfig != 'undefined' )
    {
        map = new mapScript( mapConfig );

        if (!map.ok)
        {
            return;
        }
        map.initGMap();

    }
    else
    {


    }
}


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.ok = true;
}

mapScript.prototype.initGMap = function()
{

    if (this.config.points.length < 1)
    {
        return;
    }

    this.map = new GMap2( this.box );
    this.map.setMapType( G_HYBRID_MAP );
	gMap = this.map;


    var center = new GLatLng( parseFloat ( this.config.center.lat ) , parseFloat ( this.config.center.lng ));



    with (this.map)
    {
        addControl(new GLargeMapControl());

        enableScrollWheelZoom();
        enableDoubleClickZoom();
        enableContinuousZoom();

        setCenter( center , this.config.defaultZoom );

        var tmpInfoWindow = getInfoWindow(); // init

        // add points
        var points = [];
        for (var i=0; i < this.config.points.length; i++)
        {
            var pointData = this.config.points[i];

            if (
                (pointData.lat.toString().length < 1)
                ||
                (pointData.lng.toString().length < 1)
                ||
                (pointData.iconPng.length < 1)
                ||
                (pointData.iconGif.length < 1)
            )
            {
                continue;
            }

            var point = new GLatLng( parseFloat ( pointData.lat ) , parseFloat ( pointData.lng) );
            points[points.length] = point;


            G_DEFAULT_ICON

            var icon              = new GIcon( G_DEFAULT_ICON );



            icon.image            = pointData.iconPng;
            icon.printImage       = pointData.iconGif;

            // icon.iconSize         = new GSize(18, 18);
            // icon.iconAnchor       = new GPoint(9, 9);
            // icon.infoWindowAnchor = new GPoint(9, 9);

            var marker = new GMarker(point, icon);
            marker.pointData = pointData;

            GEvent.addListener(marker, "mouseover", function()
	        {
                this.openInfoWindowHtml(this.pointData.infoWindowHtml);
	        });
            GEvent.addListener(marker, "click", function()
	        {
                document.location.href = this.pointData.pointUrl;
	        });


            addOverlay( marker );
        }

        /*

        // zoom / center to fit all points
        var hasMultiplePoints = (points.length > 1);

        var bounds = new GBounds( points );
        var sw = new GLatLng( bounds.maxY,  bounds.minX);
        var ne = new GLatLng( bounds.minY,  bounds.maxX);

        var b = new GLatLngBounds(sw, ne);
        var newzoom = getBoundsZoomLevel( b );

        if (newzoom > 8)
        {
            newzoom = 8;
        }
        var newcenter = b.getCenter();
        setCenter (newcenter, newzoom);

        */

    }
    var self = this;
}


