// <![CDATA[


/***************************
 * GMapLegend.js
 *
 * Created by Jennifer Bowen 2008-04-02
 * Last updated 2008-04-02
 * 
 * Added functionality for the GMapWrapper class.
 * Provides a legend that can be automatically
 * populated from the list of layers or manually
 * set.  Layers can be optionally be toggled from
 * the legend. If arranged manually, legend layers
 * can be categorized heirarchically.
 *
 ****************************/
function GMapLegend(objmap) {
    this.objmap = objmap;
    this.setMoveable(false);
    this.setMutable(false);
    this.setToggleable(true);
    this.setHierarchicalToggle(false);
    this.legendtree = null;
    this.legendlock = 1;
    this.legendLeftOffset = objmap.legendLeftOffset;
    this.legendRightOffset = objmap.legendRightOffset;
    this.legendTopOffset = objmap.legendTopOffset;
    this.legendBottomOffset = objmap.legendBottomOffset;
    this.initLegend();
}

GMapLegend.prototype.hideLegendLayers = function(i) {
    if ( i > this.flatlist.length ) return;

    var legendLayer = document.getElementById("layer_" + i);
    if ( this.flatlist[i].layer == null ) {
        for ( j = i + 1; j < this.flatlist.length; j++ ) {
            if ( this.flatlist[i].level == this.flatlist[j].level) {
                break;
            }

            var curlayer = this.flatlist[j].layer;
            if ( curlayer == null ) {
                checkbox = document.getElementById("layer_" + j);
                checkbox.checked = false;
            } else {
                if ( this.objmap.layerIsShown(curlayer) ) {
                    this.objmap.hideLayer(curlayer);
                    checkbox = document.getElementById("layer_" + j);
                    checkbox.checked = false;
                }
            }
        }
    } else {
        if ( this.objmap.layerIsShown(this.flatlist[i].layer) ) {
            this.objmap.hideLayer(this.flatlist[i].layer);
        }
    }
    legendLayer.checked = false;
}

GMapLegend.prototype.showLegendLayers = function(i) {
    if ( i > this.flatlist.length ) return;

    var legendLayer = document.getElementById("layer_" + i);
    if ( this.flatlist[i].layer == null ) {
        for ( j = i + 1; j < this.flatlist.length; j++ ) {
            if ( this.flatlist[i].level == this.flatlist[j].level) {
                break;
            }

            var curlayer = this.flatlist[j].layer;
            if ( curlayer != null ) {
                if ( ! this.objmap.layerIsShown(curlayer) ) {
                    this.objmap.showLayer(curlayer);
                    checkbox = document.getElementById("layer_" + j);
                    checkbox.checked = true;
                }
            } else {
                checkbox = document.getElementById("layer_" + j);
                checkbox.checked = true;
            }
        }
    } else {
        if ( ! this.objmap.layerIsShown(this.flatlist[i].layer) ) {
            this.objmap.showLayer(this.flatlist[i].layer);
        }
    }

    legendLayer.checked = true;
}

GMapLegend.prototype.addLegendLayer = function() {
}

GMapLegend.prototype.removeLegendLayer = function() {
}

GMapLegend.prototype.deselectRadioLayer = function(id) {
    var obj = document.getElementById(id); 
    var lobj = this;
    if ( obj.fireEvent ) {
        obj.fireEvent('onclick');
        obj.checked = true;
    } else {
        var objevent = document.createEvent('MouseEvents');
        objevent.initEvent('click',true,true);
        obj.dispatchEvent(objevent);
    }
}

GMapLegend.prototype.moveLegendLayer = function(layer,position) {
    var moveablearea = this.div_.getElementsByTagName('div')[0];

    if ( layer == position ) return;

    if ( position < 0 ) position = 0;
    if ( position > moveablearea.length ) position = movablearea.length-1;

    var node = null;

    for ( var i = 0; i < moveablearea.childNodes.length; i++ ) {
        if ( layer == moveablearea.childNodes[i].legendlayer ) {
            node = moveablearea.removeChild(moveablearea.childNodes[i]);
            break;
        }
    }

    if ( node == null ) return;

    for ( var i = 0; i < moveablearea.childNodes.length; i++ ) {
        if (  position == moveablearea.childNodes[i].legendlayer ) {
            moveablearea.insertBefore(node,moveablearea.childNodes[i]);
            break;
        }
    }

    var flatrep = this.flatlist.slice(layer,layer+1);
    this.flatlist.splice(position,1,flatrep[0]);

    if ( position <= 1 ) {
        var newlevel = 0;
    } else {
        var newlevel = this.flatlist[position+1].level;
    }

    node.firstChild.className = "level_" + newlevel;

    for ( var i = 0; i < moveablearea.childNodes.length; i++ ) {
        moveablearea.childNodes[i].className = "layer_" + i;
        moveablearea.childNodes[i].legendlayer = i;
        moveablearea.childNodes[i].legendlayer = i;
    }

}

