﻿var searchResultsExtent = [];
var searchResultsPoints;
var theSearchResizeConnect;
var newSearch = false;
var searchResultsGraphicsLayer = null;


function clickResultsTab()
{
    turnOffMapGraphics();
    
    if(expanded=="right" || prevTab=="Details") 
    {
        if(!newSearch)
            theSearchResizeConnect = dojo.connect(jsMap,"onResize",function(){
		        blnResizing = false;
		        dojo.disconnect(theSearchResizeConnect);
		        loadResults();
		        });
		
        restorePanes();
	    RepositionMap();
	    ResizeMap();
        selectRadTab('Map');
    }
    else 
    {
        selectRadTab("Map");
        //disableRadTab("Property");
	    resizeRightPaneControls();
        resizeLeftPaneControls();
        if(!newSearch)
            loadResults();
    }
}

function resizeResultsControl()
{
    var arrSize;
	arrSize = ResizeInPane('paneLeftBody', 'divSearchResultsGrid',		345, 200, 27, 26);
	if (m_radgridSearchResultsCompressed_MTV) ResizeSearchResultsGrid(arrSize,m_radgridSearchResultsCompressed_MTV);
	arrSize = ResizeInPane('paneLeftBody', 'divSearchResultsWideGrid',	700, 200, 28, 0);
	if (m_radgridSearchResultsWide_MTV) ResizeSearchResultsGrid(arrSize,m_radgridSearchResultsWide_MTV);
	
	arrSize = ResizeInPane('paneLeftBody', 'divDojoResults',		345, 200, 27, 26);
}

function ResizeSearchResultsGrid(arrSize,grid)
{
	if (arrSize == null)	return;

	var intWidth  = arrSize[0];

    if (grid == null)
    	var tableGrid = m_radgridSearchResultsWide_MTV.Control;
	else
	    var tableGrid = grid.Control;
	if (tableGrid)
	{
		tableGrid.style.width = (intWidth - 22) + "px";
		tableGrid.width =		(intWidth - 22) + "px";
	}
}
function expandSearchResults(expand)
{
    swapDIV('divPage_SearchResults', expand);
}
function loadResults()
{
    showLayer("BusyIndicator");
    if(searchResultsExtent.length>0)
    {
        //setMapPoints("searchResults",searchResultsExtent,searchResultsPoints);
        turnOffMapGraphics();
	    turnOnMapGraphics("searchResultsGraphics")
	    setExtent(searchResultsExtent[0],searchResultsExtent[1],searchResultsExtent[2],searchResultsExtent[3]);
	}	
	else
    {
    	__doPostBack('bbusMap_MapProcessPanel', 'MapEvent=ZoomResultSet&ResultSet=searchResults&Page=resultsPage');
        GACTTrack("Results");
    }
}

function doSearch(blnDetailed)
{
	// show the "searching..." graphic
	showSearching();
	newSearch = true;
    searchResultsExtent = [];
    searchResultsPoints = "";
	// enable the Search Results tab and switch to it
	enableRadTab('Results');
	selectRadTab('Results');
	selectRadTab('Map');
		
    //__doPostBack('bbusSearchForm$buttonSearch', '');
    __doPostBack('bbusSearchForm$updateSearchFormSearch','Event=BeginSearch');
    GACTTrack("Search");
    var hiddenSearchGrouping = document.getElementById('bbusSearchForm_hiddenSearchGrouping').value;
	GACTTrackEvent("Search Results","Map View",hiddenSearchGrouping);
    newSearch = false;
}

function doXML()
{
    // show the "searching..." graphic
	showSearching();
	newSearch = true;
    searchResultsExtent = [];
    searchResultsPoints = "";
	// enable the Search Results tab and switch to it
	enableRadTab('Results');
	selectRadTab('Results');
	selectRadTab('Map');
		
    //__doPostBack('bbusSearchForm$buttonSearch', '');
    __doPostBack('bbusSearchForm$updateSearchFormSearch','Event=HiddenXMLResults');
    GACTTrack("Search");
    var hiddenSearchGrouping = document.getElementById('bbusSearchForm_hiddenSearchGrouping').value;
	GACTTrackEvent("Search Results","Map View",hiddenSearchGrouping);
    
    newSearch = false;
    document.getElementById("bbusSearchResults_hiddenResultsXML").value = "";
}

