﻿/// <reference path="googlemaps-intellisense.js" />
/// <reference path="Utilities.js" />
/// <reference path="SearchResults.js" />
/// <reference path="SearchFunctions.js" />
/// <reference path="MapFunctions.js" />

var GISP_CommunitySearchResultBuilder = function() { };
GISP_CommunitySearchResultBuilder.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


        //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);

    },


    CreateMarker: function(pResult, pType) {
        var myPhoto = pResult.Thumbnail == null ? "" : pResult.Thumbnail;

        var myCurrentIndex = 1;
        //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:ToggleSaveRemoveReport(\'' + pResult.ID + '\');" title=' + (myResultIsSaved ? '"Click here to remove this property"' : '"Click here to save this property"') + ">" + '<span>' + (myResultIsSaved ? 'Remove' : 'Save') + '</span>' + '</a><span>' + (myResultIsSaved ? ' from' : ' to') + ' folder</span>';

        var myHTMLText = '\
            <table id="miniwindow" style="font: 12px;height:145px;">\
                <tr><td colspan="3"><img src="http://images.zoomprospector.com/client/assets/' + _GISP_Theme + '/InfoWindowLogo.jpg"/></td></tr>\
                <tr><td colspan="3"><hr/></tr>\
                <tr>\
                    <td valign="top" style="width:160px;">\
                        <a href="javascript:ViewCommunityReport(\'' + pResult.ID + '\',\'COMMUNITY\');"><img src="' + myPhoto + '"></a>\
                    </td>\
                    <td valign="top" style="width:300px;">\
                        <div>\
                            <a class="ResultAddress" href="javascript:ViewCommunityReport(\'' + pResult.ID + '\',\'COMMUNITY\')">\
                                ' + pResult.Name + ', ' + 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" >\
                            <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 property">Zoom</a>&nbsp<span>In</span></td>\
                            </tr><tr>\
                                <td><a class="tinyButtonLink ButtonTextLink small" href="javascript:ViewCommunityReport(\'' + pResult.ID + '\',\'FULL\');" title="Click here to view the full property report">Full</a>&nbsp<span>Report</span></td>\
                            </tr>\
                            <tr>\
                                <td><a class="tinyButtonLink ButtonTextLink small" href="javascript:ViewCommunityReport(\'' + pResult.ID + '\',\'LABORFORCE\');" title="">Labor Force</a>&nbsp<span>Report</span></td>\
                            </tr>\
                            <tr>\
                                <td><a class="tinyButtonLink ButtonTextLink small" href="javascript:ViewCommunityReport(\'' + pResult.ID + '\',\'DEMOGRAPHICS\');" title="">Demographics</a>&nbsp<span>Report</span></td>\
                            </tr>\
                            <tr>\
                                <td><a class="tinyButtonLink ButtonTextLink small" href="javascript:ViewCommunityReport(\'' + pResult.ID + '\',\'CONSUMERSPENDING\');" title="">Consumer Spending</a>&nbsp<span>Report</span></td>\
                            </tr>\
                        </table>\
                    </td>\
                </tr>\
            </table>';


        var myLatLng = new google.maps.LatLng(pResult.lat, pResult.lng);

        //determine if the property type is specific
        var myPropertyType = "COMMUNITY";

        //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: pResult.ID, Modifier: myPropertySavedModifer });
        myMarker.ID = pResult.ID;

        return myMarker;
    },


    CreateListing: function(pCommunity, pIndex, pType, pOrientation) {
        var myResultIsSaved = _SSR.IsResultSaved(pCommunity.ID);
        var myCardRaw = this.GetRawCard();
        var myListing = myCardRaw;

        myListing = myListing.replace(/@ID/g, pCommunity.ID)
            .replace(/@SSR_TYPE/g, pType)
            .replace("@SAVE_TITLE", myResultIsSaved ? "Click here to remove this community" : "Click here to save this community")
            .replace("@SAVE_REMOVE", myResultIsSaved ? 'Remove' : 'Save')
            .replace("@SAVE_TOFROM", myResultIsSaved ? 'from' : 'to')
            .replace("@SAVED", myResultIsSaved ? "saved" : "")
            .replace("@PROPERTY_IMAGE", pCommunity.Thumbnail)
            .replace(/@NAME/, pCommunity.Name)
            .replace(/@STATE/, GetStateAbbreviation(pCommunity.ID.substr(0, 2)))
            .replace(/@POPULATION/, pCommunity.Population == null ? "" : pCommunity.Population)
            .replace(/@LABOR_FORCE_SIZE/, pCommunity.LaborForceSize == null ? "" : pCommunity.LaborForceSize)
            .replace(/@JOB_GROWTH/, pCommunity.JobGrowth == null ? "" : pCommunity.JobGrowth)
            .replace(/@BACHELORS_HIGHER_PCT/, pCommunity.BachelorsHigherPct == null ? "" : pCommunity.BachelorsHigherPct)
            .replace(/@HH_INCOME_MEDIAN/, pCommunity.HHIncomeMedian == null ? "" : pCommunity.HHIncomeMedian)
            .replace(/@TRAVEL_TIME/, pCommunity.TravelTime == null ? "" : pCommunity.TravelTime + " minutes")
            .replace(/@DOMESTIC_AIRPORTS/, pCommunity.DomesticAirports == null ? "" : pCommunity.DomesticAirports)
            .replace(/@DISTANCE_TO_INT_AIRPORT/, pCommunity.DistanceToInt == null ? "" : pCommunity.DistanceToInt)
            .replace(/@DISTANCE_TO_RAIL/, pCommunity.DistanceToRail == null ? "" : pCommunity.DistanceToRail)
            .replace(/@INDEX/, pIndex)
            .replace(/@NUMBER/, pIndex + 1)
            .replace("@TOTALCOUNT", _SSR._GetCount(pType));


        if (pOrientation == "vertical")
            myListing = myListing.replace("@PGR_STYLE", 'position:absolute;top:470px;width:160px;text-align:right;');
            
        return myListing;
    },

    CreateNullListing: function(pIndex, pType) {
        var myCardRaw = this.GetRawCard();
        var myListing = myCardRaw;

        myListing = myListing.replace(/@ID/, "CARD_" + pIndex)
            .replace("@SAVE_TITLE", "Click here to save this community")
            .replace("@SAVE_REMOVE", 'Save')
            .replace("@SAVE_TOFROM", 'to')
            .replace("@SAVED", "")
            .replace("@PROPERTY_IMAGE", "/common/images/loading/PropertyLoading.gif")
            .replace(/@NAME/, "Loading...")
            .replace(/@STATE/, "Loading...")
            .replace(/@POPULATION/, "Loading...")
            .replace(/@LABOR_FORCE_SIZE/, "Loading...")
            .replace(/@JOB_GROWTH/, "Loading...")
            .replace(/@BACHELORS_HIGHER_PCT/, "Loading...")
            .replace(/@HH_INCOME_MEDIAN/, "Loading...")
            .replace(/@TRAVEL_TIME/, "Loading...")
            .replace(/@DOMESTIC_AIRPORTS/, "Loading...")
            .replace(/@DISTANCE_TO_INT_AIRPORT/, "Loading...")
            .replace(/@DISTANCE_TO_RAIL/, "Loading...")
            .replace(/@INDEX/, pIndex)
            .replace(/@NUMBER/, pIndex + 1)
            .replace("@TOTALCOUNT", _SSR._GetCount(pType));
        return myListing;
    },

    GetRawCard: function() {
        var myPropertyHTMLRaw = "\
        <div class='property' id='@ID' resultID='@INDEX'>\
            <div class='innerPropertyWrapper'>\
            <div class='propertyImageBox' id='propertyImageBox_@ID'>\
               <b class='propTop'></b>\
               <div class='propertyImagefg'>\
                    <div class='saveSlider'>\
                        <div class='saveSliderClosed zSpecial'>\
                            <a><b></b></a></div>\
                        <ul class='saveSliderOpen zSpecial'>\
                            <li><a class='saveLink' href=\"javascript:ToggleSaveRemoveReport('@ID')\" title='@SAVE_TITLE'>@SAVE_REMOVE</a>&nbsp;<span class='saveToFrom'>@SAVE_TOFROM</span>&nbsp;Folder</li>\
                            <li><a href=\"javascript:ZoomToProperty('@ID')\" title='Zoom the map to this property'>Zoom</a> to Community</li>\
                            <li><a href=\"javascript:ViewCommunityReport('@ID','COMMUNITY');\" title='View all the reports for this community '>View</a> Reports</li>\
                        </ul>\
                    </div>\
                    <!--/saveSlider-->\
                    <img src='@PROPERTY_IMAGE' class='img' width='148px' height='88px' />\
                    <div class='@DECORATOR_CLASS'></div>\
                    <div class='savedDecorator @SAVED'></div>\
                   <div class='ieClear'></div>\
                </div>\
                <!--/propertyImagefg-->\
                <b class='propBot'></b>\
            </div>\
            <!--/propertyImageBox-->\
            <div class='propertyInfoBox'>\
                <b class='propTop'></b>\
                <div class='propertyInfofg'>\
                    <h3 class='ellipsis'><a href=\"javascript:ViewCommunityReport('@ID','COMMUNITY');\" title='View all the reports for this community '>@NAME - @STATE</a></h3>\
                    <ul>\
                        <li class='ellipsis'>Population: @POPULATION</li>\
                        <li class='ellipsis'>Labor Force Size: @LABOR_FORCE_SIZE</li>\
                        <li class='ellipsis'>Job Growth: @JOB_GROWTH</li>\
                        <li class='ellipsis'>Bachelors: @BACHELORS_HIGHER_PCT</li>\
                        <li class='ellipsis'>Household Income.: $@HH_INCOME_MEDIAN</li>\
                    </ul>\
                    <ul>\
                        <li class='ellipsis'>Travel Time: @TRAVEL_TIME</li>\
                        <li class='ellipsis'>Dom. Airports: @DOMESTIC_AIRPORTS</li>\
                        <li class='ellipsis'>Dist Itnl Air: @DISTANCE_TO_INT_AIRPORT</li>\
                        <li class='ellipsis'>Dist. to Rail: @DISTANCE_TO_RAIL</li>\
                    </ul>\
                    <div style='@PGR_STYLE'>@NUMBER of @TOTALCOUNT</div>\
                    <div class='clear'></div>\
               </div>\
                <!--propertyInfofg-->\
                <b class='propBot'></b>\
            </div>\
            <!--/propertyInfoBox-->\
            <div class='clear'></div></div>\
        </div>\
        ";

        return myPropertyHTMLRaw;
    }
};                                         //end prototype definition






