/*----------------    FILE HEADER ---------------------------------------
 This file is part of Geoide.
 Copyright (C) 2005-2006 by:
 IDgis B.V.
 http://www.idgis.nl

 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:

 Herman Assink
 IDgis bv
 P.O. Box 15
 7450 AA Holten
 The Netherlands
 E-Mail: herman.assink@idgis.nl

 * @version 1.4.0
 * @author IDgis team
 * 
 ------------------------------------------------------------------------*/
class nl.idgis.giclient.framework.ButtonGrid extends MovieClip {

    private var width:Number = -1; // Set by init object.
    private var height:Number = -1; // Set by init object.
    private var numRows:Number = -1; // Set by init object.
    private var numColumns:Number = -1; // Set by init object.
    private var rowLabels:Array = null; // Set by init object.
    private var columnLabels:Array = null; // Set by init object.
    private var values:Array = null; // Set by init object.
    private var eventListeners:Array = new Array();
    private var depth:Number = 0;
    private var activeButtonIndex:Number = -1; // Also index to the current value.

    function onLoad():Void {
        drawAll();
    }

    function addEventListener(funczion:Function):Void {
        for (var i:Number = 0; i < eventListeners.length; i++) {
            if (eventListeners[i] == funczion) {
                return; // Function already an event listener.
            }
        }
        eventListeners.push(funczion);
    }

    private function drawAll():Void {
        var maxLabelWidth:Number = 0;
        var maxLabelHeight:Number = 0;
        var textFormat:TextFormat = new TextFormat();
        textFormat.font = "_sans";
        textFormat.size = 10;

        for (var i:Number = 0; i < numRows; i++) {
            if (maxLabelWidth < textFormat.getTextExtent(rowLabels[i]).textFieldWidth) {
                maxLabelWidth = textFormat.getTextExtent(rowLabels[i]).textFieldWidth;
            }
        }
        for (var i:Number = 0; i < numColumns; i++) {
            if (maxLabelHeight < textFormat.getTextExtent(columnLabels[i]).textFieldHeight) {
                maxLabelHeight = textFormat.getTextExtent(columnLabels[i]).textFieldHeight;
            }
        }

        var cellWidth:Number = (width - maxLabelWidth) / numColumns;
        var cellHeight:Number = (height - maxLabelHeight) / numRows;
        var k:Number = 0; // Cell iterator.
        lineStyle(1, 0x000000, 100);
        for (var i:Number = 0; i < numRows; i++) {
            createTextField("rowLabel" + i, depth++, 0, maxLabelHeight + (i * cellHeight), maxLabelWidth, cellHeight);
            this["rowLabel" + i].setNewTextFormat(textFormat);
            this["rowLabel" + i].text = rowLabels[i];
            for (var j:Number = 0; j < numColumns; j++) {
                if (i == 0) {
                    createTextField("columnLabel" + j, depth++, maxLabelWidth + (j * cellWidth), 0, cellWidth, maxLabelHeight);
                }
                this["columnLabel" + j].setNewTextFormat(textFormat);
                this["columnLabel" + j].text = columnLabels[j];
                moveTo(maxLabelWidth + (j * cellWidth), maxLabelHeight + (i * cellHeight));
                lineTo(maxLabelWidth + ((j + 1) * cellWidth), maxLabelHeight + (i * cellHeight));
                lineTo(maxLabelWidth + ((j + 1) * cellWidth), maxLabelHeight + ((i + 1) * cellHeight));
                lineTo(maxLabelWidth + (j * cellWidth), maxLabelHeight + ((i + 1) * cellHeight));
                lineTo(maxLabelWidth + (j * cellWidth), maxLabelHeight + (i * cellHeight));

                attachMovie("KnopRondBasis", "button" + k, depth++,
                                                 {_x: maxLabelWidth + ((j + 0.5) * cellWidth),
                                                  _y: maxLabelHeight + ((i + 0.5) * cellHeight),
                                                  _width: cellWidth * 0.8, _height: cellHeight * 0.8});
                this["button" + k].index = k;
                this["button" + k].onRelease = function() {
                    this._parent.onClickButton(this);
                };
                k++;
            }
        }
    }

    function onClickButton(target:MovieClip):Void {
        if (activeButtonIndex > -1) {
            this["button" + activeButtonIndex].gotoAndStop(1);
        }
        target.gotoAndStop(3);
        activeButtonIndex = target.index;

        var funczion:Function = null;
        var eventObject:Object = {target: this, subTarget: target, value: values[activeButtonIndex]};
        for (var i:Number = 0; i < eventListeners.length; i++) {
            funczion = eventListeners[i];
            funczion.call(this, eventObject);
        }
    }

}