/*

    Javascript for layers

*/

// ------------------------- CLASSES

//listing/grouping of layers collection
function IMS2_LayerList(){
    var self = this;
    
    this.layers = new Array();
    this.groups = new Array();
    
    function toString(){
        return(self.layers.join("\n"));
    }
    this.toString = toString;
    
    function Add(id){
        var newLayer = new IMS2_Layer(id);
        var li = self.IndexOf(id);
        if(li < 0){
            li = self.layers.length;
        }
        self.layers[li] = newLayer;
        return(newLayer);
    }
    this.Add = Add;
    
    function Get(id){
        for(var i = 0;i < self.layers.length; i++){
            var curl = self.layers[i];
            if(curl && (curl.id == id)){
                return(curl);
            }
        }
        return(null);
    }
    this.Get = Get;
    
    function IndexOf(id){
        for(var i = 0;i < self.layers.length; i++){
            var curl = self.layers[i];
            if(curl && (curl.id == id)){
                return(i);
            }
        }
        return(-1);
    }
    this.IndexOf = IndexOf;
    
    //analysis references layers by their sde table name
    //use this function to get the user-friendly name
    function GetNameFromDataset(ds){
		for(var i = 0;i < self.layers.length; i++){
            var curl = self.layers[i];
            if(curl && (curl.dataset.toUpperCase() == ds.toUpperCase())){
                return(curl.id);
            }
        }
        return(null);
    }
    this.GetNameFromDataset = GetNameFromDataset;
    
    function AddGroup(id){
        var newGroup = new IMS2_LayerGroup(id);
        var gi = self.IndexOfGroup(id);
        if(gi < 0){
            gi = self.groups.length;
        }
        self.groups[gi] = newGroup;
        return(newGroup);
    }
    this.AddGroup = AddGroup;
    
    function GetGroup(id){
        for(var i=0;i < self.groups.length; i++){
            var curg = self.groups[i];
            if(curg && (curg.id == id)){
                return(curg);
            }
        }
        return(null);
    }
    this.GetGroup = GetGroup;
    
    function IndexOfGroup(id){
        for(var i = 0;i < self.groups.length;i++){
            var curg = self.groups[i];
            if(curg && (curg.id == id)){
                return(i);
            }
        }
        return(-1);
    }
    this.IndexOfGroup = IndexOfGroup;
    
    function SortGroups(asc){
        var il = self.groups.length - 1;
        for(var i = 0; i < il; i++){
            var jl = il - i;
            for(j=0;j < jl; j++){
                var jp1 = j + 1;
                if(
                (asc)?(self.groups[j].order > self.groups[jp1].order):(self.groups[j].order < self.groups[jp1].order)
                ){
                    var tmp = self.groups[j];
                    self.groups[j] = self.groups[jp1];
                    self.groups[jp1] = tmp;
                }
            }
        }
    }
    this.SortGroups = SortGroups;
    
    function SortLayers(asc){
        var il = self.layers.length - 1;
        for(var i = 0; i < il; i++){
            var jl = il - i;
            for(j=0;j < jl; j++){
                var jp1 = j + 1;
                if(
                (asc)?(self.layers[j].order > self.layers[jp1].order):(self.layers[j].order < self.layers[jp1].order)
                ){
                    var tmp = self.layers[j];
                    self.layers[j] = self.layers[jp1];
                    self.layers[jp1] = tmp;
                }
            }
        }
    }
    this.SortLayers = SortLayers;
    
    function GetGroupedLayers(){
        self.SortGroups(true);
        self.SortLayers(true);
        var gl = new Array();
        
        var ll = new Array();
        for(var i = 0; i < self.layers.length; i++){
            if(0 == self.layers[i].groupid){
                ll[ll.length] = self.layers[i];
            }
        }
        if(ll && (ll.length > 0)){
            gl[gl.length] = [null,ll,null];
        }
        if(self.groups.length > 0){
            for(var i = 0;i < self.groups.length; i++){
                ll = new Array();
                var curg = self.groups[i];
                for(var j = 0;j < self.layers.length; j++){
                    var curl = self.layers[j];
                    if(curg.id == curl.groupid){
                        ll[ll.length] = curl;       
                    }
                }
                gl[gl.length] = [curg.alias,ll,curg.id];
            }
        }        
        return(gl);
    }
    this.GetGroupedLayers = GetGroupedLayers;
    
    //process layer info from server
    function Update(node,curScale){
        if(node){            
            var cnode = null;
            var stampVal = 0;
            for(var childi = 0; childi < node.childNodes.length; childi++){
                cnode = node.childNodes[childi];
                if(cnode){
                    if("Layer" == cnode.tagName){
                        //try{
                            var id = cnode.attributes.getNamedItem("id").value;
                            if(id && ("" != id)){
                                var l = self.Get(id);
                                var isNewLayer = false;
                                if(!l){
                                    l = self.Add(id);
                                    isNewLayer = true;
                                }
                                if(l){
                                    try{
                                        l.name = cnode.attributes.getNamedItem("name").value;
                                    }catch(e){;}
                                    try{
                                        l.alias = cnode.attributes.getNamedItem("alias").value;
                                    }catch(e){;}
                                    try{
                                        l.type = cnode.attributes.getNamedItem("type").value;
                                    }catch(e){;}
                                    try{
                                        l.dataType = cnode.attributes.getNamedItem("dataType").value;
                                    }catch(e){;}
                                    try{
                                        l.minScale = cnode.attributes.getNamedItem("minScale").value;
                                    }catch(e){;}
                                    try{
                                        l.maxScale = cnode.attributes.getNamedItem("maxScale").value;
                                    }catch(e){;}
                                    try{
                                        l.groupid = parseInt(cnode.attributes.getNamedItem("layerGroup").value);
                                    }catch(e){;}
                                    try{
                                        l.dataset = cnode.attributes.getNamedItem("dataset").value;
                                    }catch(e){;}
                                    try{
                                        l.idField = cnode.attributes.getNamedItem("idField").value;
                                    }catch(e){;}
                                    try{
                                        l.order = parseInt(cnode.attributes.getNamedItem("order").value);
                                    }catch(e){;}
                                    try{
                                        l.defaultVisible = cnode.attributes.getNamedItem("defaultVisible").value;
                                        l.defaultVisible = ("true" == "" + l.defaultVisible.toLowerCase()) ? true : false;
                                        if(isNewLayer){
                                            l.visible = l.defaultVisible;
                                        }
                                    }catch(e){;}
                                    try{
                                        l.primaryDisplay = cnode.attributes.getNamedItem("primaryDisplay").value;
                                    }catch(e){;}
                                    try{
                                        l.useGaz = cnode.attributes.getNamedItem("useGaz").value;
                                    }catch(e){;}
                                    try{
                                        l.useQuery = cnode.attributes.getNamedItem("useQuery").value;
                                    }catch(e){;}
                                    try{
                                        l.useAnalysis = cnode.attributes.getNamedItem("useAnalysis").value;
                                    }catch(e){;}
                                    try{
                                        //alert(cnode.attributes.getNamedItem("envelope").value);
                                        var t = cnode.attributes.getNamedItem("envelope").value;
                                        t = t.replace('(', '[');
                                        t = t.replace(')', ']');
                                        t = t.replace('(', '[');
                                        t = t.replace(')', ']');
                                        t = t.replace('(', '[');
                                        t = t.replace(')', ']');
                                        var ev = eval(t);
                                        //alert(t);
                                        //alert(ev.length);
                                        l.envelope = new IMS2_Envelope(ev[0][0],ev[0][1],ev[1][0],ev[1][1]);
                                        
                                    }catch(e){;}
                                    //try{
                                        //l.visible = cnode.attributes.getNamedItem("visible").value;
                                        //l.visible = ("true" == ("" + l.visible.toLowerCase()));
                                    //}catch(e){;}
                                }
                            }
                        //}catch(e){;}
                    }
                    else if("Group" == cnode.tagName){
                        //try{
                            var id = parseInt(cnode.attributes.getNamedItem("id").value);
                            if(id && id > 0){
                                var g = self.GetGroup(id);
                                if(!g){
                                    g = self.AddGroup(id);
                                }
                                if(g){
                                    try{
                                        g.alias = cnode.attributes.getNamedItem("alias").value;
                                    }catch(e){;}
                                    try{
                                        g.order = parseInt(cnode.attributes.getNamedItem("order").value);
                                    }catch(e){g.order = 0;}
                                }
                            }                            
                        //}catch(e){;}
                    }
                }
            }
        }
        self.UpdateScales(curScale);
    }
    this.Update = Update;
    
    function UpdateScales(curScale){
        if(curScale && self.layers){
            for(var i = 0;i < self.layers.length; i++){
                var l = self.layers[i];
                if(l){
                    l.withinScale = l.isScaleWithin(curScale);
                }
            }
        }
    }
    this.UpdateScales = UpdateScales;
}

