/* TV 2 namespace */
if(typeof TV2 == 'undefined'){ TV2 = {}; }
if(typeof TV2.GO == 'undefined'){ TV2.GO = {}; }

TV2.GO.tabList = (function(){

    var lists = new Array(),

    add = function(nodes){
        /* add a list of tab lists to keep track of */
        for(var l = nodes.length-1; l>=0; l--){
            lists.push(new List(nodes[l]));
        }
    },

    getTabs = function(node){
        /* */
        return YAHOO.util.Dom.getElementsByClassName('tab','li',node);
    };

    /* list */
    var List = function(node){
        this.node = node;
        this.refresh(0);
    };
        List.prototype.createTabBar = function(){

            var ol = document.createElement('ol');

            for(var i = 0, l = this.tabs.length; i<l; i++){
                /* create a list item; get the titles of the sections and
                 * add it to the list */
                var li = document.createElement('li');
                var anchor = document.createElement('a');

                var title = YAHOO.util.Dom.getElementsByClassName(
                    'title',
                    'h3',
                    this.tabs[i]
                )[0];

                var text = document.createTextNode(title.firstChild.nodeValue || 'undefined');
                anchor.appendChild(text);
                anchor.className = 'tab-n'+i;
                if(i === 0) anchor.className += ' selected';
                anchor.href = '#';

                ol.className = 'tab-selector';

                li.appendChild(anchor);
                ol.appendChild(li);
            }

            this.tabBar = YAHOO.util.Dom.insertBefore(
                ol,
                YAHOO.util.Dom.getFirstChildBy(
                    this.node,
                    function(o){ return YAHOO.util.Dom.hasClass(o, 'hfeed'); }
                )
            );

        };

        List.prototype.refresh = function(startAt){
            /* refresh the list */
            this.tabs = getTabs(this.node);
            this.current = null;
            this.changeTo( typeof startAt == 'number' ? startAt : 0 );
            this.createTabBar();
            this.addListeners();

        };

        List.prototype.removeListeners = function(){

        };

        List.prototype.addListeners = function(){
            /* remove old listeneres on the tabs and add new */
            YAHOO.util.Event.addListener(
                this.tabBar,'click',this.changeTo, {}, this
            );
        };

        List.prototype.changeTo = function(i){
            /* change to the given tab */
             YAHOO.util.Event.preventDefault(i);

            /* if the call is made by a click handler, fetch the number */
            var target = YAHOO.util.Event.getTarget(i);
            if(target){
                if(target.tagName.toLowerCase() == 'a'){
                    var num = target.className.replace(/tab-n([0-9]*)/,"$1");
                    i = parseInt(num,10);
                }
            }

            var node;
            switch(typeof i){
                case 'number' :
                    // if the given number is larger than the number of items
                    // in the list, use the fist.
                    node = (this.tabs.length > i)
                        ? this.tabs[i]
                        : this.tabs[0];
                break;
                default: return; /* exit if the input doesn't make sense */
            }

            /* remove display from previous current */
            YAHOO.util.Dom.removeClass(this.current, 'display');

            /* add 'display' to new current */
            YAHOO.util.Dom.addClass(this.current = node, 'display');

            if(this.tabBar && target){
                /* remove the 'selected'-class from the old 'current' tab(s) */
                YAHOO.util.Dom.getElementsByClassName(
                    'selected', 'a', this.tabBar, function(o){
                        YAHOO.util.Dom.removeClass(o, 'selected')
                    }
                );

                /* apply the 'selected'-class to the new current tab */
                YAHOO.util.Dom.addClass(target, 'selected');
            }
        };

    return {
        init : function(){
            // look for tab lists on the page
            add(YAHOO.util.Dom.getElementsByClassName(
                'content-article-tablist', 'div', 'top'
            ));

        },
        /* add a tab list functionality to a tab list */
        add : add,
        debug : function(){ }
    };
}());

YAHOO.util.Event.onDOMReady(function(){ TV2.GO.tabList.init(); });