/* 
Gizmo(QP) Web Framework - http://gizmojo.org/
------------------------------------------------------------------------------
Copyright (C) 2007 Mario Ruggier
License: GNU LGPL, http://www.gnu.org/copyleft/lesser.html
------------------------------------------------------------------------------
$Id: tabbed.js 522 2007-04-09 08:11:14Z mario $ 
*/

// tabbed sections

/* Sample generated HTML for a Tabbed : 
<div id="%(gizmoid)s_tabsections" class="tabbed">
    <ul id="%(gizmoid)s_tablist" class="tablist">
        <li><a onclick="%(gizmoid)s.show_section('%(section_name)s');"
               href="javascript:void(0);" name="%(section_name)s">
                    %(section_label)s</a></li>
        ... <li> per section ...
    </ul>
    <div id="%(gizmoid)s_%(section_name)s" class="section invisible">
        %(section_content)s
    </div>
    ... <div> per section ...
</div>
*/

function Tabbed (gizmoid, visible) {
    this.gizmoid = gizmoid;
    this.visible = visible;
    this.sections = null; // { name:label }
    //this.set_event_handler('show_section', this.on_show_section); 
}
Tabbed.prototype = new Gizmo;
// Note: section names are added to elem_names per Tabbed instance
Tabbed.prototype.elem_names = ['tabsections', 'tablist']; 
Tabbed.prototype.event_handlers = { 'show_section': null };

Tabbed.prototype.onload = function () {
    this.show_section(this.visible);
};
Tabbed.prototype.get_elem_names = function () {
    //+ update elem_names on add_sections... 
    // As it is currently, is_exposed is broken.
    var names = [];
    names.concat(this.elem_names);
    for (var sname in this.sections) {
        names.push(sname);
    }
    return names;
};
Tabbed.prototype.show_section = function (name) {
    var tablinks = this.get_elem('tablist').getElementsByTagName('A');
    for (var i=0; i<tablinks.length; i++) {
        if (getNodeAttribute(tablinks[i],'name')==name) {
            addElementClass(tablinks[i], 'current');
        }
        else {
            if (hasElementClass(tablinks[i],'current')) {
                removeElementClass(tablinks[i], 'current');
            }
        }
    }
    for (var sname in this.sections) {
        makeInvisible(this.get_elem(sname));
    }
    makeVisible(this.get_elem(name));
    this.visible = name;
    this.handle_event('show_section');
};
Tabbed.prototype.get_visible = function () {
    return this.visible;
};
Tabbed.prototype.on_show_section = function () {};
