RegionenNavigationPanel = function()
{
    //-----< define object vars >-----------------------------------------------
    this.currentState = '';
    this.navigating = false;
    //-----< create search combo >------------------------------------------
    this.search_store = new Ext.data.Store({
        proxy : new Ext.data.HttpProxy({
            url : 'services/RegionenSearch.php?source=bayern'
        }),
        reader : new Ext.data.JsonReader({
            root : 'regionen',
            totalProperty : 'count',
            id : 'reg_nr'
        }, [
            {name: 'name', mapping: 'reg_name'},
            {name: 'regnr', mapping: 'reg_nr'}
        ])
    });
    this.search_combo = new Ext.form.ComboBox({
        store : this.search_store,
        displayField : 'name',
        typeAhead : true,
        mode : 'remote',
        minChars : 2,
        triggerAction : 'all',
        emptyText : 'Suche...',
        loadingText : 'Suche läuft...',
        forceSelection : true,
        selectOnFocus : true,
        width : 150
    });
    //-----< create root node >-------------------------------------------------
    this.root_node = new Ext.tree.AsyncTreeNode({
        text : 'Root',
        draggable : false,
        id : '00'
    });
    this.root_node.on('expand', this.onRootNodeExpand, this);
    //-----< call parent constructor >------------------------------------------
    RegionenNavigationPanel.superclass.constructor.call(this, {
        animate : true,
        enableDD : false,
        rootVisible : true,
        lines : false,
        loader : new ParameterizedTreeLoader({
            dataUrl: 'services/RegionenTree.php'
        }),
        border : true,
        autoScroll : true,
        collapseFirst : false,
        singleExpand : false,
        root : this.root_node,
        tbar : this.search_combo
    });
    //-----< additional tasks after parents constructor has run >---------------
    this.addEvents({regnrselect:true});
    this.addEvents({strukdatselect:true});
    this.addEvents({articleselect:true});
    this.addEvents({firmendatselect:true});
    this.on('click', this.onClick, this);
    this.on('expandnode', this.iterativeCollapseNeighbours, this);
    this.on('resize',function(panel,adjWidth,adjHeight,rawWidth,rawHeight){this.search_combo.setWidth(adjWidth)});
    this.search_combo.on('select',this.onSearchComboSelect,this);
};

