// ---------------------------------------------------------------------

function DNTabViewController (config) {
    // Sanity check the config object.
    if (typeof(config) === "Object") {
        console.error("You must specify a configuration object.");
        return null;
    }
    else if (typeof(config.tabPanelMap) === "Object") {
        console.error("You must set supply a tabPanelMap config option.");
        return null;
    }
    
    // Instance variables.
    this.config = config;
    this.tabPanelMap = config.tabPanelMap;
    this.tabSelectedClass = config.tabSelectedClass;
    this.tabUnselectedClass = config.tabUnselectedClass;
    
    // Ensure that each element specified actually exists.
    for (var tabId in this.tabPanelMap) {
        var panelId = this.tabPanelMap[tabId];
        
        // Check that we were given good ids.
        if (!$(tabId)) {
            console.error("Failed to find element '" + tabId + "'.");
            continue;
        }
        else if (!$(panelId)) {
            console.error("Failed to find element '" + panelId + "'.");
            continue;
        }
    }
}

// ---------------------------------------------------------------------
// DNTabViewController.showPanelForTab()

DNTabViewController.prototype.showPanelForTab = function (tab) {
    var panelId = this.tabPanelMap[tab.id];
    
    this.hideAll();
    if (this.tabSelectedClass) tab.className = this.tabSelectedClass;
    $(panelId).style.display = "block";
}

// ---------------------------------------------------------------------
// DNTabViewController.hideAll()

DNTabViewController.prototype.hideAll = function () {
    for (var tabId in this.tabPanelMap) {
        var panelId = this.tabPanelMap[tabId];
        
        if (!$(panelId)) {
            console.error("Failed to find element '" + panelId + "'.");
            continue;
        }
        
        $(panelId).style.display = "none";
        $(tabId).className = this.tabUnselectedClass;
    }
}

// ---------------------------------------------------------------------
