﻿/// <reference path="googlemaps-intellisense.js" />
/// <reference path="Utilities.js" />
/// <reference path="SearchResults.js" />
/// <reference path="SearchFunctions.js" />
/// <reference path="MapFunctions.js" />

var GISP_CommunitySearchResultBulider = function() { };
GISP_CommunitySearchResultBulider.prototype = {


    CreateCommunitySearchResult: function() {

        var myCurrentIndex = _SSR._GetCurrentIndex(_SSR._CurrentViewType);
        var myCurrentType = _SSR._CurrentViewType;
        if ((_SSR._GetCurrentIndex(myCurrentType) < _SSR._GetStopAtIndex(myCurrentType)) & !_SSR._StopProcessCurrentIterativeAsyncResultOperation) {

            this.CreateMarkerAndListing(myCurrentIndex, myCurrentType, _SSR.GetResultByIndex(myCurrentIndex, myCurrentType));

            //if this is the last function, set the map zooom and remove the progress bar
            if (myCurrentIndex == _SSR._GetStopAtIndex(myCurrentType) - 1) {
                //remove the progress bar
                RemoveProgressBar();
            }
            else {
                _SSR._SetCurrentIndex(myCurrentIndex + 1, myCurrentType);

                var myCallback = function() {
                    _CommunitySearchResultBuilder.CreateCommunitySearchResult();

                }; //end method
                setTimeout(myCallback, 1);
            }; //end if last of not call self recursilvely
        }; //end if
    },

    CreateMarkerAndListing: function(pIndex, pType, pResult) {
        var myProperty = this.CreateMarker(pIndex, pResult);

        //push the property to the array
        _propertyMarkers.push(myProperty);
        //update progress bar
        _progressBar.updateLoader(1);
        //add icon
        _mapBar.map.addOverlay(myProperty);


        //Gets the icon created for the property
        var myPropertyIcon = myProperty.getIcon().image;
        var myStyle = pIndex % 2 == 0 ? "rowStyle_a" : "rowStyle_b";
        var myResultIsSaved = _SSR.IsResultSaved(pResult.ID);
        this.CreateListingRow(pResult, _SSR._Table, myStyle, myPropertyIcon, myResultIsSaved);

    },

    CreateSiteSearchResultsTable: function() {
        var myTable = document.createElement("table");
        GISPlanning_MapUtilities_SetHTMLAttributes(myTable, { cellPadding: "0", cellSpacing: "0", border: "0", id: "ContentPlaceholderNodeToReplace" });
        myTable.style.width = "100%";
        return myTable;
    },

    CreateMarker: function(pIndex, pResult) {
        var myCurrentIndex = pIndex;
        var myCurrentType = "COMMUNITY";

        if (pResult.ID.toString().indexOf("COMMUNITY_") == -1) {
            pResult.ID = 'COMMUNITY_' + pResult.ID;
        }; //


        var myPhoto = pResult.thumbnail;
        pResult.photo = myPhoto; //here we have already calculated the thumbnail version


        //setup the saved/remove link
        var myResultIsSaved = _SSR.IsResultSaved(pResult.ID);
        var mySaveRemoveLink = '<a class="tinyButtonLink ButtonTextLink small nonexportable" id="hrefSaveResultInfoWindow_' + pResult.ID + '" href="javascript:ToggleSaveRemoveProperty(\'' + pResult.ID + '\');" title=' + (myResultIsSaved ? '"Click here to remove this property"' : '"Click here to save this property"') + ">" + '<img align="absmiddle" src="/common/images/buttons/save_inactive.png" /><span>' + (myResultIsSaved ? 'Remove' : 'Save') + '</span>' + '</a>';

        var myHTMLText = '\
            <table id="miniwindow" style="font: 12px;height:145px;">\
                <tr>\
                    <td valign="top" style="width:160px;">\
                        <img src="' + myPhoto + '">\
                    </td>\
                    <td valign="top" style="width:300px;">\
                        <div>\
                            <a class="ResultAddress" href="javascript:ViewCommunityReport(\'' + pResult.ID + '\')">\
                                ' + pResult.Name + ' County, ' + pResult.State + ' \
                            </a>\
                        </div>\
                        <div>\
                            <strong> Population:</strong> ' + pResult.Population + '<br />\
                        </div>\
                        <div>\
                            <strong> Labor Force Size:</strong> ' + pResult.LaborForceSize + '<br />\
                        </div>\
                        <div>\
                            <strong>  Bachelors Degree or Higher:</strong> ' + pResult.BachelorsHigherPct + '<br />\
                        </div>\
                        <div>\
                            <strong> Household Median Income:</strong> ' + pResult.HHIncomeMedian + '<br />\
                        </div>\
                        <div>\
                            <strong> Job Growth Rate:</strong> ' + pResult.JobGrowth + '<br />\
                        </div>\
                        <div>\
                            <strong> Commute Travel Time:</strong> ' + pResult.TravelTime + '<br />\
                        </div>\
                    </td>\
                    <td>\
                        <table cellpadding="1" cellspacing="0" border="0" style="border-left:solid 1px silver">\
                            <tr>\
                                <td>' + mySaveRemoveLink + '</td>\
                            </tr>\
                            <tr>\
                                <td><a class="tinyButtonLink ButtonTextLink small" href="javascript:ZoomToProperty(\'' + pResult.ID + '\');" title="Click here to zoom in on this community"> <img align="absmiddle" src="/common/images/buttons/zoom_inactive.png" /><span>Zoom In</span></a></td>\
                            </tr>\
                            <tr>\
                                <td><a class="tinyButtonLink ButtonTextLink small" href="javascript:ViewCommunityReport(\'' + pResult.ID + '\');" title="Click here to view the full community report"> <img align="absmiddle" src="/common/images/buttons/community_inactive.png" /><span>Full Report</span></a></td>\
                            </tr>\
                            <tr>\
                                <td><span style="position:relative"><a class="tinyButtonLink popupLink ButtonTextLink small" id="hrefViewDemoInfoWindow_' + pResult.ID + '" href="javascript:InteractWithDemographicReport(\'' + pResult.ID + '\',\'hrefViewDemoInfoWindow_\');" title="Click here to view the demographic report for this community"><img align="absmiddle" src="/common/images/buttons/communityreport_inactive.png" /><span>Demographic Report<span></a></span></td>\
                            </tr>\
                        </table>\
                    </td>\
                </tr>\
            </table>';


        var myLatLng = new GLatLng(pResult.lat, pResult.lng);

        //determine if the property type is specific
        var myPropertyType = "COMMUNITY";



        var iconID = null;
        if (_SSR._GetPageSize(myCurrentType) <= _SSR._PageSizeDEFAULT) {
            iconID = "_" + (myCurrentIndex % _SSR._GetPageSize(myCurrentType) + 1);
        }; //end if less than 27 results

        //Determine if the property has been saved
        var myPropertySavedModifer = myResultIsSaved ? "_on" : ""; //uses the "on" version of the icon if it is saved

        //create the icon and associate the ID with the it by adding a field to the object
        var myMarker = GISPlanning_MapUtilities_CreateMarker(myLatLng, myHTMLText, myPropertyType, { ID: iconID, Modifier: myPropertySavedModifer });
        myMarker.ID = "" + pResult.ID;

        return myMarker;

    },




    CreateListingRow: function(pSite, pTable, pStyle, pIcon, pIsSaved) {

        var mySaveRemoveLink = '<a class="tinyButtonLink listingFunctionButton nonexportable" style="visibility:hidden;" id="hrefSaveResult_' + pSite.ID + '" href="javascript:ToggleSaveRemoveProperty(\'' + pSite.ID + '\');" title=' + (pIsSaved ? '"Click here to remove this property"' : '"Click here to save this property"') + ">" + '<img align="absmiddle" src="/common/images/buttons/save_inactive.png" /><span>' + (pIsSaved ? 'Remove' : 'Save') + '</span>' + '</a>';


        var myCell_1 = document.createElement("td");
        GISPlanning_MapUtilities_SetHTMLAttributes(myCell_1, { rowSpan: 2 });
        GISPlanning_MapUtilities_SetHTMLAttributes(myCell_1.style, { width: "83px" });
        myCell_1.innerHTML = '\
                    <table style="width:100%">\
                        <tr>\
                            <td align="center">\
                                <a href="javascript:ZoomToPropertyModal(\'' + pSite.ID + '\');" title="Click here to zoom in on this property"><img src="' + pIcon + '" id="imgSearchResultIcon_' + pSite.ID + '"/></a>\
                            </td>\
                        </tr>\
                         <tr>\
                            <td align="left">\
                                ' + mySaveRemoveLink + '\
								<a class="tinyButtonLink listingFunctionButton nonexportable" style="visibility:hidden;" href="javascript:SearchPropertiesInCommunity(\'' + pSite.Placecode + '\')" title="Click here to search for properties in this community">\
                                	<img src="/common/images/buttons/property_inactive.png" align="absmiddle"><span>Properties</span></a>\
                             </td>\
                        </tr>\
                    </table>';


        var myCell_2 = document.createElement("td");
        GISPlanning_MapUtilities_SetHTMLAttributes(myCell_2, { rowSpan: 2, vAlign: "top" });
        GISPlanning_MapUtilities_SetHTMLAttributes(myCell_2.style, { padding: "2px", textAlign: "left", width: "150px" });
        myCell_2.innerHTML = '\
                    <a href="javascript:ViewCommunityReport(\'' + pSite.ID + '\')" title="Click here to view reports on this community">\
                        <img src="' + pSite.photo + '">\
                    </a>';
        //Reflection.add(myCell_2.getElementsByTagName("img")[0], { height: .5, opacity: .5 });

        var myCell_3 = document.createElement("td");
        GISPlanning_MapUtilities_SetHTMLAttributes(myCell_3, { vAlign: "top", colSpan: 3 });
        GISPlanning_MapUtilities_SetHTMLAttributes(myCell_3.style, { padding: "2px", textAlign: "left", fontSize: "9pt" });
        myCell_3.innerHTML = '\
                        <a class="ResultAddress" href="javascript:ViewCommunityReport(\'' + pSite.ID + '\')" title="Click here to view reports on this community">\
                            ' + pSite.Name + ' County, ' + pSite.State + ' \
                        </a>';

        var myCell_3A = document.createElement("td");
        GISPlanning_MapUtilities_SetHTMLAttributes(myCell_3A, { vAlign: "top" });
        GISPlanning_MapUtilities_SetHTMLAttributes(myCell_3A.style, { padding: "2px", textAlign: "left", fontSize: "9pt", width: "260px" });
        myCell_3A.innerHTML = '\
                        <strong> Population:</strong> ' + pSite.Population + '<br />\
                        <strong> Labor Force Size:</strong> <span style="position:relative;">' + pSite.LaborForceSize + '</a></span><br />\
                        <strong> Job Growth Rate:</strong> <span style="position:relative;">' + pSite.JobGrowth + '</a></span><br />';

        var myCell_4 = document.createElement("td");
        GISPlanning_MapUtilities_SetHTMLAttributes(myCell_4.style, { padding: "2px", textAlign: "left", fontSize: "9pt", width: "230px" });
        GISPlanning_MapUtilities_SetHTMLAttributes(myCell_4, { vAlign: "top" });
        myCell_4.innerHTML = '\
                        <strong> Bachelors Degree or Higher:</strong> ' + pSite.BachelorsHigherPct + '<br/>\
                        <strong> Household Median Income:</strong> ' + pSite.HHIncomeMedian + '<br />\
                        <strong> Commute Travel Time:</strong> ' + pSite.TravelTime;

        var myCell_5 = document.createElement("td");
        GISPlanning_MapUtilities_SetHTMLAttributes(myCell_5.style, { padding: "2px", textAlign: "left", fontSize: "9pt", width: "230px" });
        GISPlanning_MapUtilities_SetHTMLAttributes(myCell_5, { vAlign: "top" });
        myCell_5.innerHTML = '\
                        <strong> Domestic Airports in County:</strong> ' + pSite.DomesticAirports + '<br/>\
                        <strong> Distance to Interstate (miles):</strong> ' + pSite.DistanceToInt + '<br />\
                        <strong> Distance to Rail (miles):</strong> ' + pSite.DistanceToRail;


        var myListingRow = pTable.insertRow(pTable.rows.length);
        myListingCell = document.createElement("td");
        myListingRow.appendChild(myListingCell);

        var myListingTable = document.createElement("table");
        GISPlanning_MapUtilities_SetHTMLAttributes(myListingTable, { cellPadding: 0, cellSpacing: 0, border: 0 });

        myListingCell.appendChild(myListingTable);

        var myTopRow = myListingTable.insertRow(0);
        var myBottomRow = myListingTable.insertRow(1);
        myTopRow.setAttribute("align", "left");
        myTopRow.style.height = "30px";
        myTopRow.className = pStyle;
        myBottomRow.setAttribute("align", "left");
        myBottomRow.style.height = "60px";
        myBottomRow.className = pStyle;


        myTopRow.appendChild(myCell_1);
        myTopRow.appendChild(myCell_2);
        myTopRow.appendChild(myCell_3);
        myBottomRow.appendChild(myCell_3A);
        myBottomRow.appendChild(myCell_4);
        myBottomRow.appendChild(myCell_5);



        //only show the function buttons on mouseover
        $(myListingTable).mouseover(function() {
            $(myCell_1).find(".listingFunctionButton").css({ visibility: "visible" });
            $(myListingTable).addClass("activeListingRow");
        });
        $(myListingTable).mouseout(function() {
            $(myCell_1).find(".listingFunctionButton").css({ visibility: "hidden" });
            $(myListingTable).removeClass("activeListingRow");
        });

    },
    GetSortByHTML: function(pCurrentlyChecked) {
        return "<em class='popupBox' style='width: 180px;height: 180px;'>\
                <table cellpadding='0' cellspacing='0' border='0'>\
                    <tr>\
                        <td colspan='4'>\
                            Choose a field to sort by:\
                        </td>\
                    </tr>\
                    <tr>\
                        <td>\
                            <input type='radio' name='Sorter' value='population' id='rbSort_address' onclick='SortResults(this.value);' " + (pCurrentlyChecked == "population" ? "checked='checked'" : "") + " />\
                        </td>\
                        <td>\
                            Population\
                        </td>\
                        <td>\
                            <input type='radio' name='Sorter' value='ID' id='rbSort_default' onclick='SortResults(this.value);' " + (pCurrentlyChecked == "ID" ? "checked='checked'" : "") + " />\
                        </td>\
                        <td>\
                            Unsorted\
                        </td>\
                    </tr>\
                    <tr>\
                        <td>\
                            <input type='radio' name='Sorter' value='name' id='rbSort_zip' onclick='SortResults(this.value);' " + (pCurrentlyChecked == "name" ? "checked='checked'" : "") + "  />\
                        </td>\
                        <td>\
                            Name\
                        </td>\
                        <td>\
                            <input type='radio' name='Sorter' value='state' id='rbSort_type' onclick='SortResults(this.value);' " + (pCurrentlyChecked == "state" ? "checked='checked'" : "") + "  />\
                        </td>\
                        <td>\
                            State\
                        </td>\
                    </tr>\
                    <tr>\
                        <td>\
                            <input type='radio' name='Sorter' value='HHIncomeMedian' id='rbSort_city' onclick='SortResults(this.value);' " + (pCurrentlyChecked == "HHIncomeMedian" ? "checked='checked'" : "") + "  />\
                        </td>\
                        <td colspan=3>\
                            Household Income\
                        </td>\
                    </tr>\
                </table>\
            </em>";
    }
};                                   //end prototype definition





