﻿// ************************************************************************************************
//
// Karttricket RWC - Client functions
// Copyright Informationsfabriken AB
//
// ************************************************************************************************

// ************************************************************************************************
// ************************************************************************************************
// Karttricket RWC globalization strings
// ************************************************************************************************
// ************************************************************************************************
KarttricketRWCGlobalization.prototype.Lead_AvailableMaps = null;
KarttricketRWCGlobalization.prototype.Lead_AvailableMapVariants = null;
KarttricketRWCGlobalization.prototype.Lead_AvailableRoutes = null;
KarttricketRWCGlobalization.prototype.Lead_AvailableViews = null;
KarttricketRWCGlobalization.prototype.Lead_CursorDistance = null;
KarttricketRWCGlobalization.prototype.Lead_DeleteAll = null;
KarttricketRWCGlobalization.prototype.Lead_DeleteLast = null;
KarttricketRWCGlobalization.prototype.Lead_ExplicitSearchButton = null;
KarttricketRWCGlobalization.prototype.Lead_ExplicitSearchTextBox = null;
KarttricketRWCGlobalization.prototype.Lead_SearchButton = null;
KarttricketRWCGlobalization.prototype.Lead_SearchHits = null;
KarttricketRWCGlobalization.prototype.Lead_SearchResultBox = null;
KarttricketRWCGlobalization.prototype.Lead_SearchTextBox = null;
KarttricketRWCGlobalization.prototype.Lead_TotalDistance = null;
KarttricketRWCGlobalization.prototype.Msg_ClickMap = null;
KarttricketRWCGlobalization.prototype.Msg_FeedbackSaved = null;
KarttricketRWCGlobalization.prototype.Msg_HomePositionSaved = null;
KarttricketRWCGlobalization.prototype.Msg_LayerSaved = null;
KarttricketRWCGlobalization.prototype.Msg_MaxZoomLevelReached = null;
KarttricketRWCGlobalization.prototype.Msg_MinZoomLevelReached = null;
KarttricketRWCGlobalization.prototype.Msg_MissingFeedbackDescription = null;
KarttricketRWCGlobalization.prototype.Msg_MissingHomePostition = null;
KarttricketRWCGlobalization.prototype.Msg_MissingObjectTitle = null;
KarttricketRWCGlobalization.prototype.Msg_MissingPoints = null;
KarttricketRWCGlobalization.prototype.Msg_MissingRouteName = null;
KarttricketRWCGlobalization.prototype.Msg_MissingViewName = null;
KarttricketRWCGlobalization.prototype.Msg_MissingLayerName = null;
KarttricketRWCGlobalization.prototype.Msg_ObjectSaved = null;
KarttricketRWCGlobalization.prototype.Msg_RouteSaved = null;
KarttricketRWCGlobalization.prototype.Msg_ViewSaved = null;
KarttricketRWCGlobalization.prototype.Opt_SelectLayer = null;
KarttricketRWCGlobalization.prototype.Opt_SelectRoute = null;
KarttricketRWCGlobalization.prototype.Opt_SelectView = null;
KarttricketRWCGlobalization.prototype.Tab_Layers = null;
KarttricketRWCGlobalization.prototype.Tab_Maps = null;
KarttricketRWCGlobalization.prototype.Tab_Routes = null;
KarttricketRWCGlobalization.prototype.Tab_Search = null;
KarttricketRWCGlobalization.prototype.Tab_Views = null;
KarttricketRWCGlobalization.prototype.ToolTip_DeleteAll = null;
KarttricketRWCGlobalization.prototype.ToolTip_DeleteLast = null;
KarttricketRWCGlobalization.prototype.ToolTip_LoadHome = null;
KarttricketRWCGlobalization.prototype.ToolTip_Print = null;
KarttricketRWCGlobalization.prototype.ToolTip_SaveHome = null;
KarttricketRWCGlobalization.prototype.ToolTip_SaveLayer = null;
KarttricketRWCGlobalization.prototype.ToolTip_SaveObject = null;
KarttricketRWCGlobalization.prototype.ToolTip_SaveRoute = null;
KarttricketRWCGlobalization.prototype.ToolTip_SaveView = null;
KarttricketRWCGlobalization.prototype.ToolTip_SendFeedback = null;
KarttricketRWCGlobalization.prototype.ToolTip_ShowHelp = null;
KarttricketRWCGlobalization.prototype.ToolTip_ShowInfo = null;
KarttricketRWCGlobalization.prototype.ToolTip_ZoomIn = null;
KarttricketRWCGlobalization.prototype.ToolTip_ZoomOut = null;


