// We define the function first
function LegendControl() {
}

// To "subclass" the GControl, we set the prototype object to
// an instance of the GControl object
LegendControl.prototype = new GControl();

// Creates a one DIV for each of the buttons and places them in a container
// DIV which is returned as our control element. We add the control to
// to the map container and return the element for the map class to
// position properly.
LegendControl.prototype.initialize = function(map) {
    var container, szColor, oElement;

    // apply the legend colors : TRAILS
    // NB: due to the fact that "backgroundColor" is not printable by default
    // in all browsers, simples <image> were added to replace this portion.
/*
    szColor = options['styles'][0][1]['color'];
    oElement = document.getElementById("legendTQColor");
    oElement.style.backgroundColor = szColor;

    szColor = options['styles'][0][2]['color'];
    oElement = document.getElementById("legendRGColor");
    oElement.style.backgroundColor = szColor;

    szColor = options['styles'][0][3]['color'];
    oElement = document.getElementById("legendLCColor");
    oElement.style.backgroundColor = szColor;

    szColor = options['styles'][0][4]['color'];
    oElement = document.getElementById("legendFEColor");
    oElement.style.backgroundColor = szColor;
*/

    // apply the legend colors : TRAILS CONDITIONS
/*    szColor = options['styles'][1][1]['color'];
    oElement = document.getElementById("legendGroomers1Color");
    oElement.style.backgroundColor = szColor;

    szColor = options['styles'][1][2]['color'];
    oElement = document.getElementById("legendGroomers2Color");
    oElement.style.backgroundColor = szColor;

    szColor = options['styles'][1][3]['color'];
    oElement = document.getElementById("legendGroomers3Color");
    oElement.style.backgroundColor = szColor;

    szColor = options['styles'][1][4]['color'];
    oElement = document.getElementById("legendGroomers4Color");
    oElement.style.backgroundColor = szColor;*/

    // groomers
        szImage = options['markers'][5][5]['image']['image'];
        oElement = document.getElementById("legendGroomerImage");
        oElement.src = szImage;

    // apply the legend icons : Services
    if (map.main.enableMoreLayers) {
        szImage = options['markers'][4][1]['image']['image'];
        oElement = document.getElementById("legendHoSImage");
        oElement.src = szImage;

        szImage = options['markers'][4][2]['image']['image'];
        oElement = document.getElementById("legendHebImage");
        oElement.src = szImage;

        szImage = options['markers'][4][3]['image']['image'];
        oElement = document.getElementById("legendResImage");
        oElement.src = szImage;

        szImage = options['markers'][4][4]['image']['image'];
        oElement = document.getElementById("legendLocImage");
        oElement.src = szImage;

        szImage = options['markers'][4][5]['image']['image'];
        oElement = document.getElementById("legendRepImage");
        oElement.src = szImage;

        szImage = options['markers'][4][6]['image']['image'];
        oElement = document.getElementById("legendEssImage");
        oElement.src = szImage;

        szImage = options['markers'][4][7]['image']['image'];
        oElement = document.getElementById("legendBarImage");
        oElement.src = szImage;

        szImage = options['markers'][4][20]['image']['image'];
        oElement = document.getElementById("legendArtImage");
        oElement.src = szImage;

        szImage = options['markers'][4][21]['image']['image'];
        oElement = document.getElementById("legendPolImage");
        oElement.src = szImage;

        szImage = options['markers'][4][22]['image']['image'];
        oElement = document.getElementById("legendBrpImage");
        oElement.src = szImage;

        szImage = options['markers'][4][23]['image']['image'];
        oElement = document.getElementById("legendYamImage");
        oElement.src = szImage;
    }

    // append the legend div in the map
    container = document.createElement("div");
    this.legendDiv = document.getElementById("legendDiv");
    container.appendChild(this.legendDiv);

    map.getContainer().appendChild(container);
    return container;
};

// By default, the control will appear in the top left corner of the
// map with 7 pixels of padding.
LegendControl.prototype.getDefaultPosition = function() {
    var oSize;
    if(options && options.legend && options.legend.offsetx !== false && options.legend.offsety !== false) {
        oSize = new GSize(options.legend.offsetx,options.legend.offsety);
    } else {
        oSize = new GSize(7, 37);
    }
    
  return new GControlPosition(G_ANCHOR_TOP_RIGHT, oSize);
};

/**
 * Method: initText
 * Called only once after the legend was created.  Initialize some text and
 *     images of the legend.
 */
LegendControl.prototype.initText = function() {
    this.legendDiv = document.getElementById('legendContentDiv');
    this.arrowImg = document.getElementById('legendCollapseButton');

    this.arrowImg.src = options.legendUpArrow;
    this.arrowImg.title = lang[options['lang']]['legendCollapseButtonTitleHide'];
    var oGroomersCheckBox = document.getElementById('groomersActivate');
    if (oGroomersCheckBox) {
        oGroomersCheckBox.title = lang[options['lang']]['groomersActivateTitle'];
    }
};

/**
 * Method: toggleLegend
 * Called when the user clicks on the toggle arrow of the legend.  Hide/Show the
 *     legend content and change some text properties.  Also changes the arrow
 *     img.
 */
LegendControl.prototype.toggleLegend = function() {
    if(this.legendDiv.style.display == "none") {
        hideDiv(this.legendDiv, false);
        this.arrowImg.src = options.legendUpArrow;
        this.arrowImg.title = 
            lang[options['lang']]['legendCollapseButtonTitleHide'];
    } else {
        hideDiv(this.legendDiv, true);
        this.arrowImg.src = options.legendDownArrow;
        this.arrowImg.title = 
            lang[options['lang']]['legendCollapseButtonTitleShow'];
    }
};

