﻿/// <reference path="~/Script/jquery-1.2.6.js"/>
/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("FlipCalc.Maps");

FlipCalc.Maps.VirtualEarthProvider = function(containerID) {

    var _containerID = containerID;
    var _map = null;
    var _lastVisibleShape = null;

    this.RenderMap = function(lat, lon, zoom) {

        _map = new VEMap(_containerID);
        //_map.AttachEvent("onendpan", SaveMapLocation);
        //_map.AttachEvent("onendzoom", SaveMapLocation);

        _map.SetDashboardSize(VEDashboardSize.Tiny);
        _map.HideScalebar();

        _map.AttachEvent("onmousewheel", function(e) { return true; });
        _map.AttachEvent("onmouseover", function(e) { return true; });
        _map.AttachEvent("onmouseout", function(e) { return true; });

        _map.AttachEvent("onclick", function(e) {

            if (e.elementID != null) {
                var shape = _map.GetShapeByID(e.elementID);
                _map.ShowInfoBox(shape);

                return true;
            }
            else {
                _map.ShowInfoBox(_lastVisibleShape);
            }
        });

        _map.LoadMap(new VELatLong(lat, lon), zoom, 'r');

    }


    this.AddPins = function(locationList) {

        //clear pins
        _map.DeleteAllPushpins();


        //var pinLocations = $get('{4}').value; //getting locations from hidden input field

        if (locationList.length == 0)
            return;

        //var locs = locationList.split('|');

        for (i = 0; i < locationList.length; i++) {

            var lat = locationList[i].lat; //locs[i].split(',')[0];
            var lon = locationList[i].lon; //locs[i].split(',')[1];
            var info = locationList[i].info; //locs[i].split(',')[2];

            var shape = new VEShape(VEShapeType.Pushpin, new VELatLong(lat, lon));

            //insert custom icon here.
            //shape.SetCustomIcon('<div style="background-color:lime; width:20px; height:20px;>DK</div>');

            if (info) {

                var infoBox = info;
                //shape.SetTitle("<h2>Custom Pin</h2>");
                shape.SetDescription(infoBox);

                //Add the shape the the map
                //_map.ClearInfoBoxStyles();
                _map.AddShape(shape);

            }



            //alert(shape.GetPrimitive().iid);

            _lastVisibleShape = shape;

            _map.ShowInfoBox(shape);

        } /**/
    }

    this.SaveMapLocation = function() {

        var center = _map.GetCenter();
        var lat = center.Latitude;
        var lon = center.Longitude;
        var zoom = _map.GetZoomLevel();

        //todo // persist these values???
    }
}

FlipCalc.Maps.VirtualEarthProvider.registerClass("FlipCalc.Maps.VirtualEarthProvider");