function KarttricketRWCGlobalization(ktCustomerConfig)
{
    KarttricketRWCGlobalization.prototype.Lead_AvailableMaps = ktCustomerConfig.Lead_AvailableMaps;
    KarttricketRWCGlobalization.prototype.Lead_AvailableMapVariants = ktCustomerConfig.Lead_AvailableMapVariants;
    KarttricketRWCGlobalization.prototype.Lead_AvailableRoutes = ktCustomerConfig.Lead_AvailableRoutes;
    KarttricketRWCGlobalization.prototype.Lead_AvailableViews = ktCustomerConfig.Lead_AvailableViews;
    KarttricketRWCGlobalization.prototype.Lead_CursorDistance = ktCustomerConfig.Lead_CursorDistance;
    KarttricketRWCGlobalization.prototype.Lead_DeleteAll = ktCustomerConfig.Lead_DeleteAll;
    KarttricketRWCGlobalization.prototype.Lead_DeleteLast = ktCustomerConfig.Lead_DeleteLast;
    KarttricketRWCGlobalization.prototype.Lead_ExplicitSearchButton = ktCustomerConfig.Lead_ExplicitSearchButton;
    KarttricketRWCGlobalization.prototype.Lead_ExplicitSearchTextBox = ktCustomerConfig.Lead_ExplicitSearchTextBox;
    KarttricketRWCGlobalization.prototype.Lead_SearchButton = ktCustomerConfig.Lead_SearchButton;
    KarttricketRWCGlobalization.prototype.Lead_SearchHits = ktCustomerConfig.Lead_SearchHits;
    KarttricketRWCGlobalization.prototype.Lead_SearchResultBox = ktCustomerConfig.Lead_SearchResultBox;
    KarttricketRWCGlobalization.prototype.Lead_SearchTextBox = ktCustomerConfig.Lead_SearchTextBox;
    KarttricketRWCGlobalization.prototype.Lead_TotalDistance = ktCustomerConfig.Lead_TotalDistance;
    KarttricketRWCGlobalization.prototype.Msg_ClickMap = ktCustomerConfig.Msg_ClickMap;
    KarttricketRWCGlobalization.prototype.Msg_FeedbackSaved = ktCustomerConfig.Msg_FeedbackSaved;
    KarttricketRWCGlobalization.prototype.Msg_HomePositionSaved = ktCustomerConfig.Msg_HomePositionSaved;
    KarttricketRWCGlobalization.prototype.Msg_LayerSaved = ktCustomerConfig.Msg_LayerSaved;
    KarttricketRWCGlobalization.prototype.Msg_MaxZoomLevelReached = ktCustomerConfig.Msg_MaxZoomLevelReached;
    KarttricketRWCGlobalization.prototype.Msg_MinZoomLevelReached = ktCustomerConfig.Msg_MinZoomLevelReached;
    KarttricketRWCGlobalization.prototype.Msg_MissingFeedbackDescription = ktCustomerConfig.Msg_MissingFeedbackDescription;
    KarttricketRWCGlobalization.prototype.Msg_MissingHomePostition = ktCustomerConfig.Msg_MissingHomePostition;
    KarttricketRWCGlobalization.prototype.Msg_MissingObjectTitle = ktCustomerConfig.Msg_MissingObjectTitle;
    KarttricketRWCGlobalization.prototype.Msg_MissingPoints = ktCustomerConfig.Msg_MissingPoints;
    KarttricketRWCGlobalization.prototype.Msg_MissingRouteName = ktCustomerConfig.Msg_MissingRouteName;
    KarttricketRWCGlobalization.prototype.Msg_MissingViewName = ktCustomerConfig.Msg_MissingViewName;
    KarttricketRWCGlobalization.prototype.Msg_MissingLayerName = ktCustomerConfig.Msg_MissingLayerName;
    KarttricketRWCGlobalization.prototype.Msg_ObjectSaved = ktCustomerConfig.Msg_ObjectSaved;
    KarttricketRWCGlobalization.prototype.Msg_RouteSaved = ktCustomerConfig.Msg_RouteSaved;
    KarttricketRWCGlobalization.prototype.Msg_ViewSaved = ktCustomerConfig.Msg_ViewSaved;
    KarttricketRWCGlobalization.prototype.Opt_SelectLayer = ktCustomerConfig.Opt_SelectLayer;
    KarttricketRWCGlobalization.prototype.Opt_SelectRoute = ktCustomerConfig.Opt_SelectRoute;
    KarttricketRWCGlobalization.prototype.Opt_SelectView = ktCustomerConfig.Opt_SelectView;
    KarttricketRWCGlobalization.prototype.Tab_Layers = ktCustomerConfig.Tab_Layers;
    KarttricketRWCGlobalization.prototype.Tab_Maps = ktCustomerConfig.Tab_Maps;
    KarttricketRWCGlobalization.prototype.Tab_Routes = ktCustomerConfig.Tab_Routes;
    KarttricketRWCGlobalization.prototype.Tab_Search = ktCustomerConfig.Tab_Search;
    KarttricketRWCGlobalization.prototype.Tab_Views = ktCustomerConfig.Tab_Views;
    KarttricketRWCGlobalization.prototype.ToolTip_DeleteAll = ktCustomerConfig.ToolTip_DeleteAll;
    KarttricketRWCGlobalization.prototype.ToolTip_DeleteLast = ktCustomerConfig.ToolTip_DeleteLast;
    KarttricketRWCGlobalization.prototype.ToolTip_LoadHome = ktCustomerConfig.ToolTip_LoadHome;
    KarttricketRWCGlobalization.prototype.ToolTip_Print = ktCustomerConfig.ToolTip_Print;
    KarttricketRWCGlobalization.prototype.ToolTip_SaveHome = ktCustomerConfig.ToolTip_SaveHome;
    KarttricketRWCGlobalization.prototype.ToolTip_SaveLayer = ktCustomerConfig.ToolTip_SaveLayer;
    KarttricketRWCGlobalization.prototype.ToolTip_SaveObject = ktCustomerConfig.ToolTip_SaveObject;
    KarttricketRWCGlobalization.prototype.ToolTip_SaveRoute = ktCustomerConfig.ToolTip_SaveRoute;
    KarttricketRWCGlobalization.prototype.ToolTip_SaveView = ktCustomerConfig.ToolTip_SaveView;
    KarttricketRWCGlobalization.prototype.ToolTip_SendFeedback = ktCustomerConfig.ToolTip_SendFeedback;
    KarttricketRWCGlobalization.prototype.ToolTip_ShowHelp = ktCustomerConfig.ToolTip_ShowHelp;
    KarttricketRWCGlobalization.prototype.ToolTip_ShowInfo = ktCustomerConfig.ToolTip_ShowInfo;
    KarttricketRWCGlobalization.prototype.ToolTip_ZoomIn = ktCustomerConfig.ToolTip_ZoomIn;
    KarttricketRWCGlobalization.prototype.ToolTip_ZoomOut = ktCustomerConfig.ToolTip_ZoomOut;
}

// ************************************************************************************************
// ************************************************************************************************
// Karttricket RWC geo object
// ************************************************************************************************
// ************************************************************************************************

KarttricketRWCObject.prototype.KarttricketRWC = null;
KarttricketRWCObject.prototype.KtRWCObject = null;
KarttricketRWCObject.prototype.RWCObject = null;
KarttricketRWCObject.prototype.IsVisible = null;
KarttricketRWCObject.prototype.Id = null;


KarttricketRWCObject.prototype.Show = function()
{
    this.RWCObject.show();
    this.IsVisible = true;
}


KarttricketRWCObject.prototype.Hide = function()
{
    this.RWCObject.hide();
    this.IsVisible = false;
}


KarttricketRWCObject.prototype.SetPosition = function(rwcPoint)
{
    this.RWCObject.setPosition(rwcPoint.x, rwcPoint.y, null);
    this.Refresh();
    // Funkar inte längre i 3.3
    // this.RWCObject.layer.refresh(false);
}


KarttricketRWCObject.prototype.Refresh = function()
{
    this.RWCObject.setAttribute('title', '<div class="ktObjTitle">' + this.KtRWCObject.Title + '</div>');
    this.RWCObject.setAttribute('description', this.KtRWCObject.Description == null ? '' : '<div class="ktObjDescription">' + this.KtRWCObject.Description.replace(/[\r\n]/g, "<br />") + '</div>');
    
    if(this.KtRWCObject.LinkUrl && this.KtRWCObject.LinkTitle)
        this.RWCObject.setAttribute('link', '<div class="ktObjLink"><a target="_blank" href="' + this.KtRWCObject.LinkUrl + '">' + this.KtRWCObject.LinkTitle + '</a></div>');
    else if(this.KtRWCObject.LinkUrl)
        this.RWCObject.setAttribute('link', '<div class="ktObjLink"><a target="_blank" href="' + this.KtRWCObject.LinkUrl + '">' + this.KtRWCObject.LinkUrl + '</a></div>');
    
    if(this.KtRWCObject.FileUrl && this.KtRWCObject.FileTitle)
        this.RWCObject.setAttribute('file', '<div class="ktObjFile"><a href="' + this.KtRWCObject.FileUrl + '">' + this.KtRWCObject.FileTitle + '</a></div>');
    
    this.RWCObject.setImage(this.KarttricketRWC.StartSettings.BaseSymbolPath + '/' + this.KtRWCObject.ClientPath, this.KtRWCObject.ClientWidth, this.KtRWCObject.ClientHeight, '');
    this.RWCObject.setSelectedImage(this.KarttricketRWC.StartSettings.BaseSymbolPath + '/' + this.KtRWCObject.ClientPath, this.KtRWCObject.ClientWidth, this.KtRWCObject.ClientHeight, '');

//    this.RWCObject.setAttribute('ObjectType', 'KarttricketObject');

    // Symbol offset must be fixed here
}

function KarttricketRWCObject(karttricketRWC, ktRWCObject, layerId)
{
    this.IsVisible = true;
    this.KarttricketRWC = karttricketRWC;
    this.KtRWCObject = ktRWCObject;
    this.Id = ktRWCObject.ObjectId;

    if(layerId == null || layerId.length == 0) layerId = 'AdHocLayer';
    
    this.RWCObject = this.KarttricketRWC.RWC.createGeoObject(ktRWCObject.RT90Y, ktRWCObject.RT90X, ktRWCObject.ObjectId.toString(), layerId, 'point', null);
    this.Refresh();
    // Symbol offset must be fixed here
    this.RWCObject.activate();
}


// ************************************************************************************************
// ************************************************************************************************
// Karttricket RWC Route object
// ************************************************************************************************
// ************************************************************************************************
KarttricketRWCRoute.prototype.KarttricketRWC = null;
KarttricketRWCRoute.prototype.KtRWCRoute = null;
KarttricketRWCRoute.prototype.RWCRoute = null;
KarttricketRWCRoute.prototype.RouteLayer = null;
KarttricketRWCRoute.prototype.Objects = null;
KarttricketRWCRoute.prototype.IsVisible = null;
KarttricketRWCRoute.prototype.Id = null;


