// JavaScript Document
var http_request = false;

function makeRequest(url) {

    http_request = false;

    if (window.XMLHttpRequest) 
    { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) 
        {
            http_request.overrideMimeType('text/xml');
        }
    } 
    else if (window.ActiveXObject) 
    { // IE
        try 
        {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e) 
        {
            try 
            {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (e) 
            {
            }
        }
    }

    if (!http_request) {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
    http_request.onreadystatechange = alertContents;
    http_request.open('GET', url, true);
    http_request.send(null);
}

function alertContents() 
{
    if (http_request.readyState == 4) 
    {
        if (http_request.status == 200) 
        {
            var responseText = http_request.responseText;
            showPins(responseText);
        }
        else 
        {
            alert('There was a problem with the request.');
        }
    }
}

var googlemap;    
var geocoder;

function decodeCSV(csvText)
{
    csvText = new String(csvText);
    var retval = new Array();

    //state is either start, insideQuotes, or insideValue
    var index = 0;
    var currentRow = null;
    var currentValue = null;
    while(index < csvText.length)
    {
        if(currentRow == null)
        {
            currentRow = new Array();
        }
        
        var currentChar = csvText.charAt(index);
        index++;
        var nextChar = (index < csvText.length) ? csvText.charAt(index) : null;

        if(currentChar == ",")
        {
            currentRow.push(currentValue);
            currentValue = null;
            continue;
        }
        else if ((currentChar == "\n") || ((currentChar == "\r") && (nextChar == "\n")))
        {
            currentRow.push(currentValue);
            currentValue = null;
            retval.push(currentRow);
            currentRow = null;
            if(currentChar == "\r")
            {
              index++
            }
            continue;
        }
        else if(currentChar == "\"")
        {
            //scan forward until we find a " or a ""
            var startIndex = index;
            var quoteIndex;
            while(true)
            {
                quoteIndex = csvText.indexOf("\"", index);
                var escapedQuoteIndex = csvText.indexOf("\"\"", index);
                
                if((quoteIndex < escapedQuoteIndex) || (escapedQuoteIndex== -1))
                {
                    break;
                }
                
                index = escapedQuoteIndex + 2;
            }
            currentValue = new String(csvText.substring(startIndex, quoteIndex));
            currentValue = currentValue.replace(/\"\"/mg, "\"");
            index = quoteIndex + 1; 
            continue;
         }
         else
         {
            var commaIndex = csvText.indexOf(",", index);
            var newlineIndex = csvText.indexOf("\n", index);
            var crIndex = csvText.indexOf("\r", index);
            
            if(commaIndex == -1) 
            {  
                commaIndex = csvText.length;
            }
            if(newlineIndex == -1)
            {
                newlineIndex = csvText.length;
            }
            if(crIndex == -1)
            {
                crIndex = csvText.length;
            }
            
            var endIndex = commaIndex;
            if(newlineIndex < endIndex)
            {
                endIndex = newlineIndex;
            }
            if(crIndex < endIndex)
            {
                endIndex = crIndex;
            }
            
            currentValue = new String(csvText.substring(index - 1, endIndex));
            index = endIndex;
            continue;
         }
    }
    if(currentRow != null)
    {
        currentRow.push(currentValue);
        retval.push(currentRow);
    }
    return retval;	   
}

function SeniorCenter(dataRow)
{
	this.Label = dataRow[0];
	
	var strPoint = dataRow[1];
	var pointRegExp = /\((.*),(.*)\)/;
	pointRegExp.exec(strPoint);
	var x = parseFloat(RegExp.$1);
	var y = parseFloat(RegExp.$2);
	this.Point = new GLatLng(x, y);
   	this.Address = dataRow[2];
   	this.Url = dataRow[3];
	this.Phone = dataRow[4];
	this.Program	= dataRow[5];
	this.GetInfoWindowHtml = SeniorCenter_getInfoWindowHtml;
}

function SeniorCenter_getInfoWindowHtml()
{
	var retVal = "";
	var haveUrl = (this.Url != null) && (this.Url != "#");
	if(haveUrl)
	{
		retVal += "<a href='";
		retVal += this.Url;
		retVal += "' target='_blank'>";
	}
	retVal += this.Label;
	if(haveUrl)
	{
		retVal += "</a>";
	}
	retVal += "<br>";
	retVal += this.Address;
	retVal += "<br><big><strong>";
	retVal += this.Phone;
	retVal += "</strong></big><br>";
	if(this.Program != null)
	{
		retVal += this.Program;
	}
	
	return retVal;  
}

function showPins(responseText)
{
    var data = decodeCSV(responseText);
    for(var i=0; i < data.length; i++)
	{
        var dataRow = data[i];
		var seniorCenter = new SeniorCenter(dataRow);

        if(seniorCenter.Address != "???")
        {
            showAddress(seniorCenter);
        }
    }
}

function showAddress(seniorCenter) 
{  
	googlemap.addOverlay(createMarker(seniorCenter));        
}

function createMarker(seniorCenter) 
{  
	var marker = new GMarker(seniorCenter.Point);  
	GEvent.addListener(
		marker, 
		"click", 
		function() 
		{    
			marker.openInfoWindowHtml(seniorCenter.GetInfoWindowHtml());  
		}
	);
	return marker;
}

function showMap(address, zoomLevel) 
{  
	geocoder.getLatLng(    
		address,    
		function(point) 
		{      
			if (!point) 
			{        
				alert(address + " not found");      
			} 
			else 
			{        
				googlemap.setCenter(point, zoomLevel);
			}    
		}  
	);
}

function zoomTo()
{
	var address = document.getElementById("address").value;
	showMap(address, 11);
}
