﻿/// <reference path="googlemaps-intellisense.js" />
/// <reference path="Utilities.js" />
/// <reference path="SearchResults.js" />
/// <reference path="SearchFunctions.js" />
/// <reference path="MapFunctions.js" />

var GISP_PropertySearchResultBuilder = function () { };
GISP_PropertySearchResultBuilder.prototype = {


    CreatePropertySearchResult: 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 () {
                    _PropertySearchResultBuilder.CreatePropertySearchResult();

                }; //end method
                setTimeout(myCallback, 1);
            } //end if last of not call self recursilvely
        }; //end if
    },

    CreateMarkerAndListing: function (pIndex, pType, pResult, pIsViewable) {
        var myMarker = this.CreateMarker(pResult);
        var myListing = this.CreateListing(pResult, pIndex, pType);

        return { Marker: myMarker, Listing: myListing };
    },


    CreateMarker: function (pResult, pType) {
        //log.info("ResultsBuilderProperty CreateMarker()");
        //log.info("- type: " + pType);

        var myPhoto = "";
        //check if a photo exists
        if (pResult.Thumbnail == null || pResult.Thumbnail == '') {
            myPhoto = "/common/images/noPhoto.png";
        } else {
            //use the thumbnail.
            myPhoto = pResult.Thumbnail;
        } //end if
        //log.info("- Photo: " + myPhoto);

        //setup the saved/remove link
        var myResultIsSaved = _SSR.IsResultSaved(pResult.ID);
        //log.info("- Result is saved?: " + myResultIsSaved);
        var mySaveRemoveLink = '<a  id="hrefSaveResultInfoWindow_' + pResult.ID + '" href="javascript:ToggleSaveRemoveReport(\'' + pResult.ID + '\');">' + (myResultIsSaved ? 'Remove' : 'Save') + '</a><span>' + (myResultIsSaved ? ' from' : ' to') + ' folder</span>';
        var myBuildingName = (pResult.BuildingName == null) ? "" : "<strong> Property Name:</strong> " + pResult.BuildingName + "<br />";
        var myHTMLText = '\
            <table id="miniwindow" class="infowindow" >\
                <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 "class="info-photo">\
                        <a href="javascript:ViewPropertyReport(\'' + pResult.ID + '\',\'' + pType + '\');" style="display:block;"><img src="' + myPhoto + '"></a>\
                        <a href="javascript:ZoomToStreetViewCurrentMaker();">Show Street View</a>\
                    </td>\
                    <td valign="top" class="info-address">\
                        <a class="ResultAddress" href="javascript:ViewPropertyReport(\'' + pResult.ID + '\',\'' + pType + '\')">\
                            ' + pResult.Address + ' \
                        </a>\
                        <br/>\
                        ' + pResult.CityName + ', ' + pResult.StateName + ' ' + pResult.ZipCode + '<br />\
                        ' + myBuildingName + '\
                        <strong> County:</strong> ' + pResult.CountyName + '<br />\
                    </td>\
                    <td class="info-reports">\
                         <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:ViewPropertyReport(\'' + pResult.ID + '\',\'' + pType + '\');" 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:ViewPropertyReport(\'' + pResult.ID + '\',\'' + pType + '\', \'LABORFORCE\');" title="">Labor Force</a>&nbsp<span>Report</span></td>\
                            </tr>\
                            <tr>\
                                <td><a class="tinyButtonLink ButtonTextLink small" href="javascript:ViewPropertyReport(\'' + pResult.ID + '\',\'' + pType + '\', \'DEMOGRAPHICS\');" title="">Demographics</a>&nbsp<span>Report</span></td>\
                            </tr>\
                            <tr>\
                                <td><a class="tinyButtonLink ButtonTextLink small" href="javascript:ViewPropertyReport(\'' + pResult.ID + '\',\'' + pType + '\', \'CONSUMERSPENDING\');" title="">Consumer Spending</a>&nbsp<span>Report</span></td>\
                            </tr>\
                            <tr>\
                                <td><a class="tinyButtonLink ButtonTextLink small" href="javascript:ViewPropertyReport(\'' + pResult.ID + '\',\'' + pType + '\', \'BUSINESS\');" title="Click here to view the business report around this property">Business</a>&nbsp<span>Report</span></td>\
                            </tr>\
                        </table>\
                    </td>\
                </tr>\
            </table>';


        var myLatLng = new google.maps.LatLng(pResult.lat, pResult.lng);
        //log.info("- Lat: " + pResult.lat + " Lng: " + pResult.lng);

        //determine if the property type is specific
        var myPropertyType = pType;
        switch (pResult.type) {
            case "Office":
            case "office":
                myPropertyType = "OFFICE";
                break;
            case "vacantland":
            case "land":
                myPropertyType = "RETAIL";
                break;
            case "Industrial":
            case "industrial":
                myPropertyType = "INDUSTRIAL";
                break;
            case "Warehouse":
            case "warehouse":
                myPropertyType = "SPECIAL";
                break;
        }; //end switch



        //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;
        _propertyMarkers.push(myMarker);

        return myMarker;

    },




    CreateListing: function (pSite, pIndex, pType, pOrientation) {

        var myResultIsSaved = _SSR.IsResultSaved(pSite.ID);

        var myFormattedCustomerAttributes = '';

        //this will still return an array with one element, even if there are not attributes
        var myCustomerAttributesRaw = pSite.CustomerDefinedAttributes.split("|");
        var myFeaturedDescription = null;

        for (var i = 0; i < myCustomerAttributesRaw.length; i++) {
            //this will still return an array with one element, and the element will have a length of 0
            var myAttributeRaw = myCustomerAttributesRaw[i].split(":");
            if (myAttributeRaw[0].length !== 0) {
                if (myAttributeRaw[0] != "Featured Site Description") {
                    myFormattedCustomerAttributes += '<li class="ellipsis" title="' + myAttributeRaw[1] + '"><strong>' +
                    myAttributeRaw[0] + ':</strong> ' + myAttributeRaw[1] + '</li>';
                } //end if not FEATURED DESCRIPTION
                else {
                    myFeaturedDescription = myAttributeRaw[1];
                } //end if FEATURED_DESCRIPTION
            } //enf if not empty
        }; //end for each attribute

        var myCardRaw = this.GetRawCard();
        var myListing = myCardRaw;

        var mySubtypesFull = pSite.SubTypes.join(", ");
        var mySubtypes = (mySubtypesFull.length > 20) ? mySubtypesFull.substring(0, 19) + ' &hellip;' : mySubtypesFull;

       

        myListing = myListing.replace(/@SITEID/g, pSite.ID);
        myListing = myListing.replace(/@SSR_TYPE/g, pType);
        myListing = myListing.replace("@SAVE_TITLE", myResultIsSaved ? "Click here to remove this property" : "Click here to save this property");
        myListing = myListing.replace("@SAVE_REMOVE", myResultIsSaved ? 'Remove' : 'Save');
        myListing = myListing.replace("@SAVE_TOFROM", myResultIsSaved ? 'from' : 'to');
        myListing = myListing.replace("@SAVED", myResultIsSaved ? "saved" : "");
        myListing = myListing.replace("@PROPERTY_IMAGE", pSite.Thumbnail);
        myListing = myListing.replace("@DECORATOR_CLASS", "decorated " + pSite.DisplayDecorator);
        myListing = myListing.replace(/@ADDRESS/g, pSite.Address);
        myListing = myListing.replace(/@BUILDING_NAME/g, _NameOrAddressDefault ? ((pSite.BuildingName == null) ? "n/a" : pSite.BuildingName) : pSite.Address);
        myListing = myListing.replace(/@CITY/g, pSite.CityName);
        myListing = myListing.replace("@ZIPCODE", pSite.ZipCode);
        myListing = myListing.replace(/@COUNTY/g, pSite.CountyName);
        myListing = myListing.replace("@STATE", GetStateAbbreviation(pSite.StateID));
        myListing = myListing.replace("@MINSIZE", pSite.MinSize);
        myListing = myListing.replace("@MAXSIZE", pSite.MaxSize);
        myListing = myListing.replace("@TYPE", mySubtypes);
        myListing = myListing.replace("@FULL_TYPE", mySubtypesFull);
        myListing = myListing.replace(/@INDEX/g, pIndex);
        myListing = myListing.replace(/@NUMBER/g, pIndex + 1);
        myListing = myListing.replace("@TOTALCOUNT", _SSR._GetCount(pType));
        myListing = myListing.replace("@DisplaySummaryAttributes", myFormattedCustomerAttributes);
        myListing = myListing.replace("@FEATURED_DESCRIPTION", myFeaturedDescription);
        myListing = myListing.replace("@FEATURED_DESC_VISIBILITY", myFeaturedDescription != null ? 'block;' : 'none;');

        myListing = myListing.replace("@PGR_STYLE", '');
        //        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(/@SITEID/g, "CARD_" + pIndex);
        myListing = myListing.replace("@SAVE_TITLE", "Click here to save this property");
        myListing = myListing.replace("@SAVE_REMOVE", 'Save');
        myListing = myListing.replace("@SAVE_TOFROM", 'to');
        myListing = myListing.replace("@SAVED", '');
        myListing = myListing.replace("@PROPERTY_IMAGE", "/common/images/loading/PropertyLoading.gif");
        myListing = myListing.replace(/@ADDRESS/g, "Loading...");
        myListing = myListing.replace(/@BUILDING_NAME/g, "Loading...");
        myListing = myListing.replace(/@CITY/g, "Loading...");
        myListing = myListing.replace(/@COUNTY/g, "Loading...");
        myListing = myListing.replace("@STATE", "Loading...");
        myListing = myListing.replace("@ZIPCODE", "Loading...");
        myListing = myListing.replace("@MINSIZE", "Loading...");
        myListing = myListing.replace("@TYPE", "Loading...");
        myListing = myListing.replace("@FORSALE", "Loading...");
        myListing = myListing.replace("@FORLEASE", "Loading...");
        myListing = myListing.replace(/@INDEX/g, pIndex);
        myListing = myListing.replace(/@NUMBER/g, pIndex + 1);
        myListing = myListing.replace("@TOTALCOUNT", _SSR._GetCount(pType));

        return myListing;
    },

    GetRawCard: function () {
        var myPropertyHTMLRaw = "\
        <div class='property' id='@SITEID' resultID='@INDEX'>\
            <div class='innerPropertyWrapper'>\
            <div class='propertyImageBox' id='propertyImageBox_@SITEID'>\
               <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('@SITEID')\" title='@SAVE_TITLE'>@SAVE_REMOVE</a>&nbsp;<span class='saveToFrom'>@SAVE_TOFROM</span>&nbsp;Folder</li>\
                            <li><a href=\"javascript:ZoomToProperty('@SITEID')\" title='Zoom the map to this property'>Zoom</a> to Property</li>\
                            <li><a href=\"javascript:ViewPropertyReport('@SITEID','@SSR_TYPE');\" title='View all the reports for this property '>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'>\
                    <h4 class='ellipsis' style='display:@FEATURED_DESC_VISIBILITY;'>\
                        @FEATURED_DESCRIPTION</h4>\
                    <h3>\
                        <a href=\"javascript:ViewPropertyReport('@SITEID','@SSR_TYPE');\">@BUILDING_NAME</a></h3>\
                    <ul>\
                        <li class='ellipsis'><strong>Address:</strong><span title='@ADDRESS'> @ADDRESS</span></li>\
                        <li class='ellipsis'><strong>City:</strong><span title='@CITY'> @CITY</span></li>\
                        <li class='ellipsis'><strong>County:</strong><span title='@COUNTY'> @COUNTY</span></li>\
                        <li class='ellipsis'><strong>Zip Code:</strong>@ZIPCODE</li>\
                    </ul>\
                    <ul>\
                        <li class='subTypes'><strong>Type</strong>:<span title='@FULL_TYPE'> @TYPE</span></li>\
                        <li class='ellipsis'>@MINSIZE</li>\
                        <li class='ellipsis'>@MAXSIZE</li>\
                    </ul>\
                    <ul>\
                        <!-- This section needs to be dynamically created based on the customer specific display attributes -->\
                        @DisplaySummaryAttributes\
                    </ul>\
                    <div class='pagerControl' 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






