/*
 * xMDB Viewer - XML Movie Database Viewer; DHTML viewer for XML movie databases
 * Copyright (C) 2006 Attila Szabo
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 * See the GNU General Public License for more info at http://www.gnu.org/licenses/gpl.txt
 */

function TabbedNav (){
    var oContainer;
    var aItems = [];
    var selectedItem = null; // holds array index of the selected menu item

    this._constructor = function (containerId){
        oContainer = document.getElementById(containerId);
        if (!oContainer){
            return null;
        }
    }

    this.items = {
        length : function(){
            return aItems.length;
        },

        Add : function (oArgs){

            // validation 
            if (typeof oArgs != 'object')
                alert('Error: argument passed to method Add is of invalid data type!');
            if (!oArgs.label)
                alert("Error: mandatory option 'label' is missing from the passed option object to method 'Add'!");

            oArgs.css   = oArgs.css || '';         // CSS style sheet selector 
            oArgs.title = oArgs.title || '';       // tooltip text
            oArgs.link  = oArgs.link || '';        // URL to navigate to when menu item is clicked
            oArgs.onSelect = oArgs.onSelect || null; // callback function invoked when the menu item is selected (either programatically or by a mouse click)


            // Create LI HTML element
            var liElement = document.createElement("li");
            liElement.className = oArgs.css;


            // create HREF HTML element
            var hrefElement = document.createElement("a");
            hrefElement.title = oArgs.title;
            hrefElement.className = 'tabbedNav_lnk';
            if (oArgs.link != '')hrefElement.href = oArgs.link;


            var oClickHandler = {};
            oClickHandler.index = aItems.length;
            oClickHandler.callBackFunction = oArgs.onSelect;
            oClickHandler.tabbedNav = this;

            hrefElement._clickHandler = oClickHandler;
            oClickHandler = null;

            hrefElement.onclick = function (e){

                // make clicked item selected
                this._clickHandler.tabbedNav.MakeItemSelected(this._clickHandler.index);

                // call user defined callback function
                var callBackFnc = this._clickHandler.callBackFunction;
                if (callBackFnc)
                    callBackFnc.call(this, this._clickHandler.index);
            }


            oContainer.appendChild(liElement);
            liElement.appendChild(hrefElement);
            hrefElement.appendChild(document.createTextNode(oArgs.label));

            aItems.push(liElement);
            return liElement;
        },


        MakeItemSelected : function (itemIndex){
            var oLiElement = null;

            // deselect previously selected menu item
            if (typeof this.selectedItem == 'number'){
                oLiElement = aItems[this.selectedItem];
                if (oLiElement.firstChild && oLiElement.firstChild.nodeName.toLowerCase() == 'a')
                    CSSJS('remove',oLiElement.firstChild, 'tabbedNav_lnk_active');
            }

            // make menu item selected
            this.selectedItem = itemIndex;
            oLiElement = aItems[itemIndex];
            if (oLiElement.firstChild && oLiElement.firstChild.nodeName.toLowerCase() == 'a'){
                CSSJS('add',oLiElement.firstChild, 'tabbedNav_lnk_active');
            }
        }


    }


    this._constructor.apply(this, arguments); // call constructor method
}
