FuguSlideManager = new AJS.Class({
  containerDiv: null,
  slides: new Object(),
  activeSlides: new Object(),
  homeSlide1: null,
  homeSlide2: null,
    
  // Constructor
	init : function() { 
    this.containerDiv = AJS.$('slides_wrapper');
    this.containerDiv.innerHTML = '';
    this.containerDiv.style.display = 'block';
  },
		
	update : function(orderPos,rightToLeft) {
    try {
      var lookUp = window.dataLoader.getLookUp();
      var ordered = window.dataLoader.getOrdered();
            
      //console.log('hier '+orderPos+' '+ordered.length+' '+rightToLeft);
      
      if(this.homeSlide1==null){
         this.homeSlide1 = this.getSlide(lookUp['']);
         this.homeSlide1.name = 'home';
      }
      if(this.homeSlide2==null){
         this.homeSlide2 = new FuguHomeSlide(lookUp[''],this.containerDiv);
         this.homeSlide2.name = 'home red';
         //this.homeSlide2.boxDiv.style.backgroundColor = '#FF0000';
         this.slides['-home2-'] = this.homeSlide2;
      }
                  
      var currentObj = lookUp[ordered[orderPos]];
      if(currentObj==null){
        return; 
      }
        
      var centerSlide = null;
      var k = ordered[orderPos];
      if(k!=''){
        centerSlide = this.getSlide(currentObj);
      }
            
      var leftSlide = null;
      var rightSlide = null;
            
      if(orderPos>0){
        var pos = orderPos-1;
        var k = ordered[pos];
        if(k!=''){
          var previousObj = lookUp[k];
          if(previousObj!=null){
            leftSlide = this.getSlide(previousObj);
          }
        }
      }
      
      if(orderPos>=0){
        var pos = orderPos+1;
        if(pos>=ordered.length){
          pos = 0; 
        }
        var k = ordered[pos];
        if(k!=''){
          var nextObj = lookUp[k];
          if(nextObj!=null){
            rightSlide = this.getSlide(nextObj);
          }
        }
      }
      
      
      //Handle home
      if(leftSlide==null || rightSlide==null){
        var temp = null;
        if(this.activeSlides!=null && this.activeSlides.center!=null && this.activeSlides.center.obj.urlkey==''){
          temp = this.activeSlides.center;
        }
        if(temp!=null){
          if(rightToLeft){
            leftSlide= temp;
          }
          else {
            rightSlide = temp;
          }
        }
      }
      
      if(centerSlide==null){
        if(rightToLeft){
          var temp = null;
          if(this.activeSlides!=null && this.activeSlides.right!=null && this.activeSlides.right.obj.urlkey==''){
            temp = this.activeSlides.right;
          }
          if(temp!=null){
            centerSlide = temp;
          }
        }
        else {
          var temp = null;
          if(this.activeSlides!=null && this.activeSlides.left!=null && this.activeSlides.left.obj.urlkey==''){
            temp = this.activeSlides.left;
          }
          if(temp!=null){
            centerSlide = temp;
          }
        }
      }
      
      if(rightSlide==leftSlide){
        leftSlide=null; 
      }
        
      //Fill up with home slides
      var arr = [this.homeSlide1, this.homeSlide2];
      if(leftSlide==null){
        for(var i in arr){
          if(centerSlide!=arr[i] && rightSlide!=arr[i]){
            leftSlide = arr[i];
            break;
          }
        }
      }
      if(centerSlide==null){
        for(var i in arr){
          if(leftSlide!=arr[i] && rightSlide!=arr[i]){
            centerSlide = arr[i];
            break;
          }
        }
      }
      if(rightSlide==null){
        for(var i in arr){
          if(leftSlide!=arr[i] && centerSlide!=arr[i]){
            rightSlide = arr[i];
            break;
          }
        }
      }
                             
      if(leftSlide!=null && leftSlide!=centerSlide){
        leftSlide.moveTo('left',rightToLeft);
      }
      if(centerSlide!=null){
        centerSlide.moveTo('center',rightToLeft);
      }
      if(rightSlide!=null && rightSlide!=centerSlide){
        rightSlide.moveTo('right',rightToLeft);
      }

      this.activeSlides = {left: leftSlide, center: centerSlide, right: rightSlide};
      
      //Hide all others
      for(var i in this.slides){
        var s = this.slides[i];
        var found = false;
        for(var j in this.activeSlides){
          if(this.activeSlides[j]==s){
            found = true;
            break;
          }
        }
        if(!found){
          s.moveTo('',rightToLeft); 
        }
      }
    }
    catch(e){
      //console.log(e);
    }
	},
  
  getSlide: function(obj){
     var slide = this.slides[obj['urlkey']];
     if(slide==null){
       var cl = null;
       if(obj['type']=='home'){
         cl = window.FuguHomeSlide;
       }
       else if(obj['type']=='news'){
         cl = window.FuguNewsSlide;
       }
       else if(obj['type']=='portfolio'){
         cl = window.FuguPortfolioSlide;
       }
       else if(obj['type']=='portrait'){
         var t = obj['TEMPLATE'];
         if(t!=null && t.indexOf('template_')==0){
           t = t.substring(9);
           if(t=='team'){
             cl = window.FuguTeamSlide;
           }
           else if(t=='contact'){
             cl = window.FuguContactSlide;
           }
           else if(t=='customers'){
             cl = window.FuguCustomersSlide;
           }
         }
       }
       
       if(cl==null) {
         cl = window.FuguSlide;
       }
       slide = new cl(obj,this.containerDiv);
       this.slides[obj['urlkey']] = slide;
       /*
       for(var i in window){
         if(window[i]==cl){
            console.log(i); 
         }
       }
       */
     }
     return slide;
  }
});