/******************************************************************************
 * kaLegend - a structured legend that supports grouped layers, visibility, 
 *            expand/collapse, and queryability
 *
 * copyright DM Solutions Group Inc.
 *
 * $Id: kaLegend.js,v 1.12 2005/11/04 16:54:32 pspencer Exp $
 *
 ******************************************************************************
 *
 * To use kaLegend:
 * 
 * 1) add a script tag to your page:
 * 
 * <script type="text/javascript" src="kaLegend.js"></script>
 *
 * 2) add a <div> element to your page to contain the legend.  The div must
 *    have a unique id:
 *
 * <div id="legend"></div>
 * 
 * 3) create a new instance of kaLegend and pass it the id of the div:
 * 
 * myKaLegend = new kaLegend( 'legend' );
 *
 * and that's it :)
 *
 * TODO:
 *
 * - drag and drop layer re-ordering would be nice, see script.alicio.us
 * 
 *****************************************************************************/

/******************************************************************************
 * kaLegend
 * 
 * internal class to handle the legend.
 * 
 * oKaMap - the ka-Map object to attach to.
 * szID - string, the id of a div that will contain the legend
 * bStatic - boolean, true to use static legends, false to use dynamic legends
 *
 *****************************************************************************/
function kaLegend(oKaMap, szID, bStatic)
{
    this.kaMap = oKaMap;
    this.domObj = this.kaMap.getRawObject(szID);
    this.type = (bStatic)?'static':'dynamic';
    this.expanders = [];
    this.queryCBs = [];
    
    this.urlBase = this.kaMap.server;
    this.urlBase += (this.urlBase!=''&&this.urlBase.substring(-1)!='/')?'':'/';
	
    this.showQueryCBs = true;    
    
    if (this.type == 'static')
    {
        this.domImg = document.createElement( 'img' );
        this.domImg.src = this.kaMap.aPixel.src;
        this.domObj.appendChild( this.domImg );
    }
    else
    {
        this.domObj.innerHTML = '&nbsp;';
    }
    
    this.kaMap.registerForEvent( KAMAP_SCALE_CHANGED, this, this.update );
    this.kaMap.registerForEvent( KAMAP_MAP_INITIALIZED, this, this.update );
    this.kaMap.registerForEvent( KAMAP_LAYERS_CHANGED, this, this.draw );
}
    
kaLegend.prototype.update = function(eventID)
{
    var url = '';
    if (this.type == 'static')
    {
        this.domImg.src = 'legend.php?map=' + 
                          this.kaMap.currentMap + 
                          '&scale='+this.kaMap.getCurrentScale();
    }
    else
    {
        if (eventID == KAMAP_MAP_INITIALIZED)
        {
            while(this.domObj.childNodes.length > 0)
                this.domObj.removeChild(this.domObj.childNodes[0]);
            	this.draw();
        }
        else if (eventID == KAMAP_SCALE_CHANGED)
        {
            var oMap = this.kaMap.getCurrentMap();
            var aLayers = oMap.getLayers();
            var s = this.kaMap.getCurrentScale();
            for (var i in aLayers)
            {
                var oLayer = aLayers[i];
                var oImg = this.kaMap.getRawObject( 'legendImg_' + oLayer.name);
                if (oImg)
                {
                    oImg.src = 'legend.php?map=' + 
                               this.kaMap.currentMap + '&scale=' + s + '&g=' + 
                               oLayer.name;
                }
            }
        }
    }
}

/**
 * legend.draw( szContents )
 *
 * render the contents of a legend template into a div
 */
kaLegend.prototype.draw = function()
{
    this.urlBase = this.kaMap.server;
    this.urlBase += (this.urlBase!=''&&this.urlBase.substring(-1)!='/')?'':'/';

    var oMap = this.kaMap.getCurrentMap();

    this.expanders = [];

// Comentado porque sino, al aņadir una capaWMS, nos quita los checks de todas las capas
//    this.queryCBs = [];

    if (this.domObj.childNodes.length == 0)
        this.domObj.appendChild(this.createHeaderHTML());
		
    var aLayers = oMap.getAllLayers();
    for (var i=(aLayers.length-1);i>=0;i--)
    {
        if (aLayers[i].kaLegendObj == null)
        {
            this.createLayerHTML( aLayers[i] );
        }
        else
        {
            try{this.domObj.removeChild( aLayers[i].kaLegendObj );}
            catch(e){};
        }
    }

    for (var i=(aLayers.length-1);i>=0;i--)
    {
        this.domObj.appendChild( aLayers[i].kaLegendObj );
    }

    if (this.kaMap.isIE4)
    {
        for(var i=0; i<this.queryCBs.length; i++)
        {
            this.queryCBs[i].checked = this.queryCBs[i].oLayer.visible;
        }
    }	
   return;
}

kaLegend.prototype.createHeaderHTML = function()
{
    var d, t, tb, tr, td, img;
    
    d = document.createElement( 'div' );
    d.className = 'kaLegendTitle';
    
    t = document.createElement( 'table' );
    
    t.setAttribute('width','100%');
    t.setAttribute('cellPadding', "0");
    t.setAttribute('cellSpacing', "0");
    t.setAttribute('border', "0");
    
    tb = document.createElement( "tbody" );
    
    tr = document.createElement( 'tr' );
    td = document.createElement( 'td' );
	td.setAttribute('height', '16');
    td.appendChild(document.createTextNode( 'Capas' ));
    tr.appendChild( td );
    
    tr.appendChild( td );
    tb.appendChild(tr);
    t.appendChild(tb);
    d.appendChild(t);
    return d;
}

kaLegend.prototype.createLayerHTML = function( oLayer )
{
    var d, t, tb, tr, td, expander, cb, img, name;
    
    d = document.createElement( 'div' );
    d.id = 'group_' + oLayer.name;
    d.className = "kaLegendLayer";
    
    name = oLayer.name;
    if (name == '__base__')
    {
        name = 'Base';
    }
    
    t = document.createElement('table');
    t.setAttribute('width','100%');
    t.setAttribute('cellPadding', "0");
    t.setAttribute('cellSpacing', "0");
    t.setAttribute('border', "0");

    tb = document.createElement( 'tbody' );
    tr = document.createElement('tr');
    td = document.createElement('td');
    td.setAttribute( "width", "0");	
    
    tr.appendChild(td);
    
    td = document.createElement('td');
    td.width = '22';
    if (oLayer.name != '__base__')
    {
        cb = document.createElement( 'input' );
        cb.type = 'checkbox';
        cb.name = 'layerVisCB';
        cb.value = oLayer.name;
        cb.checked = oLayer.visible;
        cb.kaLegend = this;
        cb.oLayer = oLayer;
        cb.onclick = kaLegend_toggleLayerVisibility;
        this.queryCBs.push(cb);
        td.appendChild( cb );
    }
    else
    {
        td.innerHTML = '&nbsp;';
    }
    tr.appendChild(td);
    
    td = document.createElement( 'td' );
    td.innerHTML = name;
    tr.appendChild(td);
    tb.appendChild(tr);
    t.appendChild( tb );
    d.appendChild(t);

    oLayer.kaLegendObj = d;
}

function kaLegend_toggleLayerVisibility()
{
    this.kaLegend.kaMap.setLayerVisibility( this.value, this.checked );
}


