﻿/// <reference path="googlemaps-intellisense.js" />
/// <reference path="Utilities.js" />
/// <reference path="SearchResults.js" />
/// <reference path="SearchFunctions.js" />
/// <reference path="MapFunctions.js" />

var GISP_SiteSearchResultBulider = function() { };
GISP_SiteSearchResultBulider.prototype = {


    CreateSiteSearchResult: 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() {
                    _SiteSearchResultBuilder.CreateSiteSearchResult();

                }; //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 = "SITES";

        if (pResult.ID.toString().indexOf("SITES") == -1) {
            pResult.ID = 'SITES_' + pResult.ID;
        }; //


        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
        pResult.photo = myPhoto;

        //setup the saved/remove link
        var myResultIsSaved = _SSR.IsResultSaved(pResult.ID);
        var mySaveRemoveLink = '<a class="tinyButtonLink ButtonTextLink small" 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;">\
                        <a class="ResultAddress" href="javascript:ViewPropertyReport(\'' + pResult.ID + '\')">\
                            ' + pResult.address + ' \
                        </a>\
                        <br/>\
                        ' + pResult.city + ' , ' + pResult.state + ' ' + pResult.zipcode + '<br />\
                        <strong> Building:</strong> ' + pResult.bldgname + '<br />\
                        <strong> County:</strong> ' + pResult.county + '<br />\
</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 property"> <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:ViewPropertyReport(\'' + pResult.ID + '\');" title="Click here to view the full property report"> <img align="absmiddle" src="/common/images/buttons/document_inactive.png" /><span>Full Report</span></a></td>\
                            </tr><tr>\
                                <td><a class="tinyButtonLink ButtonTextLink small" href="javascript:ViewBusinessReport(\'' + pResult.ID + '\');" title="Click here to view the business report around this property"> <img align="absmiddle" src="/common/images/buttons/economicreport_inactive.png" /><span>Business 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 around this property"><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 = "BUILDING";
        switch (pResult.type) {
            case "Office":
            case "office":
                myPropertyType = "OFFICE";
                break;
            case "vacantland":
            case "land":
                myPropertyType = "LAND";
                break;
            case "Industrial":
            case "industrial":
                myPropertyType = "INDUSTRIAL";
                break;
            case "Warehouse":
            case "warehouse":
                myPropertyType = "WAREHOUSE";
                break;
            case "Callcenter":
            case "callcenter":
                myPropertyType = "CALLCENTER";
                break;
        }; //end switch


        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 + '\');" class="thickbox" title="Click here to view a map around this property"><img src="' + pIcon + '" id="imgSearchResultIcon_' + pSite.ID + '"/></a>\
                            </td>\
                        </tr>\
                         <tr>\
                            <td align="left">\
                                ' + mySaveRemoveLink + '\
                            </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:ViewPropertyReport(\'' + pSite.ID + '\')" title="Click here to view reports on this property">\
                        <img src="' + pSite.photo + '">\
                    </a>';

        var myCell_3 = document.createElement("td");
        GISPlanning_MapUtilities_SetHTMLAttributes(myCell_3, { vAlign: "top", colSpan: 4 });
        GISPlanning_MapUtilities_SetHTMLAttributes(myCell_3.style, { padding: "2px", textAlign: "left", fontSize: "9pt" });
        myCell_3.innerHTML = '\
                        <a class="ResultAddress" href="javascript:ViewPropertyReport(\'' + pSite.ID + '\')" title="Click here to view reports on this property">\
                            ' + pSite.address + ' \
                        </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 = '\
                        <span style="position:relative;"><a id="hrefcity_' + pSite.ID + '"  href="javascript:InteractWithGeoEntity(\'' + pSite.ID + '\',\'city\',\'' + pSite.CityID + '\');" class="InteractiveResultLink popupLink" title="Click here to modify your search with this city">' + pSite.city + '</a></span> , <span style="position:relative;"><a id="hrefstate_' + pSite.ID + '" href="javascript:InteractWithGeoEntity(' + pSite.ID + ',\'state\',\'' + pSite.state + '\');" class="InteractiveResultLink popupLink"  title="Click here to modify your search with this state">' + pSite.state + '</a></span> <span style="position:relative;">' + pSite.zipcode + '</span><br />\
                        <strong> Building:</strong> ' + pSite.bldgname + '<br />\
                        <strong> County:</strong> <span style="position:relative;"><a id="hrefcounty_' + pSite.ID + '" href="javascript:InteractWithGeoEntity(\'' + pSite.ID + '\',\'county\',\'' + pSite.CountyID + '\');" class="InteractiveResultLink popupLink" title="Click here to modify your search with this county">' + pSite.county + '</a></span><br />';
  		 

//<strong> RIDA:</strong> <span style="position:relative;"><a id="hrefregion_' + pSite.ID + '" href="javascript:InteractWithGeoEntity(\'' + pSite.ID + '\',\'region\',\'' + pSite.RIDA + '\');" class="InteractiveResultLink popupLink" title="Click here to modify your search with this region">' + pSite.RIDA + '</a></span><br />';





        var myCell_4 = document.createElement("td");
        GISPlanning_MapUtilities_SetHTMLAttributes(myCell_4.style, { padding: "2px", textAlign: "left", fontSize: "9pt", width: "155px" });
        GISPlanning_MapUtilities_SetHTMLAttributes(myCell_4, { vAlign: "top" });
        myCell_4.innerHTML = '\
                        <strong> Type:</strong> ' + pSite.type + '<br/>\
                        <strong> For Sale:</strong> ' + pSite.forsale + '<br />\
                        <strong> For Lease:</strong> ' + pSite.forlease;

        var myCell_5 = document.createElement("td");
        GISPlanning_MapUtilities_SetHTMLAttributes(myCell_5.style, { padding: "2px", textAlign: "left", fontSize: "9pt", width: "155px" });
        GISPlanning_MapUtilities_SetHTMLAttributes(myCell_5, { vAlign: "top" });
        myCell_5.innerHTML = '\
                        <strong> Size:</strong> ' + pSite.minsize + ' to ' + pSite.maxsize + ' ' + (pSite.type == 'vacantland' ? 'acres' : 'sqft') + '<br />\
                        <strong> Building Sqft:</strong> ' + (pSite.size != null ? pSite.size : '---') + '<br />\
                        <strong> Total Acres:</strong> ' + (pSite.lotsize != null ? pSite.lotsize : '---') + '<br />';

        var myCell_6 = document.createElement("td");
        GISPlanning_MapUtilities_SetHTMLAttributes(myCell_6, { vAlign: "top" });
        GISPlanning_MapUtilities_SetHTMLAttributes(myCell_6.style, { padding: "2px", textAlign: "left", fontSize: "9pt", width: "155px" });

        //distance to interstate can be somewhat long and promplematic to display
        var myDistanceToInterstate = pSite.distinterstate != null ? pSite.distinterstate : "";
        if (myDistanceToInterstate.length > 10) {
            myDistanceToInterstate = myDistanceToInterstate.substr(0, 7) + "...";
        }; ; //end if to long


	 if (pSite.type == 'vacantland') {


        myCell_6.innerHTML = '\
                        <strong> Rail:</strong> ' + pSite.rail + '<br />\
                        <strong> Dist. to Interstate:</strong> <span title="' + pSite.distinterstate + '">' + myDistanceToInterstate + '</span><br />';
                        

	} else {

	   myCell_6.innerHTML = '\
                        <strong> Rail:</strong> ' + pSite.rail + '<br />\
                        <strong> Dist. to Interstate:</strong> <span title="' + pSite.distinterstate + '">' + myDistanceToInterstate + '</span><br />\
                        <strong> Min Ceiling:</strong> ' + (pSite.minceiling != null ? pSite.minceiling : '---') + '<br />\
                        <strong> Max Ceiling:</strong> ' + (pSite.maxceiling != null ? pSite.maxceiling : '---') + '<br />';


	}


        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.className = pStyle;
        myBottomRow.setAttribute("align", "left");
        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);
        myBottomRow.appendChild(myCell_6);


        //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='address' id='rbSort_address' onclick='SortResults(this.value);' " + (pCurrentlyChecked == "address" ? "checked='checked'" : "") + " />\
                        </td>\
                        <td>\
                            Address\
                        </td>\
                        <td>\
                            <input type='radio' name='Sorter' value='default' id='rbSort_default' onclick='SortResults(this.value);' " + (pCurrentlyChecked == "default" ? "checked='checked'" : "") + " />\
                        </td>\
                        <td>\
                            Unsorted\
                        </td>\
                    </tr>\
                    <tr>\
                        <td>\
                            <input type='radio' name='Sorter' value='zipcode' id='rbSort_zip' onclick='SortResults(this.value);' " + (pCurrentlyChecked == "zipcode" ? "checked='checked'" : "") + "  />\
                        </td>\
                        <td>\
                            Zip Code\
                        </td>\
                        <td>\
                            <input type='radio' name='Sorter' value='type' id='rbSort_type' onclick='SortResults(this.value);' " + (pCurrentlyChecked == "type" ? "checked='checked'" : "") + "  />\
                        </td>\
                        <td>\
                            Type\
                        </td>\
                    </tr>\
                    <tr>\
                        <td>\
                            <input type='radio' name='Sorter' value='city' id='rbSort_city' onclick='SortResults(this.value);' " + (pCurrentlyChecked == "city" ? "checked='checked'" : "") + "  />\
                        </td>\
                        <td>\
                            City\
                        </td>\
                        <td>\
                            <input type='radio' name='Sorter' value='size' id='rbSort_size' onclick='SortResults(this.value);' " + (pCurrentlyChecked == "size" ? "checked='checked'" : "") + "  />\
                        </td>\
                        <td>\
                            Size\
                        </td>\
                    </tr>\
                    <tr>\
                        <td>\
                            <input type='radio' name='Sorter' value='county' id='rbSort_county' onclick='SortResults(this.value);' " + (pCurrentlyChecked == "county" ? "checked='checked'" : "") + "  />\
                        </td>\
                        <td>\
                            County\
                        </td>\
                        <td>\
                            <input type='radio' name='Sorter' value='ceiling' id='rbSort_ceiling' onclick='SortResults(this.value);' " + (pCurrentlyChecked == "ceiling" ? "checked='checked'" : "") + " />\
                        </td>\
                        <td>\
                            Ceiling\
                        </td>\
                    </tr>\
                    <tr>\
                        <td>\
                            <input type='radio' name='Sorter' value='state' id='rbSort_state' onclick='SortResults(this.value);' " + (pCurrentlyChecked == "state" ? "checked='checked'" : "") + " />\
                        </td>\
                        <td>\
                            State\
                        </td>\
                        <td>\
                        </td>\
                        <td>\
                        </td>\
                    </tr>\
                    <tr>\
                        <td>\
                            <input type='radio' name='Sorter' value='region' id='rbSort_region' onclick='SortResults(this.value);' " + (pCurrentlyChecked == "region" ? "checked='checked'" : "") + "  />\
                        </td>\
                        <td>\
                            Region\
                        </td>\
                        <td>\
                        </td>\
                        <td>\
                        </td>\
                    </tr>\
                </table>\
            </em>";
    }
};                                    //end prototype definition





