/*
    Document   : lbgm.garmin
    Created on : 02-Jan-2010, 11:58:01
    Author     : Andreas Hadjigeorgiou
    Description:
        Purpose of the stylesheet follows.
*/

lbgm.garmin = {

    wnd: null,

    init: function() {
        lbgm.app.includeJS('src/js/Prototype/prototype.js');
        lbgm.app.includeJS('src/js/Garmin/util/Util-Broadcaster.js');
        lbgm.app.includeJS('src/js/Garmin/util/Util-BrowserDetect.js');
        lbgm.app.includeJS('src/js/Garmin/util/Util-DateTimeFormat.js');
        lbgm.app.includeJS('src/js/Garmin/util/Util-PluginDetect.js');
        lbgm.app.includeJS('src/js/Garmin/util/Util-XmlConverter.js');
        lbgm.app.includeJS('src/js/Garmin/activity/GarminMeasurement.js');
        lbgm.app.includeJS('src/js/Garmin/activity/GarminSeries.js');
        lbgm.app.includeJS('src/js/Garmin/activity/GarminSample.js');
        lbgm.app.includeJS('src/js/Garmin/activity/GarminActivity.js');
        lbgm.app.includeJS('src/js/Garmin/activity/GpxActivityFactory.js');
        //lbgm.app.includeJS('src/js/Garmin/device/GarminObjectGenerator.js');
        lbgm.app.includeJS('src/js/Garmin/device/GarminGpsDataStructures.js');
        lbgm.app.includeJS('src/js/Garmin/device/GarminPluginUtils.js');
        lbgm.app.includeJS('src/js/Garmin/device/GarminDevice.js');
        lbgm.app.includeJS('src/js/Garmin/device/GarminDevicePlugin.js');
        lbgm.app.includeJS('src/js/Garmin/device/GarminDeviceControl.js');
        lbgm.app.includeJS('src/js/LBGarminDeviceControl.js');
    },

    loadWaypoints: function() {
        this.wnd = new Ext.Window({
            id: "gpxGarminWnd",
            //layout:'anchor',
            width:400,
            height:148,
            closeAction:'close',
            plain: false,
            closable: true,
            draggable: true,
            title: 'Garmin Device',
            constrain: true,
            autoScroll: true,
            resizable: true,
            modal: true,
            style: {opacity:0.85},
            items: [{
                id: 'garmin_status',
                html: 'Loading ...'
            },
            {id: 'pbar'},
            {
                id: 'jqgWaypointslist'
            }]
        });

        this.wnd.on('show', function() {
            var pb = new Ext.ProgressBar({
               id: 'gpb',
               renderTo: 'pbar'
            });

            pb.wait();

            obj = new LBGarminDeviceControl("garmin_status", ["http://lightblack.homelinux.com/geomaps/", "56299970d023927f115fa3e82906bde6",
                                    "http://192.168.10.101", "338dca7e3b3da1d14ab7fcf19b36e124"],
                            lbgm.garmin.controlCB);
        });

        this.wnd.on('close', function() {lbgm.garmin.wnd = null});

        this.wnd.show();
    },

    controlCB: function(action, param) {
        //alert(action);
        if(action == 'updatePB') {
            Ext.getCmp('gpb').updateText(param);
        }
        else if(action == 'closeWnd') {
            lbgm.garmin.wnd.close();
        }
        else if(action == 'listWaypoints') {
            //alert(Ext.encode(param));
            //var t = param[0];
            //alert(t.wpname);

            
            var wpTab = Ext.getCmp('waypointPanel');

            if(wpTab != undefined) {
                if(wpTab.items.length > 0) wpTab.removeAll(true);

                var store = new Ext.data.JsonStore({
                    // store configs
                    autoDestroy: true,
                    data: param,
                    fields: ['id', 'wpname', 'lat', 'lon']
                });

                var wpGrid = new Ext.ux.grid.DataGrid({
                    title: 'Waypoints',
                    displayOnly: true,
                    forceFit: true,
                    store: store,
                    primaryKey: 'id',

                    colModel: new Ext.grid.ColumnModel({
                        columns: [
                            {name: 'id', header: 'ID', dataIndex: 'id'},
                            {name: 'wpname', header: 'Name', dataIndex: 'wpname', sortable: true},
                            {name: 'lat', header: 'Latitude', dataIndex: 'lat'},
                            {name: 'lon', header: 'Longitude', dataIndex: 'lon'},
                            {name: 'desc', header: 'Description', dataIndex: 'desc', editable: true, editor: {xtype: "textfield"}}
                        ]
                    })
                });
                
                wpTab.add(wpGrid);
                store.loadData(param);
                Ext.getCmp('gpx-panel').doLayout();
                Ext.getCmp('waypointPanel').setActiveTab(0);
            }
        }
    }
};

