var map=null,marker=null,mapEl=null,currMap=0,mapInt=null,prevMap=-1;
var point;
var geocoder;
function CapitalCitiesCache()
{
    GGeocodeCache.apply(this);
}
CapitalCitiesCache.prototype = new GGeocodeCache();
CapitalCitiesCache.prototype.reset = function() {
    GGeocodeCache.prototype.reset.call(this);
    for (var j in revArr) {
      this.put(revArr[j].name, revArr[j]);
    }
  }
function addAddressToMap(response)
{
    map.clearOverlays();
    if (response && response.Status.code != 200) 
    {
        alert("Unable to locate " + decodeURIComponent(response.name));
    }
    else 
    {
        point=new GLatLng(response.lat,response.lon);
        map.setCenter(point,16);
        marker=new GMarker(point)
        map.addOverlay(marker);
        marker.openInfoWindowHtml(response.descr);
        //map.openInfoWindowHtml(point,revArr[currMap].descr);
    }
}
function initMap()
{
    if(!mapEl)
    {
        mapEl=document.createElement("div");
        mapEl.style.float='left';
        mapEl.style.width='421px';
        mapEl.style.height='400px';
        document.getElementById("mapScrollContent").appendChild(mapEl);
        map=new GMap2(mapEl);
        point=new GLatLng(revArr[currMap].lat,revArr[currMap].lon);
	    map.setCenter(point,16);
	    marker=new GMarker(point)
	    map.addOverlay(marker)
	    marker.openInfoWindowHtml(revArr[currMap].descr);
	    //map.openInfoWindowHtml(point,revArr[currMap].descr);
	    geocoder = new GClientGeocoder();
        geocoder.setCache(new CapitalCitiesCache());
    }
    else
    {
        if(marker)
        {
            //marker.remove();
            marker=null;
        }
        if(point)
            point=null;
        geocoder.getLocations(revArr[currMap].name, addAddressToMap);       
    }
    if(prevMap!=-1)
    {
        var div=$('rl'+prevMap);
        div.style.background=(prevMap%2)==1?'#FFFFFF':'#FFEBCC';
        div.style.color='';
    }
    var div=$('rl'+currMap);
    div.style.background='#FF9900';
    div.style.color='#FFFFFF';
//    point=new GLatLng(revArr[currMap].lat,revArr[currMap].lon);
//    map.setCenter(point,16);
//    marker=new PdMarker(point);
//    map.addOverlay(marker);
//    marker.openInfoWindowHtml(revArr[currMap].descr);
    prevMap=currMap;
}
function nextMap()
{
    currMap++;
    if(currMap>=revArr.length)
        currMap=0;
    initMap();
}
function previousMap()
{
    currMap--;
    if(currMap<0)
        currMap=revArr.length-1;
    initMap();
}
function mapContainerMouseEvent(evType)
{
    if(!revArr.length)
        return;
    if(mapInt)
    {
        clearInterval(mapInt);
        mapInt=null;
    }
}
function initMaps()
{
    if(revArr.length)
    {
        initMap();
    }
}