﻿
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<g_OLMapCtrls.length;i++)
        {
            divIds.push(g_OLMapCtrls[i].id);
        }
        return divIds;
    }
    
    ///Refreshes maps of the passed mapControl (divId). Can pass null for mapControl parameter in version 6.2
    this.RefreshMap = function(mapControl)
    {
        RefreshMap();
    }
    
    ///Returns a string array of map Urls of all maps in the passed mapControl. Can pass null for mapControl parameter in version 6.2
    this.GetMaps = function(mapControl)
    {		
		//get onpoint layer.
		var layers = g_OLMapCtrls[0].layers;
		var onpointLayer = null;
		for (var i=0;i<layers.length;i++)
		{
			if (layers[i].CLASS_NAME == "OpenLayers.Layer.OnPoint")	//should be a contant
			{
				onpointLayer = layers[i];
				break;
			}
		}
		
		var maps = null;
		if (onpointLayer)
		{
			maps = onpointLayer.currentUrls.split('|');
		}
		else
		{
			maps = new Array();
		}
		       
        return maps;
    }
    
    ///Returns envelope object with current extents for passed map control. Can pass null for mapControl parameter in version 6.2
    this.GetMapExtents = function(mapControl)
    {
        return g_OLMapCtrls[0].getExtent();
    }
    
    ///Converts pixel (image) coordinates to map coordinates and returns the map point.
    this.ToMapPoint = function(x,y,mapControl)
    {
        var point = {x:x,y:y};
        var extents = this.GetMapExtents(mapControl);        
        if (extents != null)
        {
			var size = g_OLMapCtrls[0].getSize();
            point.x = extents.left + x * (extents.getWidth())/size.w;
            point.y = extents.top - y * (extents.getHeight())/size.h;   
        }
        return point;
    }
    
    ///Register to receive DrawingFinished event. Parameter callBackName is name of function to be invoked to notify the event.
    ///The function must exist in the MapViewer document. To pass function pointer when this condition cannot be met, use RegisterDrawingFinishedHandler.
    this.RegisterDrawingFinishedHandlerByName = function(callBackName)
    { 
		if (this.m_dfFunctions == null)
        {
            this.m_dfFunctions = new Array();
        }         
        this.m_dfFunctions.push(callBackName);        
        if (this.m_receivingDFEvents == false)
        {
			var map = g_OLMapCtrls[0];
			map.drawLayer.events.register('featuresadded',g_objSDK,g_objSDK.HandleDrawingFinished);
            this.m_receivingDFEvents = true;
        }                        
    }
    
    ///Unregister to stop receiving notifications.
    this.UnRegisterDrawingFinishedHandlerByName = function(callBackName)
    {		
		if (this.m_dfFunctions == null)
            return;
            
        for (var i=0;i<this.m_dfFunctions.length;i++)
        {
            if (this.m_dfFunctions[i] == callBackName)
            {
               this.m_dfFunctions.splice(i,1);
               break;
            }            
        }        
    }
    
    ///Register to receive DrawingFinished event. Parameter callBack is function pointer to be invoked to notify the event.
    this.RegisterDrawingFinishedHandler = function(callBack)
    {   
		var map = g_OLMapCtrls[0];
        map.drawLayer.events.register('featuresadded',g_objSDK,callBack);       
    }
    
    ///Unregister to stop receiving notifications.
    this.UnRegisterDrawingFinishedHandler = function(callBack)
    {		
        var map = g_OLMapCtrls[0];
        map.drawLayer.events.unregister('featuresadded',g_objSDK,callBack);    
    }
    this.HandleDrawingFinished = function(features)
    {   
        if (this.m_dfFunctions == null)
            return;
                 
        for (var i=0;i<this.m_dfFunctions.length;i++)
        {
            try
            {             
                eval(this.m_dfFunctions[i] + "(features.features[0])");
            }
            catch (e)
            {
				alert(e.message);
			}
        }
    }
    
    ///Register to receive MapRefreshed event. Parameter callBack is function pointer to be invoked to notify the event.    
    this.RegisterMapRefreshedHandler = function(callBack)
    {
        if (this.m_mrFunctions == null)
        {
            this.m_mrFunctions = new Array();
        }         
        this.m_mrFunctions.push(callBack);        
        if (this.m_receivingMREvents == false)
        {
			var map = g_OLMapCtrls[0];
			//map.events.register(map.MAPEVENT_TYPES.ZOOMEND, g_objSDK, g_objSDK.HandleMapRefreshed);
			map.events.register(map.MAPEVENT_TYPES.MOVEEND, g_objSDK, g_objSDK.HandleMapRefreshed);
            this.m_receivingMREvents = true;
        }        
    }
    this.UnRegisterMapRefreshedHandler = function(callBack)
    {      
        
        if (this.m_mrFunctions == null)
            return;
            
        for (var i=0;i<this.m_mrFunctions.length;i++)
        {
            if (this.m_mrFunctions[i] == callBack)
            {
               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){}
        }
    }
    
    ///Returns last shape drawn on map control.
    this.GetLastShape = function()
    {
		var map = g_OLMapCtrls[0];
		if (map.drawLayer && map.drawLayer.features.length > 0)
		{
			 return map.drawLayer.features[map.drawLayer.features.length - 1];
		}
		return null;
    }
    
    ///Sets draw mode for map control. Allowed numeric values for mode are: 
    /*  0: POINT
        1: LINE
        2: POLY_LINE
        3: POLYGON
        4: RECTANGLE
        5: CIRCLE        
        100: NONE    */		
    this.SetMapDrawMode = function(mode)
    {
		SetDrawMode(mode);
    }
    
    //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();