function IMS2_LayerGroup(id){
    var self = this;
    
    this.id = id;
    this.alias = "";
    this.order = 0;
}

//layer object - store all necessary info client-side to reduce server hits
//allows more control through code
function IMS2_Layer(id){
    var self = this;
    
    this.id = id;
    this.type = "";
    this.dataType = "";
    this.name = id;
    this.alias = null;
    this.visible = false;
    this.defaultVisible = false;
    this.minScale = -1;
    this.maxScale = -1;
    this.withinScale = false;
    this.groupid = 0;
    this.order = 0;
    this.primaryDisplay = "";
    this.fieldsCollection;//= new IMS2_FieldsCollection();
    this.xmlReq;
    this.dynamic = false;
    this.dataset = "";
    this.workspace = "";
    this.renderer = "";
    this.selectedOIDs = new Array();
    this.defQuery;
    this.existingws = false;
    this.useGaz = false;
    this.useQuery = false;
    this.useAnalysis = false;
    this.envelope;
    this.idField = "OBJECTID";//the field for unique id, usually OBJECTID
    
    function toString(){
        return("" + ((self.visible)?("X"):("_")) + " " + ((self.isScaleWithin(50))?("X"):("_")) + " " + self.id + "(" + self.minScale + "," + self.maxScale + ")");
    }
    this.toString = toString;
    
    function isScaleWithin(curScale){
        if((curScale >= self.minScale) || (-1 == self.minScale)){
            if((curScale <= self.maxScale) || (-1 == self.maxScale)){
                return(true);
            }
        }
        return(false);
    }
    this.isScaleWithin = isScaleWithin;
    
    //get fields for the layer.  if the layer already has a fields collection return the collection, otherwise get the info from the server
    function GetFields(){
		if(!self.fieldsCollection)
			self.MakeFieldsRequest();
			
		return(self.fieldsCollection)		
    }
    this.GetFields = GetFields;
    
    function MakeFieldsRequest(){
		var requestMade = false;
        var mapctl = ((IMS2_MapManager)?(IMS2_MapManager.GetMap("MapControl1")):(null));
        self.xmlReq = IMS2_createXMLHTTPRequest();
        if(self.xmlReq){
            var url = "webservice.asmx/GetFieldIDs";
            self.xmlReq.onreadystatechange = self.ProcessFieldsRequest;
            self.xmlReq.open("POST", url, false);//synchronous call
            self.xmlReq.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            self.xmlReq.send("servicename=" + mapctl.mapdatasource + "&layerid=" + self.id);
            requestMade = true;
        }
        return(requestMade);
    }
    this.MakeFieldsRequest = MakeFieldsRequest;
    
    function ProcessFieldsRequest(){
		if(self.xmlReq){
            if( self.xmlReq.readyState == 4 )
		    {
			    if( self.xmlReq.status == 200 )
			    {
			        self.fieldsCollection = new IMS2_FieldsCollection();
			        var root = self.xmlReq.responseXML.documentElement;   
			        if(root){
			            for(var childi = 0; childi < root.childNodes.length; childi++){
							var node = root.childNodes[childi];
			                //self.fieldsCollection.Add(node.attributes.getNamedItem("name").value,node.attributes.getNamedItem("alias").value,node.attributes.getNamedItem("type").value);
			                var f = new IMS2_Field(node.attributes.getNamedItem("name").value,node.attributes.getNamedItem("alias").value,node.attributes.getNamedItem("type").value);
			                f.useGaz = node.attributes.getNamedItem("useGaz").value;
			                f.useQuery = node.attributes.getNamedItem("useQuery").value;
			                f.useAnalysis = node.attributes.getNamedItem("useAnalysis").value;
			                self.fieldsCollection.AddField(f);
						}
			        }
			    }else{
			        alert("Connection error: " + self.xmlReq.status + "\n" + self.xmlReq.statusText + "\n" + self.xmlReq.responseText);
			    }
			    try{
			        self.xmlReq.abort();
			    }catch(e){;}
			    self.xmlReq = null;
			}
        }
    }
    this.ProcessFieldsRequest = ProcessFieldsRequest;
    
//    //send call to server for metadata.  metadata is queried directly using sql, not through AGS 
//    function GetMetadata(){
//		var requestMade = false;
//        if(null == self.xmlReq){
//            self.xmlReq = IMS2_createXMLHTTPRequest();
//            if(self.xmlReq){
//                var url = ServicesURL + "metadataservice.asmx/GetMetadataSDE";
//                
//                var table = self.dataset;
//                //only take the table name from dataset
//                if(table.split(".")){
//					table = table.split(".")[table.split(".").length - 1];
//                }
//                var reqXMLString = new String("<MetadataSpecs StylesheetPath=\"C:\\AGS_Share\\Metadata\\TG_FGDC.xslt\"><FeatureLayer sdeTableName=\"" + table + "\" /></MetadataSpecs>");
//                reqXMLString = reqXMLString.replace('%', '%25');
//                self.xmlReq.onreadystatechange = self.ProcessGetMetadata;
//                self.xmlReq.open("POST", url, true);
//                self.xmlReq.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
//                self.xmlReq.send("metadataspecs=" + reqXMLString);
//                requestMade = true;
//                //self.UpdateLoadingFeedback();
//            }
//        }
//        return(requestMade);    
//    }
//    this.GetMetadata = GetMetadata;
//    
//    function ProcessGetMetadata(){
//		if(self.xmlReq){
//            if( self.xmlReq.readyState == 4 )
//		    {
//			    if( self.xmlReq.status == 200 )
//			    {
//			        if(self.xmlReq.responseXML.normalize){
//			            self.xmlReq.responseXML.normalize();
//			        }
//			        var root = self.xmlReq.responseXML.documentElement;   
//			        
//			        if(root.childNodes[0])
//			        {
//						var data = null;
//			            try{
//							data = root.childNodes[0].attributes[0].value;
//						}catch(e){
//			                data = null;
//						}
//						document.getElementById("mapMetadataPane").innerHTML = data;
//			        }
//			    }else{
//			        alert("Connection error: " + self.xmlReq.status + "\n" + self.xmlReq.statusText + "\n" + self.xmlReq.responseText);
//			    }
//			    try{
//			        self.xmlReq.abort();
//			    }catch(e){;}
//			    self.xmlReq = null;
//			    //self.UpdateLoadingFeedback();
//			}
//        }
//    }
//    this.ProcessGetMetadata = ProcessGetMetadata;


}

