﻿/// <reference path="googlemaps-intellisense.js" />
/// <reference path="Utilities.js" />
/// <reference path="SearchResults.js" />
/// <reference path="SearchFunctions.js" />
/// <reference path="MapFunctions.js" />

var GISP_BusinessSearchResultBuilder = function() { };
GISP_BusinessSearchResultBuilder.prototype = {


    CreateSearchResult: 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 () {
                    _BusinessSearchResultBuilder.CreateSearchResult();

                }; //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.BIN);
        this.CreateListingRow(pResult, _SSR._Table, myStyle, myPropertyIcon, myResultIsSaved);

    },


    CreateMarker: function (pResult, pType) {
        var myPhoto = "/common/images/cards/business.png";

        var myCurrentIndex = 1;
        //setup the saved/remove link
        var myResultIsSaved = _SSR.IsResultSaved(pResult.BIN);
        var mySaveRemoveLink = '<a class="tinyButtonLink ButtonTextLink small nonexportable" id="hrefSaveResultInfoWindow_' + pResult.BIN + '" href="javascript:ToggleSaveRemoveReport(\'' + pResult.BIN + '\',\'BUSINESSES\');" title=' + (myResultIsSaved ? '"Click here to remove this business"' : '"Click here to save this business"') + ">" + '<span>' + (myResultIsSaved ? 'Remove' : 'Save') + '</span>' + '</a><span>' + (myResultIsSaved ? ' from' : ' to') + ' folder</span>';

        //clean up the URL
        // clean up pBusiness.URL
        if (pResult.URL !== null && pResult.URL !== undefined && pResult.URL != "") {
            var r = /https?:\/\//;
            var m = r.exec(pResult.URL);
            if (m === undefined || m === null) {
                pResult.URL = "http://" + pResult.URL.toLowerCase();

            }
        }
        else {
            pResult.URL = "";
        }



        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;">\
                        <img src="' + pResult.Thumbnail + '" style="display:block";>\
                        <a href="javascript:ZoomToStreetViewCurrentMaker();">Show Street View</a>\
                    </td>\
                    <td valign="top" style="width:300px;">\
                        <h4>\
                            ' + pResult.BusinessName + '\
                        </h4>\
                        <strong>\
                            ' + pResult.City + ', ' + pResult.State + ' ' + pResult.ZIP + '\
                        </strong>\
                        <div>\
                            <span class="promptText"> Industry:</span> ' + pResult.NAICSDesc + '<br />\
                        </div>\
                        <div>\
                            <span class="promptText"> Employees(estimated):</span> ' + pResult.Employees + '<br />\
                        </div>\
                        <div>\
                            <span class="promptText"> Sales(estimated):</span> ' + pResult.Sales + '<br />\
                        </div>\
                        <div>\
                            <span class="promptText"> Website:</span> <a href="' + pResult.URL + '" target="_blank">' + pResult.URL + '</a><br />\
                        </div>\
                    </td>\
                    <td style="width:100px">\
                          <table cellpadding="1" cellspacing="0" border="0" >\
                            <tr>\
                                <td>' + mySaveRemoveLink + '</td>\
                            </tr><tr>\
                                <td><a class="tinyButtonLink ButtonTextLink small" href="javascript:ZoomToProperty(\'' + pResult.BIN + '\');" title="Click here to zoom in on this business">Zoom</a>&nbsp<span>In</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 = "BUSINESS";

        //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.Color });
        myMarker.ID = pResult.BIN;

        return myMarker;
    },


    CreateListing: function (pBusiness, pIndex, pType, pOrientation) {
        var myResultIsSaved = _SSR.IsResultSaved(pBusiness.BIN);
        var myCardRaw = this.GetRawCard();
        var myListing = myCardRaw;

        // clean up pBusiness.URL
        if (pBusiness.URL !== null && pBusiness.URL !== undefined && pBusiness.URL != "") {
            var r = /https?:\/\//;
            var m = r.exec(pBusiness.URL);
            if (m === undefined || m === null) {
                pBusiness.URL = "http://" + pBusiness.URL.toLowerCase();
                
            }
        }
        else {
            pBusiness.URL = "";
        }

        myListing = myListing
            .replace(/@ID/g, pBusiness.BIN)
            .replace(/@SSR_TYPE/g, pType)
            .replace("@SAVE_TITLE", myResultIsSaved ? "Click here to remove this business" : "Click here to save this business")
            .replace("@SAVE_REMOVE", myResultIsSaved ? 'Remove' : 'Save')
            .replace("@SAVE_TOFROM", myResultIsSaved ? 'from' : 'to')
            .replace("@SAVED", myResultIsSaved ? "saved" : "")
            .replace("@CARD_IMAGE", pBusiness.Thumbnail)
            .replace(/@NAME/, pBusiness.BusinessName)
            .replace(/@COLOR/, pBusiness.Color)
            .replace(/@ADDRESS/, pBusiness.StreetAddress + "<br/>" + pBusiness.City, ", " + pBusiness.State + " " + pBusiness.ZIP)
            .replace(/@SALES/, pBusiness.Sales == null ? "" : pBusiness.Sales)
            .replace(/@EMPLOYEES/, pBusiness.Employees == null ? "" : pBusiness.Employees)
            .replace(/@TYPE/, pBusiness.BusinessTypeDescription == null ? "" : pBusiness.BusinessTypeDescription)
            .replace(/@NAICSDESC/, pBusiness.NAICSDesc == null ? "" : pBusiness.NAICSDesc)
            .replace(/@YEARS/, pBusiness.YearsInBusiness == null ? "" : pBusiness.YearsInBusiness)
            .replace(/@OWNERSHIP/, pBusiness.OwnershipCodeDescription == null ? "" : pBusiness.OwnershipCodeDescription)
            .replace(/@WEB/g, pBusiness.URL)
            .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 business")
            .replace("@SAVE_REMOVE", 'Save')
            .replace("@SAVE_TOFROM", 'to')
            .replace("@SAVED", "")
            .replace("@CARD_IMAGE", "/common/images/loading/PropertyLoading.gif")
            .replace(/@NAME/, "Loading...")
            .replace(/@COLOR/, "")
            .replace(/@ADDRESS/, "Loading...")
            .replace(/@EMPLOYEES/, "Loading...")
            .replace(/@SALES/, "Loading...")
            .replace(/@TYPE/, "Loading...")
            .replace(/@NAICSDESC/, "Loading...")
            .replace(/@YEARS/, "Loading...")
            .replace(/@OWNERSHIP/, "Loading...")
            .replace(/@WEB/g, "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','BUSINESSES')\" 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 Business</li>\
                        </ul>\
                    </div>\
                    <!--/saveSlider-->\
                    <img src='@CARD_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'><span style='display:inline-block;background-color:#@COLOR;height:10px;width:10px;margin:0px 3px 3px 0px'></span>@NAME</h3>\
                    <ul>\
                        <li class='ellipsis'>Employees(est): @EMPLOYEES</li>\
                        <li class='ellipsis'>Sales(est): @SALES</li>\
                        <li class='ellipsis'>Type: @TYPE</li>\
                        <li class='ellipsis'>Industry: @NAICSDESC</li>\
                    </ul>\
                    <ul>\
                        <li class='ellipsis'>Years in Biz: @YEARS</li>\
                        <li class='ellipsis'>Ownership: @OWNERSHIP</li>\
                    </ul>\
                     <ul>\
                        <li class='ellipsis'>Website: <a href='@WEB' title='@WEB' target='_blank'>@WEB</a></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






