var sObj = null;

function slideObj(){

  // init vars
  this.wrap           = null;
  this.box            = null; 
  this.row            = null;
  this.expander       = null;
  this.stimer         = null;
  this.extimer        = null;
  this.lock           = false; 
  this.sliding        = false;
  this.loaded         = false;
  this.toPos          = 0;
  this.oldPos         = 0;
  this.oldWidth       = 0;  
  this.defaultSpeed   = 10;  
  this.ms             = 30;
  this.speed          = this.defaultSpeed;
  this.exspeed        = this.defaultSpeed; 
  this.toClear        = [];
  this.page           = '';
  this.html           = '';
  this.oldRowHtml     = '';
  this.DexpandHeight  = 320;
  this.expandHeight   = this.DexpandHeight;
  this.boxFader       = null;
  this.expanderFader  = null;   
  this.expandCallback = null;

  //object functions
  this.setBox = function(id){
    var e = getE(id);
    if( e ){
      this.box = e;
    }   
  };
  
  this.setSpeed = function(){
  
    var bx, bd, ex, ed, xdiff, ediff, diff, mod;
    
    bx = parseInt(this.box.style.left);
    bd = this.toPos;
    
    ex = this.expandHeight;
    ed = this.oldExHeight;
    
    xdiff = Math.abs(bx - bd);
    ediff = Math.abs(ex - ed); 
    
    diff = xdiff / ediff;
    
    this.speed *= diff;
  
  };
  
  this.initSlide = function(id,page){
    
    if( this.box ){ 
    
      this.close();
    
    }else{

      this.setBox(id);
      
      if( this.box ){
      
        if( page ){
          this.page = page; 
        } 
    
        this.toPos = 0;
      
        this.wrap     = getE('orange_row_wrap');
        this.row      = getE('orange_row');  
        this.expander = getE('expander');
        
        this.oldRowHtml = this.wrap.innerHTML;    
    
        this.oldPos       = parseInt(this.box.style.left);
        this.oldWidth     = parseInt(this.box.style.width);
        this.oldExHeight  = parseInt(this.expander.style.height);
      
        this.boxFader  = new faderObj(this.box);
        this.exFader   = new faderObj(this.expander.firstChild);
        this.rowFader  = new faderObj(this.wrap);
      
        this.sliding = true;
        
        this.setSpeed();
        
        this.loadContent();
      
        this.clearWrap();
        
        this.expandRow();        

        this.slideBox(); 
      
      }
      
    }
  
  };  
  
  this.slideBox = function(){
             
    if( this.box ){
     
      var leftPos, smod;
  
      var leftPos = parseInt(this.box.style.left);
      
      leftPos >= this.toPos ? smod = -1 : smod = 1;    
    
      leftPos += this.speed * smod;
      
      if( (smod == 1 && leftPos >= this.toPos) || (smod == -1 && leftPos <= this.toPos) ){        
        this.box.style.left = this.toPos + 'px';
        this.stopSlide();
        this.fillContents();    
      }else{        
        this.box.style.left = leftPos + 'px';
        var thisObj = this;
        this.stimer = setTimeout( function(){ thisObj.slideBox(); thisObj = null; }, this.ms ); 
      }
       
    }
  
  }; 
  
  this.stopSlide = function(){
  
    this.sliding = false;
  
    if( this.stimer ){
      clearTimeout(this.stimer);
      this.stimer = null;
    }    
  
  };
  
  this.close = function(){
  
    var thisObj = this;
    
    this.expandHeight = this.oldExHeight;
    
    this.exFader.TimeToFade = 250;
    
    thisObj.rowFader.initFade(-1);
  
    this.exFader.initFade(-1,
    function(){
      thisObj.expander.firstChild.innerHTML = '';
      thisObj.exspeed = 20;
      thisObj.expandRow(
      function(){
        thisObj.wrap.innerHTML = thisObj.oldRowHtml;
        thisObj.rowFader.initFade(1);
        thisObj.reset();
        thisObj = null;
      });
    });
  
  }
  
  this.reset = function(){
  
    this.wrap           = null;
    this.box            = null; 
    this.row            = null;
    this.expander       = null;
    this.stimer         = null;
    this.extimer        = null;
    this.sliding        = false;
    this.toPos          = 0;
    this.oldPos         = 0;
    this.toClear        = [];
    this.page           = '';
    this.html           = '';
    this.oldRowHtml     = '';
    this.expandHeight   = this.DexpandHeight;
    this.boxFader       = null;
    this.exFader        = null;
    this.rowFader       = null;  
    this.speed          = this.defaultSpeed;
    this.exspeed        = this.defaultSpeed;   
        
  }
  
  this.clearWrap = function(){
  
    var cols, clen, ic, e, f;
    
    this.toClear = []; 
                 
    if( this.wrap ){  
    
      cols = this.wrap.childNodes;
      
      clen = cols.length;
      
      for( ic = 0; ic < clen; ic++ ){
      
        e = cols[ic];        
        
        if( e.tagName == 'DIV' ){
                                                 
          if( e.id != this.box.id ){
          
            f = new faderObj(e); 
            
            f.TimeToFade = 300;
        
            this.toClear.push(f);
                                  
            f.initFade();  
          
          }
        
        }
       
      }   
    
    }
  
  };  
  
  this.loadContent = function(){
 
    if( this.page ){
    
      var url = 'kmod_handler.php?c='+new Date().getTime()+'&page='+ encodeURIComponent(this.page);
      
      var thisObj = this;
    
      ajax(url,this.box.id,'GET');
    
    }  
  
  }; 
  
  this.fillContents = function(){
                                
    if( this.box && this.html && this.boxFader ){
      
      this.boxFader.TimeToFade = 200;
      
      var thisObj = this;
      
      this.expander.firstChild.innerHTML = thisObj.html;
      this.exFader.TimeToFade = 1000;
      this.exFader.initFade(1);   
      this.box.style.background = 'none';    
    
    }   
  
  }; 
  
  this.expandRow = function(func){
  
    if( this.extimer ){
      clearTimeout(this.extimer);
      this.extimer = null;
    }
  
    if( this.expander && this.expandHeight >= 0 ){ 
    
      var h, smod;
    
      h = parseInt(this.expander.style.height);
      
      h > this.expandHeight ? smod = -1 : smod = 1;
      
      h += this.exspeed * smod;
  
      if( (smod == 1 && h >= this.expandHeight) || (smod == -1 && h <= this.expandHeight) ){     
      
        this.expander.style.height = this.expandHeight + 'px';

        if( typeof(func) == 'function' ){
          func();
        } 

        return false;
        
      }else{
        this.expander.style.height = h + 'px';
        var thisObj = this; 
        this.extimer = setTimeout( function(){ thisObj.expandRow(func); thisObj = null; },this.ms);
      }                                   
    
    }
  
  };

}

