POI = {};
      
    // poi icon status values
	var disabled = -1;
	var off = 0;
	var on = 1;
	var MapUIImagePath = "/Map/images/"; 
	var poiArr = new Array();
	poiArr[0] = new Array(5, 'childcare', 'Child Care', false, 0); //Pin Type ID, Short Name, Display Name, Pins visible on map
	poiArr[1] = new Array(6, 'schools', 'School', false, 0);
	poiArr[2] = new Array(7, 'restaurants', 'Restaurant', false, 0);
	poiArr[3] = new Array(8, 'shopping', 'Shopping', false, 0);
	poiArr[4] = new Array(9, 'grocery', 'Grocery', false, 0);
	poiArr[5] = new Array(10, 'gas', 'Gas Station', false, 0);
	poiArr[6] = new Array(11, 'bank', 'Bank', false, 0);
	poiArr[7] = new Array(12, 'park', 'Entertainment/Recreation', false, 0);
	poiArr[8] = new Array(13, 'hospital', 'Hospital', 0);
	poiArr[9] = new Array(14, 'transport', 'Transportation', false, 0);
	poiArr[10] = new Array(15, 'lodging', 'Hotel', false, 0);
	poiArr[11] = new Array(16, 'worship', 'Worship', false, 0);
	poiArr[12] = new Array(17, 'police', 'Police Department', false, 0);
	poiArr[13] = new Array(18, 'fire', 'Fire Department', false, 0);
	poiArr[14] = new Array(19, 'library', 'Library', false, 0);
	poiArr[15] = new Array(20, 'postoffice', 'Post Office', false, 0);
	poiArr[16] = new Array(21, 'cultural', 'Cultural Center', false, 0);

