var strOldLayerArray = "";
var gblArryChartsPrint = [];

/************************************************************
displayCharts
Author: Felix de Bie
Date: Nov 27/2008

Description:

Takes the list of currently visible charts, and displays
them on the map
************************************************************/

function displayCharts(charts){

   var chartList=charts.split("|");
   var hideList=chartList[0].split(",");
   var addList=chartList[1].split(",");

   document.getElementById('loadingDivMap').style.display = 'block';

   if (chartList[0].length!=0) {

      for (var i = 0; i < hideList.length; i++)
         {
            objTemp = eval('v'+hideList[i]);
            oGMap.removeOverlay(objTemp);
            objTemp.hide();

            // Remove the object from the printable array.
            for (var intCounter = 0; intCounter < gblArryChartsPrint.length; intCounter ++)
            {
                if (objTemp == gblArryChartsPrint[intCounter])
                {
                    gblArryChartsPrint.splice(intCounter, 1);
                    break;
                }
            }
         }
   }
   if (chartList[1].length>2) {

      for (var i = 0; i < addList.length; i++)
         {
             objTemp = eval('v'+addList[i]);
             oGMap.addOverlay(objTemp);
             objTemp.show();

            // Add the object to the printable array.
            gblArryChartsPrint.push(objTemp);
         }
   }

    document.getElementById('loadingDivMap').style.display = 'none';
}



/************************************************************
hideCharts
Author: Felix de Bie
Date: Nov 27/2008

Description:

Takes the list of currently visible charts, and removes
them from the map
************************************************************/

function hideCharts(charts){
   var chartList=charts.split(",");

   for (var i = 0; i < chartList.length; i++)
      {
         objTemp = eval('v'+chartList[i]);
         oGMap.removeOverlay(objTemp);
         objTemp.hide();
      }

}

/************************************************************
getCharts()
Author: Felix de Bie
Date: Nov 27/2008

Description:

This loops through the legend and makes a string based on the
position of the checkboxes. For example, if the first and third
checkboxes were checked, the string would be "02". This is
passed to the database to retrieve the proper chart list
************************************************************/

function getCharts()
{
    document.getElementById("loadingDivMap").style.display = "block";

    var objChkLayers = document.getElementById("frmLegend").chkLegendDataLayer;
    var objHidLayerSource = document.getElementById("frmLegend").hidLegendDataLayerSource;
    var strUrl = "";
    var intCounter = 0;

    // Reset the list checked table names.
    var strNewLayerArray = "";

    // Ensure that there are data layers.
    if (objHidLayerSource)
    {
        // Get a count of data layer check boxes.
        var intCheckCount = ((objChkLayers.length == null) || (objChkLayers.length == undefined)) ? 1 : objChkLayers.length;

        // Loop through the hidden layer boxes.
        for (intCounter = 0; intCounter < intCheckCount; intCounter ++)
        {
            // Check to see if there is only 1 data layer row (1 data layer row gives an undefined length count).
            // Get the corresponding hidden field.
            var objTempHidden = (intCheckCount == 1) ? objHidLayerSource : objHidLayerSource[intCounter];

            var objTempHidden1 = (intCheckCount == 1) ? objChkLayers : objChkLayers[intCounter];

            if (objTempHidden1.checked)
                strNewLayerArray += objTempHidden.value + ",";
        }
    }

    // Remove the last comma.
    strNewLayerArray = strNewLayerArray.substring(0, strNewLayerArray.length - 1);

    // Gets the chart list from the database,
    // displays the charts and assigns the chart list to the charts variable
    //GLog.write("/jsp/showCharts.jsp?bounds=" + oGMap.getBounds().toString().replace(/ /g, "").replace(/\(/g, "").replace(/\)/g, "") + "&layerArray=" + layerArray);
    if(!oldBounds)
    {
        oldBounds = "0,0,1,1";
    }

    strUrl = "./jsp/showCharts.jsp?browser="+gblStrBrowserMode+"&oldBounds=" + oldBounds + "&newBounds=" + oGMap.getBounds().toString().replace(/ /g, "").replace(/\(/g, "").replace(/\)/g, "") + "&newLayerArray=" + strNewLayerArray + "&oldLayerArray=" + strOldLayerArray;
    //GLog.write(strUrl);

    // Create a new XML HTTP object for AJAX operation.
    var oTempXmlHttp = getNewXmlHTTPObject();

    // Process the AJAX request.
    if (!isXmlHTTPNull(oTempXmlHttp))
        doXMLRequest(oTempXmlHttp, strUrl, "post", true, "", "processDataCallback(pObjXmlHTTP)");

    oTempXmlHttp = null;

    oldBounds = oGMap.getBounds().toString().replace(/ /g, "").replace(/\(/g, "").replace(/\)/g, "");
    strOldLayerArray = strNewLayerArray;
}

function processDataCallback(pObjXmlHTTP)
{
    displayCharts(pObjXmlHTTP.responseText);
}


/************************************************************
Refresh Charts
Author: Felix de Bie
Date: Nov 27/2008

Description:

When the legend is clicked, this function will hide the current
charts and then get a new set based on the current clicked
charts in the legend
************************************************************/

function refreshCharts(){
 //  hideCharts(charts);
   getCharts();
 }
