
   function fAccordion(id) {
      try {
         this.container=document.getElementById(id);
         if (!this.container)
            throw 'No element with id "'+id+'" found.';
         this.panels=[];
         for (var x=0; x<this.container.childNodes.length; x++) {
            var y=this.container.childNodes[x];
            if (y.nodeType==1 && y.nodeName.toLowerCase()=='div') {
               var pan = new fAccordionPanel(y, this);
               this.panels.push(pan);               
            }
         }
         if (this.panels.length<2)
            throw 'No or not enough panels within container (id:'+id+') found.';   
            
         this.pos=this.panels.length-1;
         this.current=this.panels[this.pos];  
         
      } catch (e) {
         fScript.handleException(e,'fAccordion.__construct');
      }
   }
  
   fAccordion.prototype = {
  
      padding: 0,     
      pos: 0,
      current: null,
      padding: 10,
     
      panels: [],
      busy: 0,
      
      onBeforeSwitch: null,
      onAfterSwitch: null,
      
      setPadding: function(w) {
         this.padding = w; 
      },
     
      show: function(p) {
         if ((this.busy > 0) || (this.pos == p)) return;
         
         this.target = p;
         if (typeof this.onBeforeSwitch == 'function') {
            this.onBeforeSwitch(this.pos, this.target);
         }
         p > this.pos ? this.goLeft(p) : this.goRight(p);
      },
  
      goLeft: function(p) {
         //dump('goLeft:'+p+'\n');
         for(var x=this.pos+1; x<=p; x++) {
            //dump('moveLeft: '+x+'\n');
            this.busy++;
            this.panels[x].moveLeft();
         }
      },
     
      goRight: function(p) {
         //dump('goRight:'+p+'\n');
         for(var x=this.pos; x>p; x--) {
            //dump('moveRight: '+x+'\n');
            this.busy++;
            this.panels[x].moveRight();
         }
      },
      
      onMotionFinished: function() {
         this.busy--;
         if (this.busy == 0) {
            if (typeof this.onAfterSwitch == 'function') {
               this.onAfterSwitch(this.pos, this.target);
            }
            this.pos = this.target;
            this.target = null;
         }
      }
      
   }
  
  
   function fAccordionPanel(panel, ref){
      this.panel = panel;
      this.ref   = ref;
      this.position = ref.panels.length;
      
   }
   fAccordionPanel.prototype = {
      
      type: 'horizontal',
      
      ref: null,
      panel: null,
      
      position: 0,
      padding: 0,
      
      moveLeft: function() {
         var p = this._calcStartStop();
         this._doMove(p.right, p.left);
      },
   
      moveRight: function() {
         var p = this._calcStartStop();
         this._doMove(p.left, p.right);
      },
      
      _calcStartStop: function() {
         return {
            left:  this.position * this.ref.padding,
            right: this.ref.container.clientWidth - ((this.ref.panels.length - this.position) * this.ref.padding) 
         };  
      },
      
      _doMove: function(start,stop) {
         var x=new fTween(
            this.panel.style, 
            (this.type=='horizontal' ? 'left' : 'top'),
            'regularEaseInOut',
            start,
            stop,
            0.5,
            'px'
         );
         var myself = this;
         x.onMotionFinished = function() {
            myself.ref.onMotionFinished();
         }
         x.start();
      }
   }
