/**
 * A TabWindow object can be used to control a tabWindow
 * eventually with scrollable tabs.
 * @constructor
 */
function TabWindow(tabWindow) {
    this.tabWindow = tabWindow;
    this.options = {
        maxShowTabs: 99
    };
    this.tabElements = null;
    this.firstVisible = 0;
    this.lastVisible = 0;
    this.activeTab = 0;
}

TabWindow.prototype.initialize = function(options) {
    this.merge(this.options, options);
    var me = this;
    var tabHeader = pDomApi.getElementsByClassName(this.tabWindow, 'ul', 'tabHeader')[0];
    this.tabElements = pDomApi.getElementsByClassName(tabHeader, '*', 'normalTab');

    this.moveLogo();

    if (this.tabElements.length <= 0) {
        // no tabs available
        return false;
    }

    this.tabScrollPrevious = pDomApi.getElementsByClassName(this.tabWindow, '*', 'tabScrollPrevious')[0];
    this.tabScrollNext = pDomApi.getElementsByClassName(this.tabWindow, '*', 'tabScrollNext')[0];

    if (this.tabElements.length <= this.options.maxShowTabs) {
        pDomApi.setClassName(this.tabScrollPrevious, 'hide', 'show');
        pDomApi.setClassName(this.tabScrollNext, 'hide', 'show');
    } else {
        pDomApi.addEvent(this.tabScrollPrevious, 'click', function(e) { me.tabScroll(e, 'previous'); });
        pDomApi.addEvent(this.tabScrollNext, 'click', function(e) { me.tabScroll(e, 'next'); });
    }
    this.initTabs();

    //Check if the list needs to be scrolled
    while (pDomApi.hasClassName(this.tabElements[this.activeTab], 'hide')) {
        this.tabScroll(null, 'next');
    }

    this.handleScrollers();
};

TabWindow.prototype.initTabs = function() {
    this.tabCount = this.tabElements.length;

    var isActive;

    for (var i = 0; i < this.tabCount; i++) {
        isActive = pDomApi.hasClassName(this.tabElements[i], 'itemOn');
        if (isActive) {
            this.activeTab = i;
        }
        if (i >= this.options.maxShowTabs) {
            pDomApi.setClassName(this.tabElements[i], 'hide', 'show');
        }
    }

    // set last visible tab
    this.lastVisible = i > this.options.maxShowTabs ? this.options.maxShowTabs - 1 : i - 1;
};

TabWindow.prototype.tabScroll = function(e, direction) {
    var index = 0;

    if (direction == 'next') {
        index = this.tabCount - 1;
    }

    if (pDomApi.hasClassName(this.tabElements[index], 'hide')) {

        if (direction == 'next') {
            //Go from lastVisible to next tab and make visible
            pDomApi.setClassName(this.tabElements[++this.lastVisible], 'show', 'hide');
            // Hide first visible
            pDomApi.setClassName(this.tabElements[this.firstVisible++], 'hide', 'show');

        } else {
            //Go from firstVisible to previous tab and make visible
            pDomApi.setClassName(this.tabElements[--this.firstVisible], 'show', 'hide');

            //Hide last visible tab
            pDomApi.setClassName(this.tabElements[this.lastVisible--], 'hide', 'show');
        }
        this.handleScrollers();
    }
    if (e) {
        // prevent default action
        pDomApi.preventDefault(e);
    }
}

TabWindow.prototype.handleScrollers = function() {

    // next scroller
    if (pDomApi.hasClassName(this.tabElements[this.tabCount - 1], 'hide')) {
        pDomApi.setClassName(this.tabScrollNext, 'active', 'inactive');
    } else {
        pDomApi.setClassName(this.tabScrollNext, 'inactive', 'active');
    }

    // previous scroller
    if (pDomApi.hasClassName(this.tabElements[0], 'hide')) {
        pDomApi.setClassName(this.tabScrollPrevious, 'active', 'inactive');
    } else {
        pDomApi.setClassName(this.tabScrollPrevious, 'inactive', 'active');
    }
};

TabWindow.prototype.merge = function(obj1, obj2) {
    for (var key in obj2) {
        obj1[key] = obj2[key];
    }
};

TabWindow.prototype.moveLogo = function() {
    var mainLogo = document.getElementById('mainLogo');
    if (mainLogo) {
        var tabBody = pDomApi.getElementsByClassName(this.tabWindow, '*', 'tabBody')[0];
        tabBody.insertBefore(mainLogo, tabBody.firstChild);
    }
};

pDomApi.addEvent(window, 'domload', function() {

    var tabWindows = pDomApi.getElementsByClassName(document.body, '*', 'tabWindow');
    for (var i =0; i < tabWindows.length; i++) {
        var tabWindow = new TabWindow(tabWindows[i]);
        tabWindow.initialize({ maxShowTabs: 6});
    }
});
