
/*

    Javascript for legend
    
    handles refreshing and formatting legend on UI

*/

// ------------------------- CLASSES

function IMS2_LegendControl(id,mapid){
    var self = this;
    
    this.id = id;
    this.mapid = mapid;
    
    function UpdateImage(legendUri){
        if(legendUri && ("" != legendUri)){
            var imgtag = self.GetImageTag();
            if(imgtag){
                imgtag.src = legendUri;
            }else{
                var contdiv = self.GetContainingDiv();
                contdiv.innerHTML = "<img id=\"_maplegend_" + self.id + "_img\" src=\"" + legendUri + "\" alt=\"legend\" />";
            }
        }
    }
    this.UpdateImage = UpdateImage;
    
    function GetContainingDiv(){
        return(document.getElementById("_maplegend_" + self.id));
    }
    this.GetContainingDiv = GetContainingDiv;
    
    function GetImageTag(){
        return(document.getElementById("_maplegend_" + self.id + "_img"));
    }
    this.GetImageTag = GetImageTag;
}

//multiple maps + multiple layer lists = multiple legends
//handle that here
function IMS2_LegendControlManager(){
    var self = this;
    
    this.controls = new Array();
    
    function UpdateImage(legendUri){
        for(var i=0; i < self.controls.length;i++){
            var curc = self.controls[i];
            if(curc){
                curc.UpdateImage(legendUri);
            }
        }
    }
    this.UpdateImage = UpdateImage;
    
    function Add(id,mapid){
        var ctrl = null;
        if(self.controls && id){
            var legendi = IndexOf(id);
            if(legendi < 0){
                legendi = self.controls.length;
            }
            ctrl = new IMS2_LegendControl(id,mapid);
            if(ctrl){
                self.controls[legendi] = ctrl;
            }
        }
        return(ctrl);
    }
    this.Add = Add;
    
    function Get(legendid){
        if(self.controls){
            for(var i = 0;i < self.controls.length; i++){
                var curlegendc = self.controls[i];
                if(curlegendc && (legendid == curlegendc.id)){
                    return(curlegendc);
                }
            }
        }
        return(null);
    }
    this.Get = Get;
    
    function IndexOf(legendid){
        var legendi = -1;
        if(self.controls){
            for(var i = 0;i < self.controls.length; i++){
                var curlegendc = self.controls[i];
                if(curlegendc && (legendid == curlegendc.id)){
                    legendi = i;
                    break;
                }
            }
        }
        return(legendi);
    }
    this.IndexOf = IndexOf;
    
}

// ------------------------- FUNCTIONS

function IMS2_RegisterLegendControl(id,mapid){
    if(mapid && id && IMS2_MapManager){
        if(!IMS2_MapManager.AttachLegendControlToMap(id,mapid)){
            var tuple = [id,mapid];
            IMS2_RegisterLegendControlPool[IMS2_RegisterLegendControlPool.length] = tuple;
        }
    }
}

function IMS2_DelayRegisterLegendControls(){   
    if(IMS2_RegisterLegendControlPool && IMS2_MapManager){
        for(var i = 0;i < IMS2_RegisterLegendControlPool.length; i++){
            var tpl = IMS2_RegisterLegendControlPool[i];
            IMS2_MapManager.AttachLegendControlToMap(tpl[0],tpl[1])
        }
        IMS2_RegisterLegendControlPool = new Array();
    }
}

// ------------------------- GLOBALS

var IMS2_RegisterLegendControlPool = new Array();