KarttricketRWCRoute.prototype.Hide = function()
{
    if(this.IsVisible == false) return;
    this.RouteLayer.hide();
    this.RWCRoute.hide();
    this.IsVisible = false;
}


KarttricketRWCRoute.prototype.Show = function()
{
    if(this.IsVisible == true) return;
    this.RouteLayer.show();
    this.RWCRoute.show();
    this.IsVisible = true;
}


KarttricketRWCRoute.prototype.AddObject = function(ktObject)
{
    var ObjTmp = null;
    if(ktObject)
        ObjTmp = this.GetObject(ktObject.ObjectId);

    if(ObjTmp)
        ObjTmp.Show();
    else
    {
        var KtRWCGeoObject = new KarttricketRWCObject(this.KarttricketRWC, ktObject, this.RouteLayer.id);
        this.Objects.push(KtRWCGeoObject);
    }
}


KarttricketRWCRoute.prototype.GetObject = function(objectId)
{
    for(var i = 0; i < this.Objects.length; i++)
        if(this.Objects[i].Id == objectId)
            return this.Objects[i];
    return null;
}


KarttricketRWCRoute.prototype.IsObjectLoaded = function(objectId)
{
    for(var i = 0; i < this.Objects.length; i++)
        if(this.Objects[i].Id == objectId)
            return true;
    return false;
}


KarttricketRWCRoute.prototype.GetObjectIndex = function(objectId)
{
    for(var i = 0; i < this.Objects.length; i++)
        if(this.Objects[i].Id == objectId)
            return i;
    return null;
}


KarttricketRWCRoute.prototype.GetObject = function(objectId)
{
    for(var i = 0; i < this.Objects.length; i++)
        if(this.Objects[i].Id == objectId)
            return this.Objects[i];
    return null;
}


KarttricketRWCRoute.prototype.RemoveObject = function(objectId)
{
    var Idx = this.GetObjectIndex(objectId);
    if(Idx != null) 
    {
        this.KarttricketRWC.RWC.removeGeoObject(objectId.toString(), this.RouteLayer.id);
        this.Objects.splice(Idx, 1);
        return true;
    }
    else 
        return false;
}

KarttricketRWCRoute.prototype.RemoveAllObjects = function()
{
    while(this.Objects.length > 0)
    {
        this.KarttricketRWC.RWC.removeGeoObject(this.Objects[0].Id, this.RouteLayer.id);
        this.Objects.splice(0, 1);
    }
}

KarttricketRWCRoute.prototype.GetPointsFromCoordString = function(rt90Coords)
{
    var Coords = new Array();
    var Parts = rt90Coords.split(',');
    for(var i = 0; i < Parts.length; i++)
        Coords.push(new RWCPoint(parseFloat(Parts[i+1]), parseFloat(Parts[i++])));
    return Coords;
}

function KarttricketRWCRoute(karttricketRWC, ktRWCRoute)
{
    this.Objects = new Array();
    this.IsVisible = true;
    this.KarttricketRWC = karttricketRWC;
    this.KtRWCRoute = ktRWCRoute;
    this.Id = ktRWCRoute.RouteId;
    

    this.RouteLayer = this.KarttricketRWC.RWC.getObjectLayerById('routeLayer' + ktRWCRoute.RouteId.toString());
    if(!this.RouteLayer)
        this.RouteLayer = this.KarttricketRWC.RWC.addObjectLayer('routeLayer' + ktRWCRoute.RouteId.toString(), ktRWCRoute.RWCTemplate == null ? 'ktRwcObjects' : ktRWCRoute.RWCTemplate);
    this.RouteLayer.draw();
    this.RouteLayer.show();

    this.RWCRoute = this.KarttricketRWC.RWC.createGeoObject(ktRWCRoute.RT90StartY, ktRWCRoute.RT90StartX, ktRWCRoute.RouteId.toString(), this.RouteLayer.id, 'line', this.GetPointsFromCoordString(ktRWCRoute.RT90Coordinates));
    if(ktRWCRoute.Color)
        this.RWCRoute.color = ktRWCRoute.Color;
    if(ktRWCRoute.Style)
        this.RWCRoute.style = ktRWCRoute.Style;
    if(ktRWCRoute.Width && ktRWCRoute.Width > 0)
        this.RWCRoute.lineWidth = ktRWCRoute.Width;
    this.RWCRoute.setAttribute('title', ktRWCRoute.Name  + ' (' + Math.round(ktRWCRoute.Distance).toString() + 'm)');
//    this.RWCRoute.setAttribute('description', ktRWCRoute.Description);
    this.RWCRoute.activate();

    if(this.KtRWCRoute)
    {
        if(this.KtRWCRoute.KtObjects)
        {
            for(var i = 0; i < this.KtRWCRoute.KtObjects.length; i++)
                this.AddObject(this.KtRWCRoute.KtObjects[i]);
        }
    }
}


// ************************************************************************************************
// ************************************************************************************************
// Karttricket RWC Layer object
// ************************************************************************************************
// ************************************************************************************************
KarttricketRWCLayer.prototype.KarttricketRWC = null;
KarttricketRWCLayer.prototype.KtRWCLayer = null;
KarttricketRWCLayer.prototype.RWCLayer = null;
KarttricketRWCLayer.prototype.Objects = null;
KarttricketRWCLayer.prototype.IsVisible = null;
KarttricketRWCLayer.prototype.Id = null;

KarttricketRWCLayer.prototype.Show = function()
{
    if(this.IsVisible == true) return;
    this.RWCLayer.show();
    this.IsVisible = true;
}


KarttricketRWCLayer.prototype.Hide = function()
{
    if(this.IsVisible == false) return;
    this.RWCLayer.hide();
    this.IsVisible = false;
}


KarttricketRWCLayer.prototype.GetObject = function(objectId)
{
    for(var i = 0; i < this.Objects.length; i++)
        if(this.Objects[i].Id == objectId)
            return this.Objects[i];
    return null;
}


KarttricketRWCLayer.prototype.GetObjectIndex = function(objectId)
{
    for(var i = 0; i < this.Objects.length; i++)
    {
        if(this.Objects[i].Id == objectId)
        {
            return i;
        }
    }
    return null;
}


KarttricketRWCLayer.prototype.IsObjectLoaded = function(objectId)
{
    for(var i = 0; i < this.Objects.length; i++)
        if(this.Objects[i].Id == objectId)
            return true;
    return false;
}


KarttricketRWCLayer.prototype.RemoveObject = function(objectId)
{
    var Idx = this.GetObjectIndex(objectId);
    if(Idx != null) 
    {
        this.KarttricketRWC.RWC.removeGeoObject(objectId.toString(), this.RWCLayer.id);
        this.Objects.splice(Idx, 1);
        return true;
    }
    else 
        return false;
}


KarttricketRWCLayer.prototype.RemoveAllObjects = function()
{
    while(this.Objects.length > 0)
    {
        this.KarttricketRWC.RWC.removeGeoObject(this.Objects[0].Id, this.RWCLayer.id);
        this.Objects.splice(0, 1);
    }
}


KarttricketRWCLayer.prototype.AddObject = function(ktObject)
{
    var ObjTmp = this.GetObject(ktObject.ObjectId);
    if(ObjTmp)
        ObjTmp.Show();
    else
    {
        var KtRWCGeoObject = new KarttricketRWCObject(this.KarttricketRWC, ktObject, this.RWCLayer.id);
        this.Objects.push(KtRWCGeoObject);
    }
}


