//$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/apps/igeoportal-standard/branches/2.5_testing/legend.js $
/*----------------------------------------------------------------------------
 This file is part of deegree, http://deegree.org/
 Copyright (C) 2001-2009 by:
 - Department of Geography, University of Bonn -
 and
 - lat/lon GmbH -

 This library is free software; you can redistribute it and/or modify it under
 the terms of the GNU Lesser General Public License as published by the Free
 Software Foundation; either version 2.1 of the License, or (at your option)
 any later version.
 This library is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 details.
 You should have received a copy of the GNU Lesser General Public License
 along with this library; if not, write to the Free Software Foundation, Inc.,
 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

 Contact information:

 lat/lon GmbH
 Aennchenstr. 19, 53177 Bonn
 Germany
 http://lat-lon.de/

 Department of Geography, University of Bonn
 Prof. Dr. Klaus Greve
 Postfach 1147, 53001 Bonn
 Germany
 http://www.geographie.uni-bonn.de/deegree/

 e-mail: info@deegree.org
----------------------------------------------------------------------------*/

function Legend (label, bgcolor, layerList, symWidth, symHeight) {
	
    // variable declaration
    this.label = label;
    this.bgcolor = bgcolor;
    this.symWidth = symWidth;
    this.symHeight = symHeight;
    this.layerList = layerList;
    this.targetDocument = null;
    this.parentNode = null;

    // method declaration
    this.paint = paint;
    this.repaint = repaint;
    this.addTable = addTable;
    this.addRow = addRow;
    this.addColumn = addColumn;
    this.createImage = createImage;

    // method implementation
    function paint(targetDocument, parentNode) {
    	this.targetDocument = targetDocument;
    	this.parentNode = parentNode;
    	this.repaint();
    }

    function repaint() {
    	removeNodeById( this.targetDocument, label );
        var factory = new WMSRequestFactory();
        var table = this.addTable( label, this.targetDocument, this.parentNode);
        var tr = this.addRow( this.targetDocument, table, this.bgcolor, this.symheight+6 );
        var col = this.addColumn( this.targetDocument, tr, this.bgcolor, "100%", 3 );
        var tn =  this.targetDocument.createTextNode( label );
        col.appendChild( tn );
        var groups = this.layerList.getLayerGroups();
        for (var i = 0; i < groups.length; i++) {
            var layers = groups[i].getLayers();
            for (var j = 0; j < layers.length; j++) {
                if ( layers[j].isVisible() ) {
                    tr = this.addRow( this.targetDocument, table, this.bgcolor, this.symheight+6 );
                    
                    var sessionId = null;
                    if ( parent.controller.vSessionKeeper != null ) {
                        sessionId = parent.controller.vSessionKeeper.id;
                    }
                    var req = factory.createGetLegendGraphicRequest(groups[i], layers[j],
                                            this.symWidth, this.symHeight, sessionId );

                    var td = this.addColumn( this.targetDocument, tr, this.bgcolor, this.symWidth+6, 1 );
                    tn =  this.targetDocument.createTextNode( layers[j].getTitle() );
                    //write bold
                    var b = this.targetDocument.createElement("b");
                    td.appendChild( b );
                    b.appendChild( tn );
                    var br = this.targetDocument.createElement("br");
                    td.appendChild( br );
                    this.createImage(this.targetDocument, td, layers[j].getName(), req);
                }
            }
        }
    }

    function addTable( id, targetDocument, parentNode){
        var table = targetDocument.createElement("TABLE");
        var oTBody0 = targetDocument.createElement("TBODY");
        table.setAttribute("border", 1);
        table.setAttribute("id", id);
        table.setAttribute("cellpadding", 0);
        table.setAttribute("cellspacing", 0);
        table.setAttribute("width", "100%");
        //JM: refactored (css)
        //if param bgcolor is set in WMC, this overrides settings in deegree.css
        if ( this.bgcolor != null && this.bgcolor != "undefined" && this.bgcolor != "null" && this.bgcolor != "" && this.bgcolor != "-" ) {
            table.style.backgroundColor = this.bgcolor;
        }
        table.appendChild( oTBody0 );
        parentNode.appendChild(table);
        return oTBody0;
    }

    function addRow( targetDocument, table, bgcolor, height ) {
        var tr = targetDocument.createElement("tr");
        table.setAttribute("valign", "middle");
        // this has no effect in IE -> set bgcolor in addTable as well
        if ( bgcolor != null && bgcolor != "undefined" && bgcolor != "null" && bgcolor != "" && bgcolor != "-" ) {
            tr.style.backgroundColor = bgcolor;
        }
        tr.setAttribute("height", height);
        table.appendChild(tr);
        return tr;
    }

    function addColumn( targetDocument, tr, bgcolor, width, colspan ) {
        var td = targetDocument.createElement("td");
        //JM: refactored (css)
        //if param bgcolor is set in WMC, this overrides settings in deegree.css
        if ( bgcolor != null && bgcolor != "undefined" && bgcolor != "null" && bgcolor != "" && bgcolor != "-" ) {
            td.style.backgroundColor = bgcolor;
        }
        tr.setAttribute("valign", "middle");
        tr.setAttribute("colspan", colspan);
        td.setAttribute("align", 'left');
        td.setAttribute("width", width);
        tr.appendChild(td);
        return td;
    }

    function createImage(targetDocument, parentNode, id, src) {
        var newImage = targetDocument.createElement("img");
        newImage.setAttribute("id", "leg"+id);
        //newImage.setAttribute("name", id);
        newImage.setAttribute("src", src);
        newImage.setAttribute("alt", "not found");
        newImage.setAttribute("border", 0);
        parentNode.appendChild(newImage);
    }
}
