/* QwwJs_MapCtlMgr
 * 
 * Part of the QlikWeb WorkBench Javascript Library (http://www.qlikwebworkbench.com)
 *
 * Copyright (c) 2007 Industrial CodeBox Ltd. All Rights Reserved.
 * http://www.industrialcodebox.com
 * Email support@industrialcodebox.com for licensing and support information.
 */
function QwwJs_MapCtlMgr(cfgObj)
{
    var tblMgrCfg = {};
    this.Cfg = cfgObj;
    this.LatCol =  cfgObj.LatitudeColumnIndex != -1 ? cfgObj.LatitudeColumnIndex : -1;
    this.LonCol =  cfgObj.LongitudeColumnIndex != -1 ? cfgObj.LongitudeColumnIndex : -1;
        
    if(!this.Cfg.MaxNumberOfRecords)
        this.Cfg.MaxNumberOfRecords = 100;
            
    tblMgrCfg.ColumnIndexes = cfgObj.ColumnIndexes;
    tblMgrCfg.ObjectID = cfgObj.ObjectID;
    tblMgrCfg.PageSize = cfgObj.MaxNumberOfRecords;

    this.TblMgr = new QwwJs_TableMgr(tblMgrCfg);
            
    this.GetMapCtl = function(){return this.map;};
    
    this.onTblUpdated = function(data)
    {        
        if(data.Cfg.OnCreateMapObject == null)
            alert("OnCreateMapObject function not set for map ctl");
            
        if(data.Cfg.OnRenderBegin) data.Cfg.OnRenderBegin();
                
        data.map.DeleteAllShapes();
        
        var counter = 0;
        
        var total = data.TblMgr.NoRows;
                
        for(var i = 0; i < total; i++)
        {            
            var row = data.TblMgr.GetRow(i);
            
            var lat, lon;
            
            if(data.LatCol == -1)
            {
                alert("LatitudeColumn not set.");
                return;
            }
            else if(data.LonCol == -1)
            {
                alert("LongitudeColumn not set.");
                return;   
            }
            else
            {
                lat = row[data.LatCol].Text;
                lon = row[data.LonCol].Text;
            }

            var shape = null;
                                                        
            if(data.Cfg.OnCreateMapObject)
            {
                var obj = data.Cfg.OnCreateMapObject(row, data.map);
            
                if(obj != null) // if developer returns null we assume they don't want a pin
                {
                    if(obj.VEShape != null)
                    {
                        shape = obj.VEShape;
                    }
                    else
                    {    
                        try{
                        var loc = new VELatLong(lat, lon);
                        }
                        catch(e){continue;}
                        
                        shape = new VEShape(VEShapeType.Pushpin, loc);
                        if(obj.Icon != null)
                            shape.SetCustomIcon(obj.Icon);
                        
                        shape.SetTitle(obj.Title);
                        shape.SetDescription(obj.Description);
                    }
                }
            }
            else
            {
                var loc = new VELatLong(lat, lon);
                shape = new VEShape(VEShapeType.Pushpin, loc);
                shape.SetTitle("CreatePushPinObject not registered, no title available");
                shape.SetDescription("CreatePushPinObject not registered, no description available");
            }

            if(shape != null)
            {
                data.map.AddShape(shape);
                shape.Record = row;
            }

            counter++;
            
            if(data.Cfg.OnProgressUpdate)
                data.Cfg.OnProgressUpdate(counter, total);
        }
        
        if(data.Cfg.OnRenderComplete) data.Cfg.OnRenderComplete();

    };
        
    this.SetCentreAndZoom = function(lat, lon, zoom)
    {
        var loc = new VELatLong(lat, lon);

        this.map.SetCenterAndZoom(loc, zoom);
    };
    
    this.shapeClicked = function(e)
    {   
        if(e.elementID != null)
        {        
            // Not sure how to get at the map control in the 
            // shapeClicked event other than tagging the span with it
            var m = document.getElementById(cfgObj.MapElementID);
                        
            var shape = m.MapMgr.map.GetShapeByID(e.elementID);
                        
            m.MapMgr.TblMgr.SelectSingleRecord(shape.Record.RecordNumber, cfgObj.TableSelectColumn);
        }
    };
  
    this.TblMgr.RegisterUpdatedCallBack(this.onTblUpdated, this);
    
    this.OnDocumentLoaded = function()
    {
        var id = this.Cfg.MapElementID
        this.map = new VEMap(id);
        
        // Not sure how to get at the map control in the 
        // shapeClicked event other than tagging the span with it
        document.getElementById(cfgObj.MapElementID).MapMgr= this;
                
        this.map.LoadMap();
        this.map.AttachEvent("onclick", this.shapeClicked);
        
        if(this.Cfg.InitialLongitude && this.Cfg.InitialLatitude)
        {
            var zoom = 5;
            
            if(this.Cfg.InitialZoom)
                zoom = this.Cfg.InitialZoom;
                
            
            
            try{  
            this.SetCentreAndZoom(this.Cfg.InitialLatitude, this.Cfg.InitialLongitude, Math.round(zoom));
            }catch(e){alert(e.message);}
        }
    };

    if(qwwHub)
        qwwHub.Register(this);
};