function KarttricketRWCLayer(karttricketRWC, ktRWCLayer)
{
    this.Objects = new Array();
    this.IsVisible = true;
    this.KarttricketRWC = karttricketRWC;
    this.KtRWCLayer = ktRWCLayer;


    if(ktRWCLayer)
        this.Id = ktRWCLayer.LayerId;
    else    
        this.Id = 'AdHocLayer';
    
    this.RWCLayer = this.KarttricketRWC.RWC.addObjectLayer(this.Id.toString(), 'ktRwcObjects');
    this.RWCLayer.draw();

    if(this.KtRWCLayer)
    {
        if(this.KtRWCLayer.KtObjects)
        {
            for(var i = 0; i < this.KtRWCLayer.KtObjects.length; i++)
                this.AddObject(this.KtRWCLayer.KtObjects[i]);
        }
    }
}


// ************************************************************************************************
// ************************************************************************************************
// Karttricket RWC View object
// ************************************************************************************************
// ************************************************************************************************
KarttricketRWCView.prototype.KarttricketRWC = null;
KarttricketRWCView.prototype.KtRWCView = null;
KarttricketRWCView.prototype.Objects = null;
KarttricketRWCView.prototype.Routes = null;
KarttricketRWCView.prototype.IsVisible = null;
KarttricketRWCView.prototype.Id = null;


KarttricketRWCView.prototype.Hide = function()
{
    if(this.IsVisible == false) return;
    for(var i = 0; i < this.Objects.length; i++)
        this.Objects[i].Hide();
    for(i = 0; i < this.Routes.length; i++)
        this.Routes[i].Hide();
    this.IsVisible = false;
}


KarttricketRWCView.prototype.Show = function()
{
    this.ResetPosition();
    if(this.IsVisible == true) return;
    for(var i = 0; i < this.Objects.length; i++)
        this.Objects[i].Show();
    for(i = 0; i < this.Routes.length; i++)
        this.Routes[i].Show();
    this.IsVisible = true;
    this.ResetPosition();
}


KarttricketRWCView.prototype.ResetPosition = function()
{
    this.KarttricketRWC.RWC.setMapCenter(this.KtRWCView.StartRT90Y, this.KtRWCView.StartRT90X);
    if(this.KtRWCView.StartScale > 0)
        this.KarttricketRWC.RWC.setScale(this.KtRWCView.StartScale);

    if(this.KarttricketRWC.MapId != this.KtRWCView.MapId)
        this.KarttricketRWC.LoadMap(this.KtRWCView.MapId);
}


KarttricketRWCView.prototype.AddObject = function(ktRWCObject)
{
    var ObjTmp = this.GetObject(ktRWCObject.ObjectId);
    if(ObjTmp)
        ObjTmp.Show();
    else
        this.Objects.push(ktRWCObject);
}


KarttricketRWCView.prototype.GetObject = function(objectId)
{
    for(var i = 0; i < this.Objects.length; i++)
        if(this.Objects[i].Id == objectId)
            return i;
    return null;
}


KarttricketRWCView.prototype.GetObjectIndex = function(objectId)
{
    for(var i = 0; i < this.Objects.length; i++)
        if(this.Objects[i].Id == objectId)
            return this.Objects[i];
    return null;
}


KarttricketRWCView.prototype.RemoveObject = function(objectId)
{
    var Idx = this.GetObjectIndex(objectId);
    if(Idx != null)
    { 
        this.KarttricketRWC.RWC.removeGeoObject(objectId.toString(), 'AdHocLayer');
        this.Objects.splice(Idx, 1);
        return true;
    }
    else 
        return false;
}


KarttricketRWCView.prototype.AddRoute = function(ktRWCRoute)
{
    this.Routes.push(ktRWCRoute);
//    this.KarttricketRWC.Routes.push(ktRWCRoute);
}


KarttricketRWCView.prototype.GetRouteIndex = function(routeId)
{
    for(var i = 0; i < this.Routes.length; i++)
        if(this.Routes[i].Id == routeId)
            return i;
    return null;
}


KarttricketRWCView.prototype.DeleteRoute = function(objectId)
{
    var Idx = this.GetRouteIndex(routeId);
    if(Idx) 
        this.Routes.splice(Idx, 1);
}


function KarttricketRWCView(karttricketRWC, ktRWCView)
{
    this.Objects = new Array();
    this.Routes = new Array();
    this.IsVisible = true;
    this.KarttricketRWC = karttricketRWC;
    this.KtRWCView = ktRWCView;
    this.Id = ktRWCView.ViewId;
    
    var ObjTmp = null;
    
    if(this.KtRWCView.KtObjects)
    {
        for(var i = 0; i < this.KtRWCView.KtObjects.length; i++)
        {
            ObjTmp = this.KarttricketRWC.GetObject(this.KtRWCView.KtObjects[i].ObjectId);
            if(ObjTmp)
                ObjTmp.Show();
            else
                this.AddObject(new KarttricketRWCObject(this.KarttricketRWC, this.KtRWCView.KtObjects[i], null));
        }
    }
    if(this.KtRWCView.KtRoutes)
    {
        for(i = 0; i < this.KtRWCView.KtRoutes.length; i++)
        {
            this.KarttricketRWC.RemoveRoute(this.KtRWCView.KtRoutes[i].RouteId);
            this.AddRoute(new KarttricketRWCRoute(this.KarttricketRWC, this.KtRWCView.KtRoutes[i]));
        }
    }
    this.ResetPosition();
}




// ************************************************************************************************
// ************************************************************************************************
// Karttricket RWC
// ************************************************************************************************
// ************************************************************************************************


// The RWC map object
KarttricketRWC.prototype.RWC = null;



// Initialization parameters for RWC
KarttricketRWC.prototype.SpatialAceWMSUrl = null;
KarttricketRWC.prototype.ViewName = null;
KarttricketRWC.prototype.MapElementId = null;
KarttricketRWC.prototype.ConfigUrl = null;
KarttricketRWC.prototype.KtServerRoot = null;
KarttricketRWC.prototype.RWCPrefix = null;
KarttricketRWC.prototype.MapId = null;
KarttricketRWC.prototype.Locale = null;

// MapCommandRWC Service
KarttricketRWC.prototype.MapCommand = null;



// Callback function when map is loaded and ready
KarttricketRWC.prototype.KtUserInit = null;



// Object holding globalized strings
KarttricketRWC.prototype.Globalization = null;



// User key/id
KarttricketRWC.prototype.UserKey = null;



// Key based start settings
KarttricketRWC.prototype.StartSettings = null;



// Arrays for holding layers, routes, views
KarttricketRWC.prototype.Layers = null;
KarttricketRWC.prototype.Routes = null;
KarttricketRWC.prototype.Views = null;


// State vars
KarttricketRWC.prototype.CurrentTool = null;

KarttricketRWC.prototype.EnableObjectEdit = false;
KarttricketRWC.prototype.EnableRouteEdit = false;

KarttricketRWC.prototype.SelectedObject = null;
KarttricketRWC.prototype.SelectedLayer = null;
KarttricketRWC.prototype.SelectedRoute = null;
KarttricketRWC.prototype.SelectedView = null;

// Handlers

// Function to handle map clicks: function(RWCPoint)
KarttricketRWC.prototype.OnMapClicked = null;
// Function to handle when tool changes from "measureDistance" to something else.
KarttricketRWC.prototype.OnRouteFinished = null;
KarttricketRWC.prototype.OnDistanceChanged = null;
KarttricketRWC.prototype.OnClickToolChanged = null;

KarttricketRWC.prototype.ClickHandlerRegistered = null;

KarttricketRWC.prototype.OnObjectSelected = null;
KarttricketRWC.prototype.OnLayerSelected = null;
KarttricketRWC.prototype.OnViewSelected = null;
KarttricketRWC.prototype.OnRouteSelected = null;