GMapLegend.prototype.isLegendLayerShown = function(i) {
    var toggleStatus = true;
    for ( var j = i + 1; j < this.flatlist.length; j++ ) {
        if ( this.flatlist[i].level == this.flatlist[j].level) {
            break;
        }
        toggleStatus = toggleStatus && this.flatlist[j].shown;
    }

    return toggleStatus;
}


GMapLegend.prototype.setMoveable = function(moveability) {
    this.isMoveable = moveability;
}

GMapLegend.prototype.setMutable = function(mutability) {
    this.isMutable = mutability;
}

GMapLegend.prototype.setHierarchicalToggle = function(htoggle) {
    this.isHierarchicalToggleable = htoggle;
}

GMapLegend.prototype.setToggleable = function(toggleable) {
    this.isToggleable = toggleable;
}

GMapLegend.prototype.setToggleable = function(toggleable) {
    this.isToggleable = toggleable;
}

GMapLegend.prototype.onLayerChange = function(legendlayer,layernum,id) {
}

GMapLegend.prototype.initLegend = function() {
    var outer = document.getElementsByTagName("body")[0];
    var posref = document.getElementById(this.objmap.mapid);

    if ( ! outer || ! posref ) {
        return;
    }
    var pos = findPos(posref);

    this.div_ = document.getElementById("MapLegend");
    if ( ! this.div_ ) {
        this.div_ = document.createElement("div");
        this.div_.id = "MapLegend";
        this.div_.className="floating";
        outer.appendChild(this.div_);
    }

    if ( this.div_.className == "floating" ) {
        if ( this.legendLeftOffset != null ) 
            this.div_.style.left = (pos[0] + parseInt(this.legendLeftOffset)) + "px";
        else 
            this.div_.style.right = (outer.offsetWidth - pos[0] - posref.offsetWidth + parseInt(this.legendRightOffset)) + "px";

        if ( this.legendBottomOffset != null ) 
            this.div_.style.bottom = (outer.offsetHeight - pos[1] - posref.offsetHeight + parseInt(this.legendBottomOffset)) + "px";
        else 
            this.div_.style.top = (pos[1] + parseInt(this.legendTopOffset)) + "px";
    }

    if ( this.objmap.showLegendButton ) {
        this.createLegendButton();
    }

}


