﻿var KfMap = {
    map: {},
    xhrUrl: "/services/mapsearch.aspx?",
    lat: null,
    lon: null,
    mapData: [],
    XHRequest: {},
    isDirty: null,
    isDirty: false,
    requestQuery: null,
    html: [],
    options: {
        addQuery: null,
        blat: null,
        blon: null,
        searchType: null
    },
    RequestData: function () {

        document.getElementById('mapLoader').className = "";

        if (window.XMLHttpRequest) {
            KfMap.XHRequest = new XMLHttpRequest();
        }
        else if (window.ActiveXObject) {
            KfMap.XHRequest = new ActiveXObject("Microsoft.XMLHTTP");
        }
        else {
            alert(notSupport);
            return false;
        };

        if (KfMap.lat != 0 && KfMap.lon != 0) {

            KfMap.requestQuery = "lat=" + KfMap.lat + "&lon=" + KfMap.lon + "&blat=" + KfMap.options.blat + "&blon=" + KfMap.options.blon;

            requestUrl = KfMap.xhrUrl + KfMap.requestQuery + "&" + KfMap.options.addQuery;
        }
        else {
            requestUrl = KfMap.xhrUrl + KfMap.options.addQuery;
        }

        KfMap.XHRequest.open("get", requestUrl, true);

        KfMap.XHRequest.onreadystatechange = function () { KfMap.ResponseHandler(); };

        KfMap.XHRequest.send(null);
    },
    ResponseHandler: function () {


        if (KfMap.XHRequest.readyState == 4) {
            if (KfMap.XHRequest.status == 200) {

                KfMap.mapData = eval(KfMap.XHRequest.responseText);

                if (!KfMap.isDirty) {
                    KfMap.isDirty = true;
                    KfMap.CreateMap();
                    delete KfMap.XHRequest;
                }
                else {
                    KfMap.AddPoints();
                    delete KfMap.XHRequest;
                };
            }
            else {
                alert(errorInSerach);
            };
        };
    },
    CreateMap: function () {
        KfMap.map = new GMap2(document.getElementById('map'));

        KfMap.map.setMapType(G_NORMAL_MAP);
        KfMap.map.addControl(new GLargeMapControl());
        G_NORMAL_MAP.getMaximumResolution = function (latlng) { return 16; };
        G_NORMAL_MAP.getMinimumResolution = function (latlng) { return 4; };

        var latlngbounds = new GLatLngBounds(new GLatLng(KfMap.lat, KfMap.lon));
        latlngbounds.extend(new GLatLng(KfMap.options.blat, KfMap.options.blon));

        KfMap.map.setCenter(new GLatLng(KfMap.lat, KfMap.lon), KfMap.map.getBoundsZoomLevel(latlngbounds) - 1);

        GEvent.addListener(KfMap.map, "dragend", function () { KfMap.AddListeners(); });
        GEvent.addListener(KfMap.map, "zoomend", function () { KfMap.AddListeners(); });

        KfMap.AddPoints();
    },
    AddPoints: function () {

        KfMap.map.clearOverlays();


        var clusterStyle = [{ url: "/library/images/icoMultiProperties.png", height: 24, width: 24, opt_anchor: [12, 12]}];

        var zoom = KfMap.map.getZoom();
        KfMap.html = [];
        var counter = 0;

        if (zoom > 13) {

            for (var x = 0; x < KfMap.mapData.length; x++) {

                var point = new GLatLng(KfMap.mapData[x].a, KfMap.mapData[x].n);

                KfMap.html[x] = "<iframe src=" + KfMap.mapData[x].i + " width=600 height=244 scrolling=no frameborder=0 align=left />";

                var propMarker = KfMap.CreateHTML(point, x);

                KfMap.map.addOverlay(propMarker);
                propMarker = null;

            };
        }
        else {

            var batch = [];
            
                for (var x = 0; x < KfMap.mapData.length - 1; x++) {
                var point = new GLatLng(KfMap.mapData[x].a, KfMap.mapData[x].n);

                KfMap.html[x] = "<iframe src=" + KfMap.mapData[x].i + " width=600 height=244 scrolling=no frameborder=0 align=left />";

                var propMarker = KfMap.CreateHTML(point, x);

                batch.push(propMarker);

                propMarker = null;

            };
            var markerClusterer = new MarkerClusterer(KfMap.map, batch, { styles: clusterStyle });
        };
        KfMap.clearData();
    },
    CreateHTML: function (point, x) {

        var icon = new GIcon();
        icon.image = "/library/images/kfhousesimple.png";
        icon.iconSize = new GSize(20, 20);
        icon.iconAnchor = new GPoint(10, 10);


        var marker = new GMarker(point, { icon: icon, title: viewProperty });

        GEvent.addListener(marker, "click", function () {
            KfMap.CreateInfo(KfMap.html[x]);
        });

        return marker;
    },
    CreateInfo: function (html) {
        var infoOverlay = this;
        infoOverlay = document.createElement('div');
        infoOverlay.className = "infoOverlay";
        infoOverlay.style.width = document.body.clientWidth + 'px';
        if (navigator.appName.indexOf("Microsoft") == -1) {
            infoOverlay.style.height = document.body.clientHeight + 20 + 'px';
        }
        else {
            infoOverlay.style.height = document.body.clientHeight + 'px';
        }

        var infoContainer = this;
        infoContainer = document.createElement('div');
        infoContainer.className = "infoContainer";

        infoContainer.style.width = document.documentElement.clientWidth + "px";
        infoContainer.style.height = document.documentElement.clientHeight + "px";

        var infoHolder = this;
        infoHolder = document.createElement('div');
        infoHolder.className = "infoHolder";

        var infoClose = this;
        infoClose = document.createElement('div');
        infoClose.className = "infoClose";

        var content = this;
        content = html;

        infoHolder.innerHTML = content;
        infoHolder.appendChild(infoClose);

        infoOverlay.onclick = function () {
            document.body.removeChild(infoOverlay);
        };
        infoClose.onclick = function () {
            document.body.removeChild(infoOverlay);
        };

        infoContainer.appendChild(infoHolder);
        infoOverlay.appendChild(infoContainer);
        document.body.appendChild(infoOverlay);
    },
    AddListeners: function () {

        document.getElementById('mapLoader').className = "";
        var bounds = KfMap.map.getBounds();
        var center = KfMap.map.getCenter();

        KfMap.lat = eval("\"" + center.lat() + "\"").slice(0, 8);
        KfMap.lon = eval("\"" + center.lng() + "\"").slice(0, 8);
        KfMap.options.blat = eval("\"" + bounds.getNorthEast().lat() + "\"").slice(0, 8);
        KfMap.options.blon = eval("\"" + bounds.getNorthEast().lng() + "\"").slice(0, 8);

        var bounds = null;
        var center = null;

        var timer = setTimeout(function () { KfMap.RequestData(); }, 500);
    },
    clearData: function () {

        KfMap.mapData = null;
        KfMap.XHRequest = null;
        KfMap.requestQuery = null;
        delete KfMap.AddPoints.markerClusterer;
        document.getElementById('mapLoader').className = "hidden";
    },
    SearchMap: function () {
        KfMap.RequestData();
    },
    LifestyleMap: function () {

        KfMap.map = new GMap2(document.getElementById('map'));

        KfMap.isDirty = true;

        KfMap.RequestData();

        KfMap.map.setCenter(new GLatLng(0, 0), 2);
    },
    DetailsMap: function () {

        var map = new GMap2(document.getElementById("mapDetails"), { size: new GSize(560, 470) });

        map.addControl(new GLargeMapControl());
        map.removeMapType(G_HYBRID_MAP);
        var mapControl = new GMapTypeControl();
        map.addControl(mapControl);
        G_NORMAL_MAP.getMaximumResolution = function (latlng) { return 16; };
        G_NORMAL_MAP.getMinimumResolution = function (latlng) { return 7; };

        map.setCenter(new GLatLng(KfMap.lat, KfMap.lon), 16);

        var icon = new GIcon();
        icon.image = "/library/images/kfhousesimple.png";
        icon.iconSize = new GSize(20, 20);
        icon.iconAnchor = new GPoint(10, 10);

        map.addOverlay(new GMarker(new GLatLng(KfMap.lat, KfMap.lon), { icon: icon }));

        document.getElementById('mapLoader').className = "hidden";
    }
};
document.unload = GUnload();