KarttricketRWC.prototype.SetClickTool = function(clickTool)
{
    // "getInfo",  "getPosition", "measureDistance", "setCenter","routeSearch" or "".
    var CurrentToolTemp = this.CurrentTool;
    switch(clickTool)
    {
        case 'getPosition':
        {
            this.CurrentTool = clickTool;
            this.RWC.setClickTool(clickTool);
            break;
        }
        case 'getInfo':
        {
            this.CurrentTool = clickTool;
            this.RWC.setClickTool(clickTool);
            break;
        }
        case 'measureDistance':
        {
            this.CurrentTool = clickTool;
            this.RWC.setClickTool(clickTool);

            if(this.SelectedRoute && this.SelectedRoute.IsVisible && this.EnableRouteEdit)
            {
                this.LoadRouteInMeasureTool(this.SelectedRoute);
            }

            break;
        }
        case 'setCenter':
        {
            this.CurrentTool = clickTool;
            this.RWC.setClickTool(clickTool);
            break;
        }
        case 'routeSearch':
        {
            this.CurrentTool = clickTool;
            this.RWC.setClickTool(clickTool);
            break;
        }
        default:
        {
            this.CurrentTool = 'getPosition';
            this.RWC.setClickTool('getPosition');
            break;
        }
    }
    
    if(CurrentToolTemp == 'measureDistance' && this.CurrentTool != CurrentToolTemp)
    {
        if(this.OnRouteFinished != null)
            this.OnRouteFinished();
        if(this.SelectedRoute)
            this.SelectedRoute.RouteLayer.show();
    }

    if(this.OnClickToolChanged) this.OnClickToolChanged(this.CurrentTool);
}


KarttricketRWC.prototype.LoadRouteInMeasureTool = function(ktRWCRoute)
{
    if(this.CurrentTool != 'measureDistance')
        return;
        
    var ScrP = null;

    for(var i = 0; i < ktRWCRoute.RWCRoute.points.length; i++)
    {
        ScrP = this.RWC.mapToScreenCoordinates(ktRWCRoute.RWCRoute.points[i].x, ktRWCRoute.RWCRoute.points[i].y);
        this.RWC.clickTool.click(ScrP.x, ScrP.y);
    }

//    this.RWC.removeGeoObject(ktRWCRoute.RWCRoute.id, ktRWCRoute.RouteLayer.id);
//    this.RWC.removeObjectLayer(ktRWCRoute.RouteLayer.id);
    ktRWCRoute.RouteLayer.hide();
    
    if(this.RWC.clickTool && this.OnDistanceChanged)
        this.OnDistanceChanged(this.CalcDistanceFromMeasureTool());

}

KarttricketRWC.prototype.DeleteLastRoutePoint = function()
{
    if(this.CurrentTool == 'measureDistance' && this.RWC.clickTool && this.RWC.clickTool.pointArray && this.RWC.clickTool.pointArray.length > 0)
    {
        this.RWC.clickTool.saMap.removeGeoObject(this.RWC.clickTool.pointArray[this.RWC.clickTool.pointArray.length - 1].id, this.RWC.clickTool.layer.id);
        this.RWC.clickTool.pointArray.splice(this.RWC.clickTool.pointArray.length - 1, 1);
        this.RWC.clickTool.line.points.splice(this.RWC.clickTool.line.points.length - 1, 1);
        this.RWC.clickTool.totalDistance -= this.RWC.clickTool.distances[this.RWC.clickTool.distances.length - 1];
        this.RWC.clickTool.distanceArray.splice(this.RWC.clickTool.distanceArray.length - 1, 1);
        this.RWC.clickTool.distances.splice(this.RWC.clickTool.distances.length - 1, 1);
        this.RWC.clickTool.measureState--;
        this.RWC.clickTool.layer.draw();

        if(this.RWC.clickTool && this.RWC.clickTool.totalDistance && this.OnDistanceChanged)
            this.OnDistanceChanged(this.CalcDistanceFromMeasureTool());
    }
}

KarttricketRWC.prototype.DeleteAllRoutePoints = function()
{
    if(this.CurrentTool == 'measureDistance' && this.RWC.clickTool && this.RWC.clickTool.pointArray && this.RWC.clickTool.pointArray.length > 0)
    {
        this.RWC.clickTool.cancel();
        if(this.OnDistanceChanged) this.OnDistanceChanged(0.0);
    }

}



KarttricketRWC.prototype.UnselectObject = function()
{
    if(this.SelectedObject)
    {
        this.SelectedObject.RWCObject.clientNode.image.style.border = '';
        this.SelectedObject = null;
    }
}



KarttricketRWC.prototype.SelectObject = function(ktObject)
{
    if(this.SelectedObject) this.UnselectObject();
    this.SelectedObject = ktObject;
    this.SelectedObject.RWCObject.clientNode.image.style.border = 'solid 2px red';
    if(this.OnObjectSelected) this.OnObjectSelected(ktObject);
}

KarttricketRWC.prototype.UnselectLayer = function()
{
    this.SelectedLayer = null;
}

KarttricketRWC.prototype.SelectLayer = function(layerId)
{
    var LayerObj = this.GetLayer(layerId)
    if(LayerObj)
    {
        this.SelectedLayer = LayerObj;
        LayerObj.Show();
        if(this.OnLayerSelected) this.OnLayerSelected(LayerObj);
    }
    else
        this.LoadLayer(layerId);
}



KarttricketRWC.prototype.UnselectRoute = function()
{
    this.SelectedRoute = null;
}

KarttricketRWC.prototype.SelectRoute = function(routeId)
{
    var RouteObj = this.GetRoute(routeId)
    if(RouteObj)
    {
        this.SelectedRoute = RouteObj;
        RouteObj.Show();
        if(this.OnRouteSelected) this.OnRouteSelected(RouteObj);
    }
    else
        this.LoadRoute(routeId);
}


KarttricketRWC.prototype.UnselectView = function()
{
    this.SelectedView = null;
}

KarttricketRWC.prototype.SelectView = function(viewId)
{
    var ViewObj = this.GetView(viewId)
    if(ViewObj)
    {
        this.SelectedView = ViewObj;
        ViewObj.Show();
        if(this.OnViewSelected) this.OnViewSelected(ViewObj);
    }
    else
        this.LoadView(viewId);
}


KarttricketRWC.prototype.GetClickedObject = function(rwcMap, point)
{
    var layers = rwcMap.getObjectLayers();
    var n = layers.length;
    for (var i=0; i<n; i++)
    {
        var objectArray = rwcMap.getGeoObjectsArray(layers[i].id);
	    if (objectArray)
	    {
	        m=objectArray.length;
	        for (var j=0;j<m;j++)
	        {
	            if (objectArray[j].boundingArea.inside(point.x,point.y))
	            {
		            return this.GetObject(objectArray[j].id);
	            }
	        }
	    }
    }
    return null;
}



// "getInfo",  "getPosition", "measureDistance", "setCenter","routeSearch" or "".
KarttricketRWC.prototype.OnClickHandler = function(rwcMap, point)
{
    switch(this.CurrentTool)
    {
        case 'getPosition':
        case 'getInfo':
        {
            if(this.EnableObjectEdit)
            {
                var Obj = this.GetClickedObject(rwcMap, point);
                if(Obj == this.SelectedObject)
                    this.UnselectObject();
                else if(Obj)
                    this.SelectObject(Obj);
                else if(this.SelectedObject) {
                    if(confirm('Är du säker på att du vill flytta objektet?'))
                        this.SelectedObject.SetPosition(point);
                }
            }

            if(this.OnMapClicked) this.OnMapClicked(point);

            break;
        }
        case 'measureDistance':
        {
            if(this.RWC.clickTool && this.OnDistanceChanged)
                this.OnDistanceChanged(this.CalcDistanceFromMeasureTool());
            break;
        }
    }
}

