/**
 * @author POP.webdev
 * @version 0.2
 * @lastmodified 3/24/2008
 * @classDescription Provides a pairing of list item "tabs" with content elements and allows switching between them.
 * @return {Object}	Returns a new instance of the class.
 * @projectDescription
 * 
 * Dependencies:
 * - prototype v1.6 or later.
 */
var TabSwitcher = Class.create({
    initialize: function(links, items, options) {
        this.options = Object.extend({
            initialTabIndex: 0,
            tabOnClassName: 'selected',
            tabOffClassName: null,
            bodyOnStyles: { position: 'static' },
            bodyOffStyles: { top: '-50001px', position: 'absolute' }

        }, options || {});
        // get dom references for tab and "body" content elements
        this.links = $$(links);
        this.items = $$(items);
        if (this.items.length == 0 || this.links.length == 0) { return; }

        // attach event handlers
        this.links.invoke('observe', 'click', this._linkClick.bindAsEventListener(this));

        // show initial item
        this.setItem(this.links[this.options.initialTabIndex]);
    },

    _linkClick: function(e) {
        Event.stop(e);  // stop event
        var link = Event.findElement(e, 'a');
        this.setItem(link); // show item
        if (this.options.onTabClick) {
            this.options.onTabClick.call(this, link);
        };
    },

    setItem: function(elLnk) {

        // add/remove selected class on parent LI (tab elements)
        var tabOffClassName = this.options.tabOffClassName;
        var tabOnClassName = this.options.tabOnClassName;

        this.links.each(function(lnk) {
            if (elLnk === lnk) {
                lnk.up('li').addClassName(tabOnClassName);
                if (tabOffClassName != null) {
                    lnk.up('li').removeClassName(tabOffClassName);
                }
            }
            else {
                lnk.up('li').removeClassName(tabOnClassName);
                if (tabOffClassName != null) {
                    lnk.up('li').addClassName(tabOffClassName);
                }
            }
        });

        // show/hide items ("body" content elements)
        var attHref = elLnk.readAttribute("href").replace(/.*#+/, '');
        this.items.each(function(elBody) {
            if (elBody.id == attHref) {
                elBody.setStyle(this.options.bodyOnStyles);
            }
            else {
                elBody.setStyle(this.options.bodyOffStyles);
            }
        } .bind(this));
    }
});
/*

document.observe("dom:loaded", function(){
	// without options
	var ts = new TabSwitcher("ul#tabs li a", "div.tab_content div");

	// with options
	ts = new TabSwitcher("ul#tabs li a", "div.tab_content div[id^=tab_]", {
		initialTabIndex: 2,
		tabOnClassName: 'on',
		tabOffClassName: 'off',
		bodyOnStyles: {
			display: 'block'
		},
		bodyOffStyles: {
			display: 'none'
		}
	});


});

*/