//render layer list (TOC)
function IMS2_LayerListControl(id,mapid,refreshOnChange){
    var self = this;
    
    this.id = id;
    this.mapid = mapid;
    this.refreshOnChange = refreshOnChange;
    this.activeLayer = "";
    this.layerStyle = null;
    this.layerStyleSelected = null;
    this.groupStyle = null;
    this.imgPlus = null;
    this.imgMinus = null;
    
    
    function GetContainingDiv(){
        return(document.getElementById("_layerlistdiv_" + self.id));
    }
    this.GetContainingDiv = GetContainingDiv;
    
    function GenerateGroupExpandButton(groupid){
        var htm = "";
        htm += "<span id=\"_layerlistdiv_" + self.id + "_grpexp_" + groupid + "\" onclick=\"IMS2_ToggleLayerGroupExpandie('" + self.mapid + "','" + self.id + "','" + groupid + "');\">";
        if(self.imgMinus){
            htm += "<img src=\"" + self.imgMinus + "\" alt=\"Collapse\" style=\"margin:2px 4px 0px 2px;\"/>";
        }else{
            htm += "[-]";
        }
        htm += "</span>";
        return(htm);
    }
    this.GenerateGroupExpandButton = GenerateGroupExpandButton;
    
    function GenerateLayerGroupHTML(groupAlias,layers,groupID){
        var htm = "";
        if(layers){
            if(groupID){
                htm += "<div" + ((self.groupStyle)?(" " + ((IMS2_IsStyleString(self.groupStyle))?("style"):("class")) + "=\"" + self.groupStyle + "\""):("")) + ">" + self.GenerateGroupExpandButton(groupID) + groupAlias + "</div><div id=\"_layerlistdiv_" + self.id + "_grp_" + groupID + "\"";
            }else{
                htm += "<div";
            }
            htm += " >";
            htm += "<table width=\"100%\"  border=\"0\" cellspacing=\"0\" cellpadding=\"1\">";
            for(var i = 0;i < layers.length; i++){
                var curl = layers[i];
                if(curl){
                    htm += "<tr id=\"_layerlistdiv_" + self.id + "_row_" + curl.id + "\" "
                    //htm += ((self.layerStyle)?(" " + ((IMS2_IsStyleString(self.layerStyle))?("style"):("class")) + "=\"" + self.layerStyle + "\""):(""));
                    htm += ((self.layerStyle)?("class=\"" + self.layerStyle + "\""):(""));
                    htm += " ><td width=\"20\"><input id=\"_layerlistdiv_" + self.id + "_chk_" + curl.id + "\" type=\"checkbox\"";
                    if(curl.visible){
                        htm += " checked";
                    }
                    htm += " />";
                    htm += "</td><td onclick=\"IMS2_SetActiveLayer('" + self.mapid + "','" + curl.id + "');\" width=\"*\" >";
                    //htm += "<label for=\"_layerlistdiv_" + self.id + "_chk_" + curl.id + "\" " + ((IMS2_IsStyleString(self.layerStyle))?(""):("class=\"" + self.layerStyle + "\"")) + ">";
                    htm += ((curl.alias)?(curl.alias):(curl.name));
                    //htm += "</label>";
                    htm += "</td>";
                    htm += "</tr>";
                }
            }
            htm += "</table>";
            htm += "</div>";
            
        }
        return(htm);
    }
    this.GenerateLayerGroupHTML = GenerateLayerGroupHTML;
        
    function Update(ll){
        if(ll){
            var contDiv = self.GetContainingDiv();
            var missingRow = false;
            for(var i = 0;i < ll.layers.length;i++){
                var curl = ll.layers[i];
                if(curl){
                    if(!(document.getElementById("_layerlistdiv_" + self.id + "_chk_" + curl.id))){
                        missingRow = true;
                        break;
                    }
                }
            }
            if(missingRow){
                var layerHTMLString = "";
                var gl = ll.GetGroupedLayers();
                if(gl){
                    for(var i = 0;i < gl.length;i++){
                        var layergroup = gl[i];
                        if(layergroup){
                            if(layergroup[1].length > 0){
                                layerHTMLString += self.GenerateLayerGroupHTML(layergroup[0],layergroup[1],layergroup[2]);
                            }
                        }
                    }
                }
                contDiv.innerHTML = layerHTMLString;
            }
            for(var i = 0;i < ll.layers.length;i++){
                var curl = ll.layers[i];
                var chk = document.getElementById("_layerlistdiv_" + self.id + "_chk_" + curl.id);
                var row = document.getElementById("_layerlistdiv_" + self.id + "_row_" + curl.id);
                if(chk){
                    chk.checked = (curl.visible) ? (true) : (false);
                    chk.disabled = (curl.withinScale) ? (false) : (true);
                    if(!chk.onclick){
                        chk.onclick = self.OnUserChange;
                    }
                }
                if(row){
                    row.className = (
                    ((curl.id == self.activeLayer) && self.layerStyleSelected)
                    ?
                    (self.layerStyleSelected)
                    :
                    ((self.layerStyle)?(self.layerStyle):(""))
                    )
                }
            }
        }
    }
    this.Update = Update;
    
    function OnUserChange(){
        var ll = null;
        if(IMS2_MapManager){
            ll = IMS2_MapManager.GetLayerList(self.mapid);
            if(ll){
                for(var i = 0;i < ll.layers.length;i++){
                    var curl = ll.layers[i];
                    chk = document.getElementById("_layerlistdiv_" + self.id + "_chk_" + curl.id);
                    if(chk){
                        curl.visible = (chk.checked)?(true):(false);
                    }
                }
            }
            if(self.refreshOnChange){
                IMS2_MapManager.RefreshMap(self.mapid,null);
            }
        }
    }
    this.OnUserChange = OnUserChange;
    
    function SetActiveLayer(lid,ll){
        self.activeLayer = lid;
        if(ll){
            self.Update(ll);
        }
        
        //get layer metadata if metadata tab is open
//        if(document.getElementById("mapMetadataPane").style.display == "block"){
//			ll.Get(lid).GetMetadata();
//        }
    }
    this.SetActiveLayer = SetActiveLayer;
    
}