KarttricketRWC.prototype.CalcDistanceFromMeasureTool = function()
{
    if(this.RWC.clickTool && this.RWC.clickTool.distanceArray && this.RWC.clickTool.distanceArray.length > 1)
    {
        var a, b, dist = 0.0;
        for(var i = this.RWC.clickTool.distanceArray.length - 1; i >= 1; i--)
        {
            a = Math.pow(this.RWC.clickTool.distanceArray[i].x - this.RWC.clickTool.distanceArray[i - 1].x, 2);
            b = Math.pow(this.RWC.clickTool.distanceArray[i].y - this.RWC.clickTool.distanceArray[i - 1].y, 2);
            dist += Math.sqrt(a + b);
        }
        return dist;
    }
    else
        return 0.0;
}



KarttricketRWC.prototype.GetVisibleObjectIds = function()
{
    var i, j;
    var ObjectIds = new Array();
    
    for(i = 0; i < this.Layers.length; i++)
        if(this.Layers[i].IsVisible)
            for(j = 0; j < this.Layers[i].Objects.length; j++)
                if(this.Layers[i].Objects[j].IsVisible)
                    ObjectIds.push(this.Layers[i].Objects[j].Id);

    for(i = 0; i < this.Views.length; i++)
        if(this.Views[i].IsVisible)
            for(j = 0; j < this.Views[i].Objects.length; j++)
                if(this.Views[i].Objects[j].IsVisible)
                    ObjectIds.push(this.Views[i].Objects[j].Id);

    return ObjectIds;
}



KarttricketRWC.prototype.GetVisibleRouteIds = function()
{
    var i, j;
    var RouteIds = new Array();

    for(i = 0; i < this.Routes.length; i++)
        if(this.Routes[i].IsVisible)
            RouteIds.push(this.Routes[i].Id);

    for(i = 0; i < this.Views.length; i++)
        if(this.Views[i].IsVisible)
            for(j = 0; j < this.Views[i].Routes.length; j++)
                if(this.Views[i].Routes[j].IsVisible)
                    RouteIds.push(this.Views[i].Routes[j].Id);

    return RouteIds;
}



KarttricketRWC.prototype.IsLayerLoaded = function(layerId)
{
    for(var i = 0; i < this.Layers.length; i++)
        if(this.Layers[i].Id == layerId)
            return true;
    return false;
}


KarttricketRWC.prototype.GetLayer = function(layerId)
{
    for(var i = 0; i < this.Layers.length; i++)
        if(this.Layers[i].Id == layerId)
            return this.Layers[i];
    return null;
}

KarttricketRWC.prototype.GetLayerIndex = function(layerId)
{
    for(var i = 0; i < this.Layers.length; i++)
        if(this.Layers[i].Id == layerId)
            return i;
    return null;
}


KarttricketRWC.prototype.RemoveLayer = function(layerId)
{
    var LayerObj = this.GetLayer(layerId);
    if(LayerObj)
    {
        LayerObj.RemoveAllObjects();
        var Idx = this.GetLayerIndex(layerId);
        this.Layers[Idx] = null;
        this.Layers.splice(Idx, 1);
    }    
}


KarttricketRWC.prototype.HideAllLayers = function()
{
    if(this.Layers.length > 1)
        for(var i = 1; i < this.Layers.length; i++)
            this.Layers[i].Hide();
    this.SelectedLayer = null;
}


KarttricketRWC.prototype.IsRouteLoaded = function(routeId)
{
    for(var i = 0; i < this.Routes.length; i++)
        if(this.Routes[i].Id == routeId)
            return true;
    return false;
}


KarttricketRWC.prototype.GetRoute = function(routeId)
{
    for(var i = 0; i < this.Routes.length; i++)
        if(this.Routes[i].Id == routeId)
            return this.Routes[i];
    return null;
}

KarttricketRWC.prototype.GetRouteIndex = function(routeId)
{
    for(var i = 0; i < this.Routes.length; i++)
        if(this.Routes[i].Id == routeId)
            return i;
    return null;
}


KarttricketRWC.prototype.RemoveRoute = function(routeId)
{
    var RouteObj = this.GetRoute(routeId);
    if(RouteObj)
    {
        RouteObj.RemoveAllObjects();
        this.RWC.removeGeoObject(RouteObj.RWCRoute.id, RouteObj.RouteLayer.id);
        var Idx = this.GetRouteIndex(routeId);
        this.Routes.splice(Idx, 1);
    }    
}



KarttricketRWC.prototype.HideAllRoutes = function()
{
    for(var i = 0; i < this.Routes.length; i++)
        this.Routes[i].Hide();
    this.SelectedRoute = null;
}


KarttricketRWC.prototype.IsObjectLoaded = function(objectId)
{
    for(var i = 0; i < this.Layers.length; i++)
        if(this.Layers[i].IsObjectLoaded(objectId))
            return true;
    for(i = 0; i < this.Routes.length; i++)
        if(this.Routes[i].IsObjectLoaded(objectId))
            return true;
    return false;
}


KarttricketRWC.prototype.GetObject = function(objectId)
{
    var ObjTmp = null;
    for(var i = 0; i < this.Layers.length; i++)
    {
        ObjTmp = this.Layers[i].GetObject(objectId);
        if(ObjTmp)
            return ObjTmp;
    }
    for(i = 0; i < this.Routes.length; i++)
    {
        ObjTmp = this.Routes[i].GetObject(objectId);
        if(ObjTmp)
            return ObjTmp;
    }
    return null;
}


KarttricketRWC.prototype.RemoveObject = function(objectId)
{
    for(var i = 0; i < this.Layers.length; i++)
    {
        if(this.Layers[i].RemoveObject(objectId))
            break;
    }
    for(i = 0; i < this.Routes.length; i++)
    {
        if(this.Routes[i].RemoveObject(objectId))
            break;
    }
    for(i = 0; i < this.Views.length; i++)
        if(this.Views[i].RemoveObject(objectId))
            break;
}



KarttricketRWC.prototype.GetView = function(viewId)
{
    for(var i = 0; i < this.Views.length; i++)
        if(this.Views[i].Id == viewId)
            return this.Views[i];
    return null;
}

KarttricketRWC.prototype.GetViewIndex = function(viewId)
{
    for(var i = 0; i < this.Views.length; i++)
        if(this.Views[i].Id == viewId)
            i;
    return null;
}


KarttricketRWC.prototype.HideAllViews = function()
{
    for(var i = 0; i < this.Views.length; i++)
        this.Views[i].Hide();
    this.SelectedView = null;
}

KarttricketRWC.prototype.RemoveView = function(viewId)
{
    var ViewObj = this.GetView(viewId);
    if(ViewObj)
    {
        while(ViewObj.Routes.length > 0)
        {
            ViewObj.Routes[0].RemoveAllObjects();
            this.RWC.removeGeoObject(ViewObj.Routes[0].RWCRoute.id, ViewObj.Routes[0].RouteLayer.id);
            ViewObj.Routes.splice(0, 1);
        }
        
        while(ViewObj.Objects.length > 0)
            ViewObj.RemoveObject(ViewObj.Objects[0].Id);

        this.Views.splice(this.GetViewIndex(viewId), 1);
    }
}



