//<![CDATA[
var mapx;
var mapdivx;
var gcoderx;
var mgrx;
var xconfig;
var datax;
var initScriptx;


function loadx() {
  if (GBrowserIsCompatible()) {
      mapdivx = document.getElementById("xmap");

      if (mapdivx == null) {
          return;
      }
      
      mapx = new GMap2(mapdivx);
        
      mapx.enableDoubleClickZoom();
   
      gcoderx = new GClientGeocoder();
    
      GDownloadUrl(xconfig,HandleConfigResponsex);                    
  }
}

function HandleConfigResponsex(vdata,responseCode)
{
    var fail = '';
    if (responseCode==200)
    {
        var xml = GXml.parse(vdata);
        if (xml)
        {                                        
            //alert(vdata);     //uncomment line to debug xml created         
            xconfig = xml.documentElement.getElementsByTagName('config')[0];
            datax = xml.documentElement.getElementsByTagName('data')[0];
            initScriptx = xml.documentElement.getElementsByTagName('initscript')[0];
            fail = GmapInitx(); 
        } else {
            fail = 'error on xml';
        }
    } else {
        fail = 'error on get configuration';
    }
    if  ((fail) && (fail!=''))
        alert(fail);
}

function Initializex()
{
    if (initScriptx.firstChild)
    {
        if (initScriptx.firstChild.data)
        {
            eval(initScriptx.firstChild.data);
        }
    }
}


function GmapInitx()
{
    //size part
    var size = xconfig.getElementsByTagName('size')[0];
    var width = size.getElementsByTagName('width')[0].firstChild.data;
    var height = size.getElementsByTagName('height')[0].firstChild.data;
    setMapSizex(width,height);
    
    //map center
    var center = xconfig.getElementsByTagName('center')[0];
    center = center.getElementsByTagName('poi')[0];
    
    //add zoom control
    mapx.addControl(new GSmallMapControl());

    var gcenter = new gAddressx(center);
    var zoom = xconfig.getElementsByTagName('zoom')[0].firstChild.data;
    zoom = parseInt(zoom);
    findCenterx(gcenter,zoom);
    
    setTimeout(GmapPutDatax,500);
    if (initScriptx)
        setTimeout(Initializex,500);
    
}
function GmapPutDatax()
{
    var pois = datax.getElementsByTagName('poi');
    
    for (var i = 0; i<pois.length; i++)
    {
        var poi = new gAddressx(pois[i]);  
        updateMarkerx(poi, null);
    }
}

function centerItx(ll,zoom)
{
    mapx.setCenter(ll,zoom);     
}
function findCenterx(address,zoom)
{
    var ll = null;
    
    if ( (address.lat) && (address.lng))
    {
        ll = new GLatLng(address.lat,address.lng);
        centerItx(ll,zoom);
        
    } else {        
        var where = address.city + ',' + address.country;
        gcoderx.getLatLng(where ,function(point)
                                {                                                                
                                    if (point)
                                    {      
                                       centerItx(point,zoom);
                                    } else {
                                        gcoderx.getLatLng(address.country,function(point)
                                        {
                                            if (point)
                                                centerItx(point,zoom);
                                        });
                                    }
                                 }
       );

    }
}

//creates a custom icon with onclick text
function createMarkerx(point,html,icon) {
        var marker;
        if(icon)
            marker = new GMarker(point,icon);
        else
            marker = new GMarker(point);
        if(html)
        {
            GEvent.addListener(marker, "click", 
                function() {
                    marker.openInfoWindowHtml(html);
            });
        }    
        return marker;        
      }
      
function updateMarkerx(address,marker)
{
    var ll = null;
    
    if ( (address.lat) && (address.lng))
    {        
        ll = new GLatLng(address.lat,address.lng);
        var theIcon = null;
        
        if( (address.icon) && (address.icon.indexOf(".") > 0 ) )
        {
            var baseIcon = new GIcon();
            //baseIcon.iconSize=new GSize(60,30);
            //baseIcon.shadowSize=new GSize(56,32);
            baseIcon.iconAnchor=new GPoint(16,32);
            baseIcon.infoWindowAnchor=new GPoint(16,0);
            baseIcon.shadow=address.icon;
            //creating custom icon
            theIcon = new GIcon(baseIcon, address.icon);            
        }
        
        var marker = createMarkerx(11, address.info, theIcon)
        //add marker to map     
        marker.setPoint(ll);
        mapx.addOverlay(marker);
        
    } else {        
        var where = address.city + ',' + address.country;
        gcoderx.getLatLng(where ,function(point)
                                {                                                                
                                    if (point)
                                    {      
                                        uMarker(point,marker,address.info);
                                    } else {
                                        gcoderx.getLatLng(address.country,function(point)
                                        {
                                            if (point)
                                                uMarker(point,marker,address.info);
                                        });
                                    }
                                 }
       );

    }
}

  

function setMapSizex(width,height) {
    if (width.toString().indexOf("em", 0) == -1) {
        mapdivx.style.width = width + 'px';
    }
    else 
    {
        mapdivx.style.width = width;
    }

    if (height.toString().indexOf("em", 0) == -1) {
        mapdivx.style.height = height + 'px';
    } 
    else 
    {
        mapdivx.style.height = height;
    }
    mapx.checkResize();
}

//build data object based on config xml
function gAddressx(xnode)
{
    if (xnode.getElementsByTagName('city')[0].firstChild)     
        this.city = xnode.getElementsByTagName('city')[0].firstChild.data;
    else
        this.city = '';
        
    this.info = '';    
    if (xnode.getElementsByTagName('info')[0])
    {    
        if (xnode.getElementsByTagName('info')[0].firstChild)     
            this.info = xnode.getElementsByTagName('info')[0].firstChild.data;
    } 
    
    if (xnode.getElementsByTagName('country')[0].firstChild)             
        this.country = xnode.getElementsByTagName('country')[0].firstChild.data;
    else
        this.country = '';
    var gpoint = xnode.getElementsByTagName('gpoint')[0];   
    if (gpoint.getElementsByTagName('lat')[0].firstChild)     
        this.lat = parseFloat(gpoint.getElementsByTagName('lat')[0].firstChild.data);
    else 
        this.lat = null;
    if (gpoint.getElementsByTagName('lng')[0].firstChild)
        this.lng = parseFloat(gpoint.getElementsByTagName('lng')[0].firstChild.data);        
    else
        this.lng = null;
      
    this.icon = null;    
    if (xnode.getElementsByTagName('icon')[0])
    {  
        if (xnode.getElementsByTagName('icon')[0].firstChild)
            this.icon = xnode.getElementsByTagName('icon')[0].firstChild.data;
    }
     
    this.iconshadow = null; 
    if (xnode.getElementsByTagName('iconshadow')[0])
    {            
        if (xnode.getElementsByTagName('iconshadow')[0].firstChild)     
            this.iconshadow = xnode.getElementsByTagName('iconshadow')[0].firstChild.data;
    }       
                          
        
}
//]]>