﻿

//////////////////////////////////////////////////////
// Bing map
//////////////////////////////////////////////////////

function SeBingMapCtrl() { }
SeBingMapCtrl.prototype = {
    zIndex: 1050,
    focusedElementId: "",
    selectedElementId: "",

    userSettings: null,

    handleLoadMap: function() {
        var bingMap = this;
        bingMap.SetMapStyle(VEMapStyle.Road);
        bingMap.SetMapMode(VEMapMode.Mode2D);
        bingMap.SetTileBuffer(1);

        bingMap.AttachEvent("onclick", bingMap.mapCtrl.handleClick(bingMap));
        bingMap.AttachEvent("onmousewheel", bingMap.mapCtrl.handleScroll(bingMap));
        bingMap.AttachEvent("onstartpan", bingMap.mapCtrl.handleStartPan(bingMap));

        bingMap.mapCtrl.addObjects(bingMap);
    },

    addObjects: function(bingMap) {

        var pinIconWrp = jQ("#" + bingMap.mapCtrl.userSettings.containerId).parent().find("." + bingMap.mapCtrl.userSettings.mapPinClass + "Wrapper");
        var location = bingMap.GetCenter();

        var wrappers = jQ("#" + bingMap.mapCtrl.userSettings.containerId).parent().find("." + bingMap.mapCtrl.userSettings.mapObjClass + "Wrapper");

        if (wrappers.length > 0) {
            for (var i = 0; i < wrappers.length; i++) {
                var control = jQ(wrappers[i]);
                var object = new VEShape(VEShapeType.Pushpin, bingMap.mapCtrl.getVELatLong(control));

                var content = pinIconWrp.html();
                if (bingMap.mapCtrl.userSettings.drawJustPins == false) {
                    content = control.html();
                    object.Keywords = control.find("." + bingMap.mapCtrl.userSettings.mapObjClass).attr("id");
                }
                object.SetCustomIcon(content);
                bingMap.AddShape(object);
                bingMap.mapCtrl.objects.push(object);
                control.remove();
            }
        } else {
            if (bingMap.mapCtrl.userSettings.floatingPin) {
                bingMap.AttachEvent("onendpan", bingMap.mapCtrl.handleEndPan(bingMap));

                var centerPixel = bingMap.LatLongToPixel(bingMap.GetCenter());
                var pinIcon = jQ("#" + bingMap.mapCtrl.userSettings.containerId).parent().find("img." + bingMap.mapCtrl.userSettings.mapPinClass);
                var top = centerPixel.x - 15;
                var left = centerPixel.y - 31;
                pinIcon.css({
                    left: top + "px",
                    top: left + "px"
                });
                pinIconWrp.show();
            } else {
                //					var control = jQ("." + bingMap.mapCtrl.userSettings.mapPinClass + "Wrapper");
                //					var object = new VEShape(VEShapeType.Pushpin, location);
                //					object.SetCustomIcon(control.html());
                //					bingMap.AddShape(object);
                //					objects.push(object);
            }
        }

        if (bingMap.mapCtrl.objects.length > 0) {
            bingMap.SetMapView(bingMap.mapCtrl.objects);
            if (parseInt(bingMap.GetZoomLevel()) > 14) {
                bingMap.SetZoomLevel("14");
                bingMap.SetCenter(bingMap.GetCenter());
            }

            if (bingMap.mapCtrl.objects.length == 1) {
                jQ("#" + bingMap.mapCtrl.userSettings.containerId).find("." + bingMap.mapCtrl.userSettings.mapObjClass + ".place").each(function(i, obj) {
                    bingMap.mapCtrl.focusPlace(bingMap, obj);
                });
            }
        } else {
            if (bingMap.mapCtrl.userSettings.centerLat != "" && bingMap.mapCtrl.userSettings.centerLong != "") {
                location = new VELatLong(parseFloat(bingMap.mapCtrl.userSettings.centerLat), parseFloat(bingMap.mapCtrl.userSettings.centerLong));
            } else if (navigator.geolocation) {
                //                navigator.geolocation.getCurrentPosition(function(position) {
                //                    location = new VELatLong(position.coords.latitude + "", position.coords.longitude + "", 0, VEAltitudeMode.Default);
                //                    bingMap.SetCenter(location);
                //                    bingMap.SetZoomLevel("12");
                //                },
                //					function(err) {
                //					    bingMap.SetCenter(location);
                //					    bingMap.SetZoomLevel("6");
                //					},
                //					{ maximumAge: 600000, timeout: 15000 });
            } else {
                bingMap.SetCenter(location);
                bingMap.SetZoomLevel("6");
            }
        }
    },

    setMapView: function(location) {
        var latitude = parseFloat(control.attr("latitude"));
        var longitude = parseFloat(control.attr("longitude"));
        return new VELatLong(latitude, longitude, 0, VEAltitudeMode.Default);
    },

    // extracts coordinates from map objects
    getVELatLong: function(control) {
        var latitude = parseFloat(control.attr("latitude"));
        var longitude = parseFloat(control.attr("longitude"));
        return new VELatLong(latitude, longitude, 0, VEAltitudeMode.Default);
    },

    // handlers
    handleClick: function(bingMap) {
        return (function(e) {
            if (e.elementID != null) {
                var object = bingMap.GetShapeByID(e.elementID);
                object.SetZIndex(bingMap.mapCtrl.zIndex++);
                var points = object.GetPoints();
                bingMap.SetCenter(points[0]);

                if (object.Keywords && object.Keywords != "") {

                    var element = jQ("#" + bingMap.mapCtrl.userSettings.containerId).find("#" + object.Keywords);
                    var checkbox = element.find("input[type=checkbox]");
                    if (element.attr("id") == bingMap.mapCtrl.focusedElementId) {
                        if (element.hasClass("shout")) {
                            bingMap.mapCtrl.focusShout(bingMap, element);
                        }
                        else if (element.hasClass("place")) {
                            bingMap.mapCtrl.focusPlace(bingMap, element);
                        }
                        if (checkbox.length > 0 && checkbox.is(":visible")) {
                            if (element.attr("id") == bingMap.mapCtrl.selectedElementId) {
                                bingMap.mapCtrl.selectedElementId = "";
                                checkbox.attr("checked", "");
                                jQ("#" + bingMap.mapCtrl.userSettings.selectedItemFieldId).val("");
                            } else {
                                bingMap.mapCtrl.selectedElementId = element.attr("id");
                                checkbox.attr("checked", "checked");
                                jQ("#" + bingMap.mapCtrl.userSettings.selectedItemFieldId).val(element.attr("id").substring(2));
                            }
                        } else {
                            document.location.href = element.attr("detailsUrl");
                        }
                    } else {
                        bingMap.mapCtrl.blurEverything(bingMap);
                        if (element.hasClass("shout")) {
                            bingMap.mapCtrl.focusShout(bingMap, element);
                        }
                        else if (element.hasClass("place")) {
                            bingMap.mapCtrl.focusPlace(bingMap, element);
                        }
                        if (element.attr("id") == bingMap.mapCtrl.selectedElementId) {
                            checkbox.attr("checked", "checked");
                        }
                    }
                }
            }
        });
    },

    handleScroll: function(bingmap) {
        return (function(e) {
            return true;
        });
    },

    handleStartPan: function(bingMap) {
        return (function(e) {
            return true;
        });
    },

    handleEndPan: function(bingMap) {
        return (function(e) {
            var center = bingMap.GetCenter();
            if (bingMap.mapCtrl.userSettings.latFieldId) {
                jQ("#" + bingMap.mapCtrl.userSettings.latFieldId).val(center.Latitude);
            }
            if (bingMap.mapCtrl.userSettings.latFieldId) {
                jQ("#" + bingMap.mapCtrl.userSettings.longFieldId).val(center.Longitude);
            }
        });
    },

    // efects
    focusShout: function(bingMap, control) {
        var element = jQ(control);
        element.find(".content").show();
        bingMap.mapCtrl.focusedElementId = element.attr("id");
    },

    focusPlace: function(bingMap, control) {
        var element = jQ(control);
        element.find(".content").css("display", "block");
        bingMap.mapCtrl.focusedElementId = element.attr("id");
    },


    blurEverything: function(bingMap) {
        jQ("#" + bingMap.mapCtrl.userSettings.containerId).find("." + bingMap.mapCtrl.userSettings.mapObjClass + ".shout").each(function(i, obj) {
            bingMap.mapCtrl.blurShout(bingMap, obj);
        });
        jQ("#" + bingMap.mapCtrl.userSettings.containerId).find("." + bingMap.mapCtrl.userSettings.mapObjClass + ".place").each(function(i, obj) {
            bingMap.mapCtrl.blurPlace(bingMap, obj);
        });
    },

    blurShout: function(bingMap, control) {
        var element = jQ(control);
        element.find(".content").hide();
        bingMap.mapCtrl.focusedElementId = "";
    },

    blurPlace: function(bingMap, control) {
        var element = jQ(control);
        element.find(".content").hide();
        bingMap.mapCtrl.focusedElementId = "";
    }
};



if (!SE) {
    var SE = new Object();
}

SE.map = {
    activeMaps: new Array(),

    defaultSettings: {
        containerId: "BingMap",
        latFieldId: "",
        longFieldId: "",
        selectedItemFieldId: "",
        fixedView: false,
        centerLat: "",
        centerLong: "",
        floatingPin: true,
        drawJustPins: false,
        mapObjClass: "mapObj",
        mapPinClass: "mapPin"
    },

    load: function(options, whenReady) {
        var userSettings = jQ.extend({}, this.defaultSettings, options);
        if (jQ("#" + userSettings.containerId).length == 0) {
            return;
        }
        var bingMap = new VEMap(userSettings.containerId);
        bingMap.SetDashboardSize(VEDashboardSize.Tiny);
        bingMap.mapCtrl = new SeBingMapCtrl();
        bingMap.mapCtrl.userSettings = userSettings;
        bingMap.mapCtrl.objects = new Array();
        bingMap.onLoadMap = bingMap.mapCtrl.handleLoadMap;
        this.activeMaps.push(bingMap);

        if (whenReady) {
            jQ(document).ready(function() {
                bingMap.LoadMap();
            });
        } else {
            bingMap.LoadMap();
        }
    }
};
