﻿Type.registerNamespace('DTG.Dollar.Web.Consumer.Common.Ajax.RoutingMap');

/// <summary>Extender Enum DistanceUnit</summary>
DTG.Dollar.Web.Consumer.Common.Ajax.RoutingMap.DistanceUnit = function() {
    throw Error.invalidOperation();
}
DTG.Dollar.Web.Consumer.Common.Ajax.RoutingMap.DistanceUnit.prototype = {
    Miles : 0,
    Kilometers : 1
}
DTG.Dollar.Web.Consumer.Common.Ajax.RoutingMap.DistanceUnit.registerEnum('DTG.Dollar.Web.Consumer.Common.Ajax.RoutingMap.DistanceUnit', false);

/// <summary>Extender Enum RouteType</summary>
DTG.Dollar.Web.Consumer.Common.Ajax.RoutingMap.RouteType = function() {
    throw Error.invalidOperation();
}
DTG.Dollar.Web.Consumer.Common.Ajax.RoutingMap.RouteType.prototype = {
    Quickest : 0,
    Shortest : 1
}
DTG.Dollar.Web.Consumer.Common.Ajax.RoutingMap.RouteType.registerEnum('DTG.Dollar.Web.Consumer.Common.Ajax.RoutingMap.RouteType', false);


/// <summary>
/// RoutingMapBehavior Extender Control
///     This extender displays the Virtual Earth map control and driving directions (route) container for Map Route.  
///     requires start and end addresses to route a map.  This extender displays the Virtual Earth (VE) map control.
///     Version 5 of VE is set as the default for all browsers except Safari and Opera where a custom version 4 is
///     used.  The v4 MapControlLocal.js, BroswerVECompat.js, BroswerVECompat2.js, and SafariCompat.js javascript 
///     files are is down loaded from the web Common/scripts/ folder and calls the Microsoft VE v4 control. 
///     Tried to make script files embedded resource of this extender, but sarfari browser would not 
///     work correctly with VE (no display).
///     The code for this custom version of VE4 javascript was download from
///     https://code.poly9.com/trac/browser/vev4_saf.
///     Safari is not supported.
/// The basic steps to add Safari compatibility are:
///     - to include the following code before the MapControl inclusion: BrowserVECompat.js.
///     - replace the production MapControl from Microsoft's server to the local MapControl.js file
///         (please note that this is only to be done as a temporary step while the Safari layer is integrated
///         into Microsoft's production code - it is bad practice to copy and reference a local MapControl file!)
///         This is being done in the GetScriptReferences overridden method.
///     - add the BrowserVECompat2.js code after the MapControl.js inclusion
///     
/// By default this control loads on initialize extender method  GetScriptReferences is overridden to setup
///     the correct down load of the VE javascript.
/// </summary>
/// <remarks>
///    properties: 
///      {name: 'TargetControlID', type: String} : Default value for extenders identifing the div map control
///      {name: 'RouteElementId', type: String} : value identifing the route div control
///      {name: 'StartAddress', type: String} : string value identifing the start address e.g. 5400 Legacy Plano TX 75024 US
///      {name: 'EndAddress', type: String} : string value identifing the end address
///      {name: 'Units', type: DistanceUnit} : miles or kilometers
///      {name: 'RoutingType', type: RouteType} : quickest or shortest
///      {name: 'RouteHeader', type: String) : Route Header text
///      {name: 'RouteErrorMsg', type: String) : Message to display when route can not be determined
/// </remarks>
/// <history>
///     <change date="03/23/2007 12:18:00" ticket="">
///         <author>Steven Berenbrock</author>
///         <description>Initial version.</description>
///     </change>
///     <change date="09/14/2007 11:08:00" ticket="Issue:2749">
///         <author>Steven Berenbrock</author>
///         <description>Added new property for route error.  used when a route cannot be determined by VE</description>
///     </change>
///     <change date="09/14/2007 20:00:00" ticket="code improvement">
///         <author>Steven Berenbrock</author>
///         <description>determined techinque to make callback in extender.  added callback Delegate and 
///             used in load route call</description>
///     </change>
/// </history>
DTG.Dollar.Web.Consumer.Common.Ajax.RoutingMap.RoutingMapBehavior = function(element) {
    DTG.Dollar.Web.Consumer.Common.Ajax.RoutingMap.RoutingMapBehavior.initializeBase(this, [element]);
    ///<summary>RoutingMapBehavior Extender</summary>
    ///<param name="element">Associated element</param>

    //Properties
    this._routeElementId = null;
    this._startAddress = null;
    this._endAddress = null;
    this._units = DTG.Dollar.Web.Consumer.Common.Ajax.RoutingMap.DistanceUnit.Miles;
    this._routingType = DTG.Dollar.Web.Consumer.Common.Ajax.RoutingMap.RouteType.Quickest;
    this._routeHeader = null;
    this._routeErrorMsg = null;

    // Member variables
    this._map = null;
    this._routeElement = null;
    this._display = 'block';

    // Event delegates
    this._callback = null;
}