//we have to manage multiple layer lists because we have multiple maps - each with their own list of layers
function IMS2_LayerListControlManager(){
    var self = this;
    
    this.controls = new Array();
    this.activeLayer = "";
    
    function SetActiveLayer(lid,ll){
        self.activeLayer = lid;
        for(var i = 0;i < self.controls.length;i++){
            var curllc = self.controls[i];
            if(curllc){
                curllc.SetActiveLayer(lid,ll);
            }
        }       
    }
    this.SetActiveLayer = SetActiveLayer;
    
    function Update(ll){
        if(ll && self.controls){
            for(var i = 0;i < self.controls.length;i++){
                var curllc = self.controls[i];
                if(curllc){
                    curllc.Update(ll);
                }
            }
        }
    }
    this.Update = Update;
    
    function Add(llcid,mapid,refreshOnChange,layerStyle,layerStyleSelected,groupStyle,imgPlus,imgMinus){
        var ctrl = null;
        if(self.controls && llcid){
            var lli = IndexOf(llcid);
            if(lli < 0){
                lli = self.controls.length;
            }
            ctrl = new IMS2_LayerListControl(llcid,mapid,refreshOnChange);
            if(ctrl){
                ctrl.layerStyle = layerStyle;
                ctrl.layerStyleSelected = layerStyleSelected;
                ctrl.groupStyle = groupStyle;
                ctrl.imgPlus = imgPlus;
                ctrl.imgMinus = imgMinus;
                ctrl.SetActiveLayer(self.activeLayer,null);
                self.controls[lli] = ctrl;
            }
        }
        return(ctrl);
    }
    this.Add = Add;
    
    function Get(llcid){
        if(self.controls){
            for(var i = 0;i < self.controls.length; i++){
                var curllc = self.controls[i];
                if(curllc && (llcid == curllc.id)){
                    return(curllc);
                }
            }
        }
        return(null);
    }
    this.Get = Get;
    
    function IndexOf(llcid){
        var lli = -1;
        if(self.controls){
            for(var i = 0;i < self.controls.length; i++){
                var curllc = self.controls[i];
                if(curllc && (llcid == curllc.id)){
                    lli = i;
                    break;
                }
            }
        }
        return(lli);
    }
    this.IndexOf = IndexOf;
}

