﻿
Type.registerNamespace("Igisa");

Igisa.WayPoint = function(longitude, latitude)
{
    this.Longitude = longitude;
    this.Latitude = latitude;
}
Igisa.WayPoint.prototype = 
{
    IsGeoCoded: function() { return this.Latitude != null && this.Longitude != null; }
}

Igisa.WayPoints = function()
{
    this._wayPoints = new Array();
}
Igisa.WayPoints.prototype = 
{
    Add: function( longitude, latitude )
    {
        Array.add( this._wayPoints, new Igisa.WayPoint(longitude, latitude) );
    }
    ,Get: function( index )
    {
        if( index >= 0 && index < this._wayPoints.length )
            return this._wayPoints[index];
        return new Igisa.WayPoint(null, null);    
    }
}

var ecMap = null;
var wayPoints = new Igisa.WayPoints();

function GetECMap()
{
   ecMap = new eContent.Map('myMap');
   ecMap.UseRequestQueue(true);
   ecMap.LoadMap();
   ecMap.SetSetupCompleteHandler(OnSetupComplete);
   ecMap.SetPopupOnHover(false);
   ecMap.SetMapStyle(eContent.MapStyle.Shaded);
   ecMap.SetTourLoadedHandler(OnTourLoaded);
   ecMap.SetRequestErrorHandler(OnRequestError);
}

function OnSetupComplete()
{
    ecMap.LoadHotels();
}

function OnRequestError(error)
{
    if( error.number == ERROR_CODES.Special )
        alert('Leider konnte keine Strecke berechnet werden, bitte ändern Sie Ihre Auswahl');
    else
        alert(error.description);
}

function OnTourLoaded()
{
    ecMap.MoveToCurrentTour();
    $get('txtTourLength').innerHTML = ecMap.GetCurrentTourLength() | 0; 
}

function CalcValue(value)
{
    var result = (Math.pow(value/12,2)+0.301).toFixed(2);
    if( result > 3.0 )
        result = 3.0;
    return result;
}

function ShowIgisaRoute()
{
    var start = wayPoints.Get($get('ddlStart').value);                              
    var ende = wayPoints.Get($get('ddlStop').value); 
    
    if( start.IsGeoCoded() && ende.IsGeoCoded() )
    {
        var cat = $get('ddlKategorie').value;
               
        var sever = 0;
        if( $get('btnLeicht').checked )
            sever = 1;
        else if( $get('btnMittel').checked )
            sever = 5;
        else if( $get('btnSchwer').checked )
            sever = 9;
            
        var asphalt_mc = CalcValue($get('sliderAsphalt').value);
        var grit_mc = CalcValue($get('sliderGrit').value);
        var trail_mc = CalcValue($get('sliderTrail').value);

        ecMap.ShowIgisaRoute(cat, sever, start.Longitude, start.Latitude, ende.Longitude, ende.Latitude, asphalt_mc, grit_mc, trail_mc);
        
        var strDownload = "IgisaConverter.ashx?cat=" + cat + "&sever=" + sever;
        strDownload += "&slon=" + start.Longitude + "&slat=" + start.Latitude;
        strDownload += "&dlon=" + ende.Longitude + "&dlat=" + ende.Latitude;
        strDownload += "&asphalt_mc=" + asphalt_mc + "&grit_mc=" + grit_mc + "&trail_mc=" + trail_mc;
        $get('gpxDownload').href = strDownload;
        $get('divGPX').style.display = 'block';
    }
    else
    {
        $get('gpxDownload').href = '';
        $get('divGPX').style.display = 'none';
    }
}

