//$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/apps/igeoportal-standard/branches/2.5_testing/htmllayer.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
----------------------------------------------------------------------------*/

/*
 * represent a HTML layer containing a group of WMS layers assigned to a MapModel
 * id = unique ID of a HTML layer
 * zIndex = HTML layer z-Index -> vertical position of the layer
 * type = type of the layer; HIGHLIGHT, WMSLAYER, WFSLAYER ...
 */
function HTMLMapLayer(id, zIndex, type, top, left, layerGroup, mapModel) {

    this.top = top;
    this.left = left;

    this.id = id;
    this.layerGroup = layerGroup;
    this.mapModel = mapModel;
    this.zIndex = zIndex;
    this.type = type;
    this.targetDocument = null;

    this.getId = getId;
    this.getType = getType;
    this.getLayerGroup = getLayerGroup;
    this.setLayerGroup = setLayerGroup;
    this.getMapModel = getMapModel;
    this.setMapModel = setMapModel;
    this.paint = paint;
    this.createImage = createImage;
    this.repaint = repaint;

    function getId() {
        return this.id;
    }

    function getType() {
        return this.type;
    }

    function getLayerGroup() {
        return this.layerGroup;
    }

    function setLayerGroup(layerGroup) {
        this.layerGroup = layerGroup;
        this.layerGroup.setChanged( true )
    }

    function getMapModel() {
        return this.mapModel;
    }

    function setMapModel(mapModel) {
        this.mapModel = mapModel;
    }

    /*
     * 'paints' (adds) the HTML layer to the passed document/node
     */
    function paint(targetDocument, parentElement) {
        this.targetDocument = targetDocument;
        if ( this.layerGroup.isChanged() ) {
            var fac = new WMSRequestFactory();
            var src = null;
            if ( controller.vSessionKeeper != null ) {
                src = fac.createGetMapRequest( layerGroup, mapModel, controller.vSessionKeeper.id );
            } else {
                src = fac.createGetMapRequest( layerGroup, mapModel, null );
            }
            if ( src != null ) {
                this.createImage(src, targetDocument, parentElement);
            } else {
                var node = targetDocument.getElementById( this.id );
                if ( node != null ) {
                    parentElement.removeChild( node );
                }
                return false;
            }
        }
        return true;
    }

    function createImage(src, targetDocument, parentElement){       
        var newImage = targetDocument.createElement("img");
        newImage.setAttribute( "id", this.id );
        newImage.setAttribute( "name", layerGroup.name );
        newImage.setAttribute( "src", src );
        newImage.setAttribute( "border", 0 );
        newImage.setAttribute( "width", mapModel.getWidth() );
        newImage.setAttribute( "height", mapModel.getHeight() );
        newImage.style.position = "absolute";
        newImage.style.top = this.top;
        newImage.style.left = this.left;
        newImage.style.border = "0";
        newImage.style.left = "0px";
        newImage.style.top = "0px";
        newImage.style.zIndex = this.zIndex;
		targetDocument.getElementById( "mapCanvas" ).appendChild( newImage );
    }

    function repaint() {
        if ( this.layerGroup.isChanged() ) {
            var src = WMSRequestFactory.createGetMapRequest( layerGroup, mapModel, controller.vSessionKeeper.id );
            targetDocument.getElementById( "img:" + this.id ).src = src;
        }
    }
}
