DaReZaMainPanel = function()
{
    //-----< define object vars >-----------------------------------------------
    this.currentState = '';
    this.selectedStdtabId = null;
    this.selectedStdtabRegionaleinheiten = Array();
    //-----< create stack panel for 'search' >----------------------------------
    this.search_panel = new Ext.Panel({
        autoScroll : true,
        border : true,
        autoLoad : 'services/search.php'
    });
    //-----< create navigation panels >-----------------------------------------
    this.articlenavigation_panel = new ArticleNavigationPanel();
    this.regionennavigation_panel = new RegionenNavigationPanel();
    this.stdtabnavigation_panel = new Ext.tree.TreePanel({
        animate : true,
        enableDD : false,
        rootVisible : true,
        lines : false,
        loader : new ParameterizedTreeLoader({
            dataUrl: 'services/StdtabTree.php'
        }),
        border : true,
        autoScroll : true,
        collapseFirst : false,
        singleExpand : false,
        root :new Ext.tree.AsyncTreeNode({
           text: 'Standardtabellen',
           draggable:false,
           id:'00'
        }),
        rootVisible : false
    });
    this.tabgennavigation_panel = new TabgenNavigationPanel();
    this.navigationstack_panel = new Ext.Panel({
        region : 'west',
        split : true,
        minSize : 100,
        width : 250,
        autoScroll : false,
        border : false,
        layout : 'card',
        items : [ this.articlenavigation_panel, this.regionennavigation_panel, this.stdtabnavigation_panel, this.tabgennavigation_panel ]
    });
    //-----< create content panels >--------------------------------------------
    this.articlecontent_panel = new Ext.Panel({
        autoScroll : true,
        border : true
    });
    this.regionencontent_panel = new RegionenContentPanel();
    this.tabgen_panel = new TabgenPanel();
    this.contentstack_panel = new Ext.Panel({
        region : 'center',
        autoScroll : false,
        border : false,
        layout : 'card',
        items : [ this.articlecontent_panel, this.regionencontent_panel, this.tabgen_panel ]
    });
    //-----< create explorer like panel >---------------------------------------
    this.explorer_panel = new Ext.Panel({
        autoScroll : false,
        border : false,
        layout : 'border',
        items : [ this.navigationstack_panel, this.contentstack_panel ]
    });
    //-----< call parent constructor >------------------------------------------
    DaReZaMainPanel.superclass.constructor.call(this, {
        region : 'center',
        layout : 'card',
        border : false,
        activeItem : 0,
        items: [ this.explorer_panel, this.search_panel ]
    });
    //-----< add events >-------------------------------------------------------
    this.articlenavigation_panel.on('articleselect', this.onArticleSelect, this);
    this.stdtabnavigation_panel.on('click', this.onStdtabClick, this);
    this.regionennavigation_panel.on('regnrselect', this.regionencontent_panel.showRegnr, this.regionencontent_panel);
    this.regionennavigation_panel.on('strukdatselect', this.regionencontent_panel.showStrukdat, this.regionencontent_panel);
    this.regionennavigation_panel.on('articleselect', this.regionencontent_panel.showArticle, this.regionencontent_panel);
    this.regionennavigation_panel.on('firmendatselect', this.regionencontent_panel.showFirmendat, this.regionencontent_panel);
    this.tabgennavigation_panel.on('selectstep', this.tabgen_panel.showStep, this.tabgen_panel);
    this.tabgennavigation_panel.on('newtable', this.onTabgenNewTable, this);
    this.tabgen_panel.on('navigatetostep', this.tabgennavigation_panel.gotoStep, this.tabgennavigation_panel );
};