GMapLegend.prototype.addLegendEntries = function() {
    while (this.div_.hasChildNodes()) {
        this.div_.removeChild(legend.firstChild);
    }

    var obj = this;
    var style = 'default';

    var tbodywrap = document.createElement("div");
    this.div_.appendChild(tbodywrap);

    this.flatlist = this.getListFromLayers();
    var lastlevel = null;
    var category = "top";
    var parent_lid = "group_0";
    var radionone = null;
    var category = "top";

    var maxIconWidth = 0;
    for ( var j = 0; j < this.flatlist.length; j++ ) {
        if ( this.flatlist[j].icon ) {
            //var is = eval("this.objmap.styles." + this.flatlist[j].icon); 
            var is = this.objmap.styles[this.flatlist[j].icon];
            if ( is && is.normal != null ) is = this.objmap.styles[is.normal];
            if ( (is != null ) && (is.icon!=null) ) { 
                iw = is.icon.iconSize; 
                maxIconWidth = Math.max(maxIconWidth,iw.width);
            }
        }
    }

    for ( var j = 0; j < this.flatlist.length; j++ ) {
        var legendrow = document.createElement("div");
        var wrappercol = document.createElement("div");

        var cn = "";
        var icon = null;
        var i = this.flatlist[j].layer;
        var level = this.flatlist[j].level;
        if ( level != lastlevel ) {
            cn = " grouphead";
            category = legendlabel;
            parent_lid = "group_" + category;
        }
        var legendlabel = this.flatlist[j].label;
        var shown = this.flatlist[j].shown;
        if ( typeof(i) == "undefined" || i === null ) {
            if ( this.flatlist[j].layer !== null ) {
              this.flatlist[j].shown = this.isLegendLayerShown(j);
            }
            shown = this.flatlist[j].shown;
            legendrow.className="row category" + cn;
            var entrytype = "checkbox";
        } else {
            style = this.objmap.layers[i].legendIconStyle;
            if ( this.objmap.styles[style] && this.objmap.styles[style].normal != null ) {
                style = this.objmap.styles[style].normal;
            } 
            if (this.objmap.styles[style])
                var entrytype = this.objmap.styles[style].listtype;
            else
                var entrytype = "checkbox";
            legendrow.className="row";
            obj.objmap.layers[i].legendlayer = j;
        }

        wrappercol.className="col level_" + level;

        shown = shown && ( this.objmap.callCount == 1 );

        legendrow.id = "ind_" + idSafeLabel(legendlabel);

        legendrow.appendChild(wrappercol);
        if ( this.isMutable ) {
            GEvent.addDomListener(legendrow,"mousedown",function(event) {
                if ( ! this.parentNode.isMoving ) {
                    this.parentNode.isMoving=true;
                    this.parentNode.movingrow=this.legendlayer;
                    this.parentNode.className = "moving";
                    this.className += " activerow";
                }
            } );
            GEvent.addDomListener(legendrow,"mouseup",function(event) {
                if ( this.parentNode.isMoving ) {
                    this.parentNode.className = "";
                    this.parentNode.isMoving=false;
                    var oldrow = this.parentNode.childNodes[this.parentNode.movingrow];
                    oldrow.className = oldrow.className.replace(/ activerow/,"");
                    obj.moveLegendLayer(this.parentNode.movingrow,this.legendlayer);
                }
            } );
        }
  
        if ( this.flatlist[j].icon && this.flatlist[j].type != "site" ) {
            //var iconstyle = eval("this.objmap.styles." + this.flatlist[j].icon);
            var iconstyle = this.objmap.styles[this.flatlist[j].icon];
            if ( iconstyle && iconstyle.normal != null )
                iconstyle = this.objmap.styles[iconstyle.normal];
            if ( iconstyle && iconstyle.icon && iconstyle.icon.image ) {
                var src = iconstyle.icon.image;
                var iw = iconstyle.icon.iconSize; 
                if ( (this.objmap.isIE && ! this.objmap.isIE7) && src.match(/.png$/)) {
                    // Fake alpha transparency in older IE
                    var icon = document.createElement("img");
                    icon.style.display = 'inline';
                    icon.style.border = 'none';
                    icon.src="/images/spacer.gif";
                    icon.style.backgroundImage = 'url(/images/spacer.gif)';
                    icon.height = iw.height + "px";
                    icon.style.height = iw.height + "px";
                    icon.style.lineHeight = iw.height + "px";
                    icon.width = iw.width + "px";
                    icon.style.width = iw.width + "px";

                    icon.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
                } else {
                    var icon = document.createElement("img");
                    icon.style.marginRight = "2px";
                    icon.style.height = iw.height + "px";
                    icon.style.width = iw.width + "px";
                    icon.src = src;
                }
                var lpadding = Math.floor((maxIconWidth-iw.width)/2 ) + 1;
                var rpadding = Math.ceil((maxIconWidth-iw.width)/2 ) + 4;
                icon.style.marginLeft=lpadding + "px";
                icon.style.marginRight=rpadding + "px";
                icon.style.verticalAlign="middle";
                wrappercol.appendChild(icon);
            }
        }
             
        if ( this.flatlist[j].type == "site" ) {
            var eltype = "a";
        } else {
            var eltype = "span";
        }
        var ltext = document.createElement(eltype);
        ltext.innerHTML = legendlabel;
        if ( this.flatlist[j].type != "site" && this.objmap.showLegendHelp && this.flatlist[j].helplink ) {
            var anchor = document.createElement("a");
            anchor.href="#";
            anchor.helplink = this.flatlist[j].helplink;
            anchor.innerHTML = "?"
            ltext.innerHTML += " ";
            ltext.appendChild(anchor);
            GEvent.addDomListener(anchor,"click",function(event) {
                obj.showHelpDialog(event,this.helplink);
                if ( event.preventDefault ) {
                  event.preventDefault();
                }
                if ( typeof(event.stopPropagation) != "undefined" ) {
                    event.stopPropagation();
                } else {
                    event.cancelBubble=true;
                }
                event.returnValue=false;
            });
        }
        ltext.className = "legendText";
        ltext.style.verticalAlign="middle";
        ltext.style.paddingRight="6px";

        if ( this.flatlist[j].type == "site" ) {
            ltext.layer = this.flatlist[j].layer;
            ltext.sitename = this.flatlist[j].label;
            ltext.href="#";
            ltext.name = "sites";
            ltext.className += "_site";
            var mapobj = this.objmap;


            var onItemSelect = function(obj) {
                var sitegroups = document.getElementsByName("sites");
                for (var k = 0; k < sitegroups.length; k++ ) {
                    var ce = sitegroups[k];
                    if ( ce.innerHTML == obj.fullname ) {
                        ce.className = ce.className.replace(/_selected/g,"");
                        ce.className += "_selected";
                    } else {
                        ce.className = ce.className.replace(/_selected/g,"");
                    }
                }
                if ( ! mapobj.iwcloseevent ) {
                    GEvent.addListener(mapobj.objMap,"infowindowclose",function(event) {
                       var sg = document.getElementsByName("sites");
                        for (var n = 0; n < sg.length; n++ ) {
                            sg[n].className = sg[n].className.replace(/_selected/g,"");
                        }
                     });
                     GEvent.addListener(mapobj.objMap,"mapclick",function(event) {
                       var sg = document.getElementsByName("sites");
                        for (var n = 0; n < sg.length; n++ ) {
                            sg[n].className = sg[n].className.replace(/_selected/g,"");
                        }
                     });
                     mapobj.iwcloseevent = true;
                }
            };

            GEvent.addListener(this.objmap,'itemselected',onItemSelect);

            GEvent.addDomListener(ltext,"click",function(event) {
                if ( event.preventDefault ) {
                  event.preventDefault();
                }
                if ( event.stopPropagation ) {
                    event.stopPropagation();
                } else {
                    event.cancelBubble=true;
                }
                event.returnValue=false;
                onItemSelect({fullname:this.sitename});
                mapobj.openSiteWindowFromSidebar(this.sitename);
            });
        }


        wrappercol.appendChild(ltext);

        if ( this.flatlist[j].type != "label" && this.flatlist[j].type != "site" && this.isToggleable ) {
            var cid = "layer_" + i;
            if ( document.all ) {
                if ( shown ) {
                    var checked = "checked";
                } else {
                    var checked = "";
                }
                if ( entrytype == "radio" ) {
                    var namestr = 'name="' + parent_lid + '" ';
                } else {
                    var namestr = "";
                }
                var checkbox = document.createElement('<input ' + namestr + 'id="' + cid + '" type="' + entrytype + '" ' + checked + '>');
            } else {
                var checkbox = document.createElement("input");
                checkbox.id = cid;
                if ( entrytype == "radio" ) checkbox.name = parent_lid;
                checkbox.type = entrytype;
            }

            if ( i != null ) {
                var cid = "layer_" + i;
                legendrow.legendlayer = j;
                checkbox.legendlayer = j;
                checkbox.layer = i;
                lobj = this;
                if ( this.flatlist[j].callback != null ) {
                    var onclickAction = this.flatlist[j].callback;
                } else {
                    var onclickAction = function() {
                        var target = this;
                        var layernum = target.legendlayer;

                        if ( lobj.legendlock > 0  ) {
                            lobj.legendlock -= 1;
                            if ( lobj.objmap.showLoading ) {
                                lobj.objmap.objMessage.setMessage("Loading, please wait ...");
                                lobj.objmap.objMessage.show();
                            }
                            style = lobj.objmap.layers[target.layer].legendIconStyle;
                            if ( lobj.objmap.styles[style] && lobj.objmap.styles[style].normal != null ) {
                                style = lobj.objmap.styles[style].normal;
                            } 
                            if ( lobj.objmap.styles[style].listtype == "radio" ) {
                                var siblings = document.getElementsByName(target.name);
                                for ( var m = 0; m < siblings.length; m++ ) {
                                    if ( siblings[m].layer != null ) {
                                        if ( lobj.objmap.layerIsShown(siblings[m].layer) ) {
                                            style = lobj.objmap.layers[siblings[m].layer].legendIconStyle;
                                            if ( lobj.objmap.styles[style] && lobj.objmap.styles[style].normal != null ) {
                                                style = lobj.objmap.styles[style].normal;
                                            } 
                                            if ( lobj.objmap.styles[style].listtype == "radio" ) {
                                                lobj.objmap.hideLayer(siblings[m].layer);
                                            }
                                        }
                                    }
                                }
                                lobj.objmap.showLayer(target.layer);
                            } else {
                                if ( lobj.objmap.layerIsShown(target.layer) ) {
                                    lobj.objmap.hideLayer(target.layer);
                                    lobj.flatlist[target.legendlayer].shown = false;
                                    target.parentNode.parentNode.className="unmarked";
                                } else {
                                    lobj.objmap.showLayer(target.layer);
                                    lobj.flatlist[target.legendlayer].shown = true;
                                    target.parentNode.parentNode.className="";
                                }
                            }
                            lobj.onLayerChange(target.legendlayer,target.layer,this.id);
                            lobj.objmap.objMessage.hide();
                            lobj.legendlock += 1;
                        }
                    };
                }
            } else {
                checkbox.legendlayer = j;
                legendrow.legendlayer = j;
                
                if ( this.flatlist[j].callback != null ) {
                    var onclickAction = this.flatlist[j].callback;
                } else {

                    var onclickAction = function () {
                        if ( obj.legendlock > 0  ) {
                            obj.legendlock -= 1;
                            var target = this;
                            var layernum = target.legendlayer;
                            style = obj.objmap.layers[target.layer].legendIconStyle;
                            if ( obj.objmap.styles[style] && obj.objmap.styles[style].normal != null ) style = obj.objmap.styles[style].normal;
                            if ( obj.objmap.styles[style].listtype == "radio" ) {
                                var siblings = document.getElementsByName(target.name);
                            } else {
                                if ( obj.flatlist[target.legendlayer].shown == true ) {
                                    obj.hideLegendLayers(target.legendlayer);
                                    obj.flatlist[target.legendlayer].shown = false;
                                } else {
                                    obj.showLegendLayers(target.legendlayer);
                                    obj.flatlist[target.legendlayer].shown = true;
                                }
                            }
                            obj.onLayerChange(target.legendlayer,target.layer,this.id);
                            obj.legendlock += 1;
                        }
                    };
                }
            }
            checkbox.style.verticalAlign="middle";
            wrappercol.insertBefore(checkbox,wrappercol.firstChild);

            GEvent.addDomListener(checkbox,'click',onclickAction);

            if ( !document.all && shown ) {
                checkbox.setAttribute("checked","checked");
                legendrow.className="";
            } else {
                legendrow.className="unmarked";
            }
        }
        if ( entrytype == "radio" ) {
            if ( ! radionone ) {
                var radionone = document.createElement("div");
                var radiocol = document.createElement("div");
                radiocol.className="level_" + level;
                
                var ltext = document.createElement("span");
                ltext.innerHTML = "Hide " + category;
                ltext.className = "legendText";
                ltext.style.verticalAlign="middle";
                ltext.style.paddingRight="2px";
                radiocol.appendChild(ltext);
                
                cid = parent_lid + "_deselect";
                if ( document.all ) {
                    var checked = "";
                    var checkbox = document.createElement('<input name="' + parent_lid + '" id="' + cid + '" type="' + entrytype + '" ' + checked + '>');
                } else {
                    var checkbox = document.createElement("input");
                    checkbox.id = cid;
                    checkbox.name = parent_lid;
                    checkbox.type = entrytype;
                }

                checkbox.legendlayer = null;
                checkbox.layer = null;
                lobj = this;
                var onclickAction = function () {
                    var target = this;
                    var parent = target.parentNode;
                    var layernum = target.legendlayer;

                    if ( lobj.legendlock > 0 ) {
                        lobj.legendlock -= 1;
                        if ( lobj.objmap.showLoading ) {
                            lobj.objmap.objMessage.setMessage("Loading, please wait ...");
                            lobj.objmap.objMessage.show();
                        }
                        var siblings = document.getElementsByName(target.name);
                        for ( var m = 0; m < siblings.length; m++ ) {
                            if ( siblings[m].layer !== null && lobj.objmap.layerIsShown(siblings[m].layer) ) {
                                style = lobj.objmap.layers[siblings[m].layer].legendIconStyle;
                                if ( lobj.objmap.styles[style] && lobj.objmap.styles[style].normal != null ) style = lobj.objmap.styles[style].normal;
                
                                if ( lobj.objmap.styles[style].listtype == "radio" ) {
                                    lobj.objmap.hideLayer(siblings[m].layer);
                                }
                            }
                        }
                        lobj.onLayerChange(target.legendlayer,target.layer,target.id);
                        lobj.objmap.objMessage.hide();
                        lobj.legendlock += 1;
                    }
                };

                checkbox.style.verticalAlign="middle";
                radiocol.insertBefore(checkbox,radiocol.firstChild);

                if ( document.all ) {
                     checkbox.attachEvent('onclick',onclickAction);
                } else {
                    checkbox.addEventListener('click',onclickAction,false);
                }
                radionone.appendChild(radiocol);
                tbodywrap.appendChild(radionone);
            }
            tbodywrap.insertBefore(legendrow,radionone);
        } else {
            radionone = null;
            tbodywrap.appendChild(legendrow);
        }
        lastlevel = level;
    } 


    /* Add the header information last so horizontal centering works */
    if ( document.all ) {
        var header = document.createElement('<div class="legendHeader">');
    } else {
        var header = document.createElement("div");
        header.className = "legendHeader";
    }
    header.innerHTML = "Legend";

    var headerwrap = document.createElement("div");
    var headerrow = document.createElement("div");
    

    headerrow.appendChild(header);
    headerwrap.appendChild(headerrow);
    this.div_.insertBefore(headerwrap,tbodywrap);
    GEvent.trigger(this.objmap,'legendloaded');
}