//-----< define new class MerkmalePanel >---------------------------------------
Ext.extend(RegionenNavigationPanel, Ext.tree.TreePanel,
{

    // private
    onSearchComboSelect : function(combo,record,index)
    {
        combo.reset();
        this.gotoRegnr(record.id);
    },

    switchToBayern : function(path)
    {
        if(this.currentState == 'bayern')
        {
            if(path)
            {
                this.navigating = true;
                var x = this;
                this.expandPath(path,null,function(bSuccess,oLastNode){if(bSuccess){oLastNode.select();x.onClick(oLastNode);x.resetNavigating.defer(3000,x);}});
            }
            else
            {
                this.onClick(this.getSelectionModel().getSelectedNode());
            }
            return;
        }
        this.getLoader().dataUrl = 'services/RegionenTree.php';
        this.getLoader().setBaseParams( { source : 'bayern' } );
        this.currentState = 'bayern';
        this.root.setText('Bayern');
        this.reset();
        this.root.expand();
        if(path)
        {
            this.navigating = true;
            var x = this;
            this.expandPath(path,null,function(bSuccess,oLastNode){if(bSuccess){oLastNode.select();x.onClick(oLastNode);x.resetNavigating.defer(3000,x);}});
        }
        else
        {
            this.root.select();
            this.fireEvent('articleselect', this.currentState, '00');
        }
    },

    switchToIHK : function(path)
    {
        if(this.currentState == 'ihk')
        {
            if(path)
            {
                this.navigating = true;
                var x = this;
                this.expandPath(path,null,function(bSuccess,oLastNode){if(bSuccess){oLastNode.select();x.onClick(oLastNode);x.resetNavigating.defer(3000,x);}});
            }
            else
            {
                this.onClick(this.getSelectionModel().getSelectedNode());
            }
            return;
        }
        this.getLoader().dataUrl = 'services/RegionenTree.php';
        this.getLoader().setBaseParams( { source : 'ihk' } );
        this.currentState = 'ihk';
        this.root.setText('IHK-Bezirk');
        this.reset();
        this.root.expand();
        if(path)
        {
            this.navigating = true;
            var x = this;
            this.expandPath(path,null,function(bSuccess,oLastNode){if(bSuccess){oLastNode.select();x.onClick(oLastNode);x.resetNavigating.defer(3000,x);}});
        }
        else
        {
            this.root.select();
            this.fireEvent('articleselect', this.currentState, '00');
        }
    },

    reset : function()
    {
        if(this.root.loaded)
        {
            this.root.collapse(false, false);
            while(this.root.firstChild){
                this.root.removeChild(this.root.firstChild);
            }
            this.root.childrenRendered = false;
            this.root.loaded = false;
        }
    },

    gotoRegnr : function(regnr)
    {
        var path;
        if(regnr.length == 8) // Gemeinde, krsfr. Std.
        {
            if(this.currentState == 'ihk')
            {
                path = '/00/01/3909600000'; // Bayern(root) / Verwaltungseinheiten / IHK Bezirk
            }
            else
            {
                path = '/00/01/0309'; // Bayern(root) / Verwaltungseinheiten / Freistaat Bayerb
                path = path + '/' + '13' + regnr.substr(0,3); // Regierungsbezirk
            }
            if(regnr.substr(5,5) == '000')
            {
                // kreisfreie Stadt
                path = path + '/' + '12' + regnr.substr(0,8); // kreisfreie Stadt
            }
            else
            {
                // Gemeinde
                path = path + '/' + '14' + regnr.substr(0,5); // Landkreis
                path = path + '/' + '12' + regnr.substr(0,8); // Gemeinde
            }
            this.navigating = true;
            var x = this;
            this.expandPath(path,null,function(bSuccess,oLastNode){if(bSuccess){oLastNode.select();oLastNode.fireEvent('click',oLastNode);x.resetNavigating.defer(3000,x);}});
        }
        else if(regnr.length == 5) // Landkreis
        {
            if(this.currentState == 'ihk')
            {
                path = '/00/01/3909600000'; // Bayern(root) / Verwaltungseinheiten / IHK Bezirk
            }
            else
            {
                path = '/00/01/0309'; // Bayern(root) / Verwaltungseinheiten / Freistaat Bayerb
                path = path + '/' + '13' + regnr.substr(0,3); // Regierungsbezirk
            }
            path = path + '/' + '14' + regnr.substr(0,5); // Landkreis
            this.navigating = true;
            var x = this;
            this.expandPath(path,null,function(bSuccess,oLastNode){if(bSuccess){oLastNode.select();oLastNode.fireEvent('click',oLastNode);x.resetNavigating.defer(3000,x);}});
        }
        else if(regnr.length == 3) // Regbez
        {
            if(this.currentState == 'ihk')
                return;
            path = '/00/01/0309'; // Bayern(root) / Verwaltungseinheiten / Freistaat Bayerb
            path = path + '/' + '13' + regnr.substr(0,3); // Regierungsbezirk
            this.navigating = true;
            var x = this;
            this.expandPath(path,null,function(bSuccess,oLastNode){if(bSuccess){oLastNode.select();oLastNode.fireEvent('click',oLastNode);x.resetNavigating.defer(3000,x);}});
        }
    },

    // private
    onClick : function(node)
    {
        //-----< get node type and param >--------------------------------------
        var node_id = node.id;
        var node_type = node_id.substr(0,2);
        var node_param = node_id.substr(2);
        //-----< fire events depending on node type >---------------------------
        if(node_type == '42')
        {
            // firmendaten: jahre
            this.fireEvent('firmendatselect', 'j', null);
        }
        else if( (node_type == '43') || (node_type == '44') || (node_type == '45') )
        {
            // firmendaten: wzbra
            this.fireEvent('firmendatselect', 'w', node_param);
        }
        else if(node_type == '38')
        {
            if(node_param.length > 0)
            {
                // strukdat
                this.fireEvent('strukdatselect', node_param);
            }
            else
            {
                this.fireEvent('articleselect', this.currentState, node_type);
            }
        }
        else if( (node_type >= '27') && (node_type <= '36') )
        {
            // special article nodes with params
            this.fireEvent('articleselect', this.currentState, node_type);
        }
        else
        {
            if(node_param.length > 0)
            {
                // regnr
                this.fireEvent('regnrselect', node_param);
            }
            else
            {
                // article
                this.fireEvent('articleselect', this.currentState, node_type);
            }
        }
        return(true);
    },

    // private
    iterativeCollapseNeighbours : function(node)
    {
        //-----< do not collapse while navigating >-----------------------------
        if(this.navigating)
        {
            return;
        }
        //-----< we stop at the root node >-------------------------------------
        if(node.isRoot)
            return;
        //-----< get my parent >------------------------------------------------
        var parent = node.parentNode;
        //-----< collapse all childNodes of my parent except me >---------------
        var i;
        for(i = 0; i < parent.childNodes.length; i++)
        {
            var bro = parent.childNodes[i];
            if(bro != node)
            {
                if( parent.isRoot )
                {
                    bro.collapseChildNodes(true);
                }
                else
                {
                    bro.collapse(true,true);
                }
            }
        }
        //-----< continue with parent node >------------------------------------
        this.iterativeCollapseNeighbours(parent);
    },

    resetNavigating : function()
    {
        //alert('resetting');
        this.navigating = false;
    },

    // private
    onRootNodeExpand : function(node)
    {
        // expand all childs
        node.expandChildNodes(false);
    }

});
