﻿/*

This file contains the results panel object. It displays all location information
under the map and contains various pagination and exporth helper methods.

*/

function ResultsPanel(panelID){

    // Constants
    var printPreviewImage = "images/print.gif";
    var exportImage = "images/export.gif";

    // Screen results table stylesheet classes
    var tableRowClassTitle = "trct";
    var tableRowClass = "trc";
    var tableRowClassAlternate = "trca";

    // Previous / Next text/html
    var literalPreviousPage = "&lt;&lt; Previous";
    var literalNextPage = "Next &gt;&gt;";

    // Maximum results to show per page in table.
    var RESULTS_PER_PAGE = 5;
    
    // Members
    var _panel = null;
    var _resultsDiv = null;
    
    var _results = null;
    
    _init();
    
    // Object constructor
    function _init(){
    
        _panel = document.createElement('div');
        _panel.id = panelID;
        _panel.className = "transparent panel_results";

        document.body.appendChild(_panel);
        
        _resultsDiv = document.createElement('div');
        _resultsDiv.className = "panel_results_container";
        _resultsDiv.innerHTML = _getDefaultContent();
        _panel.appendChild(_resultsDiv);
        
        _reposition();
    }
    
    function _reset(){
        _resultsDiv.innerHTML = _getDefaultContent();
    }
    
    // Sets the default content for the bottom panel which is displayed before searching.
    function _getDefaultContent(){
        return "<img src='images/locator_bottom.jpg'/>";
    }
    
    // Repositions this object in the window    
    function _reposition(top, left){
        if (top != null){
            _panel.style.top = top + "px";
        }
        if (left != null){
            _panel.style.left = left + "px";
        }
    }
    
    // Externally accessible method which shows the given search results.
    function _showResults(locations, page){
        _results = locations;
        _showPage(page);
        
    }
    
    // Internal method for formatting the search results.
    function _showPage(page){
        
        _resultsDiv.style.display = "none";
        
        var resultsEndIndex = (RESULTS_PER_PAGE * page);
        
        if (resultsEndIndex > _results.length){
            resultsEndIndex = _results.length;
        }
        
        var lastPage = Math.ceil(_results.length / RESULTS_PER_PAGE);
        
        var resultsStartIndex =  resultsEndIndex - (RESULTS_PER_PAGE - 1);
        
        if (resultsStartIndex < 1){
            resultsStartIndex = 1;
        }
                
        var resultsTable = "<div class='resultsTableContainer'>" + 
                           "<table style='height:100%;border-collapse: collapse;'><tr><td style='vertical-align:top;'>";
        
        resultsTable += "<table id='resultsTable' class='resultsTable'><tr>" +
                        "<th class='" + tableRowClassTitle + "'><div class='cellCommon cellResultNumber'>No.</div></th>" +
                        "<th class='" + tableRowClassTitle + "'><div class='cellCommon cellSiteNumber'>Tower No</div></th>" + 
                        "<th class='" + tableRowClassTitle + "'><div class='cellCommon cellSiteName'>Name</div></th>" +
                        "<th class='" + tableRowClassTitle + "'><div class='cellCommon cellStreet'>Street</div></th>" +
                        "<th class='" + tableRowClassTitle + "'><div class='cellCommon cellCity'>City</div></th>" +
                        "<th class='" + tableRowClassTitle + "'><div class='cellCommon cellState'>State</div></th>" +
                        "<th class='" + tableRowClassTitle + "'><div class='cellCommon cellZip'>Zip</div></th>" +
                        "<th class='" + tableRowClassTitle + "'><div class='cellCommon cellTowerType'>Type</div></th>";

                        if (isValid(_results[0].Distance)){
                            resultsTable += "<th class='" + tableRowClassTitle + "'><div class='cellCommon cellDistance'>Distance</div></th>";
                        }
        resultsTable += "</tr>";
        
        var altClass;
        for (var i = resultsStartIndex - 1; i < resultsEndIndex; i++){
            if (i % 2 == 0){
                altClass = tableRowClassAlternate;
            }
            else{
                altClass = tableRowClass;
            }
            resultsTable += "<tr>" +
                            "<td class='" + altClass + "'><div class='cellCommon cellResultNumber'><a href='javascript:panel_results.ShowLocation(" + (parseInt(i)) + ");'>" + (parseInt(i)+1) + "</a></div></td>" +
                            "<td class='" + altClass + "'><div class='cellCommon cellSiteNumber'><a href='" + _results[i].GetURL() + "' target='_blank'>" + _results[i].TowerNumber + "</a></div></td>" +
                            "<td class='" + altClass + "'><div class='cellCommon cellSiteName'>" + _results[i].TowerName + "</div></td>" +
                            "<td class='" + altClass + "'><div class='cellCommon cellStreet'>" + _results[i].Address1 + "</div></td>" +
                            "<td class='" + altClass + "'><div class='cellCommon cellCity'>" + _results[i].City + "</div></td>" +
                            "<td class='" + altClass + "'><div class='cellCommon cellState'>" + _results[i].State + "</div></td>" +
                            "<td class='" + altClass + "'><div class='cellCommon cellZip'>" + _results[i].Zip + "</div></td>"+
                            "<td class='" + altClass + "'><div class='cellCommon cellTowerType'>" + _results[i].TowerLocation + "</div></td>";
                            
                            if (isValid(_results[i].Distance)){
                                resultsTable += "<td class='" + altClass + "'><div class='cellCommon cellDistance'>" + _results[i].Distance + " mi</div></td>";
                            }
                            
            resultsTable += "</tr>";
        }
        resultsTable += "</table></td>";
        
        resultsTable += "<td style='width: 200px;vertical-align:middle;'><div class='resultsPagingControlContainer'><div class='resultsPagingControl'>" +
                        "<a href='" + dal.PPProxy + dal.GetLastCallURL() + "&pp=1" + "' id='lnkPrintResults' target='_blank'><img class='imgPrint' id='imgPrint' src='" + printPreviewImage + "' alt='Print Preview'></img></a>&nbsp;&nbsp;&nbsp;&nbsp;" + 
                        "<a href='" + dal.ExportProxy + dal.GetLastCallURL() + "&dl=1" + "' id='lnkExportResults'><img class='imgExport' id='imgExport' src='" + exportImage + "' alt='Export to Excel (CSV)'></img></a><br/>";
                         
        
        if (page > 1){
            resultsTable += "<a href='javascript:panel_results.ShowPage(\"" + (parseInt(page)-1) + "\");'>" + literalPreviousPage + "</a>";
        }
        else{
            resultsTable += literalPreviousPage;
        }
        
        resultsTable += " | ";
        
        if (page < lastPage){
            resultsTable += "<a href='javascript:panel_results.ShowPage(\"" + (parseInt(page)+1) + "\");'>" + literalNextPage + "</a>";
        }
        
        else{
            resultsTable += literalNextPage;
        }
        
        resultsTable += "<br/>" + resultsStartIndex + " - " + resultsEndIndex + " of " + _results.length + "";
        
        resultsTable += "</div></td></tr></table></div>";

        _resultsDiv.innerHTML = resultsTable;
        
        _resultsDiv.style.display = "block";
        
        resizePanels();
    }
    
    
    // Center and zooms on a result given its index in the list.
    function _showLocation(siteIndex){
        map.SetCenterAndZoom(_results[siteIndex]);
        map.ShowBubble(_results[siteIndex]);
    }
    
    // Gets a location from results based on it's siteId.
    function _getIndexBySiteId(siteId){
        var index = null;
        for (var i = 0; i < _results.length; i++){
            if (_results[i].SiteId == siteId){
                index = i;
                break;
            }
        }
        return index;
    }
    

    // Center and zoom on a result given its id
    function _showLocationBySiteId(siteId){
        var index = _getIndexBySiteId(siteId);
        if (index != null){
            _showLocation(index);
        }
    }
    
    // Returns a handle to this object
    function _getElement(){
        return document.getElementById(_panel.id);
    }
    
    // Resizes this object based on window size.
    function _resize(){
        _panel.style.width = document.documentElement.clientWidth + "px";
        var resultsTable = document.getElementById("resultsTable");
        
        if (isValid(resultsTable)){
            resultsTable.style.width = (document.documentElement.clientWidth - 200) + "px";
        }
    }
    
    // Returns a particular found result.
    function _getLocation(index){
        return _results[index];
    }
    
    // Return a result by site ID.
    function _getLocation2(siteId){
        var index = _getIndexBySiteId(siteId);
        if (index == null){
            return null;
        }
        else{
            return _results[index];
        }
    }
            
    this.Reset = _reset;
    this.Reposition = _reposition;
    this.Resize = _resize;
    this.GetElement = _getElement;
    this.ShowResults = _showResults;
    this.ShowPage = _showPage;
    this.ShowLocation = _showLocation;
    this.ShowLocation2 = _showLocationBySiteId;
    this.GetLocation = _getLocation;
    this.GetLocation2 = _getLocation2;
}