/***************************
 * GMapLegend.getListFromLayers()
 * 
 * Returns a flattened list of objects containing labels, level, and
 * corresponding layer.
 ****************************/
GMapLegend.prototype.getListFromLayers = function(rootnode,level) {
    if ( rootnode == null ) {
        rootnode = this.legendtree;
    }

    if ( level == null ) {
        level = 0;
    }

    if ( this.legendtree == null ) {
        var legendentries = new Array();
        for ( var i = 0; i < this.objmap.layers.length; i++ ) {
           if ( this.objmap.layers[i].abbreviation != null && this.objmap.layers[i].level < this.objmap.maxLegendDepth ) {
               var entry = new Object();
               entry.label = this.objmap.layers[i].abbreviation;
               entry.level = 1;
               entry.layer = i;
               entry.type = "layer";
               entry.icon = this.objmap.layers[i].legendIconStyle;
               if ( this.objmap.styles[entry.icon] && this.objmap.styles[entry.icon].normal != null ) 
                   entry.icon = this.objmap.styles[entry.icon].normal;
               

               entry.shown = this.objmap.layers[i].shown || this.objmap.layerIsShown(i);
               legendentries.push(entry);

               if ( this.siteChooserInLegend ) {
                  for ( var j = 0; j < this.objmap.layers[i].placemarks.length; j++ ) {
                      var entry = new Object();
                      entry.label = this.objmap.layers[i].placemarks[j].abbreviation;
                      entry.level = 2;
                      entry.layer = i;
                      entry.icon = this.objmap.layers[i].placemarks[j].style;
                      if ( this.objmap.styles[entry.icon] && this.objmap.styles[entry.icon].normal != null ) 
                          entry.icon = this.objmap.styles[entry.icon].normal;
                      entry.shown = this.objmap.layers[i].shown || this.objmap.layerIsShown(i);
                      entry.type = "site";
                      legendentries.push(entry);
                  }
               }
           }
        }
        return legendentries;
    }

    var legendentries = new Array();
    var entry = null;
    if (rootnode.name != null) {
        var entry = new Object();
        entry.level = level;
        entry.label = rootnode.name;
        entry.shown = rootnode.shown
        entry.icon = rootnode.icon;
        if ( this.objmap.styles[entry.icon] && this.objmap.styles[entry.icon].normal != null ) 
            entry.icon = this.objmap.styles[entry.icon].normal;
        entry.layer = this.objmap.getLayerWithLabel(rootnode.name);
        entry.type = rootnode.type;
        if ( rootnode.callback != null ) {
            entry.callback = rootnode.callback;
            entry.shown = rootnode.shown;
            //entry.shown = typeof(rootnode.shown)=="undefined"?true:rootnode.shown;
            legendentries.push(entry);
        } else {
            if ( entry.layer != null ) {
                var lnum = entry.layer;
                entry.helplink = this.objmap.layers[entry.layer].helplink;
                entry.shown = this.objmap.layers[lnum].shown;
                entry.type = "layer";
                if (entry.icon == null) {
                    entry.icon = this.objmap.layers[lnum].legendIconStyle;
                    if ( this.objmap.styles[entry.icon] && this.objmap.styles[entry.icon].normal != null ) 
                        entry.icon = this.objmap.styles[entry.icon].normal;
                }
                legendentries.push(entry);
            } else {

                // Create site-specific navigation
                if ( rootnode.parent ) {
                    var entry = null;
                    var  p = rootnode.parent.name;
                    var lnum = this.objmap.getLayerWithLabel(p);
                    if ( lnum != null && lnum > -1 ) {
                        var l = this.objmap.layers[lnum];
                        for ( var j = 0; j < l.placemarks.length; j++ ) {
                            if ( l.placemarks[j].abbreviation == rootnode.name ) {
                                var entry = new Object();
                                entry.label = l.placemarks[j].abbreviation;
                                entry.layer = lnum;
                                entry.level = level;
                                entry.type = "site";
                                entry.icon = this.objmap.layers[lnum].placemarks[j].style;
                                if ( this.objmap.styles[entry.icon] && this.objmap.styles[entry.icon].normal != null ) {
                                    entry.icon = this.objmap.styles[entry.icon].normal;
    }

                                entry.shown = this.objmap.layers[lnum].shown || this.objmap.layerIsShown(lnum);
                                legendentries.push(entry);
                                continue;
                            }
                        }
                    }
                } else {
                    entry = null;
                    //legendentries.push(entry);
                }
            }
        }
    }

    if ( entry == null && rootnode.name ) {
        var entry = new Object();
        entry.level = level;
        entry.label = rootnode.name;
        entry.icon = rootnode.icon;
        if ( this.objmap.styles[entry.icon] && this.objmap.styles[entry.icon].normal != null ) 
            entry.icon = this.objmap.styles[entry.icon].normal;
        entry.shown = false;
        entry.type = "label";
        legendentries.push(entry);
    }
  
    for ( var i = 0; i < rootnode.descendents.length; i++ ) {
        if ( rootnode.descendents[i] != null ) {
            var entries = this.getListFromLayers(rootnode.descendents[i],level+1);
            for ( var j = 0; j < entries.length; j++ ) {
                legendentries.push(entries[j]);
            }
        }
    }
    return legendentries;
}