// initialize the POI Control Panel
POI.initPOI = function(position)
{
	setPOIIcons(position);
}
POI.setListingIcons = function()
{
    document.write("&#160;&#160;<img border=\"0\" id=\"listing_active_on\" alt=\"Active Listing\" src=\"" + MapUIImagePath + "/POI/listing_active.gif\" \> Active");
    
    var statusDD = document.frmSearch.elements('Status');
    if (statusDD)
    {   
        for(var i=0;i<statusDD.length;i++)
        {
            if (statusDD[i].value == 'P') // Pending
                document.write("&#160;&#160;<img border=\"0\" id=\"listing_pending_on\" alt=\"Pending Listing\" src=\"" + MapUIImagePath + "/POI/listing_pending.gif\" \> Pending");    
                  
            if (statusDD[i].value == 'S') // Sold
                document.write("&#160;&#160;<img border=\"0\" id=\"listing_sold_on\" alt=\"Sold Listing\" src=\"" + MapUIImagePath + "/POI/listing_sold.gif\" \> Sold");
        }
    }
    document.write("&#160;&#160;<img border=\"0\" id=\"listing_new_on\" alt=\"New Listing\" src=\"" + MapUIImagePath + "/POI/listing_new.gif\" \> New");
    document.write("&#160;&#160;<img border=\"0\" id=\"listing_openhouse_on\" alt=\"Open House\" src=\"" + MapUIImagePath + "/POI/listing_openhouse.gif\" \> Open");
    document.write("&#160;&#160;<img border=\"0\" id=\"listing_multiple_on\" alt=\"More than one Listing in same location\" src=\"" + MapUIImagePath + "/POI/listing_multiple.gif\" \> Multiple");
}
POI.setListingIconsToggle = function()
{
    document.write("<table width='100%' cellspacing='1' cellpadding='1'>")
    
    // Active will always display
    document.write("<tr id='activeToggle'><td align='left' class='txt_poi'>");
    document.write("<a href=\"javascript:;\"  onClick=\"javascript:Search.setListingStatusFromIcon('A,B,C')\";>");
    document.write("<img border=\"0\" id=\"listing_A,B,C_on\" alt=\"Click here to Remove Active Listings from Map\" src=\"" + MapUIImagePath + "/POI/listing_active.gif\"\>");
    document.write("<img style=\"display:none\" border=\"0\" id=\"listing_A,B,C_off\" alt=\"Click here to Display Active Listings on Map\" src=\"" + MapUIImagePath + "/POI/listing_active_off.gif\"\>");
    document.write(" Active</a></td></tr>");
    
    var statusDD = document.frmSearch.elements['Status'];
    if (statusDD)
    {   
        var len = statusDD.length;
        var checked = false;
        if (len > 0)
        {
            for(var i=0;i<statusDD.length;i++)
            {
                if (statusDD[i].value == 'P') // Pending
                {
                    document.write("<tr><td align='left' class='txt_poi'>");
                    document.write("<a href=\"javascript:;\"  onClick=\"javascript:Search.setListingStatusFromIcon('P')\";>");
                    document.write("<img style=\"display:none\" border=\"0\" id=\"listing_P_on\" alt=\"Click here to Remove Pending Listings from Map\" src=\"" + MapUIImagePath + "/POI/listing_pending.gif\"\>");
                    document.write("<img style=\"display:none\" border=\"0\" id=\"listing_P_off\" alt=\"Click here to Display Pending Listings on Map\" src=\"" + MapUIImagePath + "/POI/listing_pending_off.gif\"\>");
                    document.write(" Pending</a></td></tr>");
                }
                if (statusDD[i].value == 'S') // Sold
                {
                    document.write("<tr><td align='left' class='txt_poi'>");
                    document.write("<a href=\"javascript:;\"  onClick=\"javascript:Search.setListingStatusFromIcon('S')\";>");
                    document.write("<img style=\"display:none\" border=\"0\" id=\"listing_S_on\" alt=\"Click here to Remove Sold Listings from Map\" src=\"" + MapUIImagePath + "/POI/listing_sold.gif\"\>");
                    document.write("<img style=\"display:none\" border=\"0\" id=\"listing_S_off\" alt=\"Click here to Display Sold Listings on Map\" src=\"" + MapUIImagePath + "/POI/listing_sold_off.gif\"\>");
                    document.write(" Sold</a></td></tr>");
                }
                if (statusDD[i].checked)
                {
                    display('listing_' + statusDD[i].value + '_on');
                    hide('listing_' + statusDD[i].value + '_off');
                    Search.setListingStatusWithoutSubmit(statusDD[i]);
                    checked = true;
                }
                else
                {
                    display('listing_' + statusDD[i].value + '_off');
                    hide('listing_' + statusDD[i].value + '_on');
                }
            }
            
        }
        else
        {
            hide('poiListing');
        }
      
    }
    else
    {
        hide('poiListing');
       
    }    
    document.write("</table>"); 
      
}
POI.toggleListingIcon = function(status, hide)
{
    if (hide == 1)
    {
        hide('listing_' + status + '_on');
        display('listing_' + status + '_off');
    }
    else
    {
        hide('listing_' + status + '_off');
        display('listing_' + status + '_on');
    }
}

POI.isPOIZoom = function()
{// TODO: set zoom value variable
    if (map.getZoomLevel() >= 12) // only enable POI if zoom level >= 12
        return true;
    else
        return false;
}

POI.toggleIconsOnZoom = function()
{
    var isPOIZoom = POI.isPOIZoom();
    for(var i=0; i<poiArr.length; i++)
    {
        if (isPOIZoom) // if valid POI display zoom level
        {
            if (poiArr[i][4] == 1)
                toggleIconImage(i, 'on'); 
            else
                toggleIconImage(i, 'off'); 

        }
        else
        {
            toggleIconImage(i, 'dis'); // disable all icons
        }
    }
    //if (isPOIZoom) 
        //zoomInToViewPOIMsg(0); // hide message if displaying
}

var lockTimerID = null;
var waitTimerID = null;
var eventTimerID = null;
var WAIT_TIMER_MS = 1000; // Time to wait before we request data for an area
var LOCK_TIMER_MS = 1000; // Time to wait with processing message displaying
var EVENT_TIMER_MS = 1000;

function clearLockTimer()
{
    map.toggleProcessing(0); //hide
}