// ------------------------- FUNCTIONS

function IMS2_ToggleLayerGroupExpandie(mapid,layerlistid,groupid){
    if(layerlistid && groupid){
        var layers = document.getElementById("_layerlistdiv_" + layerlistid + "_grp_" + groupid );
        var expandie = document.getElementById("_layerlistdiv_" + layerlistid + "_grpexp_" + groupid );
        var isPlus = false;
        var imgUrl = null;
        if(layers){
            if("hidden" == layers.style["visibility"]){
                layers.style["visibility"] = "visible"; 
                layers.style["display"] = "block";
                isPlus = false;
            }else{
                layers.style["visibility"] = "hidden";
                layers.style["display"] = "none";
                isPlus = true;
            }
            if(expandie){
                var llctl = null;
                if(IMS2_MapManager){
                    llctl = IMS2_MapManager.GetLayerListControl(mapid,layerlistid);
                    if(llctl){
                        imgUrl = ((isPlus)?(llctl.imgPlus):(llctl.imgMinus));
                    }
                }
                if(imgUrl){
                    expandie.innerHTML = "<img src=\"" + imgUrl + "\" alt=\"" + ((isPlus)?("Expand"):("Collapse")) + "\" style=\"margin:2px 4px 0px 2px;\"/>";
                }else{
                    expandie.innerHTML = ((isPlus)?("[+]"):("[-]"));
                }
            }
        }
    }
}

