/*----------------    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
 * 
 ------------------------------------------------------------------------*/
import nl.idgis.giclient.framework.bars.Bar;
import nl.idgis.giclient.config.Colors;

class nl.idgis.giclient.framework.bars.BarBackGround extends MovieClip {

    private var bar:Bar = null; // Set by init object.

    private var outerLeftColor:Number = 0x000000; // Either the top or the bottom left color, depending on the vertical position of the bar.
    private var outerRightColor:Number = 0x000000; // Either the top or the bottom right color, depending on the vertical position of the bar.
    private var horiMargin:Number = 8;
    private var mainFrameThickness:Number = 8;

    function onLoad():Void {
        useHandCursor = false;
        drawAll();
        setWidth();

        bar.onLoadBarBackGround();
    }

    private function drawAll():Void {
        attachMovie("ButtonBackLeft", "left", 10);
        attachMovie("ButtonBackRight", "right", 11);

        createEmptyMovieClip("middle", 12);
        this["middle"]._x = horiMargin;
    }

    function setWidth():Void {
        this["right"]._x = _parent.width - horiMargin;

        setShadesAndColors();
    }

    function setShadesAndColors():Void {
        var stageWidth:Number = Stage.width;
        var stageHeight:Number = Stage.height;

        if (this["rightShade"] != null) {
            this["rightShade"].removeMovieClip();
        }
        if (this["bottomShade"] != null) {
            this["bottomShade"].removeMovieClip();
        }
        if (this["topRightShade"] != null) {
            this["topRightShade"].removeMovieClip();
        }
        if (this["bottomLeftShade"] != null) {
            this["bottomLeftShade"].removeMovieClip();
        }
        if (this["bottomRightShade"] != null) {
            this["bottomRightShade"].removeMovieClip();
        }

        if (_parent._x + _parent.width + (mainFrameThickness / 2) <= stageWidth - mainFrameThickness) {
            attachMovie("SchaduwVert", "rightShade", 1, {_x: _parent.width, _y: horiMargin , _width: mainFrameThickness / 2, _height: _parent.height - (2 * horiMargin)});
        }
        if (_parent._y + _parent.height + (mainFrameThickness / 2) <= stageHeight - mainFrameThickness) {
            attachMovie("SchaduwHor", "bottomShade", 2, {_x: horiMargin, _y: _parent.height, _width: _parent.width - (2 * horiMargin), _height: mainFrameThickness / 2});
        }
        if ((_parent._x + _parent.width + (mainFrameThickness / 2) <= stageWidth - mainFrameThickness) &&
                                                                                  (_parent._y >= mainFrameThickness)) {
            attachMovie("CornerShadeUR", "topRightShade", 3, {_x: _parent.width});
        }
        if ((_parent._x >= mainFrameThickness) &&
                        (_parent._y + _parent.height + (mainFrameThickness / 2) <= stageHeight - mainFrameThickness)) {
            attachMovie("CornerShadeLL", "bottomLeftShade", 4, {_y: _parent.height});
        }
        if ((_parent._x + _parent.width + (mainFrameThickness / 2) <= stageWidth - mainFrameThickness) &&
                        (_parent._y + _parent.height + (mainFrameThickness / 2) <= stageHeight - mainFrameThickness)) {
            attachMovie("CornerShadeLR", "bottomRightShade", 5, {_x: _parent.width, _y: _parent.height});
        }

        if (_parent._y < stageHeight / 2) {
            outerLeftColor = Colors.topLeftColor;
            outerRightColor = Colors.topRightColor;
        } else {
            outerLeftColor = Colors.bottomLeftColor;
            outerRightColor = Colors.bottomRightColor;
        }

        var frameWidth:Number = stageWidth - (2 * horiMargin);
        var stageX:Number = _parent._x + horiMargin;
        var frameX:Number = stageX - horiMargin;

        var leftRed:Number = (outerLeftColor & 0xFF0000) / 0x10000;
        var leftGreen:Number = (outerLeftColor & 0x00FF00) / 0x100;
        var leftBlue:Number = (outerLeftColor & 0x0000FF);
        var rightRed:Number = (outerRightColor & 0xFF0000) / 0x10000;
        var rightGreen:Number = (outerRightColor & 0x00FF00) / 0x100;
        var rightBlue:Number = (outerRightColor & 0x0000FF);

        leftRed = Math.round(((leftRed * (frameWidth - frameX)) + (rightRed * frameX)) / frameWidth);
        leftGreen = Math.round(((leftGreen * (frameWidth - frameX)) + (rightGreen * frameX)) / frameWidth);
        leftBlue = Math.round(((leftBlue * (frameWidth - frameX)) + (rightBlue * frameX)) / frameWidth);
        var leftColor:Number = (leftRed * 0x10000) + (leftGreen * 0x100) + leftBlue;

        stageX = _parent._x + _parent.width - horiMargin;
        frameX = stageX - horiMargin;

        leftRed = (outerLeftColor & 0xFF0000) / 0x10000;
        leftGreen = (outerLeftColor & 0x00FF00) / 0x100;
        leftBlue = (outerLeftColor & 0x0000FF);
        rightRed = (outerRightColor & 0xFF0000) / 0x10000;
        rightGreen = (outerRightColor & 0x00FF00) / 0x100;
        rightBlue = (outerRightColor & 0x0000FF);

        rightRed = Math.round(((leftRed * (frameWidth - frameX)) + (rightRed * frameX)) / frameWidth);
        rightGreen = Math.round(((leftGreen * (frameWidth - frameX)) + (rightGreen * frameX)) / frameWidth);
        rightBlue = Math.round(((leftBlue * (frameWidth - frameX)) + (rightBlue * frameX)) / frameWidth);
        var rightColor:Number = (rightRed * 0x10000) + (rightGreen * 0x100) + rightBlue;

        var color:Color = null;
        color = new Color(this["left"]);
        color.setRGB(leftColor);
        color = new Color(this["right"]);
        color.setRGB(rightColor);

        var colors:Array = [leftColor, rightColor];
        var alphas:Array = [100, 100];
        var ratios:Array = [0, 0xFF];
        var matrix:Object = {matrixType: "box", x: 0, y: 0, w: _parent.width - (2 * horiMargin), h: _parent.height, r: 0};

        this["middle"].clear();
        this["middle"].beginGradientFill("linear", colors, alphas, ratios, matrix);
        this["middle"].moveTo(0, 0);
        this["middle"].lineTo(_parent.width - (2 * horiMargin), 0);
        this["middle"].lineTo(_parent.width - (2 * horiMargin), _parent.height);
        this["middle"].lineTo(0, _parent.height);
        this["middle"].lineTo(0, 0);
        this["middle"].endFill();
    }

    // Roll methods for maptip in MapViewer

    function onRollOver():Void {}

    function onRollOut():Void {}

}