/*
* @name         Tabbed Content Widget
* @description  Un-obtrusive JavaScript widget for creating tabbed content panels
* @author       Scott Lewis
* @date         October 10, 2008
* @version      0.0.1
*/

var TabTool = function() {};

TabTool.prototype.tabs = function(options) {

    var self = this;
    
    self.tabs = options.tabs;
    
    for (var i=0; i<self.tabs.length; i++) {
        var tab = self.tabs[i];
        if (options.callback) {
	        tab.callback = options.callback;
	    }
        self.bind(
            self.get_by_id(tab.tab),
            "click",
            function(e) {
                return self.show_active_panel(e);
            },
            tab
        );
    }
};

TabTool.prototype.show_active_panel = function(e) {
    if (!this.has_class(e.target, "active")) {
        var tab = e.data;
        this.hide_all();
        this.make_active(this.get_by_id(tab.tab));
        this.make_active(this.get_by_id(tab.panel));
        if (typeof(tab.callback) == 'function') {
            tab.callback();
        }
    }
    if (e.stopPropagation) {
        e.stopPropagation();
        e.preventDefault();
    }
    else if (window.event) {
        window.event.cancelBubble = true;
        e.returnvalue = false;
    }
};

TabTool.prototype.hide_all = function() {
    for (var i=0; i<this.tabs.length; i++) {
        var tab = this.tabs[i];
        this.make_inactive(this.get_by_id(tab.tab));
        this.make_inactive(this.get_by_id(tab.panel));
    }
};

TabTool.prototype.get_by_id = function(id) {
    return document.getElementById(id);
};

TabTool.prototype.has_class = function(element, className) {
    if (element && element.className) {
	    return element.className.match(
	        new RegExp('(\\s|^)' + className + '(\\s|$)')
        );
	}
	return false;
};

TabTool.prototype.add_class = function(element, className) {
    if (!this.has_class(element, className)) {
        element.className += " " + className;
    }
};

TabTool.prototype.remove_class = function(element, className) {
    if (this.has_class(element, className)) {
        var reg = new RegExp('(\\s|^)'+className+'(\\s|$)');
        element.className = element.className.replace(reg,' ');
    }
};

TabTool.prototype.make_active = function(element) {
    this.add_class(element, "active");
};

TabTool.prototype.make_inactive = function(element) {
    this.remove_class(element, "active");
};

TabTool.prototype.bind = function(element, event, callback, data, capture) {
    var self = this;

    var fn = callback;
    if (data != undefined) {
        var callback = function(e) {
            e.data = data;
            return fn.apply(this, arguments);
        };
    }
        
    if (element.addEventListener) {
        element.addEventListener(event, callback, false);
    }
    else if (element.attachEvent) {
        element.attachEvent('on'+event, callback);
    }
};

/*
* Add the functionality of this tool 
* to the base ScriptHelper Object
*/

Helper.extend(TabTool);