function showSearching()
{
	var panelNoResults	= document.getElementById('bbusSearchResults_panelNoResults');
	var panelSearchResults	= document.getElementById('bbusSearchResults_panelSearchResults');
	if (panelNoResults && panelSearchResults)
	{
	    panelNoResults.style.display = 'none';
	    panelSearchResults.style.display = '';
	}
	var labelPageInfo	= document.getElementById('bbusSearchResults_labelPageInfo');
	if (labelPageInfo)
	{
	    labelPageInfo.innerHTML = '';
	}
	var itemExpand	= document.getElementById('bbusSearchResults_hlinkExpand');
	if (itemExpand)
	    itemExpand.disabled = true;
	
	
	var divGridSearchResultsCompressed	= document.getElementById('divGridSearchResultsCompressed');
	var divSearching			= document.getElementById('divSearching');

	if (divGridSearchResultsCompressed && divSearching)
	{
		// hide the form and show the "loading" graphic
		divGridSearchResultsCompressed.style.display = 'none';
		divSearching.style.display = '';
	}
}

function showNoResults()
{
	var panelNoResults	= document.getElementById('bbusSearchResults_panelNoResults');
	var panelSearchResults	= document.getElementById('bbusSearchResults_panelSearchResults');
	if (panelNoResults && panelSearchResults)
	{
	    panelNoResults.style.display = '';
	    panelSearchResults.style.display = 'none';
	}
	var labelPageInfo	= document.getElementById('bbusSearchResults_labelPageInfo');
	if (labelPageInfo)
	{
	    labelPageInfo.innerHTML = '';
	}
	var itemExpand	= document.getElementById('bbusSearchResults_hlinkExpand');
	if (itemExpand)
	    itemExpand.disabled = true;
	
	
	var divGridSearchResultsCompressed	= document.getElementById('divGridSearchResultsCompressed');
	var divSearching			= document.getElementById('divSearching');

	if (divGridSearchResultsCompressed && divSearching)
	{
		// hide the form and show the "loading" graphic
		divGridSearchResultsCompressed.style.display = 'none';
		divSearching.style.display = 'none';
	}
	newSearch = true;
    selectRadTab('Results');
    newSearch = false;
    
}

function setsearchResultsMap(resultset,showMap,xmin,ymin,xmax,ymax,points)
{
    mapResultset = resultset;
    searchResultsExtent = [];
    
    var graphicID = resultset+'Graphics';
    var rlayer = jsMap.getLayer(graphicID);
    if (rlayer != null) jsMap.removeLayer(rlayer);
    
    searchResultsGraphicsLayer = new esri.layers.GraphicsLayer({id: 'searchResultsGraphics'});
    jsMap.addLayer(searchResultsGraphicsLayer);
    dojo.connect(searchResultsGraphicsLayer, "onClick", clickLocGraphic);
    jsMap.reorderLayer(searchResultsGraphicsLayer,jsMap.layerIds.length+jsMap.graphicsLayerIds.length-1);
    
    if (xmin!=0)
    {
        if(xmin == xmax)
        {
            var units;
            for (var j=0, jl=jsMap.layerIds.length; j<jl; j++) {
              var layer = jsMap.getLayer(jsMap.layerIds[j]);
              if (currentMapView == layer.id) {
                units = layer.units;
              }
            }
            var factor = 0.1;
            if (units == "esriFeet")
            {
                factor = 30000;
            }
            if (units == "esriMeters") 
            {
                factor = 10000;
            }
            xmin -= factor;
            xmax += factor;
            ymin -= factor;
            ymax += factor;
        }
        searchResultsExtent.push(xmin);
        searchResultsExtent.push(ymin);
        searchResultsExtent.push(xmax);
        searchResultsExtent.push(ymax);
    }
    searchResultsPoints = points;
    pArray = points.split(";;;;");
    if(pArray.length >= 100)
        window.setTimeout('document.getElementById("bbusSearchResults_labelPageInfo").focus();document.getElementById("bbusSearchResults_labelPageInfo").blur();',100);
    
    if(showMap)
    {
        setMapPoints(resultset,searchResultsExtent,points);
    }
    else    hideLayer("BusyIndicator");
    
        
}

function expandResults()
{
    expandLeftPane();
    var hiddenSearchGrouping = document.getElementById('bbusSearchForm_hiddenSearchGrouping').value;
	GACTTrackEvent("Search Results","Detail View",hiddenSearchGrouping);
}

