/**
 * Tabset Controller
 * @class TabsetContoller
 * @namespave VZT
 */
VZT.TabsetController = new Class({
    Implements:VZT.MultiItemController,
    _view:null,
    initialize:function() {},
    setView:function(view) {
        this._view = view;
        this._view.addEvent('onclick',function(i) {
			      this._click();
            this.go(i);
        }.bind(this));
    },
  	_click:function(){
  		return;
  	},
    _onGo:function(i) {
        var previous = this._cur;
        var current = i;
        if (i !== this._cur) {
            this._view.displayContent(current);
            this._view.selectTab(current);
            this._view.hideContent(previous);
            this._view.deselectTab(previous);
        }
        this._cur = i;
    }
});

/**
 * View to handle the tabsets
 * @class TabsetView
 * @namespace VZT
 */
VZT.TabsetView = new Class({
    Implements:Events,
    _wrapper:null,
    _items:[],
    _tabs:[],
    _controller:null,
   initialize:function(wrapper,items,controller) {
      this._wrapper = wrapper;
      this._items = items;
      this._controller = controller;
      this._controller.setView(this);
      this.initTabs();
      this.bind();
   },
   initTabs:function() {
     this._tabs = this._wrapper.getElements('a');
     for (var i=0,len=this._tabs.length;i<len;i++) {
         if (this._tabs[i].hasClass('active')) {
             this.fireEvent('onclick',i);
             break;
         }
     }
   },
   bind:function() {
     var self = this;
     this._wrapper.addEvent('click',function(e) {
         for (var i=0,len=self._tabs.length;i<len;i++) {
             if (e.target === self._tabs[i] || self._tabs[i].hasChild(e.target)) {
                 self.fireEvent('onclick',i);

                 e.preventDefault();
                 return false;
             }
         }
     });
   },
   unbind:function() {
     this._wrapper.removeEvents('click');
   },
   displayContent:function(i) {
       this._items[i].removeClass('hidden');
        return this;
   },
   hideContent:function(i) {
     if ($type(i) === 'number') {
        this._items[i].addClass('hidden');
     }
     return this;
   },
   selectTab:function(i) {
       this.deselectTabs();
       this._tabs[i].addClass('active');
       return this;
   },
   deselectTab:function(i) {
       this._tabs[i].removeClass('active');
       return this;
   },
   deselectTabs:function() {
       this._tabs.each(function(tab,index) {
           this.deselectTab(index);
       }.bind(this));
       return this;
   },
   getItems:function() {
       return this._items;
   },
   getItem:function(i) {
   		return this._items[i];
 	},
   getTabs:function() {
		return this._tabs;
  	},
  	getTab:function(i) {
  		return this._tabs[i];
  	}
});
