﻿// Copyright (c) 2006-2007 Christopher Pietschmann (http://PietschSoft.com).
// This source is subject to the Microsoft Community License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/communitylicense.mspx.
// All other rights reserved.

Type.registerNamespace("PietschSoft.VE");

PietschSoft.VE.Map = function(element) {
    PietschSoft.VE.Map.initializeBase(this, [element]);
    
    //Properties
    this._VEMap = null;
    this._LatLong = null;
    this._Zoom = "4";
    this._MapStyle = "r";
    this._Fixed = false;
    this._MapMode = VEMapMode.Mode2D;
    this._ShowSwitch = true;
    this._ShowDashboard = true;
    this._ShowFindControl = false;
    
    this._Pushpins = null;
    this._Polylines = null;
    
    this._GetRoute_Start = null;
    this._GetRoute_End = null;
    this._GetRoute_Units = null;
    this._GetRoute_RouteType = null;
    this._GetRoute_JSCallback = null;
}

PietschSoft.VE.Map.prototype = {
    initialize : function() {
        PietschSoft.VE.Map.callBaseMethod(this, "initialize");
        
        this._LatLong = Sys.Serialization.JavaScriptSerializer.deserialize(this._LatLong);
        if (this._Pushpins != null) this._Pushpins = Sys.Serialization.JavaScriptSerializer.deserialize(this._Pushpins);
        if (this._Polylines != null) this._Polylines = Sys.Serialization.JavaScriptSerializer.deserialize(this._Polylines);
        
        if (this._GetRoute_Units == 1)
            this._GetRoute_Units = "k"; 
        else
            this._GetRoute_Units = "m";
            
        if (this._GetRoute_RouteType == 1)
            this._GetRoute_RouteType = "q";
        else
            this._GetRoute_RouteType = "s";
        
        
        if (!isNaN(this._MapStyle))
        {
            if (this._MapStyle == 2){this._MapStyle = "a";}
            else if (this._MapStyle == 3){this._MapStyle = "h";}
            else if (this._MapStyle == 4){this._MapStyle = "o";}
            else {this._MapStyle = "r";}
        }
        
        this.get_element().style.position = "relative";
        this._VEMap = new VEMap(this.get_element().id);
        
        this._VEMap.LoadMap(new VELatLong(this._LatLong.Latitude, this._LatLong.Longitude),
            this._Zoom,
            this._MapStyle,
            this._Fixed,
            this._MapMode,
            this._ShowSwitch
            );
            
        if (this._ShowDashboard == false)
            this._VEMap.HideDashboard();
        
        if (this._ShowFindControl == true)
            this._VEMap.ShowFindControl();
        
        if (this._GetRoute_Start != null)
        {
            if (this._GetRoute_Start.indexOf("Latitude\":") != -1)
            {
                this._GetRoute_Start = Sys.Serialization.JavaScriptSerializer.deserialize(this._GetRoute_Start);
                this._GetRoute_Start = new VELatLong(this._GetRoute_Start.Latitude, this._GetRoute_Start.Longitude);
            }
            if (this._GetRoute_End.indexOf("Latitude\":") != -1)
            {
                this._GetRoute_End = Sys.Serialization.JavaScriptSerializer.deserialize(this._GetRoute_End);
                this._GetRoute_End = new VELatLong(this._GetRoute_End.Latitude, this._GetRoute_End.Longitude);
            }
            this._VEMap.GetRoute(this._GetRoute_Start, this._GetRoute_End, this._GetRoute_Units, this._GetRoute_RouteType, (this._GetRoute_JSCallback.length == 0)? null : this._GetRoute_JSCallback);
        }
        
        if (this._Pushpins != null) 
        {
            for (var i = 0; i < this._Pushpins.length; i++)
            {
                var pin = new VEPushpin(
                   this._Pushpins[i].Id, 
                   new VELatLong(this._Pushpins[i].Location.Latitude, this._Pushpins[i].Location.Longitude), 
                   this._Pushpins[i].Icon_url, 
                   this._Pushpins[i].Title, 
                   this._Pushpins[i].Details,
                   this._Pushpins[i].IconStyle,
                   this._Pushpins[i].TitleStyle,
                   this._Pushpins[i].DetailStyle
                );
                pin.ShowDetailOnMouseOver = this._Pushpins[i].ShowDetailOnMouseOver;
                pin.OnMouseOverCallback = this._Pushpins[i].OnMouseOverCallback;
                
                this._VEMap.AddPushpin(pin);
            }
        }

        if (this._Polylines.length != 0)
        {
            for (var i = 0; i < this._Polylines.length; i++)
            {
                var locations = new Array();
                for (var x = 0; x < this._Polylines[i].Locations.length; x++)
                {
                    locations[locations.length] = new VELatLong(this._Polylines[i].Locations[x].Latitude, this._Polylines[i].Locations[x].Longitude);
                }
                var polyline = new VEPolyline(
                    this._Polylines[0].Id,
                    locations
                );
                polyline.SetWidth(this._Polylines[i].Width);
                polyline.SetColor(this._Polylines[i].Color);
                this._VEMap.AddPolyline(polyline);
            }
        }
            
    },
    dispose : function() {
        this._VEMap = null;
        
        PietschSoft.VE.Map.callBaseMethod(this, "dispose");
    },
    
    get_VEMap : function()
    {
        return this._VEMap;
    },
    get_LatLong : function() {
        return this._LatLong;
    },
    set_LatLong : function(value) {
        this._LatLong = value;
    },
    get_Zoom : function() {
        return this._Zoom;
    },
    set_Zoom : function(value) {
        this._Zoom = value;
    },
    get_MapStyle : function() {
        return this._MapStyle;
    },
    set_MapStyle : function(value) {
        this._MapStyle = value;
    },
    get_Fixed : function() {
        return this._Fixed;
    },
    set_Fixed : function(value) {
        this._Fixed = value;
    },
    get_MapMode : function() {
        return this._MapMode;
    },
    set_MapMode : function(value) {
        this._MapMode = value;
    },
    get_ShowSwitch : function() {
        return this._ShowSwitch;
    },
    set_ShowSwitch : function(value) {
        this._ShowSwitch = value;
    },
    get_ShowDashboard : function() {
        return this._ShowDashboard;
    },
    set_ShowDashboard : function(value) {
        this._ShowDashboard = value;
    },
    get_ShowFindControl : function() {
        return this._ShowFindControl;
    },
    set_ShowFindControl : function(value) {
        this._ShowFindControl = value;
    },
    
    get_Pushpins : function() {
        return this._Pushpins;
    },
    set_Pushpins : function(value) {
        this._Pushpins = value;
    },
    get_Polylines : function() {
        return this._Polylines;
    },
    set_Polylines : function(value) {
        this._Polylines = value;
    },
    
    get_GetRoute_Start : function() {
        return this._GetRoute_Start;
    },
    set_GetRoute_Start : function(value) {
        this._GetRoute_Start = value;
    },
    get_GetRoute_End : function() {
        return this._GetRoute_End;
    },
    set_GetRoute_End : function(value) {
        this._GetRoute_End = value;
    },
    get_GetRoute_Units : function() {
        return this._GetRoute_Units;
    },
    set_GetRoute_Units : function(value) {
        this._GetRoute_Units = value;
    },
    get_GetRoute_RouteType : function() {
        return this._GetRoute_RouteType;
    },
    set_GetRoute_RouteType : function(value) {
        this._GetRoute_RouteType = value;
    },
    get_GetRoute_JSCallback : function() {
        return this._GetRoute_JSCallback;
    },
    set_GetRoute_JSCallback : function(value) {
        this._GetRoute_JSCallback = value;
    }    
    
}

PietschSoft.VE.Map.registerClass("PietschSoft.VE.Map", Sys.UI.Control);

Sys.Application.notifyScriptLoaded();
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();