function collapseResults()
{
    restorePanes();
    var hiddenSearchGrouping = document.getElementById('bbusSearchForm_hiddenSearchGrouping').value;
	GACTTrackEvent("Search Results","Map View",hiddenSearchGrouping);
}

function resetsearchResultsMap()
{
    mapResultset = "searchResults";
    mapResultsExtent = searchResultsExtent;
    mapPoints = searchResultsPoints;
    turnOffMapGraphics();
	turnOnMapGraphics("searchResultsGraphics");
	setExtent(mapResultsExtent[0],mapResultsExtent[1],mapResultsExtent[2],mapResultsExtent[3]);
    //GACTTrackEvent("Search Results","Simple View");
}

function SR_gotoPropertyOnMap(objElement,x,y,id)
{
	currPropId = id;
    currPropX = x;
    currPropY = y;
	if (objElement)
	{
		selectSRRow(objElement,true);
		showGlow(0,0);
		window.setTimeout('gotoPropertyOnMap(currPropX,currPropY,currPropId);',1);
	}
	else
	{
		theSearchResizeConnect = dojo.connect(jsMap,"onResize",function(){
		    blnResizing = false;
		    dojo.disconnect(theSearchResizeConnect);
		    gotoPropertyOnMap(currPropX,currPropY,currPropId);
		    });
		restorePanes();
	    RepositionMap();
	    ResizeMap();
        selectRadTab('Map');
	}

	//window.setTimeout('gotoPropertyOnMap('+x+','+y+','+id+');',50);
}
function SR_gotoPropertyDetails(objElement,id, x, y)
{
	currPropId = id;
    currPropX = x;
    currPropY = y;
	
	if (objElement)
	{
		selectSRRow(objElement,true);
		showGlow(0,0);
		window.setTimeout('gotoPropertyDetails(currPropId,currPropX,currPropY);',1);
	}
	else
	{
		theSearchResizeConnect = dojo.connect(jsMap,"onResize",function(){
		    blnResizing = false;
		    dojo.disconnect(theSearchResizeConnect);
		    gotoPropertyDetails(currPropId,currPropX,currPropY);
		    });
	    restorePanes();
	    RepositionMap();
	    ResizeMap();
        selectRadTab('Map');
	}

	//window.setTimeout('gotoPropertyDetails('+id+','+x+','+y+');',10);
}

function SR_getPopUp(objElement,id)
{
	//ZoomResultSet('searchResults','resultsPage');
	
	if (objElement)
	{
		selectSRRow(objElement,true);
		showGlow(0,0);
	}
	selectRadTab('Map');
	//disableRadTab('Property');
    getPopUp(id,true);
    
}

function rowGetPopUp(objElement,id)
{
    if (objElement)
	{
		rowSelect(objElement,true);
		showGlow(0,0);
	}
	selectRadTab('Map');
    getPopUp(id,true);
}
function rowSelect(objElement, blnSelect)
{
    var divRow = objElement;
	var divTable = objElement.parentNode;
	//while ((divRow) && (divRow.className.indexOf('alternatingRow') == -1))
	while ((divTable) && (divTable.nodeName != "TABLE"))
	{	divTable = divTable.parentNode;  }      

	if (!divTable)	return;
    
	// get the current row class
	var strClass = divRow.className;
	// get the "base" class
	strClass = strClass.replace('_highlight', '');
    strClass = strClass.replace('_select',    '');

	// need to "deselect" the other "selected" row
	if (blnSelect)
	{
		var arrRows = divTable.getElementsByTagName('tr');
		for (ix=0; ix < arrRows.length; ix++)
		{
			arrRows[ix].className = arrRows[ix].className.replace('_select',    '');
		}
	}

	// add "_highlight" if this row to be highlighted
	if (blnSelect)
		divRow.className = strClass + '_select';
	else
		divRow.className = strClass;
}
function highlightRow(divRow, blnHighlight)
{
	// get the current row class
	var strClass = divRow.className;

	// don't mess with the selected row
	if (strClass.indexOf('_select') > -1)
		return;

	// get the "base" class
	strClass = strClass.replace('_highlight', '');
	
	// add "_highlight" if this row to be highlighted
	if (blnHighlight)
		divRow.className = strClass + '_highlight';
	else
		divRow.className = strClass;
}

