var boxesPerRun = 40;
var bubbleDelay = 5000;
var maxZoom = 8;
var minZoom = 3;
var boxSeed = -1;
//don't change these
var map;
var winList = null;
var currWin = 0;
var lastId = 0;
var paused = false;
var infoField = 'religion';

function initializeMap() {
    if (GBrowserIsCompatible() && document.getElementById("waitingmap")) {
        map = new GMap2(document.getElementById("waitingmap"));    
        var mt = map.getMapTypes();
        for (var i=0; i<mt.length; i++) {
            mt[i].getMinimumResolution = function() {return minZoom;}
            mt[i].getMaximumResolution = function() {return maxZoom;}
        }
        map.setCenter(new GLatLng(37.4419, -90.1419), 3);
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.enableDoubleClickZoom();
        map.enableScrollWheelZoom();
        window.setTimeout('getInfoWins(0);', 5000);
    }
}

function getInfoWins(start) {
	 if (boxSeed==null || boxSeed < 0) boxSeed = Math.floor(Math.random()*1000000);
	 if (start==null) start = 0;
    GDownloadUrl("/waitingfamilies/gmap_ajax.php?seed="+boxSeed+"&s="+start,infoWinResponse);    
}

infoWinResponse = function(data) {
        var xml = GXml.parse(data);
        winList = xml.documentElement.getElementsByTagName("win");
        lastId += winList.length;
        if (winList.length == 0) {
        	boxSeed = null;
        	lastId = 0;
        	getInfoWins();
        } else {
	        currWin = 0;
	        window.setTimeout('nextInfoWindow();', 1000);
			}
}


function nextInfoWindow() {
    if(paused) {
        window.setTimeout('nextInfoWindow()', 6000);
    }
    else if(winList[currWin] != null) {
        var lat = parseFloat(winList[currWin].getAttribute('lat'));
        var lon = parseFloat(winList[currWin].getAttribute('lon'));
        var id = winList[currWin].getAttribute('id');
        var salutation = winList[currWin].getAttribute('salutation');
        var gender = winList[currWin].getAttribute('gender');
        var herhobbies = winList[currWin].getAttribute('herhobbies');
        var hishobbies = winList[currWin].getAttribute('hishobbies');
        var religion = winList[currWin].getAttribute('religion');
        var latLng = new GLatLng(lat, lon);
        var pic = winList[currWin].getAttribute('pic');
		var state = winList[currWin].getAttribute('state');
		var ethnicity = winList[currWin].getAttribute('ethnicity');
	    var specfield = winList[currWin].getAttribute(infoField);
    	windowHTML = '<table border="0"><tr><td><a href="/waitingfamilies/show.html/'+id+'?GOOGLE"><img src="'+pic+'" width="64" height="64" style="border:1px solid #0099FF"></a></td><td valign="top"><a href="/waitingfamilies/show.html/'+id+'/profile?GOOGLE">'+salutation+'</a><BR />'+specfield+', '+state+"<br />Pref Baby Gender: " + gender + "</tr></td></table>";
        //draw the popup
        map.openInfoWindowHtml(latLng, windowHTML, {'noCloseOnClick':true});
        map.panTo(latLng);

        
        if(currWin < winList.length-1) {
            currWin++;
            window.setTimeout('nextInfoWindow();', bubbleDelay);
        }
        else if(winList.length < boxesPerRun) {
            window.setTimeout('getInfoWins('+lastId+');', 12000);
        }
        else {
            getInfoWins(lastId);
        }
    }
    else {
        window.setTimeout('getInfoWins('+lastId+');', 6000);
    }
}

function pauseWindows(id) {
    paused = !paused;
    document.getElementById('pause_'+id).src = (paused ? IMAGE_HOST+'/images/Icon_Play.gif' : IMAGE_HOST+'/images/Icon_Pause.gif');
}
