﻿if (!Poseidon.Controls.Print.Module) {
    Poseidon.Controls.Print.Module = {};
}

Poseidon.Controls.Print.Module = Ext.extend(Ext.Panel, {
    initComponent: function () {

        //		var config = 
        //		{
        //		}
        //       
        //		Ext.apply(this, Ext.apply(this.initialConfig, config));

        // Call parent (required)
        Poseidon.Controls.Print.Module.superclass.initComponent.apply(this, arguments);
    },

    getModuleType: function () {
        return this.moduleType;
    }
});

Poseidon.Controls.Print.Module.DraggableModule = Ext.extend(Poseidon.Controls.Print.Module, {
    // Floating = false is set to ensure that the panel doesn't have a shadow. It seems like the shadow
    // is moved in parent space instead of in window space. So using a shadow moves the shadow far to the
    // lower right when dragging.
    floating: false,

    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);
            }
        }
    },

    initComponent: function () {

        this.align = this.align || Poseidon.Controls.Print.AlignTypesEnum.TOP_LEFT;

        // Config object has already been applied to 'this' so properties can 
        // be overriden here or new properties (e.g. items, tools, buttons) 
        // can be added, eg:

        // Call parent (required)
        Poseidon.Controls.Print.Module.DraggableModule.superclass.initComponent.apply(this, arguments);
    }
});

Poseidon.Controls.Print.Module.FixableModule = Ext.extend(Poseidon.Controls.Print.Module.DraggableModule, {
    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;
    },

    initComponent: function () {

        this.align = this.align || Poseidon.Controls.Print.AlignTypesEnum.TOP_LEFT;

        var toggleGroupName = Ext.id(null, 'poseidon-controls-print-module-defaultmapheader-align');

        this.alignContextMenu = new Ext.menu.Menu({
            plain: true,
            items:
			{
			    xtype: 'buttongroup',
			    title: 'Justér til',
			    autoWidth: true,
			    columns: 3,
			    defaults: {
			        xtype: 'button',
			        width: '100%'
			    },
			    items: [
				{
				    xtype: 'button',
				    iconCls: 'icon-align-topleft',
				    toggleGroup: toggleGroupName,
				    handler: function (button, event) {
				        this.align = Poseidon.Controls.Print.AlignTypesEnum.TOP_LEFT;
				        this.alignContextMenu.hide();
				    },
				    pressed: this.align === Poseidon.Controls.Print.AlignTypesEnum.TOP_LEFT,
				    scope: this
				}, {
				    xtype: 'button',
				    iconCls: 'icon-align-top',
				    toggleGroup: toggleGroupName,
				    handler: function (button, event) {
				        this.align = Poseidon.Controls.Print.AlignTypesEnum.TOP;
				        this.alignContextMenu.hide();
				    },
				    pressed: this.align === Poseidon.Controls.Print.AlignTypesEnum.TOP,
				    scope: this
				}, {
				    xtype: 'button',
				    iconCls: 'icon-align-topright',
				    toggleGroup: toggleGroupName,
				    handler: function (button, event) {
				        this.align = Poseidon.Controls.Print.AlignTypesEnum.TOP_RIGHT;
				        this.alignContextMenu.hide();
				    },
				    pressed: this.align === Poseidon.Controls.Print.AlignTypesEnum.TOP_RIGHT,
				    scope: this
				}, {
				    xtype: 'button',
				    iconCls: 'icon-align-left',
				    toggleGroup: toggleGroupName,
				    handler: function (button, event) {
				        this.align = Poseidon.Controls.Print.AlignTypesEnum.LEFT;
				        this.alignContextMenu.hide();
				    },
				    pressed: this.align === Poseidon.Controls.Print.AlignTypesEnum.LEFT,
				    scope: this
				}, {
				    xtype: 'button',
				    iconCls: 'icon-align-center',
				    toggleGroup: toggleGroupName,
				    handler: function (button, event) {
				        this.align = Poseidon.Controls.Print.AlignTypesEnum.CENTER;
				        this.alignContextMenu.hide();
				    },
				    pressed: this.align === Poseidon.Controls.Print.AlignTypesEnum.CENTER,
				    scope: this
				}, {
				    xtype: 'button',
				    iconCls: 'icon-align-right',
				    toggleGroup: toggleGroupName,
				    handler: function (button, event) {
				        this.align = Poseidon.Controls.Print.AlignTypesEnum.RIGHT;
				        this.alignContextMenu.hide();
				    },
				    pressed: this.align === Poseidon.Controls.Print.AlignTypesEnum.RIGHT,
				    scope: this
				}, {
				    xtype: 'button',
				    iconCls: 'icon-align-bottomleft',
				    toggleGroup: toggleGroupName,
				    handler: function (button, event) {
				        this.align = Poseidon.Controls.Print.AlignTypesEnum.BOTTOM_LEFT;
				        this.alignContextMenu.hide();
				    },
				    pressed: this.align === Poseidon.Controls.Print.AlignTypesEnum.BOTTOM_LEFT,
				    scope: this
				}, {
				    xtype: 'button',
				    iconCls: 'icon-align-bottom',
				    toggleGroup: toggleGroupName,
				    handler: function (button, event) {
				        this.align = Poseidon.Controls.Print.AlignTypesEnum.BOTTOM;
				        this.alignContextMenu.hide();
				    },
				    pressed: this.align === Poseidon.Controls.Print.AlignTypesEnum.BOTTOM,
				    scope: this
				}, {
				    xtype: 'button',
				    iconCls: 'icon-align-bottomright',
				    toggleGroup: toggleGroupName,
				    handler: function (button, event) {
				        this.align = Poseidon.Controls.Print.AlignTypesEnum.BOTTOM_RIGHT;
				        this.alignContextMenu.hide();
				    },
				    pressed: this.align === Poseidon.Controls.Print.AlignTypesEnum.BOTTOM_RIGHT,
				    scope: this
				}]
			}
        });

        // Config object has already been applied to 'this' so properties can 
        // be overriden here or new properties (e.g. items, tools, buttons) 
        // can be added, eg:

        // Call parent (required)
        Poseidon.Controls.Print.Module.FixableModule.superclass.initComponent.apply(this, arguments);
    }
});