function selectSRRow(objElement, blnSelect)
{
	selectRow(objElement, blnSelect, m_radgridSearchResultsCompressed_MTV);
}

function selectRow(objElement, blnSelect, strDIV)
{
	// walk up the hierarchy to find the parent DIV with "alternatingRow#" class
	var divRow = objElement.parentNode;
	//while ((divRow) && (divRow.className.indexOf('alternatingRow') == -1))
	while ((divRow) && (divRow.nodeName != "TR"))
	{	divRow = divRow.parentNode;  }      

	if (!divRow)	return;
    
    strDIV.SelectRow(divRow, true);
    
//	// get the current row class
//	var strClass = divRow.className;

//	// get the "base" class
//	strClass = strClass.replace('_select',    '');
//	strClass = strClass.replace('_highlight', '');

//	// need to "deselect" the other "selected" row
//	if (blnSelect)
//	{
//		var divSearchResults = document.getElementById('divGridSearchResultsCompressed');
//		var arrRows = divSearchResults.getElementsByTagName('div');
//		for (ix=0; ix < arrRows.length; ix++)
//		{
//			selectRow(arrRows[ix], false);
//		}
//	}

//	// add "_highlight" if this row to be highlighted
//	if (blnSelect)
//		divRow.className = strClass + '_select';
//	else
//		divRow.className = strClass;
}

function SetSelectedRow(strWhichGrid,id)
{
	var radGrid_MTV	= null;

	switch (strWhichGrid)
	{
	case 'searchResults':
		radGrid_MTV = m_radgridSearchResultsCompressed_MTV;
		break;
	case 'myPortfolio':
		radGrid_MTV = m_radgridPortfolioCompressed_MTV;
		break;
	}
	
	if (radGrid_MTV == null)
		return;
		
	var rows;
	rows = radGrid_MTV.Rows;
	
	for (var ix=0; ix < rows.length; ix++)
    {
	    var cellPropertyId	= radGrid_MTV.GetCellByColumnUniqueName( rows[ix], "LINK_ID" );  
	    //right now the above row returns null always because the compressed grids do not contain the link_id column
	    if (cellPropertyId != null)
	    {
		    var strPropertyId	= Trim(cellPropertyId.innerHTML);
		
		    if (strPropertyId == id)
			{
			    radGrid_MTV.SelectRow(rows[ix].Control);
			    break;
			}
			else
			    radGrid_MTV.DeselectRow(rows[ix].Control)
	    }
	    else
	        radGrid_MTV.DeselectRow(rows[ix].Control)
    }

}

function SetInPortfolio()
{
	var radGrid_MTV	= m_radgridSearchResultsWide_MTV;
    
	if (radGrid_MTV == null)
		return;
	if (m_radgridPortfolioWide_MTV == null)
	    return;
	if (radGrid_MTV.Rows == null)
	    return;
	if (m_radgridPortfolioWide_MTV.Rows == null)
	    return;
	    
	var rows;
	rows = radGrid_MTV.Rows;
	var inPort = false;
	for (var ix=0; ix < rows.length; ix++)
    {
	    var cellPropertyId	= radGrid_MTV.GetCellByColumnUniqueName( rows[ix], "LINK_ID" );  
	    var cellButtons	= radGrid_MTV.GetCellByColumnUniqueName( rows[ix], "myGridButtons" );  
		if (cellPropertyId != null && cellButtons != null)
	    {
		    inPort = false;
		    var strPropertyId	= Trim(cellPropertyId.innerHTML);
		    
		    for (var px=0; px < m_radgridPortfolioWide_MTV.Rows.length; px++)
            {
	            var pcellPropertyId	= m_radgridPortfolioWide_MTV.GetCellByColumnUniqueName( m_radgridPortfolioWide_MTV.Rows[px], "LINK_ID" );  
	            if (pcellPropertyId != null)
	            {
		            var strPortPropertyId	= Trim(pcellPropertyId.innerHTML);
		    
		            if (strPropertyId == strPortPropertyId)
			        {
			            inPort = true;
			        }
			    }
			}
			if (inPort)
	        {
	            cellButtons.className = cellButtons.className.replace(" inPortfolio","")+" inPortfolio";
	        }
	        else
	        {
	            cellButtons.className = cellButtons.className.replace(" inPortfolio","")
	        }
	    } 
	    
    }

}
function searchHistory()
{
}


