dojo.require("dijit.layout.TabContainer");
dojo.require("dijit.layout.ContentPane");

function ContextBoxTabContainer(rootElement, portal, sourceDocType, sourceDocID, myURL, selectedTabType){
    this.rootElement = rootElement;
    this.id = dojo.attr(this.rootElement, 'id');
    this.portal = portal;
    this.sourceDocType = sourceDocType;
    this.sourceDocID = sourceDocID;
    this.myURL = myURL;
    this.selectedTabType = selectedTabType;

    //  this.tabs.x property needs to be identical to the object's type property.
    this.tabs = {};
    this.tabs.news = {title: 'News & Themen', type: 'news', status: 'clean'};
    this.tabs.forum = {title: 'Forum', type: 'forum', status: 'clean'};
    this.tabs.idesk = {title: 'Praxiswissen', type: 'idesk', status: 'clean'};

    this.tabContainer = new dijit.layout.TabContainer({style: "height: 100%; width: 100%;"}, this.id);
    var tabCount = 0;
    for (key in this.tabs){
        var title = this.tabs[key].title;
        var type = this.tabs[key].type;
        var tab = new dijit.layout.ContentPane({
            title: title,
            content: "",
            selected: (type == this.selectedTabType) ? true : false});
        // add specific information as type property to the tab
        tab.type = type;
        tab._setClassAttr('loading');
        this.tabContainer.addChild(tab);
        if (type == this.selectedTabType){
            // load first tab
            this.load(tab);
        }
        tabCount++;
    }
    this.tabContainer.startup();
   
    // set tab button's css class for rounded corners
    if (this.tabContainer.getChildren().length >= 0){
        var button = this.tabContainer.getChildren()[0].controlButton;
        button._setClassAttr('first');
        
        if (tabCount > 0){
            button = this.tabContainer.getChildren()[tabCount - 1].controlButton;
            button._setClassAttr('last');
        }
    }
    
    // connect onTabChange event
    dojo.subscribe(this.id + '-selectChild', dojo.hitch(this, 'onTabChange'));
}

ContextBoxTabContainer.prototype.load = function(tab){
    var self = this;
    var tabElement = dojo.byId(tab.id)
    var url = this.myURL;
    url += "/contextBoxContent?";
    url += "portal=" + this.portal;
    url += "&sourceDocType=" + this.sourceDocType;
    url += "&sourceDocID=" + this.sourceDocID;
    url += "&type=" + tab.type;

    dojo.xhrGet( {
        url: url,
        handleAs: "text",
        timeout: 10000,
        load: function(response, ioArgs){
            //Erfolgreicher get
            self.tabs[tab.type].status = 'loaded';
            dojo.byId(tab.id).innerHTML = response;
            tabElement.className = tabElement.className.replace(" loading", "");
        },
        error: function(response, ioArgs){
            //Fehler
            self.tabs[tab.type].status = 'failure';
            var innerHTML = "Zu diesem Beitrag sind keine weiteren Inhalte vorhanden. ";
            innerHTML += "Interessante Produkte und L&ouml;sungen finden Sie unter <a href='http://www.haufe.de/shop'>www.haufe.de/shop</a>.";
            dojo.byId(tab.id).innerHTML = innerHTML;
            tabElement.className = tabElement.className.replace(" loading", "");
        },
        handle: function(response, ioArgs){}
    });
}

ContextBoxTabContainer.prototype.onTabChange = function(tab){
    if (this.tabs[tab.type].status == 'clean'){
        this.load(tab);
    }
}