//<![CDATA[

function GMload(divId, mapWidth, mapHeight, centerXML, pois) 
{
  if (GBrowserIsCompatible())
  { 
    var mapdiv = document.getElementById(divId);
    if(mapdiv == null)
        return;
        
    var map = new GMap2(mapdiv, {size:new GSize(mapWidth,mapHeight)});
    map.enableScrollWheelZoom();
    map.enableDoubleClickZoom();
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.addMapType(G_PHYSICAL_MAP);    
    GmapInit(mapdiv, map, mapWidth, mapHeight, centerXML, pois); 
  }
}

function GmapInit(mapdiv, map, width, height, center, pois)
{
    //Set size
    setMapSize(mapdiv,map,width,height);
    
    //Set center and zoom of map
    var centerDOM = GXml.parse(center);
    
    if(!centerDOM)
        return;
    var gcenter = new point(centerDOM);
    var zoom = centerDOM.getElementsByTagName('zoom')[0].firstChild.data;
    zoom = parseInt(zoom);
    
    findCenter(map, gcenter,zoom);
    GmapPutData(map,pois);
}
function GmapPutData(map,pois)
{
    if(pois != null && pois != '')
    {   
        //Add a container for GXml.parse()
        pois = '<pois>' + pois + '</pois>';
        var xml = GXml.parse(pois);
        if(!xml)
            return;
        
        var nodes = xml.documentElement.getElementsByTagName('poi'); 
        
        if(nodes.length > 0)
        {
            for (var i = 0; i<nodes.length; i++)
                updateMarker(map,new point(nodes[i]));
        }
    }
}

function centerIt(map,ll,zoom)
{
    map.setCenter(ll,zoom);     
}
function findCenter(map, address,zoom)
{
    var ll = null;
    
    if ( (address.lat) && (address.lng))
    {
        ll = new GLatLng(address.lat,address.lng);
        centerIt(map,ll,zoom);
    }
}

function updateMarker(map,pointObject)
{
    var ll = null;
    
    if ( (pointObject.lat) && (pointObject.lng))
    {        
        ll = new GLatLng(pointObject.lat,pointObject.lng);
        uMarker(map,ll,pointObject.info);
    } 
}
function uMarker(map,ll,info)
{
    var ic = new GIcon();
    ic.image = "/images/GMarker.png";
    ic.shadow = "/images/GMarkerShadow.png";
    ic.transparent = "/images/GMarkerTransparent.png";
    ic.infoWindowAnchor = new GPoint(10, 33);
    ic.iconAnchor = new GPoint(10, 33);
    
    var opts = new Object();
    opts.icon = ic;
    opts.clickable = true;

    var marker = new GMarker(ll, opts);
    GEvent.addListener(marker,'click',function()
                                        { 
                                            marker.openInfoWindowHtml(info); 
                                        }
    );
    map.addOverlay(marker);
}
function setMapSize(mapdiv, map, width, height)
{
    mapdiv.style.width=width+'px';
    mapdiv.style.height=height+'px';
    map.checkResize();
}
function point(xnode)
{
    this.lat = null;
    this.lng = null;
    this.info = null;
    
    if(xnode.getElementsByTagName('lat')[0])
        if(xnode.getElementsByTagName('lat')[0].firstChild)
            this.lat = xnode.getElementsByTagName('lat')[0].firstChild.nodeValue;
    
    if(xnode.getElementsByTagName('lng')[0])
        if(xnode.getElementsByTagName('lng')[0].firstChild)
            this.lng = xnode.getElementsByTagName('lng')[0].firstChild.nodeValue;
    
    if(xnode.getElementsByTagName('pointinfo')[0])
        if(xnode.getElementsByTagName('pointinfo')[0].firstChild)
            this.info = xnode.getElementsByTagName('pointinfo')[0].firstChild.nodeValue;
}
//]]>