var m_radgridSearchResultsWide_MTV = null;
var m_radgridSearchResultsCompressed_MTV = null;

function radgridSearchResultsWide_MasterTableViewCreated()
{
	m_radgridSearchResultsWide_MTV = this.MasterTableView;

	arrSize = ResizeInPane('paneLeftBody', 'divSearchResultsWideGrid',	700, 200, 0, 0);
	ResizeSearchResultsGrid(arrSize,m_radgridSearchResultsWide_MTV);
	SetInPortfolio();
}
function radgridSearchResultsCompressed_MasterTableViewCreated()
{
	m_radgridSearchResultsCompressed_MTV = this.MasterTableView;

	arrSize = ResizeInPane('paneLeftBody', 'divSearchResultsGrid',	345, 200, 27, 26);
	ResizeSearchResultsGrid(arrSize,m_radgridSearchResultsCompressed_MTV);
}

function printResults(blnPropDetails)
{
    var rows;
	rows = m_radgridSearchResultsWide_MTV.SelectedRows;
	var strSelectedProperties = "";
	if (rows.length > 0) strSelectedProperties	= GetSelectedProperties('results');
    var d = new Date();
	
	var srid = jsSR.wkid;
    var mapurl;
    var showmap = true;
    for (var j=0, jl=jsMap.layerIds.length; j<jl; j++) {
      var layer = jsMap.getLayer(jsMap.layerIds[j]);
      if (currentMapView == layer.id) {
        mapurl = layer.url;
        if (layer.declaredClass == "esri.layers.ArcGISDynamicMapServiceLayer") //if there is a dynamic layer
        {
            showmap = false;
            break;
        }
      }
    }
    
    if (showmap)
    {
        var srExtent = new esri.geometry.Extent(searchResultsExtent[0], searchResultsExtent[1], searchResultsExtent[2], searchResultsExtent[3], jsSR) 
        var newExtent = srExtent.expand(2);
        
        var mapw = jsMap.width;
        var maph = jsMap.height;
        var mapname = currentMapView;
        var mapNames = document.getElementById("bbusMap_mapServiceMapNames").value;
        if (mapNames!= "")
        {
            var arrMapNames = mapNames.split(",");
	        var arrMapServices = document.getElementById("bbusMap_mapServices").value.split(",");
	        for (var am in arrMapServices)
	        {
	            if(arrMapServices[am]==currentMapView)
	                mapname = arrMapNames[am];
	        }
	    }
        
        //__doPostBack('', 'MapEvent=PrintMap&MapURL='+mapurl+'&MapName='+currentMapView+'&MinX='+mapminx+'&MinY='+mapminy+'&MaxX='+mapmaxx+'&MaxY='+mapmaxy);
        var url = '&MapURL='+mapurl+'&MapName='+mapname+'&MinX='+newExtent.xmin+'&MinY='+newExtent.ymin+'&MaxX='+newExtent.xmax+'&MaxY='+newExtent.ymax+'&MapW='+mapw+'&MapH='+maph+'&SRID='+srid;
    }
    else
        var url = '&NoMap=true';
        
    var strDialogURL
    if (blnPropDetails) 
        strDialogURL	= "dialogs/print_map.aspx?t="+d.getTime()+"&type=SearchResultsPropertyDetails&props="+strSelectedProperties+url;
    else
        strDialogURL	= "dialogs/print_map.aspx?t="+d.getTime()+"&type=Results&props="+strSelectedProperties+url;
    
	var pFrame = document.getElementById('iframePrintMap');
    pFrame.src = strDialogURL;
    
    GACTTrack("Print Results");
    var hiddenSearchGrouping = document.getElementById('bbusSearchForm_hiddenSearchGrouping').value;
	GACTTrackEvent("Search Results","Print",hiddenSearchGrouping);
          
    //__doPostBack('updateSearchResultsWideGrid', 'Event=PrintToPDF');
}

function exportResults()
{
    //var strSelectedProperties	= GetSelectedProperties('results');
	__doPostBack('updateSearchResultsWideGrid', 'Event=ExportToExcel');

    GACTTrack("Export Results");
    var hiddenSearchGrouping = document.getElementById('bbusSearchForm_hiddenSearchGrouping').value;
	GACTTrackEvent("Search Results","Export",hiddenSearchGrouping);
    
}