function clearWaitTimer()
{
    clearTimeout(waitTimerID);
    POI.updatePOI();
}
POI.displayPOI = function()
{
    //if (POI.isPOIZoom())
    //    zoomInToViewPOIMsg(0); 
    //else
    //    zoomInToViewPOIMsg(1); 

    POI.toggleIconsOnZoom();
    clearTimeout(waitTimerID);
    waitTimerID = self.setTimeout('clearWaitTimer()', WAIT_TIMER_MS);
}
POI.checkAnyPOIIsOn = function()
{
    for(var i=0; i<poiArr.length; i++)
    {
        if (poiArr[i][4] == 1)
            return true;
    }
    return false;
}
POI.updatePOI = function()
{
    var evalText = '';
    //alert('updating: ' + POI.checkAnyPOIIsOn());
    if (POI.checkAnyPOIIsOn()) // only update if any poi displaying
    { 
        if (POI.isPOIZoom())
        {
            if (map.isInCachedArea()) // in cached area
            {//alert('updating - cached area');
                for(var i=0; i<poiArr.length; i++)
                {
                   //var value = getCategoryValue(i);
                   //var categoryCount = getPinCount(poiArr[i][0])
                   if (poiArr[i][4] == 0)
                        toggleIconImage(i, 'off'); // enable icon if disabled       
                               
                   if (!(poiArr[i][3]) && poiArr[i][4] == 1) // if pins not already displaying
                   {
                        poiArr[i][3] = true; // set pin display value to true
                        toggleIconImage(i, 'on'); // show icons selected state
                        pushpin.addPinArrayToMap(eval('pin' + poiArr[i][0])); // display pins
                   }
                }
            }
            else // not in cached area
            {//alert('updating - not cached');
                for(var i=0; i<poiArr.length; i++) 
                {
                    if (poiArr[i][3]) // if pins already displaying
                    {
                        poiArr[i][3] = false; // set display status to false
                        evalText = 'pin' + poiArr[i][0];
                        pushpin.deletePinArrayFromMap(eval(evalText), poiArr[i][0]); // remove pins
                    }
                    evalText = 'pin' + poiArr[i][0]  + ' = new Array();'
                    eval(evalText);
                }
                //POI.deleteAllPins();
                getPOIData(); // get new data
            }
        }
        else // zoomed Out
        {//alert('updating - zoom out');
            for(var i=0; i<poiArr.length; i++)
            {
                // disable all icons
                poiArr[i][3] = false; 
                toggleIconImage(i, 'dis');
                evalText = 'pin' + poiArr[i][0];
                if (veMap.GetMapStyle() != VEMapStyle.Birdseye)
                    pushpin.deletePinArrayFromMap(eval(evalText), poiArr[i][0]); // remove all pins
            }
            //pushpin.deleteAllPins();
        }
    }
}
POI.toggleIcons = function(chk)
{
   if (chk.checked)
    {
        pushpin.commitListingPinsFromChkbx();
        chk.title = "Unchecking the checkbox will remove listing pins from the map";
    }
    else
    {
        // pushpin.deleteListingPinsFromChkbx();
        pushpin.deleteListingPinsFromMap();
        chk.title = "Checking the checkbox will add listing pins to the map";
    }
}
//POI.toggleListing = function(status) //status: 1-on 0-off
//{
//    if (status == 1)
//    {
//        hide('listing_off');
//        display('listing_on');
//        if (pin1)
//            pushpin.addPinArrayToMap(pin1);
//    }
//    else
//    {
//        hide('listing_on');
//        display('listing_off');
//        pushpin.deletePinArrayFromMap(pin1, 1);
//    }
//}

POI.toggleAllPOI = function(status)
{    
    if (isProcessing)
        return;
        
    if (POI.isPOIZoom())
    {
       // map.toggleProcessing(1);
        isProcessing = false;
        self.setTimeout('toggleEventDelay(' + status + ')', EVENT_TIMER_MS);
    }
    else
    {        
        //zoomInToViewPOIMsg(1); 
    }
}

