/**
*  fLoop Slider JS
*  -----------------------------------------------------------------
*  @copyright Copyright (c) 2007-2009 fCMS Development Team
*  @author Arne Blankerts <theseer@fcms.de>
*  
*  @id $Id: floop.fscript.js,v 1.5 2009/09/10 11:19:01 theseer Exp $
*/

   function fLoop(id) {
      this.obj = document.getElementById(id);
      if (!this.obj) {
         throw 'Element with id "'+id+'" not found';
      }
   }
   
   fLoop.prototype = {

      obj: null,
      
      width: 0,
      height: 0,      
      margin: 5,
      count: 3,
      
      first: 0,
      
      panels: [],
      
      tween: null,
      
      busy: false,
      mode: 'horizontal',
      
      setDimension: function(w,h) {
         this.width  = w;
         this.height = h;
      },
      
      setMargin: function(m) {
         this.margin = m; 
      },

      setTween: function(twm, tws) {      
         this.tween = {method:twm, speed:tws};
      },
      
      init: function(count) {
         var list = fScript.getChildrenByTagName('DIV', this.obj);
         if (list.length == 0) {
            throw 'No children found in container';
         }
         if (count != null) {
            this.count = count;
         }
         if (this.count < 1 || this.count > list.length -1) {
            throw 'Count '+this.count+' is out of bounds. Must be between 1 and '+ (this.panels.length - 1)
         }
         
         for (var x=0; x<list.length; x++) {
            var c = x > this.count ? this.count : x;
            var pos = this.width * c + this.margin * c;
            
            this.panels[x]=new fLoopPanel(this, list[x]);
            this.panels[x].setDistance(this.width + this.margin);
            this.panels[x].setPosition(pos,0);
            this.panels[x].setTween(this.tween);
         }
      },
      
      goLeft: function() {
         if (this.busy) return;

         this.first--;
         if (this.first==-1) {
            this.first = this.panels.length-1;
         }
         
         var pos=0 - (this.width+this.margin);
         //dump('relocate:'+this.first+' -> '+pos+'\n');
         this.panels[this.first].setPosition(pos,0);
         
         var y=this.first;
         for(var x=0; x<this.count + 1; x++) {
            this.busy++;
            if (y==this.panels.length) {               
               y=0;
            }
            //dump(' - move:'+y+'\n');
            this.panels[y].move(false);
            y++;
         }
         
      },
      
      goRight: function() {
         if (this.busy) return;         
          
         var y=this.first;
         for(var x=0; x<this.count + 1; x++) {
            this.busy++;
            if (y==this.panels.length) {               
               y=0;
            }
            //dump(' - move:'+y+' ('+(x==0)+')\n');
            this.panels[y].move(true, (x==0));
            y++;
         }         
         this.first++;
         if (this.first==this.panels.length) {
            this.first = 0;
         }
      },
      
      goUp: function() {
         if (this.busy) return;
      },
      
      goDown: function() {
         if (this.busy) return;
      }
      
   }
   
   function fLoopPanel(ref, obj) {
      this.parent = ref;
      this.obj = obj;
      this.style = obj.style;
      this.style.position = 'absolute';      
   }
   fLoopPanel.prototype = {
       parent: null,
       obj: null,       
       style: null,
       currentX: 0,
       currentY: 0,
       
       stepSize: 5,
       stepSpeed: 20,
       
       distance: 0,
       space: 0,
       
       tween: null,
       
       type: 'horizontal',
       
       setPosition: function(x,y) {
          //dump('setPosition('+x+','+y+')\n');
          this.currentX = x;
          this.currentY = y;
          
          this.style.left = x +'px';
          this.style.top  = y +'px';          
       },
       
       setType: function(type) {
           if (type != 'horizontal' && type != 'vertical') {
              throw 'Unsupported type - must be horizontal or vertical';
           }
           this.type = type;
       },
       
       setSpeed: function(speed) {
          this.stepSpeed = speed;
       },
       
       setDistance: function(dist) {
          this.distance = dist; 
       },
       
       setTween: function(o) {
         this.tween = o;
       },
       
       move: function(dir,jump) {
         var myself = this;
         
         if (this.tween) {
            var x=new fTween(
               this.style, 
               (this.type=='horizontal' ? 'left' : 'top'),
               this.tween.method,
               this.currentX,
               (dir ? this.currentX - this.distance : this.currentX + this.distance),
               this.tween.speed,
               'px'
            );
            x.onMotionFinished = function() {
               myself.currentX = (dir ? myself.currentX - myself.distance : myself.currentX + myself.distance);
               myself.parent.busy--;               
               if (jump) {
                  myself.setPosition((myself.parent.width * myself.parent.count + myself.parent.margin * myself.parent.count),0);
               }
               //dump('tween complete\n'); 
            };
            x.start();
            
         } else {
                  
            //dump('move\n');
            if (this.type == 'horizontal') {
               this.setPosition( this.currentX + ( dir ? -1 * this.stepSize : this.stepSize), this.currentY);
            } else {
               this.setPosition( this.currentX, this.currentY + ( dir ? -1 * this.stepSize : this.stepSize));
            }
            this.space += this.stepSize;
            if (this.space < this.distance) {
               window.setTimeout(function(){ myself.move(dir); }, this.stepSpeed);
               return;
            }
            this.space = 0;
            this.parent.busy--;
         }
      }
   }
   
