﻿
function SDK()
{   
    this.m_dfFunctions = null;   //for drawing finished handdler functions
    this.m_mrFunctions = null;  //for maprefreshed handler functions
    this.m_receivingDFEvents = false;
    this.m_receivingMREvents = false;
    ///Returns divIds of all mapControl objects
    this.GetMapControls = function()
    {
        var divIds = new Array();
        for (var i=0;i<mapObjects.length;i++)
        {
            divIds.push(mapObjects[i].MapImageId);
        }
        return divIds;
    }
    
    ///Refreshes maps of the passed mapControl (divId)
    this.RefreshMap = function(mapControl)
    {
        RefreshMap();
    }
    
    ///Returns a string array of map Urls of all maps in the passed mapControl
    this.GetMaps = function(mapControl)
    {
        var maps = new Array();
        for (var i=0;i<mapObjects.length;i++)
        {
            maps.push(getObject(mapObjects[i].MapImageId).src);
        }
        return maps;
    }
    
    this.GetMapExtents = function(mapControl)
    {
        return mapExtents;  //mapExtents is defined in mapControl.
    }
    
    this.FromMapPoint = function(x,y,mapControl)
    {
        var point = new Point(x,y);
        var extents = this.GetMapExtents(mapControl);        
        if (extents != null)
        {
            point.x = extents.xmin + x * (extents.xmax - extents.xmin)/mapWidth;
            point.y = extents.ymax - y * (extents.ymax - extents.ymin)/mapHeight;   
        }
        return point;
    }
    
    this.RegisterDrawingFinishedHandlerByName = function(callBackName)
    {         
        MapControlEvents.AddListener(MapControlEvent.AFTER_DRAWING,callBackName);                
    }
    this.UnRegisterDrawingFinishedHandlerByName = function(callBackName)
    {
        MapControlEvents.RemoveListener(MapControlEvent.AFTER_DRAWING,callBackName);
    }
    this.RegisterDrawingFinishedHandler = function(callBack)
    {   
        if (this.m_dfFunctions == null)
        {
            this.m_dfFunctions = new Array();
        }         
        this.m_dfFunctions.push(callBack);        
        if (this.m_receivingDFEvents == false)
        {
            MapControlEvents.AddListener(MapControlEvent.AFTER_DRAWING,'g_objSDK.HandleDrawingFinished');
            this.m_receivingDFEvents = true;
        }        
    }
    this.UnRegisterDrawingFinishedHandler = function(callBack)
    {
        if (this.m_dfFunctions == null)
            return;
            
        for (var i=0;i<this.m_dfFunctions.length;i++)
        {
            if (this.m_dfFunctions[i] == callBack)
            {
               this.m_dfFunctions.splice(i,1);
               break;
            }            
        }
    }
    this.HandleDrawingFinished = function(shape)
    {   
        if (this.m_dfFunctions == null)
            return;
                 
        for (var i=0;i<this.m_dfFunctions.length;i++)
        {
            try
            {             
                this.m_dfFunctions[i](shape);
            }
            catch (e){}
        }
    }
    this.RegisterMapRefreshedHandler = function(callBack)
    {   
        if (this.m_mrFunctions == null)
        {
            this.m_mrFunctions = new Array();
        }         
        this.m_mrFunctions.push(callBack);        
        if (this.m_receivingMREvents == false)
        {
            MapControlEvents.AddListener(MapControlEvent.AFTER_MAP_REFRESH,'g_objSDK.HandleMapRefreshed');
            this.m_receivingMREvents = true;
        }        
    }
    this.UnRegisterMapRefreshedHandler = function(callBackFunction)
    {
        if (this.m_mrFunctions == null)
            return;
            
        for (var i=0;i<this.m_mrFunctions.length;i++)
        {
            if (this.m_mrFunctions[i] == callBackFunction)
            {
               this.m_mrFunctions.splice(i,1);
               break;
            }            
        }
    }
    this.HandleMapRefreshed = function()
    {   
        if (this.m_mrFunctions == null)
            return;
                 
        for (var i=0;i<this.m_mrFunctions.length;i++)
        {
            try
            {
                this.m_mrFunctions[i]();
            }
            catch (e){}
        }
    }
    this.GetLastShape = function()
    {
        return shapesCol.GetLastShape();
    }
    
    this.SetMapDrawMode = function(mode)
    {
        SetDrawMode(mode, 'g_objSDK._HandleDrawFinished');
    }
    
    //Internal function to bypass auto postback on draw finished.
    this._HandleDrawFinished = function()
    {       
        return false;
    }
    //Not implemented
    this.TransformToGCS = function(x,y,sourceProjSys)
    {
    }
    
    //Not implemented
    this.TransformFromGCS = function(x,y,targetProjSys)
    {
    }    
}

var g_objSDK = new SDK();
