/*
 * LINGUIST.LLMAP.OpacityControl
 * Written by Joshua M. Thompson <joshua@linguistlist.org>
 *
 * This object class provides a panel that manages the list of
 * services currently attached to the map.
 */

LINGUIST.LLMAP.OpacityControl = Ext.extend(Ext.Window, {
    constructor: function(config) {
        LINGUIST.LLMAP.OpacityControl.superclass.constructor.call(this, {
            layout: 'border',
            title: 'Change Opacity',
            height: 150,
            width: 375,
            closable: false,
            buttons: [
                {
                    text: 'Set Opacity',
                    scope: this,
                    handler: function() {
                        this.hide();

                        this.service.map_layer.setOpacity(this.getComponent(0).getComponent(0).getValue() / 100);
                    }
                },
                {
                    text: 'Cancel',
                    scope: this,
                    handler: function() { this.hide(); }
                }
            ],
            items: [
                {
                    region: 'center',
                    bodyStyle: 'padding: 10px',
                    items: {
                        xtype: 'slider',
                        anchor: '-10',
                        increment: 1,
                        minValue: 0,
                        maxValue: 100,
                        value: 50,
                        vertical: false,
                        listeners: {
                            scope: this,
                            change: this.updateStatusText
                        }
                    }
                },
                {
                    region: 'south',
                    bodyStyle: 'padding: 5px; font-size: 14px; font-weight: bold; height: 20px; text-align: center;',
                    html: ''
                }
            ]
        });
    },

    show: function(service) {
        this.service = service;

        LINGUIST.LLMAP.OpacityControl.superclass.show.call(this);

        this.getComponent(0).getComponent(0).setValue(service.map_layer.opacity * 100);
        this.updateStatusText();
    },

    updateStatusText: function() {
        var panel = this.getComponent(1);
        var value = this.getComponent(0).getComponent(0).getValue();

        panel.body.update('Current Value: ' + value + '%');
    }
});

/*
 * LINGUIST.LLMAP.ServiceManager
 * Written by Joshua M. Thompson <joshua@linguistlist.org>
 *
 * This object class provides a panel that manages the list of
 * services currently attached to the map.
 */

LINGUIST.LLMAP.ServiceManager = Ext.extend(Ext.tree.TreePanel, {
    project_mode: false,

    constructor: function(config) {
        Ext.apply(config, {
            contextMenu: new Ext.menu.Menu({
                items: [
                    {
                        text: 'Zoom to Layer',
                        scope: this,
                        handler: function(item) {
                            var service = item.parentMenu.contextNode.attributes.service;

                            this.map.zoomToBounds(service.minx, service.miny, service.maxx, service.maxy);
                        }
                    },
                    {
                        text: 'Set Opacity',
                        scope: this,
                        handler: function(item) {
                            this.oc.show(item.parentMenu.contextNode.attributes.service);
                        }
                    },
                    {
                        text: 'Remove Layer',
                        scope: this,
                        handler: function(item) {
                            this.map.removeService(item.parentMenu.contextNode.attributes.service);
                        }
                    }
                ]
            }),
            rootVisible: false,
            root: new Ext.tree.TreeNode
        }, {
            animate: false,
            autoScroll: true,
            containerScroll: false
        });

        LINGUIST.LLMAP.ServiceManager.superclass.constructor.call(this, config);

/*
        if (!this.project_mode) {
            this.root.appendChild(new Ext.tree.TreeNode({
                text: 'Base Map',
                cls: 'llmap-service-q0 llmap-service-bm',
                isBaseMap: true
            }));
        }
*/

        this.oc = new LINGUIST.LLMAP.OpacityControl;

        this.addListener({
            scope: this,
            checkchange: this.handleCheckChange,
            contextmenu: function(node, e) {
                node.select();

                if ((node.attributes.layer_number == null) && !node.attributes.isBaseMap) {
                    var menu = node.getOwnerTree().contextMenu;

                    menu.contextNode = node;
                    menu.showAt(e.getXY());
                }
            }
        });
    },

    attach: function(map) {
        this.map = map;

        this.map.addListener({
            scope: this,
            addservice: function(map, service) {
                var q = service.rating;

                if (q == null) {
                    q = 0;
                }

                var node = new Ext.tree.TreeNode({
                    text: service.canonical_name,
                    cls: 'llmap-service-q' + q,
                    checked: true,
                    service: service
                });

                if (!this.project_mode && (this.root.childNodes.length == 1) && (service.minx != undefined)) {
                    map.zoomToBounds(service.minx, service.miny, service.maxx, service.maxy);
                }

                this.root.insertBefore(node, this.root.firstChild);

                this.populateNode(node);

                if (node.text != 'Base Map Overlays') {
                    node.expand(node);
                }
            },
            removeservice: function(map, service) {
                var match;

                this.root.eachChild(function(node) {
                    if (node.attributes.service === service) {
                        match = node;
                    }
                }, this);

                if (match) {
                    match.remove();
                }
            }
        });
    },

    /*initEvents: function() {
        LINGUIST.LLMAP.ServiceManager.superclass.initEvents.call(this);

        this.dropZone.setPadding(0, 0, this.getInnerHeight(), 0);
    },*/

    handleCheckChange: function(node, checked) {
        var layer = node.attributes.layer_number;

        if (layer == null) {
            node.attributes.service.setVisible(checked);
        }
        else {
            node.attributes.service.layers[layer].visible = checked;

            node.parentNode.attributes.dirty = true;
        }
    },

    populateNode: function(node) {
        var service = node.attributes.service;
        var layers  = service.layers;

        if (layers.length > 1) {
            for (var i = 0; i < layers.length; i++) {
                var config = {
                    text: service.layers[i].name,
                    cls: 'llmap-layer',
                    expandable: false,
                    checked: service.layers[i].visible,
                    service: service,
                    layer_number: i
                };

                node.appendChild(new Ext.tree.TreeNode(config));
            }
        }
        else {
            // node.setText(layers[0].name);

            layers[0].visible = true;
            service.updateLayers()
        }
    },

    updateMap: function() {
        this.root.eachChild(function(node) {
            if (node.attributes.dirty) {
                node.attributes.service.updateLayers();

                node.attributes.dirty = false;
            }
        }, this);
    }
});

Ext.ComponentMgr.registerType('svcmanager', LINGUIST.LLMAP.ServiceManager);
