﻿    var Map;
    var _timer=null;
    var _marker=null;
    var _message='';
    var blnMapLoaded = false;
    
    function pageLoad() {
        if (!blnMapLoaded) {
            GetMapData.MapData(SetupMap, onTimeout, onError);
            blnMapLoaded = true;
        }
    }

    function SetupMap(result) {
        var Lat = result.Center.Lat;
        var Lng = result.Center.Lng;
        var ZoomLevel = result.ZoomLevel;
        var Bounds, sw, ne;

        Map = new GMap2($get("MapDiv"));
        Map.setUIToDefault();

        //Map.addControl(new GSmallMapControl());
        //Map.addControl(new GMapTypeControl());
        Map.setCenter(new GLatLng(Lat, Lng), ZoomLevel);
        //Map.disableScrollWheelZoom();

        Map.disableScrollWheelZoom();
        //Map.disableDoubleClickZoom();

        Bounds = Map.getBounds();

        sw = Bounds.getSouthWest();
        ne = Bounds.getNorthEast();

        //$get("MapInfo").innerHTML="SW Point: " + sw + "<br />" + "NE Point: " + ne + "<br />";

        GEvent.addListener(Map, "dragend", MapMoved);
        GEvent.addListener(Map, "zoomend", MapZoomed);
        //GEvent.addListener(window, "load", pageLoad);

        var strFilters = f_GetFilter();

        GetMapData.GetPointData(sw.lat(), sw.lng(), ne.lat(), ne.lng(), strFilters, GetDataSuccess);
    }

    function SetupCHRMap(result) {
        var Lat = result.Center.Lat;
        var Lng = result.Center.Lng;
        var ZoomLevel = result.ZoomLevel;
        var Bounds, sw, ne;

        Map = new GMap($get("MapDiv"));

        Map.addControl(new GSmallMapControl());
        Map.addControl(new GMapTypeControl());
        Map.setCenter(new GLatLng(Lat, Lng), ZoomLevel);
        //Map.disableScrollWheelZoom();

        Map.disableScrollWheelZoom();
        Map.disableDoubleClickZoom();

        GetMapData.GetPointDataCHR(GetDataSuccess);
    }
    
    function f_GetFilter() {
        var strFilter = "";
        var blnOneFound = false;
        
        if (forIdx == 0) {
            // --------------------------------------
            // For Sale
            var blnHomeForSale = (document.getElementById("chkHomeForSale").checked == true);
            var blnCondoForSale = (document.getElementById("chkCondoForSale").checked == true);
            var blnLandForSale = (document.getElementById("chkLandForSale").checked == true);
            var blnOtherForSale = (document.getElementById("chkOtherForSale").checked == true);

            if (!(blnHomeForSale && blnCondoForSale && blnLandForSale && blnOtherForSale)) {
                if (blnHomeForSale || blnCondoForSale || blnLandForSale || blnOtherForSale) {
                    strFilter += " AND (";
                    if (blnHomeForSale) {
                        strFilter += "(strCategory = 'Detached Single Family' ";
                        blnOneFound = true;
                    }

                    if (blnCondoForSale) {
                        if (blnOneFound) {
                            strFilter += " OR ";
                        } else {
                            strFilter += "( ";
                        }
                        strFilter += "strCategory = 'Condominiums' ";
                        blnOneFound = true;
                    }

                    if (blnLandForSale) {
                        if (blnOneFound) {
                            strFilter += " OR ";
                        } else {
                            strFilter += "( ";
                        }
                        strFilter += "strCategory = 'Residential Land'";
                        blnOneFound = true;
                    }
                  
                    if (blnOtherForSale) {
                        if (blnOneFound) { strFilter += ") OR "; }
                        strFilter += "(strCategory <> 'Detached Single Family' AND strCategory <> 'Condominiums' AND strCategory <> 'Residential Land' AND strCategory <> 'Rental')";
                        blnOneFound = true;
                    }

                    if ((blnHomeForSale || blnCondoForSale || blnLandForSale) && !(blnOtherForSale)) {
                        strFilter += ") "
                    }
                    
                    if (blnOneFound) {strFilter += ") ";}

                }
            } else {
                //if (blnHomeForSale) {
                    strFilter += " AND (strCategory <> 'Rental') ";
                //}
            }

            blnOneFound = false
            // --------------------------------------
        } else {
            // --------------------------------------
            // For Rent
            var blnHomeForRent = (document.getElementById("chkHomeForRent").checked == true);
            var blnTownhouseForRent = (document.getElementById("chkTownhouseForRent").checked == true);
            var blnCondoForRent = (document.getElementById("chkCondoForRent").checked == true);
            var blnAppartment = (document.getElementById("chkAppartment").checked == true);
            //var blnOtherForRent = (document.getElementById("chkOtherForRent").checked == true);
            
            if (blnHomeForRent || blnTownhouseForRent || blnCondoForRent || blnAppartment) {
                strFilter += " AND (strCategory = 'Rental' AND (";

                if (blnHomeForRent) {
                    strFilter += " strListingClass = 'DETACHED SINGLE FAMILY' ";
                    blnOneFound = true;
                }

                if (blnTownhouseForRent) {
                    if (blnOneFound) {
                        strFilter += " OR ";
                    }

                    strFilter += " strListingClass = 'ATTACHED SINGLE FAMILY' ";
                    blnOneFound = true;
                }

                if (blnCondoForRent) {
                    if (blnOneFound) {
                        strFilter += " OR ";
                    }

                    strFilter += " strListingClass = 'CONDOMINIUM' ";
                    blnOneFound = true;
                }

                if (blnAppartment) {
                    if (blnOneFound) {
                        strFilter += " OR ";
                    }

                    strFilter += " strListingClass = 'APARTMENT'";
                    blnOneFound = true;
                }
                
                strFilter += ")) ";
                
                /*if (blnOtherForRent) {
                    if (blnOneFound) {
                        strFilter += ") OR ";
                    }
                    strFilter += " (strListingClass <> 'DETACHED SINGLE FAMILY' AND strListingClass <> 'CONDOMINIUM'))";
                } else {
                    strFilter += ")) ";
                }*/
            } else {
                strFilter += " AND (strCategory = 'Rental' AND (strListingClass = '-10001')) "
            }
            // --------------------------------------
        }

        if (forIdx == 0) {
            var blnActiveForCHR = (document.getElementById("chkActiveForCHR").checked == true);
            var blnSoldForCHR = (document.getElementById("chkSoldForCHR").checked == true);
            var blnPendingForCHR = (document.getElementById("chkPendingForCHR").checked == true);

            var blnActiveForOthers = (document.getElementById("chkActiveForOthers").checked == true);
            var blnSoldForOthers = (document.getElementById("chkSoldForOthers")) ? (document.getElementById("chkSoldForOthers").checked == true) : false;
            var blnPendingForOthers = (document.getElementById("chkPendingForOthers")) ? (document.getElementById("chkPendingForOthers").checked == true) : false;

            // --------------------------------------
            // For Carriage Hills
            if (blnActiveForCHR || blnSoldForCHR || blnPendingForCHR) {
                strFilter += " AND ";

                if (blnActiveForOthers || blnPendingForOthers || blnSoldForOthers) {
                    strFilter += "("
                }



                if (true) {
                strFilter += "((("

                if (blnActiveForCHR) {
                    strFilter += "bytActiveStatus=0";
                    blnOneFound = true;
                }

                if (blnPendingForCHR) {
                    if (blnOneFound) {strFilter += " OR "};
                    strFilter += "bytActiveStatus=1 OR bytActiveStatus=4";
                    blnOneFound = true;
                }

                if (blnSoldForCHR) {
                    if (blnOneFound) {strFilter += " OR "};
                    strFilter += "bytActiveStatus=2";
                    blnOneFound = true;
                }
            } else {
                strFilter += "((("

                if (blnActiveForCHR) {
                    strFilter += "bytActiveStatus=0";
                    blnOneFound = true;
                }

                if (blnPendingForCHR) {
                    if (blnOneFound) {strFilter += " OR "};
                    strFilter += "bytActiveStatus=1 OR bytActiveStatus=4";
                    blnOneFound = true;
                }

                if (blnSoldForCHR) {
                    if (blnOneFound) {strFilter += " OR "};
                    strFilter += "bytActiveStatus=2";
                    blnOneFound = true;
                }
                }
                
                
                
                strFilter += ")) AND (strOfficeId='ECAR' ";

                if (blnSoldForCHR || blnPendingForCHR) {
                    strFilter += "OR strSellingOfficeID='ECAR'";
                }
                strFilter += ")) ";
            }

            // --------------------------------------
            // For Others

            // User is authenticated
            if (blnPendingForOthers || blnSoldForOthers) {
                if (blnActiveForOthers || blnPendingForOthers || blnSoldForOthers) {
                    blnOneFound = false;
                    //strFilter += "|| (strOfficeId!='" & ConfigurationManager.AppSettings("CHR_OfficeID") & "' AND bytActiveStatus IN ("
                    if (blnActiveForCHR || blnSoldForCHR || blnPendingForCHR) {
                        strFilter += " OR ";
                    } else {
                        strFilter += " AND  ";
                    }
                    strFilter += " ((bytActiveStatus IN (";


                    if (blnActiveForOthers) {
                        strFilter += "0";
                        blnOneFound = true;
                    }

                    if (blnPendingForOthers) {
                        if (blnOneFound) {strFilter += ","};
                        strFilter += "1,4";
                        blnOneFound = true;
                    }

                    if (blnSoldForOthers) {
                        if (blnOneFound) {strFilter += ","};
                        strFilter += "2";
                        blnOneFound = true;
                    }

                    strFilter += ")) AND (strOfficeId!='ECAR' ";

                    if (blnSoldForOthers || blnPendingForOthers) {
                        strFilter += "AND strSellingOfficeID!='ECAR'";
                    }

                    strFilter += ")) ";
                }
            } else if (blnActiveForOthers) {
                if (blnActiveForCHR || blnSoldForCHR || blnPendingForCHR) {
                    strFilter += " OR ";
                } else {
                    strFilter += " AND  ";
                }
                strFilter += " (strOfficeId!='ECAR' AND bytActiveStatus IN (0))";
            }
            
            if ((blnActiveForCHR || blnSoldForCHR || blnPendingForCHR) && (blnActiveForOthers || blnPendingForOthers || blnSoldForOthers)) {
                strFilter += ") "
            }

            if (!(blnActiveForCHR || blnSoldForCHR || blnPendingForCHR || blnPendingForOthers || blnSoldForOthers || blnActiveForOthers)) {
                strFilter += " AND ((bytActiveStatus IN ";
                // this is nothing.  need a value in the IN clause.
                strFilter += "(1000)";
                strFilter += ")) ";
            }

        } else {
            // --------------------------------------
            // For Carriage Hills
            strFilter += " AND ((strOfficeId='ECAR' AND bytActiveStatus IN (";

            if ((document.getElementById("chkActiveForCHRRent").checked == true)) {
                if (document.getElementById("chkActiveForCHRRent").checked == true) {
                    strFilter += "0";
                 }
            } else {
                // this is nothing.  need a value in the IN clause.
                strFilter += "1000";
            }
            strFilter += ")) ";
            
            // --------------------------------------
            // For Others
            if ((document.getElementById("chkActiveForOthersRent").checked == true)) {
                strFilter += "OR (strOfficeId!='ECAR' AND bytActiveStatus IN (";
                
                if (document.getElementById("chkActiveForOthersRent").checked == true) {
                    strFilter += "0";
                 }
                strFilter += ")) ";
            }
            strFilter += ") ";
        }

        blnOneFound = false;

        if (forIdx == 0) {
            // --------------------------------------
            // Price
            if (!((document.getElementById("chkPrice1").checked == true) &&
                (document.getElementById("chkPrice2").checked == true) &&
                (document.getElementById("chkPrice3").checked == true) &&
                (document.getElementById("chkPrice4").checked == true) &&
                (document.getElementById("chkPrice5").checked == true) &&
                (document.getElementById("chkPrice6").checked == true))) {

                strFilter += " AND ( ";
                if ((document.getElementById("chkPrice1").checked == true) ||
                    (document.getElementById("chkPrice2").checked == true) ||
                    (document.getElementById("chkPrice3").checked == true) ||
                    (document.getElementById("chkPrice4").checked == true) ||
                    (document.getElementById("chkPrice5").checked == true) ||
                    (document.getElementById("chkPrice6").checked == true)) {
                        if (document.getElementById("chkPrice1").checked == true) {
                            strFilter += "curPrice BETWEEN 0 AND 199999";
                            blnOneFound = true;
                        } 
                        if (document.getElementById("chkPrice2").checked == true) {
                            if (blnOneFound) strFilter += " OR ";
                            strFilter += "curPrice BETWEEN 200000 AND 299999 ";
                            blnOneFound = true;
                        }
                        if (document.getElementById("chkPrice3").checked == true) {
                            if (blnOneFound) strFilter += " OR ";
                            strFilter += "curPrice BETWEEN 300000 AND 399999 ";
                            blnOneFound = true;
                        }
                        if (document.getElementById("chkPrice4").checked == true) {
                            if (blnOneFound) strFilter += " OR ";
                            strFilter += "curPrice BETWEEN 400000 AND 499999 ";
                            blnOneFound = true;
                        }
                        if (document.getElementById("chkPrice5").checked == true) {
                            if (blnOneFound) strFilter += " OR ";
                            strFilter += "curPrice BETWEEN 500000 AND 599999 ";
                            blnOneFound = true;
                        }
                        if (document.getElementById("chkPrice6").checked == true) {
                            if (blnOneFound) strFilter += " OR ";
                            strFilter += "curPrice > 599999 ";
                            blnOneFound = true;
                        }
                } else {
                    strFilter += "curPrice BETWEEN -1 AND -2 ";
                }
                strFilter += ") ";
            }
        } else {
            // --------------------------------------
            // Rent per Month
            if (!((document.getElementById("chkRent1").checked == true) &&
                (document.getElementById("chkRent2").checked == true) &&
                (document.getElementById("chkRent3").checked == true) &&
                (document.getElementById("chkRent4").checked == true) &&
                (document.getElementById("chkRent5").checked == true) &&
                (document.getElementById("chkRent6").checked == true))) {

                strFilter += " AND ( ";
                if ((document.getElementById("chkRent1").checked == true) ||
                    (document.getElementById("chkRent2").checked == true) ||
                    (document.getElementById("chkRent3").checked == true) ||
                    (document.getElementById("chkRent4").checked == true) ||
                    (document.getElementById("chkRent5").checked == true) ||
                    (document.getElementById("chkRent6").checked == true)) {
                        if (document.getElementById("chkRent1").checked == true) {
                            strFilter += "curPrice BETWEEN 0 AND 500";
                            blnOneFound = true;
                        } 
                        if (document.getElementById("chkRent2").checked == true) {
                            if (blnOneFound) strFilter += " OR ";
                            strFilter += "curPrice BETWEEN 501 AND 750 ";
                            blnOneFound = true;
                        }
                        if (document.getElementById("chkRent3").checked == true) {
                            if (blnOneFound) strFilter += " OR ";
                            strFilter += "curPrice BETWEEN 751 AND 1000 ";
                            blnOneFound = true;
                        }
                        if (document.getElementById("chkRent4").checked == true) {
                            if (blnOneFound) strFilter += " OR ";
                            strFilter += "curPrice BETWEEN 1001 AND 1250 ";
                            blnOneFound = true;
                        }
                        if (document.getElementById("chkRent5").checked == true) {
                            if (blnOneFound) strFilter += " OR ";
                            strFilter += "curPrice BETWEEN 1250 AND 1499 ";
                            blnOneFound = true;
                        }
                        if (document.getElementById("chkRent6").checked == true) {
                            if (blnOneFound) strFilter += " OR ";
                            strFilter += "curPrice > 1499 ";
                            blnOneFound = true;
                        }
                } else {
                    strFilter += "curPrice BETWEEN -1 AND -2 ";
                }
                strFilter += ") ";
            }
        }
                
        blnOneFound = false;
        // --------------------------------------
        // Area
        if (!((document.getElementById("chkNiceville").checked == true) &&
           (document.getElementById("chkCrestview").checked == true) &&
           (document.getElementById("chkFtWalton").checked == true) &&
           (document.getElementById("chkDestin").checked == true) &&
           (document.getElementById("chkAllOther").checked == true))) {

            if ((document.getElementById("chkNiceville").checked == true) ||
                (document.getElementById("chkCrestview").checked == true) ||
                (document.getElementById("chkFtWalton").checked == true) ||
                (document.getElementById("chkDestin").checked == true) ||
                (document.getElementById("chkAllOther").checked == true)) {

                if ((document.getElementById("chkNiceville").checked == true) ||
                    (document.getElementById("chkCrestview").checked == true) ||
                    (document.getElementById("chkFtWalton").checked == true) ||
                    (document.getElementById("chkDestin").checked == true)) {
                    strFilter += " AND (( strListingArea IN (";

                    if (document.getElementById("chkNiceville").checked == true) {
                        strFilter += "'Niceville'";
                        blnOneFound = true;
                    }
                    if (document.getElementById("chkCrestview").checked == true) {
                        if (blnOneFound) strFilter += ",";
                        strFilter += "'Crestview Area'";
                        blnOneFound = true;
                    }
                    if (document.getElementById("chkFtWalton").checked == true) {
                        if (blnOneFound) strFilter += ",";
                        strFilter += "'Fort Walton Beach'";
                        blnOneFound = true;
                    }
                    if (document.getElementById("chkDestin").checked == true) {
                        if (blnOneFound) strFilter += ",";
                        strFilter += "'Destin'";
                        blnOneFound = true;
                    }
                    strFilter += ")) ";
                }
                if (document.getElementById("chkAllOther").checked == true) {
                    if (blnOneFound)
                        strFilter += " OR ";
                    else
                        strFilter += " AND ";

                    strFilter += "( strListingArea NOT IN ('Niceville','Crestview Area','Fort Walton Beach','Destin'))";
                }
                if (blnOneFound)
                    strFilter += ")";

            } else {
                // nothing was checked so return nothing
                strFilter += " AND strListingArea = -1";
            }
        }
  
        // --------------------------------------
        // Type
        var blnWaterfront = (document.getElementById("chkWaterfront").checked == true);

        if (blnWaterfront) {
            strFilter += " AND (strWaterfront <> '') ";
        }

        var blnForeclosureDistressed = (document.getElementById("chkForeclosureDistressed").checked == true);
        if (blnForeclosureDistressed) {
            strFilter += " AND (strSaleType LIKE '%Short Sale%' OR strSaleType LIKE '%As Is%' OR strSaleType LIKE '%Foreclosure%' OR strSaleType LIKE '%REO%' OR strSaleType LIKE '%Auction%' OR strSaleType LIKE '%Pre-foreclosure%') ";
        }


        var blnGolfCourse = (document.getElementById("chkGolfCourse").checked == true);                          //FTR_LOTDESC or FTR_PROJFACILITIES
        if (blnGolfCourse) {
            strFilter += " AND (strLotDescription LIKE '%golf%') ";
        }

        var blnPool  = (document.getElementById("chkPool").checked == true);                                      //FTR_EXTERIOR maybe FTR_PROJFACILITIES
        if (blnPool) {
            strFilter += " AND (strExterior LIKE '%pool%') ";
        }

        var ddlBeds = document.getElementById("ctl00_mainContent_ddlBeds");
        var strBed = ddlBeds.options[ddlBeds.selectedIndex].value;
        if (strBed != "0") {
            strFilter += " AND (bytBed " + strBed + ") ";
        }

        var ddlBaths = document.getElementById("ctl00_mainContent_ddlBaths");
        var strBath = ddlBaths.options[ddlBaths.selectedIndex].value;
        if (strBath != "0") {
            strFilter += " AND (decBath " + strBath + ") ";
        }
        // --------------------------------------

        return strFilter;
    }
    
    function onTimeout(result) {
    }

    function onError(result) {
    }

    function MapMoved() {
        var Bounds, sw, ne;

        document.getElementById("loadingDivWrapper").style.display = "block";

        Bounds = Map.getBounds();

        sw = Bounds.getSouthWest();
        ne = Bounds.getNorthEast();

        Map.clearOverlays();

        var strFilters = f_GetFilter();

        GetMapData.GetPointData(sw.lat(), sw.lng(), ne.lat(), ne.lng(), strFilters, GetDataSuccess);
    }

    function MapZoomed(OldZoomLevel, NewZoomLevel) {
        MapMoved();
    }

    function GetDataSuccess(result) {
        var i = 0;

        //$get("MapInfo").innerHTML = "";

        Bounds = Map.getBounds();

        sw = Bounds.getSouthWest();
        ne = Bounds.getNorthEast();

        //$get("MapInfo").innerHTML="SW Point: " + sw + "<br />" + "NE Point: " + ne + "<br />";

        // 0 item is the total count entry
        var iTotal = result[0].Description;
        
        for(i=1;i<result.length;i++) {
            var pt = new GLatLng(result[i].Location.Lat, result[i].Location.Lng);

            Map.addOverlay(DisplayPointMarker(pt, result[i].Description, result[i].Type, result[i].PropertyId));

           //$get("MapInfo").innerHTML += "Point Location - Lat: " +
           //     result[i].Location.Lat + " Lng: " + result[i].Location.Lng + "<br />";
        }
            document.getElementById("propLoaded").innerHTML = (result.length-2) + " Listings Mapped, " + iTotal + " Found";
            document.getElementById("loadingDivWrapper").style.display = "none";
            Map.getPane(G_MAP_FLOAT_SHADOW_PANE).style.display = "none";
    }

    function DisplayPointMarker(point, Description, Type, PropertyId) {
        var tinyIcon = new GIcon();

        switch (Type) {
            case 0:
                tinyIcon.image = "images/carriage-icon-available.jpg";
                //tinyIcon.image = "images/black-carriage-logo.jpg";
                break;
            case 1:
                tinyIcon.image = "images/carriage-icon-pending.jpg";
                break;
            case 2:
                tinyIcon.image = "images/carriage-icon-sold.jpg";
                break;
            case 3:
                tinyIcon.image = "images/carriage-icon-rental.jpg";
                break;
            case 4:
                tinyIcon.image = "images/other-icon-available.jpg";
                break;
            case 5:
                tinyIcon.image = "images/other-icon-pending.jpg";
                break;
            case 6:
                tinyIcon.image = "images/other-icon-sold.jpg";
                break;
            case 7:
                tinyIcon.image = "images/other-icon-rentals.jpg";
                break;
            case 8:
                tinyIcon.image = "images/ch-logo-map.gif";
                tinyIcon.iconSize = new GSize(46, 38);
                tinyIcon.iconAnchor = new GPoint(20, 20);
                tinyIcon.infoWindowAnchor = new GPoint(20, 20);
                break;
            default:
                tinyIcon.image = "images/blank-red.png";
                //tinyIcon.iconSize = new GSize(12, 20);
                tinyIcon.iconAnchor = new GPoint(6, 20);
                tinyIcon.infoWindowAnchor = new GPoint(5, 1);
                break;
        }
 
        if (Type < 8) {
            tinyIcon.iconSize = new GSize(23, 19);
            tinyIcon.iconAnchor = new GPoint(6, 20);
            tinyIcon.infoWindowAnchor = new GPoint(5, 1);
        }
       

/*        tinyIcon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
        //tinyIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
        tinyIcon.iconSize = new GSize(12, 20);
        tinyIcon.shadowSize = new GSize(22, 20);
        tinyIcon.iconAnchor = new GPoint(6, 20);
        tinyIcon.infoWindowAnchor = new GPoint(5, 1);*/
        var marker = new GMarker(point, { icon:tinyIcon });

        //var gmrker = new GMarker(point);
        var Message = Description

            GEvent.addListener(marker, "click", function() {
                //Map.openInfoWindowHtml(point, Message);

                window.open ("ListingDetail.aspx?intPropertyId=" + PropertyId,"_blank","toolbar=no,menubar=no,location=no,status=no,scrollbars=yes"); 
            });

            GEvent.addListener(marker, 'mouseover', function() {
                /* This is the code that does the delay on the mouse over */
                if (_timer) 
                    clearTimeout(_timer);
                _marker = marker;
                _message = Message;
                _timer = setTimeout('_marker.openInfoWindow(_message)',500);
                
                //Map.openInfoWindowHtml(point, Message);
            });

            /*GEvent.addListener(marker, 'mouseout', function() {
                Map.closeInfoWindow();

            });*/
                        
        return(marker);
    }

/*    if (window.addEventListener) {
        window.addEventListener('load', pageLoad, false);
    } else if(window.attachEvent) {
        window.attachEvent('onload', pageLoad);
    }*/
