﻿/// <reference path="MicrosoftAjax.js" />


Type.registerNamespace("bizSpace");

bizSpace.BizMap = function (element) {
    this._bizmap = null;
    bizSpace.BizMap.initializeBase(this, [element]);
}

bizSpace.BizMap.prototype = {
    initialize: function () {

        this._createMapControl();
        if (this._querySt("page"))
            this._callService();
        else if (this._getUrlPart(1) == "servicetypes")
            this._setServiceType();
        else
            this._setSingle();



    },
    _createMapControl: function () {
        this._bizmap = new VEMap(this.get_element().id);
        var options = new VEMapOptions();
        //options.EnableBirdseye = false;
        this._bizmap.LoadMap(new VELatLong(51.49998, -0.12617), 10, VEMapStyle.Road, false, VEMapMode.Mode2D, null, 0, options);
        this._bizmap.SetCredentials("lhwgfhS3FoekAWZSF4l9DmNi2KJsXcWmv2Lv7LTfyX84b4HQ/a7a43iTW3PAiWnYIBgynHRkT");
        this._bizmap.ClearInfoBoxStyles();
        this._attachMapEvents();
    },
    _attachMapEvents: function () {
        this._bizmap.AttachEvent("onmouseover", Function.createDelegate(this, this._pinHovered));
        this._bizmap.AttachEvent("onclick", Function.createDelegate(this, this._pinClicked));
    },
    _pinHovered: function (e) {

        if (e.elementID) {
            var shape = this._bizmap.GetShapeByID(e.elementID);
            if (shape.Latitude != undefined) {

                var callback = Function.createCallback(this._zoom, { map: this._bizmap, data: shape.GetPoints()[0] });
                //settimeout needed as infobox content doesnt exist yet
                setTimeout(function () {
                    $('#zoomhref, #clusterzoom').click(callback);
                }, 1000);

            }

        }

    },
    _pinClicked: function (e) {

        if (e.elementID) {
            var shape = this._bizmap.GetShapeByID(e.elementID);
            if (shape.Latitude != undefined) {
                if (this._bizmap.GetZoomLevel() < 18) this._bizmap.SetCenterAndZoom(shape.GetPoints()[0], this._bizmap.GetZoomLevel() + 2);
            }

        }

    },
    _getUrlPart: function (index) {
        var path = window.location.pathname;
        var sections = path.split("/");
        return sections[index];
    },
    _setSingle: function () {
        var primaryId = this._getUrlPart(3);
        var service = new SearchService();
        service.GetById(primaryId, Function.createDelegate(this, this._singleServiceCallback));
    },
    _setServiceType: function () {
        var primaryId = this._getUrlPart(3);
        var service = new SearchService();
        service.Search("",
                            primaryId,
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            Function.createDelegate(this, this._serviceCallback));
    },
    _callService: function () {
        var service = new SearchService();
        var location = this._getUrlPart(2);
        service.Search(location,
                            this._querySt("types"),
                            this._querySt("ppl"),
                            this._querySt("ost"),
                            this._querySt("mti"),
                            this._querySt("dsa"),
                            this._querySt("osc"),
                            this._querySt("fcp"),
                            this._querySt("sec"),
                            this._querySt("tfa"),
                            this._querySt("bbd"),
                            this._querySt("rlx"),
                            this._querySt("size"),
                            Function.createDelegate(this, this._serviceCallback));
    },
    _serviceCallback: function (results) {
        var latLongs = new Array();
        for (var index in results) {
            var loc = new VELatLong(results[index].Latitude, results[index].Longitude);
            Array.add(latLongs, loc);
            this._addPushpin(loc, results[index]);
        }
        var options = new VEClusteringOptions();
        options.Callback = Function.createDelegate(this, this._clusteringCallback);
        this._bizmap.GetShapeLayerByIndex(0).SetClusteringConfiguration(VEClusteringType.Grid, options);
        this._bizmap.SetMapView(latLongs);
    },
    _singleServiceCallback: function (result) {
        var loc = new VELatLong(result.Latitude, result.Longitude);
        var arr = new Array();
        arr[0] = loc;
        this._addPushpin(loc, result);
        this._bizmap.SetMapView(arr);
        this._bizmap.SetMapStyle(VEMapStyle.Road);
    },
    _addPushpin: function (location, prop) {
        var shape = new VEShape(VEShapeType.Pushpin, location);
        shape.SetCustomIcon("<div class='standardPin'></div>");
        if (this._querySt("page") || this._getUrlPart(1) == "servicetypes")
            this._PopulatePropertyInfoBox(prop, shape);
        this._bizmap.AddShape(shape);
    },
    _querySt: function (ji) {
        hu = window.location.search.substring(1);
        gy = hu.split("&");
        for (i = 0; i < gy.length; i++) {
            ft = gy[i].split("=");
            if (ft[0] == ji) {
                return ft[1];
            }
        }
        return "";
    },
    _PopulatePropertyInfoBox: function (prop, pin) {
        //update title
        var title = prop.Name;

        //add price type
        title += '<div class="popuptitle"><div class="popupTown">' + prop.PlaceName + ", " + prop.AreaName + '</div></div>';

        pin.SetTitle(title);


        var desc = " ";
        //add image
        if (prop.MainImageFileName && prop.MainImageFileName != "") {
            desc = '<a href="/businesscentres/' + prop.Name + '/' + prop.PrimaryId.toString() + '/Details" target="_self"><div class="popupthumb"><img src="/images/businesscentres/d2_' + prop.MainImageFileName + '" /><div class="popupthumbframe"></div></div></a>';
        }
        desc += '<div class="viewlink"><a href="/businesscentres/' + prop.Name + '/' + prop.PrimaryId.toString() + '/Details" target="_self">Click to view full details</a></div>';
        desc += '<div class="zoomlink"><a href="#" id="zoomhref">Zoom to street level</a></div>';

        //update description
        pin.SetDescription(desc);

    },
    _zoom: function (s, c) {
        
        if (c.map.GetZoomLevel() < 18) {
            c.map.SetCenterAndZoom(c.data, c.map.GetZoomLevel() + 2);
        }
        else{
            c.map.SetCenter(c.data);
        }
        return false;
    },
    _clusteringCallback: function (clusters) {
        for (var i = 0; i < clusters.length; ++i) {
            var cluster = clusters[i];
            var clusterShape = cluster.GetClusterShape();
            clusterShape.SetCustomIcon("<div class='clusterPin'><span>" + cluster.Shapes.length + "</span></div>");
            clusterShape.SetTitle(cluster.Shapes.length + " Properties in This Area");
            cluster._clusterShape.Notes = "<a id='clusterzoom'>Click to zoom in</a>";
        }
    },
    dispose: function () {

        this._bizmap = null;
    }
}

bizSpace.BizMap.registerClass('bizSpace.BizMap', Sys.UI.Control);
if (typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