KarttricketRWC.prototype.RWCUserInit = function(mapObject)
{
    this.MapId = this.StartSettings.VirtualMapId == 0 ? this.StartSettings.MapId : this.StartSettings.VirtualMapId;

    if(this.StartSettings.BoundaryX1 > 0 && this.StartSettings.BoundaryY1 > 0 && this.StartSettings.BoundaryX2 > 0 && this.StartSettings.BoundaryY2 > 0)
    {
        this.RWC.config.viewSettings.borders.geoRect = new GeoRectangle(this.StartSettings.BoundaryY2, this.StartSettings.BoundaryX1, this.StartSettings.BoundaryY1, this.StartSettings.BoundaryX2);
        this.RWC.config.viewSettings.borders.active = true;
    }
    else
        this.RWC.config.viewSettings.borders.active = false;
    
    this.ResetStartPosition();

    // Layer for ad hoc objects, the first layer of the layer array
    // Id = AdHocLayer
    this.Layers.push(new KarttricketRWCLayer(this, null));

    if(this.ClickHandlerRegistered != true) 
    {
        this.OnClickHandler = associateObjWithCallback(this, this.OnClickHandler);
        this.RWC.registerOnClickEventHandler(this.OnClickHandler);
        this.ClickHandlerRegistered = true;
    }
    
    // Load map objects here
    if(this.StartSettings.MapObjects)
    {
        var AdHocLayer = this.GetLayer('AdHocLayer');
        for(var i = 0; i < this.StartSettings.MapObjects.length; i++)
            AdHocLayer.AddObject(this.StartSettings.MapObjects[i]);
    }

    if(this.KtUserInit)
        this.KtUserInit(this);
}



KarttricketRWC.prototype.ResetStartPosition = function()
{
    if(this.StartSettings.StartY > 0 && this.StartSettings.StartX > 0)
    {
        this.RWC.setMapCenter(this.StartSettings.StartY, this.StartSettings.StartX);
    }

    if(this.StartSettings.StartScale > 0) this.RWC.setScale(this.StartSettings.StartScale);
}


KarttricketRWC.prototype._GetCustomerConfig = function(ktCustomerConfig, context)
{
    if(ktCustomerConfig == null) {
        alert('Behörighet till karta saknas.');
        return;
    }

    context.StartSettings = ktCustomerConfig;
    context.Globalization = new KarttricketRWCGlobalization(ktCustomerConfig);

    if(context.RWC == null)
    {
        context.RWCUserInit = associateObjWithCallback(context, context.RWCUserInit);
        context.RWC = new SpatialAceMap(
            context.SpatialAceWMSUrl, 
            ktCustomerConfig.ViewName == '' ? context.ViewName : ktCustomerConfig.ViewName, 
            context.MapElementId, 
            null, 
            'kt', 
            context.RWCUserInit, 
            context.ConfigUrl, 
            context.ConfigUrl == null ? context.KtServerRoot + '/RWCConfHandler.axd?key=' + context.UserKey : context.ConfigUrl
        );
    }
    else
    {
        context.RWC.viewName = ktCustomerConfig.ViewName;
        context.RWC.refresh();
        context.RWCUserInit(context.RWC);

    }
}



KarttricketRWC.prototype._AuthenticateOnKey = function(ktAuthObject, context)
{
    if(ktAuthObject.IsAuthenticated)
    {
        context.UserKey = ktAuthObject.UserKey;
        context.MapCommand.GetCustomerConfig(context.UserKey, context.MapId, context.Locale, context._GetCustomerConfig, null, context);
    }
}

//**************************************************************************************************************
//**************************************************************************************************************
// Loaders
//**************************************************************************************************************
//**************************************************************************************************************
KarttricketRWC.prototype._LoadObject = function(ktObject, context)
{
    if(ktObject && ktObject.ObjectId)
    {
        if(ktObject.RouteId > 0 && context.IsRouteLoaded(ktObject.RouteId))
        {
            var Route = context.GetRoute(ktObject.RouteId);
            if(Route) Route.AddObject(ktObject);
        }
        else if(ktObject.LayerId > 0 && context.IsLayerLoaded(ktObject.LayerId))
        {
            var Layer = context.GetLayer(ktObject.LayerId);
            if(Layer) Layer.AddObject(ktObject);
        }
        else
            context.GetLayer('AdHocLayer').AddObject(ktObject);
    }
}


KarttricketRWC.prototype.LoadObject = function(objectId)
{
    var Obj = this.GetObject(objectId);
    if(Obj)
        Obj.Show();
    else
        this.MapCommand.LoadKtObject(this.UserKey, objectId, this._LoadObject, null, this);
}



KarttricketRWC.prototype._LoadAndCenterOnObject = function(ktObject, context)
{
    if(ktObject && ktObject.ObjectId)
    {
        if(ktObject.RouteId > 0 && context.IsRouteLoaded(ktObject.RouteId))
        {
            var Route = context.GetRoute(ktObject.RouteId);
            if(Route.IsVisible)
                Route.AddObject(ktObject);
            else
                context.GetLayer('AdHocLayer').AddObject(ktObject);
        }
        else if(ktObject.LayerId > 0 && context.IsLayerLoaded(ktObject.LayerId))
        {
            var Layer = context.GetLayer(ktObject.LayerId);
            if(Layer.IsVisible)
                Layer.AddObject(ktObject);
            else
                context.GetLayer('AdHocLayer').AddObject(ktObject);
        }
        else
            context.GetLayer('AdHocLayer').AddObject(ktObject);
            
        context.RWC.setMapCenter(ktObject.RT90Y, ktObject.RT90X);
        if(ktObject.Scale > 0)
            context.RWC.setScale(ktObject.Scale);
    }
}



KarttricketRWC.prototype.LoadAndCenterOnObject = function(objectId)
{
    var Obj = this.GetObject(objectId);
    if(Obj)
    {
        Obj.Show();
        this.RWC.setMapCenter(Obj.KtRWCObject.RT90Y, Obj.KtRWCObject.RT90X);
        if(Obj.KtRWCObject.Scale > 0)
            this.RWC.setScale(Obj.KtRWCObject.Scale);
    }
    else
        this.MapCommand.LoadKtObject(this.UserKey, objectId, this._LoadAndCenterOnObject, null, this);
}


KarttricketRWC.prototype._LoadLayer = function(ktLayer, context)
{
    if(ktLayer && ktLayer.LayerId)
    {
        var LayObj = new KarttricketRWCLayer(context, ktLayer);
        context.Layers.push(LayObj);
        context.SelectedLayer = LayObj;
        if(context.OnLayerSelected) context.OnLayerSelected(LayObj);
    }
}


KarttricketRWC.prototype.LoadLayer = function(layerId)
{
    var LayerTmp = this.GetLayer(layerId);
    if(LayerTmp)
    {
        LayerTmp.Show();
        this.SelectedLayer = LayerTmp;
    }
    else
        this.MapCommand.LoadKtLayer(this.UserKey, layerId, this._LoadLayer, null, this);
}


KarttricketRWC.prototype._SetLayer = function(ktLayer, context)
{
    if(ktLayer && ktLayer.LayerId)
        context.Layers.push(new KarttricketRWCLayer(context, ktLayer));
}



KarttricketRWC.prototype.SetLayer = function(layerId, visible)
{
    var LayerTmp = this.GetLayer(layerId);
    if(LayerTmp && visible)
        LayerTmp.Show();
    else if(LayerTmp && visible == false)
        LayerTmp.Hide();
    else
        this.MapCommand.LoadKtLayer(this.UserKey, layerId, this._SetLayer, null, this);
}



KarttricketRWC.prototype._LoadRoute = function(ktRoute, context)
{
    if(ktRoute != null && ktRoute.RouteId)
    {
        var RouteObj = new KarttricketRWCRoute(context, ktRoute);
        context.Routes.push(RouteObj);
        context.SelectedRoute = RouteObj;
        if(context.OnRouteSelected) context.OnRouteSelected(RouteObj);
    }
}


KarttricketRWC.prototype.LoadRoute = function(routeId)
{
    var RouteTmp = this.GetRoute(routeId);
    if(RouteTmp)
    {
        RouteTmp.Show();
        this.SelectedRoute = RouteTmp;
    }
    else
        this.MapCommand.LoadKtRoute(this.UserKey, routeId, this._LoadRoute, null, this);
}



