﻿if (!Poseidon.Controls.Print.Module) {
    Poseidon.Controls.Print.Module = {};
}

Poseidon.Controls.Print.Module.DefaultOverviewMap = Ext.extend(Ext.Panel, {
    style: 'background-color: transparent;',
    bodyStyle: 'background-color: transparent; border: 0px solid transparent;',
    
    floating: false,
    stretch: this.stretch || false,

    getAlignPosition: function () {
        return { x: this.align_x || this.x || 0, y: this.align_y || this.y || 0 };
    },

    setAlignPosition: function (x, y) {
        this.align_x = x;
        this.align_y = y;
    },

    map: this.map,

    layout: 'fit',

    initComponent: function () {
        this.divOverviewMap= new Ext.BoxComponent({
            id: 'poseidon-control-print-defaultoverviewmap',
            autoEl:
            {
                tag: 'div'
            },
            listeners:
            {
                afterrender: function (box) {
                    this.overviewMapControl = new OpenLayers.Control.OverviewMap(
                    {
                        minRatio: this.minRatio,
                        maxRatio: this.maxRatio,
                        mapOptions: 
                        {
                            projection: this.projection,
                            units: this.units,
                            maxExtent: this.maxExtent
                        },
                        div: this.divOverviewMap.el.dom
                    });
                    this.map.addControl(this.overviewMapControl);
                },
                scope: this
            }
        });

        var config = {
            items:
            [
                this.divOverviewMap
            ]
        };

        Ext.apply(this, Ext.apply(this.initialConfig, config));

        for (var i = 0; i < this.map.controls.length; i++) {
            if (this.map.controls[i].CLASS_NAME === 'OpenLayers.Control.ScaleLine') {
                this.map.removeControl(this.map.controls[i]);
            }
        }

        Poseidon.Controls.Print.Module.DefaultScaleLine.superclass.initComponent.apply(this, arguments);
    },

    getObject: function () {
        this.maxWidth = this.width;

        for (var i = 0; i < map.controls.length; i++) {
            if (map.controls[i].CLASS_NAME === 'OpenLayers.Control.ScaleLine') {
                this.topInUnits = map.controls[i].topInUnits;
                this.topOutUnits = map.controls[i].topOutUnits;
                this.bottomInUnits = map.controls[i].bottomInUnits;
                this.bottomOutUnits = map.controls[i].bottomOutUnits;
            }
        }

        this.update();

        return {
            "module_id": this.module_id,
            "module_name": this.module_name,
            "width": this.topWidth,
            "height": this.getSize().height * 0.5,
            "x": this.x,
            "y": this.y,
            "align_x": this.align_x || this.x || 0,
            "align_y": this.align_y || this.y || 0,
            "align": this.align
        };
    },

    draggable: {
        //      Config option of Ext.Panel.DD class.
        //      It's a floating Panel, so do not show a placeholder proxy in the original position.
        insertProxy: false,

        //      Called for each mousemove event while dragging the DD object.
        onDrag: function (e) {
            //          Record the x,y position of the drag proxy so that we can
            //          position the Panel at end of drag.
            var pel = this.proxy.getEl();
            this.x = pel.getLeft(true);
            this.y = pel.getTop(true);

            //          Keep the Shadow aligned if there is one.
            var s = this.panel.getEl().shadow;
            if (s) {
                s.realign(this.x, this.y, pel.getWidth(), pel.getHeight());
            }
        },

        //      Called on the mouseup event.
        endDrag: function (e) {
            if (this.panel.ownerCt) {
                var parentPosition = this.panel.ownerCt.getPosition();
                this.panel.setPosition(this.x - parentPosition[0], this.y - parentPosition[1]);
            } else {
                this.panel.setPosition(this.x, this.y);
            }
        }
    },

    align: this.align || Poseidon.Controls.Print.AlignTypesEnum.TOP_LEFT,

    z_index: this.z_index || 100
});