/***************************
 * GMapLegend.addLegendItem()
 * 
 * Creates an index entry with the specified text and heirarchy.
 ****************************/
GMapLegend.prototype.addLegendItem = function(parentlabel,legendtext,layerlabel,icon,type,callback,shown) {
    if ( this.legendtree == null ) {
        this.legendtree = new Object();
        this.legendtree.descendents = new Array();
    }
    if ( parentlabel == null ) {
        var parent = this.legendtree;
    } else {
        var parent = findNodeWithLabel(this.legendtree,parentlabel);
    }
    if ( parent == null ) {
        alert("Could not find node with label " + parent)
    }

    var legendnode = new Object();
    legendnode.name = legendtext;
    legendnode.layer = layerlabel;
    legendnode.parent = parent;
    if ( callback !== null ) {
        legendnode.type = type;
        legendnode.callback = callback;
        legendnode.shown = shown;
    }
    if ( ! legendnode.parent.name ) legendnode.parent = null;
    legendnode.descendents = new Array();
    if (icon != null) legendnode.icon = icon; 
    parent.descendents.push(legendnode);
}

GMapLegend.prototype.showHelpDialog = function(event,url) {
    var helptext = getRemoteText(url);
    if ( ! this.helpdialog ) {
        var legend = this;
        var parent = document.getElementById("Content");
        if ( ! parent ) return;
        var helpdialogwrapper = document.createElement("div");
        var closebutton = document.createElement("a");
        var helpdialog = document.createElement("div");
 
        helpdialogwrapper.id = "helpdialog"; 
        var lt = getEventTopLeft(event);
        helpdialogwrapper.style.left = lt[0] + "px";
        helpdialogwrapper.style.top = lt[1] + "px";
        closebutton.href="#";
        closebutton.id = "closebutton";
        parent.appendChild(helpdialogwrapper);
        helpdialogwrapper.appendChild(closebutton);
        helpdialogwrapper.appendChild(helpdialog);
        closebutton.innerHTML = "X";
        GEvent.addDomListener(closebutton,"click", function (event) {
            this.div_.hideHelpDialog();
        });

        this.objmap.mbl.addBlock("helpdialog");

        this.helpdialog = helpdialog; 
    }
    if ( helptext ) {
        this.helpdialog.innerHTML = helptext;
        this.helpdialog.parentNode.className = "shown";
    }
}

