﻿if (!Poseidon.Controls.Print.Module) {
    Poseidon.Controls.Print.Module = {};
}

Poseidon.Controls.Print.Module.DefaultLegend = Ext.extend(Poseidon.Controls.Print.Module.FixableModule, {
    //style: 'background-color: transparent;',
    //bodyStyle: 'background-color: transparent; border: 0px solid transparent;',        
	minWidth: 150,
	minHeight: 100,
	stretch: this.stretch || false,

	moduleType: 'default_legend',

    layout: 'fit',

    getObject: function () {
        return {
            "module_id": this.module_id,
            "module_name": this.module_name,
            "width": this.width,
            "height": this.height,
			"print_width": this.print_width,
			"print_height": this.print_height,
            "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,
            "legend": this.legendItems            
        };
    },  

    height: 'auto',

    z_index: this.z_index || 100,

    title: 'Signaturforklaring',    

    initComponent: function () {

        var legendItems = [];
        this.legendItems = [];
        for (var i = 0; i < this.map.layers.length; i++) {
            var currentLayer = this.map.layers[i];
            if (currentLayer.visibility) {
                if (currentLayer.legend) {
//                    legendItems.push(
//                    {
//                        xtype: 'container',
//                        layout: 'form',
//                        items:
//                        [
//                            {
//                                xtype: 'box',
//                                html: currentLayer.name
//                            }, {
//                                xtype: 'box',
//                                autoEl: {
//                                    tag: 'img',
//                                    src: currentLayer.legend
//                                }
//                            }
//                        ]
//                    });
                    this.legendItems.push(
                    {
                        name: currentLayer.name,
                        src: currentLayer.legend
                    });
                }
            }
        }

        var logo =
        {
            xtype: 'box',
            autoEl: {
                tag: 'img',
                src: this.url_reference
            },
            width: '150',
            height: '22'
        };

        var customText =
        {
            xtype: 'textarea',
            id: 'poseidon-control-print-defaultlegend-text',
            hideLabel: true,
            autoScroll: true,
            height: 50,
            width: 128
        };
		
		var width = Math.round(this.print_width * OpenLayers.DOTS_PER_INCH / this.dpi);
		var height = Math.round(this.print_height * OpenLayers.DOTS_PER_INCH / this.dpi);		
		
		var config = 
		{
			width: width > this.minWidth ? width : this.minWidth,
			height: height > this.minHeight ? height : this.minHeight,
			items:
			[
				{
					xtype: 'container',
					layout: 'form',
					style: 'padding: 10px;',
					items:
					[
						{
							xtype: 'label',
							text: 'Tilpasser sig automatisk på printet.'
						}
	//                    logo,
	//                    customText,
	//                    legendItems
					]
				}
			],
			bbar: new Ext.Toolbar({
				items:
				[
					{
						xtype: 'button',
						iconCls: 'icon-push-pin',
						handler: function (button, event) {
						    this.alignContextMenu.showAt(event.getXY());
						},
						scope: this
					}
				]
			})
		}        

        Ext.apply(this, Ext.apply(this.initialConfig, config));
		// 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.DefaultLegend.superclass.initComponent.apply(this, arguments);

        // After parent code
        // e.g. install event handlers on rendered component
    }
});