//--------------------------------------------------------------------------------------------------------
// Helpers
function setPOIIcons(position)
{
    if (position == "popup")
    {
        for (var i = 0; i < poiArr.length; i++) {
            document.write("<div class=\"poi-icon\">");
            document.write("<a href=\"javascript:;\"  onClick='javascript:togglePOI(" + i + ")';>");
            document.write("<img style=\"display:none\" border=\"0\" id=\"poi_" + i + "_dis\" title=\"Points of Interest disabled. Please zoom in to enable.\" alt=\"Points of Interest disabled. Please zoom in to enable.\" src=\"" + MapUIImagePath + "/POI/" + poiArr[i][1] + "_dis.gif\"\>");
            document.write("<img style=\"display:none\" border=\"0\" id=\"poi_" + i + "_on\" title=\"Remove " + poiArr[i][2] + " Icons from Map\" alt=\"Remove " + poiArr[i][2] + " Icons from Map\" src=\"" + MapUIImagePath + "/POI/" + poiArr[i][1] + "_on.gif\"\>");
            document.write("<img style=\"display:none\" border=\"0\" id=\"poi_" + i + "_off\" title=\"Display " + poiArr[i][2] + " Icons on Map\" alt=\"Display " + poiArr[i][2] + " Icons on Map\" src=\"" + MapUIImagePath + "/POI/" + poiArr[i][1] + "_off.gif\"\>");
            document.write("</a></div>");
            toggleIconImage(i, 'dis');
        }
    }
    else if (position == "left")
    {
        document.write("<table width='100%' cellspacing='1' cellpadding='1'>")
        for(var i=0; i<poiArr.length; i++)
        {
            document.write("<tr>");
            document.write("<td align='left' class='txt_poi'>");
            document.write("<a href=\"javascript:;\"  onClick='javascript:togglePOI(" + i + ")';>");
		    document.write("<img style=\"display:none\" border=\"0\" id=\"poi_" + i + "_dis\" alt=\"Points of Interest disabled. Please zoom in to enable.\" src=\"" + MapUIImagePath + "/POI/" + poiArr[i][1] + "_dis.gif\"\>");
		    document.write("<img style=\"display:none\" border=\"0\" id=\"poi_" + i + "_on\" alt=\"Remove " + poiArr[i][2] + " Icons from Map\" src=\"" + MapUIImagePath + "/POI/" + poiArr[i][1] + "_on.gif\"\>");
		    document.write("<img style=\"display:none\" border=\"0\" id=\"poi_" + i + "_off\" alt=\"Display " + poiArr[i][2] + " Icons on Map\" src=\"" + MapUIImagePath + "/POI/" + poiArr[i][1] + "_off.gif\"\>");
            document.write(" " + poiArr[i][2] + "</a></td>");
           // document.write("<input type=\"hidden\" name=\"POI/enbl_" + poiArr[i][1] + "\" id=\"enbl_" + poiArr[i][1] + "\" value=\"0\"/>");
            document.write("</tr>");

            toggleIconImage(i, 'dis');
        }
        document.write("</table>"); 
        
        var divListing = document.getElementById('poiListing');
        if (divListing && divListing.style.display == 'none')
        {
            hide('otherLabel');
            document.getElementById('OtherIconsContainer').style.height = 312;
       }
    }
    else // top
    {
        for(var i=0; i<poiArr.length; i++)
        {
            document.write("<span>");
            document.write("<a href=\"javascript:;\"  onClick='javascript:togglePOI(" + i + ")';>");
		    document.write("<img style=\"display:none\" border=\"0\" id=\"poi_" + i + "_dis\" alt=\"Points of Interest disabled. Please zoom in to enable.\" src=\"" + MapUIImagePath + "/POI/" + poiArr[i][1] + "_dis.gif\"\>");
		    document.write("<img style=\"display:none\" border=\"0\" id=\"poi_" + i + "_on\" alt=\"Remove " + poiArr[i][2] + " Icons from Map\" src=\"" + MapUIImagePath + "/POI/" + poiArr[i][1] + "_on.gif\"\>");
		    document.write("<img style=\"display:none\" border=\"0\" id=\"poi_" + i + "_off\" alt=\"Display " + poiArr[i][2] + " Icons on Map\" src=\"" + MapUIImagePath + "/POI/" + poiArr[i][1] + "_off.gif\"\>");
            document.write("</a> ");
            toggleIconImage(i, 'dis');
            document.write("</span>");
        }
        
    }
}

