Ext.ns('Wizard');

Wizard.Tablist = Ext.extend(Ext.TabPanel, {
    activeItem: 0,
    deferredRender: false,
    curPos: 0,
    cls: 'wizard',
    initComponent: function(){
        Ext.apply(this, {
            defaults: {
                bodyStyle: 'padding:10px',
                anchor: '98%'
            },
            items: [{
                xtype: 'uploadPanel'
            }, {
                xtype: 'editTrackPanel'
            }],
            buttons: [/*{
                text: '< Zurück',
                id: 'wzd_btn_back',
                disabled: true,
                handler: function(){
                    this.ownerCt.previousPanel(this);
                }
            },*/ 
            {
               text: 'Abbrechen',
               handler: function() {
                  if (this.ownerCt.curPos == 1) {
                     Ext.Msg.show({
                        title: 'Upload abbrechen',
                        msg: 'Möchten sie den Upload wirklich abbrechen?',
                        buttons: Ext.Msg.YESNO,
                        icon: Ext.Msg.WARNING,
                        fn: function(btn) {
                           if (btn == "yes") {
                              Ext.Ajax.request({
                                 url: './STWizardServlet',
                                 params: {
                                    action: 'deleteTrack',
                                    trackId: Ext.getCmp('tab').items.get(1).trackId
                                 },
                                 success: function(responseObject) {
                                    var response = Ext.decode(responseObject.responseText);
                                    if (response.success) {
                                       Ext.getCmp('wizardWin').close();
                                    } else {
                                       Ext.Msg.show({
                                          title: 'Fehler',
                                          msg: response.msg,
                                          icon: Ext.Msg.ERROR,
                                          buttons: Ext.Msg.OK,
                                          fn: function() {
                                             Ext.getCmp('wizardWin').close();
                                          }
                                       });
                                    }
                                 }
                              });
                           } 
                        }
                     });
                  } else {
                     Ext.getCmp('wizardWin').close();
                  } 
               }
            },{
                text: 'Weiter >',
                id: 'wzd_btn_next',
                disabled: true,
                handler: function(){
                    this.ownerCt.nextBtnHandler();
                }
            }]
        });
        Wizard.Tablist.superclass.initComponent.apply(this, arguments);
        this.sequence = this.newTrackWorkflow;
    } // eo function initComponent
    ,
    getActiveTabIndex: function(){
        for (var i = 0; i < this.items.getCount(); i++) {
            if (this.activeTab == this.items.get(i)) {
                return i;
            }
        }
        return -1;
    },
    showTab: function(index, param){
        var tab = this.items.get(index);
        if (tab) {
            this.setActiveTab(tab);
            this.setWizardTitle(tab.title);

            if (typeof(tab.initPanel) == "function") {
                tab.initPanel(param);
            }
            return true;
        }
        else {
            return false;
        }
    },
    setWizardTitle: function(title){
        this.ownerCt.setTitle(title + ' - Upload & Edit Wizard');

    },
    nextBtnHandler: function(){
        //Werte Validieren
        var curPanel = this.items.get(this.curPos);
        //Abbrechen wenn checContent methode vorhanden und false zurückgibt
        if (typeof(curPanel.submitContent) == "function") {
            curPanel.submitContent();
        }
        else {
            this.nextPanel();
        }
    },
    nextPanel: function(param){
        this.curPos++;
        if (this.curPos == 1) {
        //Ext.getCmp('wzd_btn_back').enable();
        }
        if (this.items.length - 1 == this.curPos) {
           var wzd_btn_next = Ext.getCmp('wzd_btn_next');
           wzd_btn_next.setText('Fertigstellen');
           wzd_btn_next.disable();
        }
        if (!this.showTab(this.curPos, param)) {
            var trackId = this.items.get(this.curPos-1).trackId;
            //Speichere Daten!
            this.ownerCt.close();
	    Ext.Msg.show({
		title: 'Upload & Edit Wizard',
		msg: 'Ihr Track wurde erfolgreich gespeichert und ist ab sofort unter der Track-ID STP-'+trackId+' sichtbar.',
		buttons: { cancel: 'OK', ok:'Track anzeigen' },
		fn: function(btn) {
			store.reload();
            if (btn == 'ok') {
				trackWindow(trackId);
			}
		}
	   });
        }
    },
    previousPanel: function(btn){
        if (this.items.length - 1 == this.curPos) {
            Ext.getCmp('wzd_btn_next').setText('Weiter >');
        }
        this.curPos--;
        if (this.curPos === 0) {
            btn.disable();
        }
        this.showTab(this.curPos);
        this.ownerCt.syncShadow();

    },
    setNewTrackPath: function(){
        this.sequence = this.newTrackWorkflow;
    },
    setEditExistingTrackPath: function(){
        this.sequence = this.existingTrackWorkflow;
    }


});
Ext.reg('wiz_tablist', Wizard.Tablist);
