﻿// ------------ profile panel helper class ------------ //
Ext.ux.ProfileContentPanelHelper = Ext.extend(Ext.ux.ContentPanelHelper, {

    // internal fields
    // flag to indicate we don't load panel content first time around
    // since the panel for the default tab is rendered by the ContentPanelUserControl
    // (we actually specify the ascx control itself in the ContentPanelSettings to save an additional round-trip)
    m_loadPanelContent: false,


    // standard constructor
    constructor: function(
        contentElementId, footerElementId) {

        Ext.ux.ProfileContentPanelHelper.superclass.constructor.call(this, contentElementId, footerElementId, null);
    },

    // standard initializer
    initComponent: function() {
        Ext.ux.ProfileContentPanelHelper.superclass.initComponent.apply(this, arguments);

        // extra args have to go here as base class initComponent doesn't get called for some reason
        var config = {};

        Ext.apply(this, config);
        Ext.apply(this.initialConfig, config);
    },


    // initialization
    initialize: function(tabs) {
        this.m_loadPanelContent = false;
        Ext.ux.ProfileContentPanelHelper.superclass.initialize.call(this, tabs, delegate(this, this.handleTabClick));
    },

    // handle a tab click
    handleTabClick: function(event) {
        var newTabId = event.data;
        this.doTabClick(newTabId, false);
    },

    // simulate click on default tab
    doDefaultTabClick: function() {
        var newTabId = this.getDefaultTabId();
        this.doTabClick(newTabId, false);
    },
    
    // do the tab click logic
    doTabClick: function(newTabId, forceRefresh) {

        // call content panel functions to change button states, load panel (if necessary) & execute callback once done
        var newTabData      = this.getTabDataById(newTabId);
        var currentTabData  = this.getTabDataById(this.getCurrentTabId()); // call base class x2
  
        this.setCurrentTabId(newTabId);

        // call base class
        Ext.ux.ProfileContentPanelHelper.superclass.doTabClick.call(this, this.getCurrentTabId());

        // load panel (if necessary)
        if((this.m_loadPanelContent && newTabData && newTabData['url'] && (newTabData['url'] !== "")) &&
           (!currentTabData || (currentTabData && ((currentTabData['url'] != newTabData['url']) || forceRefresh)))) {

            // load new panel
            this.loadPanelContent(newTabId, null);
        }

        this.m_loadPanelContent = true;

        return false;
    },

    // load data into the tracks grids
    loadData: function() {
        var tabData = this.getTabDataById(this.getCurrentTabId()); // call base class x2
        if (tabData) {
            this.configureForm(tabData);
        }
    },

    /* form configuration */
    configureForm: function(tabData) {
        this.showFooter(false);
    }
});