function getIconStatus(poiID) 
{
    if (document.getElementById('poi_' + poiID + '_dis').style.display == '')
        return 'dis';
    if (document.getElementById('poi_' + poiID + '_on').style.display == '')
        return 'on';
    if (document.getElementById('poi_' + poiID + '_off').style.display == '')
        return 'off';
}
function toggleIconImage(poiID, status) // id = poiArray Position, status = 'dis' or 'on' or 'off'
{
    // hide all buttons
    hide('poi_' + poiID + '_dis');
    hide('poi_' + poiID + '_on');
    hide('poi_' + poiID + '_off');
    
    if (status == 'on')
        poiArr[poiID][4] = 1;
     else if (status == 'off')
        poiArr[poiID][4] = 0;
    // display current status button
    display('poi_' + poiID + '_' + status);
}
function togglePOI(poiID)
{
    var arrName = 'pin' + poiArr[poiID][0]; // pin array name = pin<pinTypeID>
    //var pinCount = pushpin.getCountByTypeID(poiArr[poiID][0]); // get number of pins on the map for the particular type

    if (POI.isPOIZoom())
    {
        //if (isProcessing)
            //return;
            
        var status = getIconStatus(poiID);
        if (status == 'on') // unsel it
        {
            poiArr[poiID][3] = false;
            toggleIconImage(poiID, 'off');
            pushpin.deletePinArrayFromMap(eval(arrName), poiArr[poiID][0]);
        }
        else // sel it
        {
            //poiArr[poiID][3] = true;
            toggleIconImage(poiID, 'on');
            POI.updatePOI();
            //pushpin.addPinArrayToMap(eval(arrName));
            //zoomInToViewPOIMsg(0); // hide message if displaying
        }
    }
    else
    {
        //zoomInToViewPOIMsg(1); // display message
    }
}
function toggleEventDelay(status)
{
    for(var i=0; i<poiArr.length; i++)
    {
        // Using opposite value because of toggle functionality
        var curStatus = getIconStatus(i);     
                   
        if ((curStatus == 'off' && status==1) || (curStatus == 'on' && status ==0))            
            togglePOI(i);
    }
}

function zoomInToViewPOIMsg(display)
{
    message.toggleMessage('zoomInToViewPOI', display); 
}
//---------------------------------------------------------------------------------------------------------
// ajax call
function getPOIData()
{
   // Perform Ajax call and set timed event to add pushpins
   // when response is processed
   // get coordinates
   //alert('getting');
    var neLat = document.getElementById("NELat").value;
    var neLong = document.getElementById("NELong").value;
    var swLat = document.getElementById("SWLat").value;
    var swLong = document.getElementById("SWLong").value; 
    
    var midLat = parseFloat(neLat) + parseFloat(swLat);
    var midLong = parseFloat(neLong) + parseFloat(swLong);
    var url = '/Map/AJAX/GetPOI.aspx?neLat=' + neLat + '&neLong=' + neLong + '&swLat=' + swLat + '&swLong=' + swLong;
    
    if (!isProcessing)
    {
        map.toggleProcessing(1); // visible
        ajax.loadXMLDoc(url, processAjaxResponse);
    }
    else 
    {
        //alert('already processing');
    }
}

function processAjaxResponse() 
{   
    if (xmlhttp.readyState == 4) 
    { 
        try
        {
            eval(xmlhttp.responseText);
            
        }
        catch(e)
        {
            // Sometimes the async calls get invalidated by an exception.
            // It is unclear what causes the exception. Iterim solution is 
            // to reinitalize.
            isProcessing = false;
            getPOIData();
            return;
        }
        //alert('processing');



        map.cacheArea();
        POI.updatePOI();
        self.setTimeout('clearLockTimer()', LOCK_TIMER_MS);
        isProcessing = false;
    }
}
