﻿// JScript File
Type.registerNamespace("Habitat.WebParts.GeoLocSearch.JavaScript");

// Constructor
Habitat.WebParts.GeoLocSearch.JavaScript.SearchItem = function() {
    this._urlHandler = "/_layouts/Habitat.WebParts.GeoLocSearchWebPart/Data/GeoLocSearchHandler.ashx";
    this._sources = new Array();
}
Habitat.WebParts.GeoLocSearch.JavaScript.SearchItem.prototype = {
    Initialize: function(idBtn) {
        $addHandler($get(idBtn), 'click', Function.createDelegate(this, this.Search));
    },
    Search: function() {
        $get("mapAjaxLoader").style.visibility = "visible";
        // Clear the map
        if (velayerItems != null) {
            velayerItems.DeleteAllShapes();
            map.DeleteShapeLayer(velayerItems);
        }
        //    CallItems: function(delegate, idAgency, idCity, idLodgingNature, idHousingType,
        //                        idLodgingType, idWorkingStatus, isSelling)
        $proxy.CallItems(Function.createDelegate(this, this.CallBackSearch),
                         $get($ddlAgencies).value,
                         $get($ddlCities).value,
                         $get($ddlLodgingNature).value,
                         $get($ddlHousingType).value,
                         $get($ddlLodgingType).value,
                         $get($ddlWorkingStatus).value,
                         $get("cbShowSelling").checked
                         );
    },
    toString: function() {
        return 'Habitat.WebParts.GeoLocSearch.JavaScript.SearchItem';
    },
    CallBackSearch: function(list) {
        $get("mapAjaxLoader").style.visibility = "hidden";
        var pointsSetMap = [];

        // Create a new layer for items
        velayerItems = new VEShapeLayer();

        // Manage case with no results
        if (list.length == 0) {
            // No result
            //$get("").appendChild
            ManageNoResult("ve_habitat_search_map", "ve_message");
        }

        var useSmallIcon = false;
        //        if (list.length > 50) {
        //            useSmallIcon = true;
        //        }

        // Max 500 to avoid browser hanging
        for (var i = 0; i < list.length; i++) {

            // -- Start to compose the description
            var descTemp = "";


            // Eric.G - 26/08/2009 : Modification de la bulle info en fonction de la nature du POI
            switch (list[i].LabelLodgingNatureId) {
                case 1:         // Locatif pure ou à la vente
                    // Global information
                    if (list[i].LabelLodgingUsage != null)
                        descTemp += "Usage : <b>" + list[i].LabelLodgingUsage + "</b><br />";
                    if (list[i].LabelLodgingNature != null)
                        descTemp += "Nature : <b>" + list[i].LabelLodgingNature + "</b><br />";
                    if (list[i].LabelWorkingStatus != null)
                        descTemp += "Travaux : <b>" + list[i].LabelWorkingStatus + "</b><br />";

                    if (descTemp != "")
                        descTemp += "<br />";

                    // Number
                    if ((list[i].CountElement != null) && (list[i].CountElement != 0))
                        descTemp += "Nombre : <b>" + list[i].CountElement + "</b><br />";

                    if (descTemp != "")
                        descTemp += "<br />";

                    // Address
                    if (list[i].HousingName != null)
                        descTemp += list[i].HousingName + "<br />";
                    if (list[i].GroupAddress != null)
                        descTemp += list[i].GroupAddress + "<br />";
                    if (list[i].GroupPostalCode != null)
                        descTemp += list[i].GroupPostalCode + " - ";
                    if (list[i].LabelCity != null)
                        descTemp += list[i].LabelCity + "<br />";

                    if (descTemp != "")
                        descTemp += "<br /><br />";
                    // ID and more info link
                    if (list[i].IdHousingItem != null)
                        descTemp += "<div style='text-align:right;'><a target='_blank' href='"
                          + $get(field_mapInfoDetailsUrl).value + "?"
                          + $get(field_mapInfoQueryStringParameter).value + "="
                          + list[i].IdHousingItem + "'>Voir les détails...</a></div>";

                    if (descTemp != "")
                        descTemp += "<br /><br />";

                    if ((list[i].Visite3DLinkUrl != null) && (list[i].Visite3DLinkUrl != "")) {
                        descTemp += "<div style='text-align:right;'><a target='_blank' href='"
                          + list[i].Visite3DLinkUrl + "'>Visite en 3D...</a></div>";
                    }
                    break;
                case 2:         // Promotion / Accession
                    // Le Titre:
                    descTemp += "<b>Logements Neufs</b><br /><br />";
                    if (list[i].InfoDescription1 != null) {
                        descTemp += "   <b>" + list[i].InfoDescription1 + "</b><br />";
                    }
                    if (list[i].InfoDescription2 != null) {
                        descTemp += "   <b>" + list[i].InfoDescription2 + "</b><br />";
                    }
                    if (list[i].InfoDescription3 != null) {
                        descTemp += "   <b>" + list[i].InfoDescription3 + "</b><br />";
                    }
                    if (descTemp != "")
                        descTemp += "<br />";

                    if ((list[i].DateMiseEnService != null) && (list[i].DateMiseEnService != 0))
                        descTemp += "Date de mise en service : <b>" + list[i].DateMiseEnService + "</b><br /><br />";
                    // Number
                    if ((list[i].CountElement != null) && (list[i].CountElement != 0))
                        descTemp += "Nombre : <b>" + list[i].CountElement + "</b><br />";

                    if (descTemp != "")
                        descTemp += "<br />";

                    // Address
                    if (list[i].HousingName != null)
                        descTemp += list[i].HousingName + "<br />";
                    if (list[i].GroupAddress != null)
                        descTemp += list[i].GroupAddress + "<br />";
                    if (list[i].GroupPostalCode != null)
                        descTemp += list[i].GroupPostalCode + " - ";
                    if (list[i].LabelCity != null)
                        descTemp += list[i].LabelCity + "<br />";

                    if (descTemp != "")
                        descTemp += "<br /><br />";

                    // ID and more info link
                    if (list[i].GoogleMapLinkUrl != null) {
                        if (list[i].Filtrage != null) {
                            descTemp += "<div style='text-align:right;'><a target='_blank' href='"
                          + list[i].GoogleMapLinkUrl + "'>Voir la liste...</a></div>";
                        }
                        else {
                            descTemp += "<div style='text-align:right;'><a target='_blank' href='"
                          + list[i].GoogleMapLinkUrl + "'>Voir la fiche détail...</a></div>";
                        }
                    }

                    if (descTemp != "")
                        descTemp += "<br /><br />";

                    if ((list[i].Visite3DLinkUrl != null) && (list[i].Visite3DLinkUrl != "")) {
                        descTemp += "<div style='text-align:right;'><a target='_blank' href='"
                          + list[i].Visite3DLinkUrl + "'>Visite en 3D...</a></div>";
                    }

                    break;
                case 3:         // Parcelles Libres
                    descTemp += "<b>Parcelles Libres</b><br /><br />";
                    if (list[i].InfoDescription1 != null) {
                        descTemp += "   <b>" + list[i].InfoDescription1 + "</b><br />";
                    }
                    if (list[i].InfoDescription2 != null) {
                        descTemp += "   <b>" + list[i].InfoDescription2 + "</b><br />";
                    }
                    if (list[i].InfoDescription3 != null) {
                        descTemp += "   <b>" + list[i].InfoDescription3 + "</b><br />";
                    }
                    if (descTemp != "")
                        descTemp += "<br />";

                    if ((list[i].DateMiseEnService != null) && (list[i].DateMiseEnService != 0))
                        descTemp += "Date de livraison : <b>" + list[i].DateMiseEnService + "</b><br /><br />";
                    // Number
                    if ((list[i].CountElement != null) && (list[i].CountElement != 0))
                        descTemp += "Nombre : <b>" + list[i].CountElement + "</b><br />";

                    if (descTemp != "")
                        descTemp += "<br />";

                    // Address
                    if (list[i].HousingName != null)
                        descTemp += list[i].HousingName + "<br />";
                    if (list[i].GroupAddress != null)
                        descTemp += list[i].GroupAddress + "<br />";
                    if (list[i].GroupPostalCode != null)
                        descTemp += list[i].GroupPostalCode + " - ";
                    if (list[i].LabelCity != null)
                        descTemp += list[i].LabelCity + "<br />";

                    if (descTemp != "")
                        descTemp += "<br /><br />";

                    // ID and more info link
                    if (list[i].GoogleMapLinkUrl != null) {
                        if (list[i].Filtrage != null) {
                            descTemp += "<div style='text-align:right;'><a target='_blank' href='"
                          + list[i].GoogleMapLinkUrl + "'>Voir la liste...</a></div>";
                        }
                        else {
                            descTemp += "<div style='text-align:right;'><a target='_blank' href='"
                          + list[i].GoogleMapLinkUrl + "'>Voir la fiche détail...</a></div>";
                        }
                    }

                    //                    if (descTemp != "")
                    //                        descTemp += "<br /><br />";

                    if ((list[i].Visite3DLinkUrl != null) && (list[i].Visite3DLinkUrl != "")) {
                        descTemp += "<div style='text-align:right;'><a target='_blank' href='"
                          + list[i].Visite3DLinkUrl + "'>Visite en 3D...</a></div>";
                    }

                    break;
            }

            // -- End the description

            // Compose the information to display the correct icon
            var iconTemp = "";

            // Manage high number of pushpin
            if (list[i].GeoIconType != null) {
                if (useSmallIcon)
                    iconTemp = "s-";

                switch (list[i].GeoIconType) {
                    case 1:
                        iconTemp += "pin-agence.png";
                        break;
                    case 2:
                        iconTemp += "pin-logement-locatif.png";
                        break;
                    case 3:
                        iconTemp += "pin-logement-locatif-av.png";
                        break;
                    case 4:
                        iconTemp += "pin-promotion.png";
                        break;
                    case 5:
                        iconTemp += "pin-parcelle.png";
                        break;
                }
            }


            // Add the pushpin
            if (iconTemp != "") {
                AddPin(list[i].GeoLatitude,  // Latitude
                       list[i].GeoLongitude, // Longitude
                       "",                   // Title
                       descTemp,             // Description
                       "/_layouts/Habitat.WebParts.GeoLocSearchWebPart/Images/" + iconTemp,
                       useSmallIcon,
                       velayerItems);
            } else {
                AddPin(list[i].GeoLatitude,  // Latitude
                       list[i].GeoLongitude, // Longitude
                       "",                   // Title
                       descTemp,             // Description
                       "",
                       useSmallIcon,
                       velayerItems);
            }


            pointsSetMap.push(new VELatLong(list[i].GeoLatitude,
                                            list[i].GeoLongitude));


            // Hide and display the Agencies (to avoid a z-index problem)
            if (velayerAgencies != null) {
                HideAgenciesPoints();
                ShowAgenciesPoints();
            }

        }

        // Add the item layer to the map
        map.AddShapeLayer(velayerItems);

        if (pointsSetMap.length != 0)
            map.SetMapView(pointsSetMap);
    }
}

Habitat.WebParts.GeoLocSearch.JavaScript.SearchItem.registerClass('Habitat.WebParts.GeoLocSearch.JavaScript.SearchItem', Sys.Component);

// Since this script is not loaded by System.Web.Handlers.ScriptResourceHandler
// invoke Sys.Application.notifyScriptLoaded to notify ScriptManager 
// that this is the end of the script.
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