function IMS2_RegisterLayerListControl(llcid,mapid,refreshOnChange,layerStyle,layerStyleSelected,groupStyle,imgPlus,imgMinus){
    if(mapid && llcid && IMS2_MapManager){
        if(!IMS2_MapManager.AttachLayerListControlToMap(llcid,mapid,refreshOnChange,layerStyle,layerStyleSelected,groupStyle)){
            if(IMS2_RegisterLayerListControlPool){
                var tuple = [llcid,mapid,refreshOnChange,layerStyle,layerStyleSelected,groupStyle,imgPlus,imgMinus];
                IMS2_RegisterLayerListControlPool[IMS2_RegisterLayerListControlPool.length] = tuple;
            }
        }
    }
}

function IMS2_DelayRegisterLayerControls(){
    if(IMS2_RegisterLayerListControlPool && IMS2_MapManager){
        for(var i = 0;i < IMS2_RegisterLayerListControlPool.length; i++){
            var tpl = IMS2_RegisterLayerListControlPool[i];
            IMS2_MapManager.AttachLayerListControlToMap(tpl[0],tpl[1],tpl[2],tpl[3],tpl[4],tpl[5],tpl[6],tpl[7]);
        }
        IMS2_RegisterLayerListControlPool = new Array();
    }
}

// ------------------------- GLOBALS

var IMS2_RegisterLayerListControlPool = new Array();