//-----< define new class MerkmalePanel >---------------------------------------
Ext.extend(DaReZaMainPanel, Ext.Panel,
{

    unsetTabActive : function()
    {
        document.getElementById('drz-main-tab-01').className = '';
        document.getElementById('drz-main-tab-02').className = '';
        document.getElementById('drz-main-tab-03').className = '';
        document.getElementById('drz-main-tab-04').className = '';
        document.getElementById('drz-main-tab-05').className = '';
        document.getElementById('drz-main-tab-06').className = '';
        document.getElementById('drz-main-tab-07').className = '';
        document.getElementById('drz-main-tab-08').className = '';
    },

    switchToStart : function(path)
    {
        // set current state
        if(this.currentState == 'themen')
        {
            if(path)
                this.articlenavigation_panel.switchToStart(path);
            return;
        }
        this.currentState == 'themen';
        this.unsetTabActive();
        document.getElementById('drz-main-tab-01').className = 'drz-tab-strip-active';
        // set explorer panel as active panel
        this.layout.setActiveItem(0);
        // set article navigation panel as active navigation panel
        this.navigationstack_panel.layout.setActiveItem(0);
        // set article content panel as active content panel
        this.contentstack_panel.layout.setActiveItem(0);
        // switch article navigation to themen
        this.articlenavigation_panel.switchToStart(path);
    },

    switchToLinks : function(path)
    {
        // set current state
        if(this.currentState == 'links')
        {
            if(path)
                this.articlenavigation_panel.switchToLinks(path);
            return;
        }
        this.currentState == 'links';
        this.unsetTabActive();
        document.getElementById('drz-main-tab-06').className = 'drz-tab-strip-active';
        // set explorer panel as active panel
        this.layout.setActiveItem(0);
        // set article navigation panel as active navigation panel
        this.navigationstack_panel.layout.setActiveItem(0);
        // set article content panel as active content panel
        this.contentstack_panel.layout.setActiveItem(0);
        // switch article navigation to links
        this.articlenavigation_panel.switchToLinks(path);
    },

    switchToHilfe : function(path)
    {
        // set current state
        if(this.currentState == 'links')
        {
            if(path)
                this.articlenavigation_panel.switchToHilfe(path);
            return;
        }
        this.currentState == 'hilfe';
        this.unsetTabActive();
        document.getElementById('drz-main-tab-07').className = 'drz-tab-strip-active';
        // set explorer panel as active panel
        this.layout.setActiveItem(0);
        // set article navigation panel as active navigation panel
        this.navigationstack_panel.layout.setActiveItem(0);
        // set article content panel as active content panel
        this.contentstack_panel.layout.setActiveItem(0);
        // switch article navigation to hilfe
        this.articlenavigation_panel.switchToHilfe(path);
    },

    switchToBayern : function(path)
    {
        // set current state
        if(this.currentState == 'bayern')
        {
            if(path)
                this.regionennavigation_panel.switchToBayern(path);
            return;
        }
        this.currentState == 'bayern';
        this.unsetTabActive();
        document.getElementById('drz-main-tab-05').className = 'drz-tab-strip-active';
        // set explorer panel as active panel
        this.layout.setActiveItem(0);
        // set regionen navigation panel as active navigation panel
        this.navigationstack_panel.layout.setActiveItem(1);
        // set regionen content panel as active content panel
        this.contentstack_panel.layout.setActiveItem(1);
        // switch navigation
        this.regionennavigation_panel.switchToBayern(path);
    },

    switchToIHK : function(path)
    {
        // set current state
        if(this.currentState == 'ihk')
        {
            if(path)
                this.regionennavigation_panel.switchToIHK(path);
            return;
        }
        this.currentState == 'ihk';
        this.unsetTabActive();
        document.getElementById('drz-main-tab-04').className = 'drz-tab-strip-active';
        // set explorer panel as active panel
        this.layout.setActiveItem(0);
        // set regionen navigation panel as active navigation panel
        this.navigationstack_panel.layout.setActiveItem(1);
        // set regionen content panel as active content panel
        this.contentstack_panel.layout.setActiveItem(1);
        // switch navigation
        this.regionennavigation_panel.switchToIHK(path);
    },

    switchToSuchen : function()
    {
        // set current state
        if(this.currentState == 'suchen')
            return;
        this.currentState == 'suchen';
        this.unsetTabActive();
        document.getElementById('drz-main-tab-08').className = 'drz-tab-strip-active';
        // just set search panel as active panel
        this.layout.setActiveItem(1);
    },

    switchToStdtab : function()
    {
        // set current state
        if(this.currentState == 'stdtab')
            return;
        this.currentState == 'stdtab';
        this.unsetTabActive();
        document.getElementById('drz-main-tab-02').className = 'drz-tab-strip-active';
        // set explorer panel as active panel
        this.layout.setActiveItem(0);
        // set stdtab navigation panel as active navigation panel
        this.navigationstack_panel.layout.setActiveItem(2);
        // set article content panel as active content panel
        this.contentstack_panel.layout.setActiveItem(0);
        // show currently selected stdtab
        this.updateStdtab();
    },

    switchToTabgen : function()
    {
        // set current state
        if(this.currentState == 'tabgen')
            return;
        this.currentState == 'tabgen';
        this.unsetTabActive();
        document.getElementById('drz-main-tab-03').className = 'drz-tab-strip-active';
        // set explorer panel as active panel
        this.layout.setActiveItem(0);
        // set tabgen navigation panel as active navigation panel
        this.navigationstack_panel.layout.setActiveItem(3);
        // set article content panel as active content panel
        this.contentstack_panel.layout.setActiveItem(2);
    },

    setTabgenGemdat : function()
    {
        // set the berichtssystem
        this.tabgen_panel.setBerichtssystem('gemdat');
        // change available steps. will fire stepselect event.
        this.tabgennavigation_panel.setActive(true);
    },

    setTabgenKrsdat : function()
    {
        // set the berichtssystem
        this.tabgen_panel.setBerichtssystem('krsdat');
        // change available steps. will fire stepselect event.
        this.tabgennavigation_panel.setActive(true);
    },

    onTabgenNewTable : function()
    {
        // change available steps. will fire stepselect event.
        this.tabgennavigation_panel.setActive(false);
    },

    reloadOverview : function(url)
    {
        this.regionencontent_panel.reloadOverview(url);
    },

    reloadGemdat : function(url)
    {
        this.regionencontent_panel.reloadGemdat(url);
    },

    reloadKrsdat : function(url)
    {
        this.regionencontent_panel.reloadKrsdat(url);
    },

    isInIHKBezirk : function(regnr)
    {
        if(
            ( (regnr.length == 5) || (regnr.length == 8) ) &&   // Gemeinde oder Landkreis
            ( regnr.substr(0,3) == '096' ) &&                   // in Unterfranken
            ( regnr != '09661000' ) &&
            ( regnr.substr(0,5) != '09671' ) &&
            ( regnr.substr(0,5) != '09676' )
          )
        {
            return(true);
        }
        return(false);
    },

    gotoRegnr : function(regnr)
    {
        // switch to bayern or ihk
        if(!this.isInIHKBezirk(regnr))
        {
            this.switchToBayern();
        }
        else
        {
            if(! ( (this.currentState == 'bayern') || (this.currentState == 'ihk') ) )
                this.switchToIHK();
        }
        // goto regnr
        this.regionennavigation_panel.gotoRegnr(regnr);
    },

    performSearch : function(query)
    {
        var postparams = {};
        postparams['query'] = query;
        this.search_panel.load({url:'services/search.php', params: postparams});
    },

    postFirmendaten : function(url,formname)
    {
        this.regionencontent_panel.postFirmendaten(url,formname);
    },

    postStruktab : function(url,formname)
    {
        this.regionencontent_panel.postStruktab(url,formname);
    },

    onArticleSelect : function(state,id)
    {
        // load new content into panel
        this.articlecontent_panel.load('services/ArticleContent.php'+'?source='+state+'&node='+id);
    },

    onStdtabClick : function(node)
    {
        if(!node.isLeaf())
        {
            return(false);
        }
        this.selectedStdtabId = node.id;
        this.updateStdtab();
    },

    updateStdtab : function()
    {
        if(this.selectedStdtabId)
        {
            var postparams = {};
            postparams['tabid'] = this.selectedStdtabId;
            postparams['reg_cnt'] = this.selectedStdtabRegionaleinheiten.length;
            for(var i = 0; i < this.selectedStdtabRegionaleinheiten.length; i++)
            {
                postparams['reg_'+i] = this.selectedStdtabRegionaleinheiten[i].regnr;
            }
            this.articlecontent_panel.load({url: 'services/stdtab.php', params: postparams});
        }
        else
        {
            this.articlecontent_panel.load('StdtabWelcome.html');
        }
    },

    changeStdtabRegionen : function()
    {
        var x = this;
        //-----< create regionalauswahl panel >---------------------------------
        var reg_panel = new RegionaleinheitenPanel();
        reg_panel.addRegionaleinheiten(this.selectedStdtabRegionaleinheiten);
        //-----< create window >------------------------------------------------
        var window = new Ext.Window({
            title : 'Regionen auswählen',
            width : 800,
            height : 500,
            minWidth : 700,
            minHeight : 500,
            resizable : true,
            minimizable : false,
            maximizable : false,
            constrain : true,
            constrainHeader : true,
            plain : true,
            modal : true,
            footer : true,
            closable : true,
            // bodyStyle : 'padding:5px;',
            buttonAlign : 'center',
            layout : 'card',
            activeItem : 0,
            items: reg_panel,
            buttons: [{
                text: 'Übernehmen',
                // handler : this.windowsubmit
                handler: function()
                         {
                             x.selectedStdtabRegionaleinheiten = reg_panel.getRegionaleinheiten();
                             x.updateStdtab();
                             window.close();
                         }
            },{
                text: 'Abbruch',
                handler: function() { window.close(); }
            }]
        });
        // show modal
        window.show(null);
    }

});