KarttricketRWC.prototype._SetRoute = function(ktRoute, context)
{
    if(ktRoute != null && ktRoute.RouteId)
        context.Routes.push(new KarttricketRWCRoute(context, ktRoute));
}



KarttricketRWC.prototype.SetRoute = function(routeId, visible)
{
    var RouteTmp = this.GetRoute(routeId);
    if(RouteTmp && visible)
        RouteTmp.Show();
    else if(RouteTmp && visible == false)
        RouteTmp.Hide();
    else
        this.MapCommand.LoadKtRoute(this.UserKey, routeId, this._SetRoute, null, this);
}



KarttricketRWC.prototype._LoadView = function(ktView, context)
{
    if(ktView != null && ktView.ViewId)
    {
        var ViewObj = new KarttricketRWCView(context, ktView);
        context.Views.push(ViewObj);
        context.SelectedView = ViewObj;
        if(context.OnViewSelected) context.OnViewSelected(ViewObj);
    }
}


KarttricketRWC.prototype.LoadView = function(viewId)
{
    var ViewTmp = this.GetView(viewId);
    this.HideAllViews();
    if(ViewTmp)
    {
        ViewTmp.Show();
        this.SelectedView = ViewTmp;
    }
    else
        this.MapCommand.LoadKtView(this.UserKey, viewId, this._LoadView, null, this);
}


KarttricketRWC.prototype.LoadMap = function(mapId)
{
    this.MapId = mapId;
    this.MapCommand.GetCustomerConfig(this.UserKey, this.MapId, this.Locale, this._GetCustomerConfig, null, this);
}

//**************************************************************************************************************
//**************************************************************************************************************
// Lists
//**************************************************************************************************************
//**************************************************************************************************************

// Passes an array of objects to callback with the properties: int LayerId, string Name, string Description, bool Inactive
KarttricketRWC.prototype.GetLayerList = function(includeInactive, callBackFunction)
{
    this.MapCommand.GetKtLayerList(this.UserKey, includeInactive, callBackFunction, null, this);
}

// Passes an array of routes to callback with the properties: int RouteId, string Name, string Description, int Distance, bool Inactive
KarttricketRWC.prototype.GetRouteList = function(includeInactive, callBackFunction)
{
    this.MapCommand.GetKtRouteList(this.UserKey, includeInactive, callBackFunction, null, this);
}

// Passes an array of views to callback with the properties: int ViewId, string Name, string Description, bool Inactive
KarttricketRWC.prototype.GetViewList = function(includeInactive, callBackFunction)
{
    this.MapCommand.GetKtViewList(this.UserKey, includeInactive, callBackFunction, null, this);
}

// Passes an array of maps to callback with the properties: int MapId, string Name, string Description
KarttricketRWC.prototype.GetMapList = function(callBackFunction)
{
    this.MapCommand.GetKtMapList(this.UserKey, callBackFunction, null, this);
}

//**************************************************************************************************************
//**************************************************************************************************************
// Search
//**************************************************************************************************************
//**************************************************************************************************************
// Passes an array of KtObjectSimple with properties: ObjectId, Title, Description, RT90X, RT90Y, Scale, ExplicitLoading
KarttricketRWC.prototype.ObjectSearch = function(searchText, explicitLoading, callbackFunction)
{
    this.MapCommand.ObjectSearch(this.UserKey, this.MapId, searchText, explicitLoading, callbackFunction, null, this);
}


//**************************************************************************************************************
//**************************************************************************************************************
// Print
//**************************************************************************************************************
//**************************************************************************************************************

KarttricketRWC.prototype.GetPrintHandlerQueryString = function(width, height, scaleToWidth, scaleToHeight)
{
    var l = '', r = '', o = '';
    var i, j;
    
    for(i = 0; i < this.Layers.length; i++)
        if(this.Layers[i].IsVisible && isNaN(this.Layers[i].Id) == false)
            l += this.Layers[i].Id.toString() + ',';
    for(i = 0; i < this.Routes.length; i++)
        if(this.Routes[i].IsVisible)
            r += this.Routes[i].Id.toString() + ',';
    for(i = 0; i < this.Layers[0].Objects.length; i++)
        if(this.Layers[0].Objects[i].IsVisible)
            o += this.Layers[0].Objects[i].Id.toString() + ',';
    
    if(this.SelectedView)
    {
        for(j = 0; j < this.SelectedView.Objects.length; j++)
            if(this.SelectedView.Objects[j].IsVisible)
                o += this.SelectedView.Objects[j].Id.toString() + ',';
        for(j = 0; j < this.SelectedView.Routes.length; j++)
            if(this.SelectedView.Routes[j].IsVisible)
                r += this.SelectedView.Routes[j].Id.toString() + ',';
    }

    if(l.length > 0) l = l.substring(0, l.length - 1);
    if(r.length > 0) r = r.substring(0, r.length - 1);
    if(o.length > 0) o = o.substring(0, o.length - 1);

    var Rect = this.RWC.getGeoRect();
    var Point = this.RWC.getMapCenter();
    var Scale = this.RWC.getScale();

    var Width = width == null ? this.RWC.mapWidth : width;;
    var Height = height == null ? this.RWC.mapHeight : height;

    return '?u=' + this.UserKey + 
        '&w=' + Width.toString() + 
        '&h=' + Height.toString() + 
        '&scaletow=' + (scaleToWidth == null ? 0 : scaleToWidth).toString() +
        '&scaletoh=' + (scaleToHeight == null ? 0 : scaleToHeight).toString() +
        '&m=' + this.MapId.toString() + 
        '&mppx=' + ((Rect.xMax - Rect.xMin) / this.RWC.mapWidth).toString() + 
        '&mppy=' + ((Rect.yMax - Rect.yMin) / this.RWC.mapHeight).toString() + 
        '&x=' + Math.floor(Point.y).toString() +
        '&y=' + Math.floor(Point.x).toString() +
        '&s=' + Scale.toString() +
        '&o=' + o +
        '&l=' + l + 
        '&r=' + r;
}

KarttricketRWC.prototype.RegEventHandler = function(element, typeWithoutOn, funcRef)
{
    if(element.addEventListener)
        element.addEventListener(typeWithoutOn, funcRef, false);
    else if(element.attachEvent)
        element.attachEvent('on' + typeWithoutOn, funcRef);
}

//**************************************************************************************************************
//**************************************************************************************************************
// KarttricketRWC Constructor
//**************************************************************************************************************
//**************************************************************************************************************
function KarttricketRWC(spatialAceWMSUrl, defaultViewName, mapElementId, configUrl, rwcPrefix, userKey, mapId, ktUserInit, ktServerRoot, locale)
{
    this.SpatialAceWMSUrl = spatialAceWMSUrl;
    this.ViewName = defaultViewName;
    this.ConfigUrl = configUrl;
    this.MapElementId = mapElementId;
    this.KtUserInit = ktUserInit;
    this.RWCPrefix = rwcPrefix;
    this.MapId = mapId;
    this.KtServerRoot = ktServerRoot;
    this.Locale = locale == null ? 'se' : locale;
    this.Layers = new Array();
    this.Routes = new Array();
    this.Views = new Array();
    this.CurrentTool = 'getPosition';
    
    this.ClickHandlerRegistered = false;
    
    var mapElement = document.getElementById(mapElementId);
    if(!mapElement)
    {
        alert('Sidelementet ' + mapElementId + ' kan inte hittas.');
        return;
    }
    
    mapElement.style.position = 'absolute';
    
    this.MapCommand = new Kartfab.KarttricketServer.MapCommandRWC();

    if(userKey && userKey.length > 0)
        this.MapCommand.AuthenticateOnKey(userKey, this._AuthenticateOnKey, null, this);
    else 
        this.MapCommand.IsAuthenticatedByLogin(this._AuthenticateOnKey, null, this);
}