GMapLegend.prototype.hideHelpDialog = function() {
    this.helpdialog.parentNode.className = "";
    this.helpdialog.innerHTML = "";
}


/***************************
 * GMapLegend.createLegendButton
 * 
 * Creates a legend button if it doesn't already
 * exist and legend has been enabled.
 ****************************/
GMapLegend.prototype.createLegendButton = function() {
    var lbutton = document.getElementById("LegendButton");
    if ( lbutton ) {
        var obj = this;
        GEvent.clearInstanceListeners(lbutton);
        GEvent.addDomListener(lbutton, "click", function() {
            obj.toggleLegend();
        });
        return lbutton;
    }

    var gbuttons = document.getElementById("GButtons");
    if ( ! gbuttons ) {
        var gbuttons = document.createElement("div");
        gbuttons.id = "GButtons";
        var wrapper = document.getElementById(this.objmap.mapid);
        if ( ! wrapper ) {
            return null;
        }
        var message = document.getElementById("modalblocker");
        if ( message ) {
          wrapper.insertBefore(gbuttons,message);
        } else {
          wrapper.appendChild(gbuttons);
        }
    }
   
    var outer = null;
    var children = gbuttons.childNodes;
    for ( var i = 0; i < children.length; i++ ) {
        if ( children[i].className == "outer" ) {
            outer = children[i];
        }
    }

    outer = document.createElement("div");
    outer.className="outer"
    gbuttons.appendChild(outer);
    
    var lbutton = document.getElementById("LegendButton");
    if ( ! lbutton ) {
        lbutton = document.createElement("div");
        lbutton.id = "LegendButton";
        lbutton.setAttribute("title","Show legend");
        lbutton.className = "inner";
        lbutton.innerHTML = "Legend";
        outer.appendChild(lbutton);
    }

    var obj = this;
    GEvent.clearInstanceListeners(lbutton);
    GEvent.addDomListener(lbutton, "click", function() {
        obj.toggleLegend();
    });
    this.hideLegend(); 
    return lbutton;
}

/***************************
 * GMapLegend.toggleLegend()
 * 
 * Shows or hides the legend if it is enabled.
 ****************************/
GMapLegend.prototype.toggleLegend = function() { 
    if ( ! this.objmap.showLegendButton ) {
        return;
    }
    if(!this.div_) return;

    var b = document.getElementById("LegendButton");
    if ( b.className.match(/ selected/) ) {
        this.hideLegend();
    } else {
        this.showLegend();
    }
}    

GMapLegend.prototype.showLegend = function() { 
    var b = document.getElementById("LegendButton");
    if ( b ) b.className = b.className + ' selected';
    //this.div_.className = 'shown'
    this.div_.style.display = "block";
}

GMapLegend.prototype.hideLegend = function() { 
    var b = document.getElementById("LegendButton");
    if ( b ) {
        b.className = b.className.replace(/ selected/,'');
    }
    //this.div_.className = "hidden ";
    this.div_.style.display = "none";
}


// ]]>
