//$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/apps/igeoportal-standard/branches/2.5_testing/mapswitcher.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 MapSwitcher(label, listOfContexts, listSize, bgcolor) {

    // variable declaration
    this.label = label;
    this.listOfContexts = listOfContexts;
    this.listSize = listSize;
    this.bgcolor = bgcolor;

    // method declaration
    this.paint = paint;
    this.createList = createList;
    this.getLabel = getLabel;
    this.perfom = perfom;

    // implementation
    function getLabel() {
        return this.label;
    }

    function paint(targetDocument, parentElement) {
        var node = targetDocument.getElementById("MapSwitcher");
        var table = this.createList(targetDocument, parentElement);
        if ( node != null ) {
            var parentElement = node.parentNode;
            parentElement.replaceChild( node, table );
        } else {
            parentElement.appendChild( table );
        }
    }

    function createList(targetDocument) {

        var fieldset = targetDocument.createElement("fieldset");
        fieldset.setAttribute( "class", "listfont");
        var leg = targetDocument.createElement("legend");
        var b = targetDocument.createElement( "b" );
        var ltxt = targetDocument.createTextNode( label );
        b.appendChild( ltxt );
        leg.appendChild( b );
        fieldset.appendChild( leg );

        var sel = targetDocument.createElement("select");
        //JM: refactored (css)
        //if param bgcolor is set in WMC, this overrides settings in deegree.css
        if ( bgcolor != null && bgcolor != "undefined" ) {
            sel.setAttribute("style", "width:100%; background-color:" + bgcolor + ";" );
        }
        sel.setAttribute("name", "contexts" );

        if( listSize< 0 ){
            listSize = 1;
        }

        sel.setAttribute("size", listSize);
        sel['onchange'] = new Function ( "parent.controller.vMapSwitcher.perfom(this);" );

        var zopt = targetDocument.createElement("option");
        zopt.setAttribute("value", "_none");
        var ztxt = targetDocument.createTextNode( "select context..." );
        zopt.appendChild( ztxt );
        sel.appendChild( zopt );

        var list = listOfContexts.split(";");
        for (var i = 0; i < list.length; i++ ){
            var opt = targetDocument.createElement("option");
            var mc = list[i].split("|");
            if( mc[0] != "" && mc[0] != null && mc[1] != "" && mc[1] != null ){
                opt.setAttribute("value", mc[0] );
                var txt = targetDocument.createTextNode( mc[1] );
                opt.appendChild( txt );
                sel.appendChild( opt );
            }
        }

        fieldset.appendChild( sel );
        return fieldset;
    }

    function perfom( sel ) {

        if( sel.selectedIndex != 0 ){
            var req= "mapControl?rpc=<?xml version='1.0' encoding='UTF-8'?><methodCall>" +
                     "<methodName>mapClient:contextSwitch</methodName><params><param><value><struct>" +
                     "<member><name>mapContext</name><value><string>" + sel.value + "</string></value></member>" +
                     "</struct></value></param></params></methodCall>";
            parent.window.location.replace(req);
        }
    }

}