DTG.Dollar.Web.Consumer.Common.Ajax.RoutingMap.RoutingMapBehavior.prototype = {
    //*****************************************************************
    //Override methods
    //
    initialize : function() {
        DTG.Dollar.Web.Consumer.Common.Ajax.RoutingMap.RoutingMapBehavior.callBaseMethod(this, 'initialize');

        //var targetElement = this.get_element();
        
        if (this._routeElementId) {
            this._routeElement = $get(this._routeElementId);
            Sys.Debug.assert(this._routeElement != null, "Couldn't find element '" + this._routeElementId + "'");
        }                
        
        this._callback = Function.createDelegate(this, this._onRouteCallback);
    },

    dispose : function() {
        // TODO: add your cleanup code here

        DTG.Dollar.Web.Consumer.Common.Ajax.RoutingMap.RoutingMapBehavior.callBaseMethod(this, 'dispose');
    },

    //*****************************************************************
    // Property get/set methods
    //
    get_RouteElementId : function() {
        return this._routeElementId;
    },
    
    set_RouteElementId : function(value) {
        this._routeElementId = value;
    },
    
    get_StartAddress : function() {
        return this._startAddress;
    },
        
    set_StartAddress : function(value) {
        this._startAddress = value;
    },

    get_EndAddress : function() {
        return this._endAddress;
    },
        
    set_EndAddress : function(value) {
        this._endAddress = value;
    },

    get_Units : function() {
        return this._units;
    },
        
    set_Units : function(value) {
        this._units = value;
    },
    
    get_RoutingType : function() {
        return this._routingType;
    },
        
    set_RoutingType : function(value) {
        this._routingType = value;
    },
    
    get_RouteHeader : function() {
        return this._routeHeader;
    },
        
    set_RouteHeader : function(value) {
        this._routeHeader = value;
    },

    get_RouteErrorMsg : function() {
        return this._routeErrorMsg;
    },
        
    set_RouteErrorMsg : function(value) {
        this._routeErrorMsg = value;
    },

    //*****************************************************************
    // Custom methods
    //

    loadRoute : function() {
        ///<summary>Public method loadRoute</summary>
        var routeUnits = (this._units == DTG.Dollar.Web.Consumer.Common.Ajax.RoutingMap.DistanceUnit.Kilometers) ? VEDistanceUnit.Kilometers : VEDistanceUnit.Miles;
        var typeOfRoute = (this._routingType == DTG.Dollar.Web.Consumer.Common.Ajax.RoutingMap.RouteType.Shortest) ? VERouteType.Shortest :  VERouteType.Quickest;     
        
        if (this._map) {
            this._map.GetRoute(this._startAddress, this._endAddress, routeUnits, typeOfRoute, this._callback);
        }
        else {
            if (this._createMap()) {
                this._map.GetRoute(this._startAddress, this._endAddress, routeUnits, typeOfRoute, this._callback);
            }
        }    
    },
    
    hide : function() {
        ///<summary>Public method hide</summary>
        ///<remarks>hides the container using display style none</remarks>
        var targetElement = this.get_element();
        targetElement.style.display = 'none';

        if (this._routeElement) {
            this._routeElement.style.display = 'none';
        }
    },
    
    show : function() {
        ///<summary>Public method show</summary>
        ///<remarks>displays the container using display style defined in _display</remarks>
        var targetElement = this.get_element();
        targetElement.style.display = this._display;

        if (this._routeElement) {
            this._routeElement.style.display = this._display;
        }
    },
    
    clearRoute : function() {
        ///<summary>public method clearRoute - clear the route element of directions</summary>
        if (this._routeElement) {
            this._routeElement.innerHTML = "";
        }
    },
    
    formatRoute : function(route) {  
        ///<summary>public method formatRoute - format the route segments into httml
        ///     li tags and set route element</summary>
        ///<param name="route">VE Route object</param>
        if (this._routeElement) {
            var routeinfo; 
            if (this._routeHeader) {
                routeinfo = "<h2>" + this._routeHeader + "</h2>";
            }
            routeinfo += "<p>Total distance: ";
            routeinfo += (route.Itinerary.Distance + " ");
            routeinfo += (route.Itinerary.DistanceUnit + " ");
            routeinfo += (route.Itinerary.Time + "</p>");
            routeinfo += "<p>Directions:</p>";

            var steps = ("<ul>");
            var len = route.Itinerary.Segments.length;
            for(var i = 0; i < len; i++)
            {
                steps += ("<li>" + route.Itinerary.Segments[i].Instruction + " -- (");
                steps += (route.Itinerary.Segments[i].Distance + ") ");
                steps += (route.Itinerary.DistanceUnit + "</li>");             
            }
            routeinfo += (steps + "</ul>");
            this._routeElement.innerHTML = routeinfo;
        }
    },

     _createMap : function() {
        ///<summary>Private method _createMap - create VE map</summary>
        if (!this._map) {
            if (typeof(VEMap) != 'undefined') {
                var targetElement = this.get_element();
                this._map = new VEMap(targetElement.id);
                //SetDashboardSize must be called prior to LoadMap.
                this._map.SetDashboardSize(VEDashboardSize.Small);
                //VEMap.LoadMap(VELatLong, zoom, style, fixed, mode, showSwitch);
                this._map.LoadMap(null, null, 'r', false, VEMapMode.Mode2D, false); 
            }
            else {
                Sys.Debug.trace("error in call to VE Map control");
                return false;
            }
        }
        return true;            
    },
    
    //*****************************************************************
    // Event handler methods
    //
    
    _onRouteCallback : function(route) {
        ///<summary>provate event method _onRouteCallback - callback method from Load Route</summary>
        ///<param name="route">VE Route object</param>
        if (route) {
            this.formatRoute(route);
        }
        else {
            //error in determining route....display message
            if (this._map && this._routeErrorMsg) {
                this._map.ShowMessage(this._routeErrorMsg);
            }        
        }
    }
}

DTG.Dollar.Web.Consumer.Common.Ajax.RoutingMap.RoutingMapBehavior.registerClass('DTG.Dollar.Web.Consumer.Common.Ajax.RoutingMap.RoutingMapBehavior', AjaxControlToolkit.BehaviorBase);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();