// FADER
function faderObj(id){

  // init vars
  this.ftimer            = null;
  this.stage             = -1;
  this.curTick           = 0;
  this.elapsedTicks      = 0;
  this.TimeToFade        = 700;
  this.fadeDelay         = 5000;
  this.FadeTimeLeft      = this.TimeToFade;
  this.e                 = getE(id);
  this.callback          = null;
  
  this.initFade = function(stage,func){
  
    if( this.ftimer ){
      clearTimeout(this.ftimer);
      ftimer = null;
    }
    
    if( stage ){
      this.stage = stage;
    }
    
    this.FadeTimeLeft = this.TimeToFade;
    this.elapsedTicks = 0;
    this.curTick      = 0;
    this.ftimer       = null;
    this.callback     = func;  
    
    this.fade(new Date().getTime());
    
  };

  // object functions
  this.fade = function(tick){

    if( this.ftimer ){
      clearTimeout(this.ftimer);  
      this.ftimer = null;
    }
    
    if( !this.e ){
      return false;
    }

    var thisObj; 

    this.curTick      = new Date().getTime();
    this.elapsedTicks = this.curTick - tick;

    if( this.FadeTimeLeft <= this.elapsedTicks ){

      this.e.style.opacity = this.stage == 1 ? '1' : '0';
      this.e.style.filter  = 'alpha(opacity=' + (this.stage == 1 ? '100' : '0') + ')';

      this.FadeTimeLeft = this.TimeToFade;
      this.elapsedTicks = 0;

      if( this.stage == -1 ){      
        this.stage = 1;
      }else{
        if( this.e.style.filter ){
          this.e.style.removeAttribute('filter');
        }            
        this.stage = -1;
      }  
      
      if( this.ftimer ){
        clearTimeout(this.ftimer);  
        this.ftimer = null;
      }  
      
      if( this.callback ){
        this.callback();
        this.callback = null;
      }    

      return false;

    }

    this.FadeTimeLeft -= this.elapsedTicks;

    var newOpVal = this.FadeTimeLeft/this.TimeToFade;

    if( this.stage == 1 ){
      newOpVal = 1 - newOpVal;
    }    
    
    var ie = Math.round(newOpVal*100);

    this.e.style.opacity = newOpVal;
    this.e.style.filter  = 'alpha(opacity = ' + ie + ')';

    thisObj = this;
    this.ftimer  = setTimeout( function(){ thisObj.fade(thisObj.curTick); thisObj = null; }, 33);

    newOpVal = 0;

  };  
  
}  

