ark.namespace('widget.CollapsableTabs');

(function() {
    var Dom = YAHOO.util.Dom,
        Anim = YAHOO.util.Anim,
        Motion = YAHOO.util.Motion,
        Easing = YAHOO.util.Easing
        Event = YAHOO.util.Event
        CustomEvent = YAHOO.util.CustomEvent;
        
ark.widget.CollapsableTabs = function(id,config) {
    this.id = id;
    this._html;
    this.elt;

    this._collapsable = new ark.widget.Collapsable(this.id);
    this._current = null;
    this._old = null;
    this._bAnimated = false;

    if( config && config['tabs'] ) {
        this._tabs = config['tabs'];
        for( var key in this._tabs ) {
            if( typeof key != 'undefined' ) {
                var tab = this._tabs[key];
                var obj = this;
                var cb = function(e,sTab) {
                    if( !obj._bAnimted ) {
                        obj._bAnimated = true;
                        obj._update_active_state(sTab);
                        obj.TabClickedEvent.fire(e,this,sTab);
                        obj.show_tab(e,sTab); 
                    }
                }

                Event.on(tab['click_id'],'click',cb,key);
            }
        }
    }


    this._update_active_state = function(sTab) {
        for( var key in this._tabs ) {
            if( typeof key != 'undefined' ) {
                Dom.removeClass(this._tabs[key]['click_id'],'active');
            }
        }

        if( this._current != sTab ) {
            Dom.addClass(this._tabs[sTab]['click_id'],'active');
        }
    }
 

    this.collapse = function() {
        this._collapsable.collapse();
    }

    this.show_tab = function(e,sTab) {
        YAHOO.util.Event.stopEvent(e);
        this.elt = Dom.get(this.id);
        if( !this._html ) {
            this._html = this.elt.innerHTML;
        }
        if( this._old != sTab ) {
            this._collapsable.content_key = sTab;
            this._collapsable.content_url = this._tabs[sTab]['content_url'];
            if( this._old != null ) {
                this.elt.innerHTML = this._html;
                this._collapsable.resize();
                //Dom.setStyle(this.elt,'height','auto');
                this._collapsable.refresh_content();
            }

            if( this._collapsable.collapsed ) {
                this._collapsable.expand();
            } else {
            }
            this._current = sTab;
        } else {
            if( this._collapsable.collapsed ) {
                this._current = sTab;
            } else {
                this._current = null;
            }
            this._collapsable.toggle();
        }
 
        this._old = sTab;
    }

    this.collapse = function() { 
        this._collapsable.collapse();
    }

    this.expand = function() { 
        this._collapsable.expand();
    }

    this.toggle = function() { 
        this._collapsable.toggle();
    }

    this.hide = function() {
        this._collapsable.hide();
    }

    this.show = function() {
        this._collapsable.show();
    }

    this.resize = function() {
        this._collapsable.resize();
    }

    this.collapsed = function() {
        return this._collapsable.collapsed;
    }
    
    this.expanded = function() {
        return !this._collapsable.collapsed;
    }

    this.current = function() {
        return this._current;
    }
    
    this.TabClickedEvent = new CustomEvent('TabClickedEvent',this);

    this._collapsed = function(type,args) {
        if( this._current ) {
            this._current = null;
            this._update_active_state();
        }
    }

    this._expanded = function(type,args) {
        this._bAnimated = false;
    }


    this._no_content = function(type,args) {
        var tab = this._tabs[this._current];
        var a = Dom.get(tab['click_id']);
        if( a ) {
            document.location = a.href;
        }

    }

    this._collapsable.CollapsedEvent.subscribe(this._collapsed,this,true);
    this._collapsable.ExpandedEvent.subscribe(this._expanded,this,true);
    this._collapsable.NoContentEvent.subscribe(this._no_content,this,true);
 }
})();

