FuguSlide = new AJS.Class({
  obj: null,
  div: null,
  boxDiv: null,
  width: 622,
  offset: 157,
  position: '',
  moving: false,  
  
  navWidth: 20,
  navWidthActive: 40,
    
  initialized: false,
  
  // Constructor
	init : function(obj,containerDiv) { 
    //console.log(obj['type']);
    
    this.name = obj['name'];
    
    this.obj = obj;
    this.div = AJS.DIV();
    this.div.style.position = 'absolute';
    this.div.style.display = 'none';
    this.div.style.width = '100%';
    containerDiv.appendChild(this.div);
    
    var template = window.dataLoader.getTemplate(this.obj['type']);
    if(template==null)
      return;
    for(var i in this.obj){
      template = template.replace('{'+i+'}',this.obj[i]);    
    }
        
    this.div.innerHTML = template;
    
    this.boxDiv = AJS.getFirst(AJS.$bytc('div', 'contentbox', this.div));
    this.boxDiv.style.overflow = 'visible';
    this.boxDiv.style.zIndex = 10; 
    
    this.navDivLeft = AJS.DIV();
    this.boxDiv.appendChild(this.navDivLeft);
    this.navDivLeft.innerHTML = window.dataLoader.getTemplate('navigation_right');
    this.navDivLeft.style.position = 'absolute';
    this.navDivLeft.style.top = '0';
    this.navDivLeft.style.left = '-84px';
    this.navDivLeft.style.display = 'none';
        
    this.navDivRight = AJS.DIV();
    this.boxDiv.appendChild(this.navDivRight);
    this.navDivRight.innerHTML = window.dataLoader.getTemplate('navigation_left');
    this.navDivRight.style.position = 'absolute';
    this.navDivRight.style.top = '0';
    this.navDivRight.style.left = (this.width+20)+'px';
    this.navDivRight.style.display = 'none';
    
        
    AJS.AEV(this.boxDiv, 'mouseover', AJS.bind(this.moveIn, this));
		AJS.AEV(this.boxDiv, 'mouseout', AJS.bind(this.moveOut, this));
		AJS.AEV(this.boxDiv, 'click', AJS.bind(this.onClick, this));
    
    //Exec script tags
    var els = this.div.getElementsByTagName('script');
    if(els!=null){
      var els_len = els.length;
      for (var i = 0; i < els_len; i++) {
        var js = els[i].innerHTML;
        try {
          eval(js);
      } catch(ex){}
      }
    }
    
    //Replace anchors
    var host = location.host;
    var re = new RegExp('\/(de|en)\/');
    var els = this.div.getElementsByTagName('a');
    if(els!=null){
      var els_len = els.length;
      for (var i = 0; i < els_len; i++) {
        var url = els[i].href;
        if(url.indexOf('#')==-1 && url.indexOf(host)!=-1){
          if (url.search(re) != -1) {
            url = url.replace(re, '/'+language+'/#/');
            els[i].href = url; 
          } 
        }
      }
    }
    this.div.setAttribute('title',obj['name']);
    
    /*
    this.posDiv = AJS.DIV();
    this.boxDiv.appendChild(this.posDiv);
    this.posDiv.style.position='absolute';
    this.posDiv.innerHTML = 'hallo';
    */
  },
  
  moveIn : function(){
   try {
     clearTimeout(this.moveTimer);
   } catch(ex) { }
   this.handleMove(true);
  },
  moveOut : function(){
    this.moveTimer = setTimeout(AJS.$b(this.handleMoveEvent, this, [false]), 10);
    //this.handleMove(false);
  },
  handleMoveEvent : function(e,moveIn) {
     this.handleMove(moveIn);
  },
  handleMove : function(moveIn){
    if(this.moving){
      return; 
    }
    if(this.position!='left' && this.position!='right'){
      return;
    }
    
    if(this.isMovedIn == moveIn){
      return;
    }
        
    try {
      var x = 0;
			var startX = 0;
      var endX = 0;
			
      var dir = 1;
      if(this.position=='left'){
        x = -this.width;
        dir = 1;
      }
      else {
        var win = AJS.getWindowSize();
        x = win.w;
        dir = -1;
      }
            
      if(moveIn){
        startX = x+(this.navWidth*dir);
        endX = x+(this.navWidthActive*dir);
      }
      else {
        startX = x+(this.navWidthActive*dir);
        endX = x+(this.navWidth*dir);
      }
      this.isMovedIn = moveIn;
            
      new AJS.fx.Style(this.boxDiv, 'left', {duration: 300, transition: this.easeOut, fps: 120}).custom(
        startX,
        endX
      );
		} catch(ex) { }
  },
  onClick : function(moveIn){
    if(this.moving){
      return; 
    }
    if(this.position!='left' && this.position!='right'){
      return;
    }
    
    var url = this.obj['_websiteurl_'];
    if(url==null)
      url = '';
    window.navInfoClicked = true;
    location.href = '#'+url;
  },
  
  moveTo : function(newPosition,rightToLeft){
    //this.posDiv.innerHTML = this.position+'<br>'+newPosition+'<br>'+rightToLeft;
    if(this.position=='' && newPosition==''){
      return; 
    }
     
    //That we wont cross the screen
    if((newPosition=='left' && this.position=='right') || newPosition=='right' && this.position=='left'){
      //this.position = '';
    }
    
    var pos = AJS.absolutePosition(this.boxDiv);
    
    var win = null;
    var startX = 0;
    //Its hidden
    if(this.position==''){
      //Move in from left
      if(newPosition=='left' || (newPosition=='center' && !rightToLeft)){
        startX = -this.width;
      }
      else {
        if(win==null){
          win = AJS.getWindowSize();
        }
        startX = win.w;
      }
    }
    //Move from current pos
    else {
      startX = pos.x;
    }
            
    var endX = 0;
    switch(newPosition){
      case 'left':
        endX = -this.width+this.navWidth;
      break;
      case 'center':
        endX = this.offset;
      break;
      case 'right':
        if(win==null){
          win = AJS.getWindowSize();
        }
        endX = win.w-this.navWidth;
      break;
      //Hide
      default:
        if(this.position=='right' || (this.position=='center' && !rightToLeft)){
          if(win==null){
            win = AJS.getWindowSize();
          }
          endX = win.w;
        }
        else {
          endX = -this.width;
        }
      break;
    }
    
    this.boxDiv.style.left = startX+'px';

    if(newPosition=='center'){
      this.div.style.zIndex = 20; 
    }
    else {
      this.div.style.zIndex = 10; 
    }
    
    this.navDivLeft.style.display = 'none';
    this.navDivRight.style.display = 'none';
    
    if(newPosition!=''){
      //console.log('moveTo: '+this.name+':'+newPosition+'('+this.position+'):'+startX+':'+endX+' rtl: ('+(startX>endX)+') rtl: '+rightToLeft);
    }    
    
    this.moving = true;
    if(newPosition=='center'){
      this.boxDiv.style.cursor = 'auto';   
      this.boxDiv.className =  this.boxDiv.className+' contentbox_active';
    }
    else {
      this.boxDiv.style.cursor = 'pointer'; 
      this.boxDiv.className =  this.boxDiv.className.replace(' contentbox_active','');
    }

    if(!this.initialized || pos.x!=endX){
     new AJS.fx.Style(this.boxDiv, 'left', {onComplete: AJS.$b(this.moveToCompleted, this), duration: 800, transition: this.easeOut, fps: 120}).custom(
        startX,
        endX
        );
    }
        
    this.div.style.display = 'block'; 
    this.div.setAttribute('pos',newPosition);
    this.position = newPosition;
    this.initialized = true;
  },
  
  moveToCompleted : function(){
    //console.log('completed: '+this.obj['name']+' '+this.position);
    this.moving = false;
    if(this.position==''){
      this.hide(); 
    }
    else if(this.position!='center'){
       this.boxDiv.style.cursor = 'pointer'; 
    }
    
    if((this.position=='left' || this.position=='right') && !window.navInfoClicked){
      this.navDivLeft.style.display = 'block';
      this.navDivRight.style.display = 'block';
    }
    else {
      this.navDivLeft.style.display = 'none';
      this.navDivRight.style.display = 'none';
    }
  },
  
  hide : function(){
    this.position = '';
    this.div.style.display = 'none'; 
  },
  
  getSlide: function(obj){
    var urlKey = obj['urlkey'];
    var slide = this.slides[urlKey];
    if(slide==null){
      slide = new FuguSlide(obj,this.div);
      this.slides[urlKey] = slide;
    }
    return slide;
  },
  
  // Math formula for the animation
	easeOut : function(t, b, c, d) {
		return c * (-Math.pow(2, -10 * t/d) + 1) + b;
	}
});

//HOME
FuguHomeSlide = FuguSlide.extend({
  width: 976,
  offset: 0
});

FuguNewsSlide = FuguSlide.extend({
  width: 311
});
FuguTeamSlide = FuguSlide.extend({
  width: 311,
  visible: false,
  bgImg: null,
  
  init : function(obj,containerDiv) { 
    this.parent(obj, containerDiv);
    this.bgImg = AJS.getFirst(AJS.$bytc('img', 'background_image', this.div));
    this.div.parentNode.parentNode.appendChild(this.bgImg);
    this.bgImg.style.display = 'none';
  },
  
  moveTo : function(newPosition,rightToLeft){
    this.parent(newPosition,rightToLeft);
    if(newPosition!='center' && this.visible){
      this.visible = false;
      AJS.fx.fadeOut(this.bgImg, {onComplete: AJS.$b(function() { this.bgImg.style.display = 'none'; }, this)});
    }
  }, 
  
  moveToCompleted : function(){
    this.parent(); 
    if(this.position=='center' && !this.visible){
      this.visible = true;
      this.bgImg.style.width = '100%';
      AJS.fx.fadeIn(this.bgImg, {to: 1, duration: 800});
      this.bgImg.style.display = 'block';
    }
  }
});
FuguContactSlide = FuguSlide.extend({
  width: 311
});
FuguCustomersSlide = FuguSlide.extend({
  width: 311,
  visible: false,
  customersDiv: null,
  
  init : function(obj,containerDiv) { 
    this.parent(obj, containerDiv);
    this.customersDiv = AJS.getFirst(AJS.$bytc('div', 'customers_bg', this.div));
    this.div.parentNode.parentNode.appendChild(this.customersDiv);
    this.customersDiv.style.display = 'none';
  },
  
  moveTo : function(newPosition,rightToLeft){
    this.parent(newPosition,rightToLeft);
    if(newPosition!='center' && this.visible){
      this.visible = false;
      AJS.fx.fadeOut(this.customersDiv, {onComplete: AJS.$b(function() { this.customersDiv.style.display = 'none'; }, this)});
    }
  }, 
  
  moveToCompleted : function(){
    this.parent(); 
    if(this.position=='center' && !this.visible){
      this.visible = true;
      AJS.fx.fadeIn(this.customersDiv, {to: 0.8, duration: 800});
      this.customersDiv.style.display = 'block';
    }
  }
});
FuguPortfolioSlide = FuguSlide.extend({
  galleryImg: null,
  gallery: null,
  visible: false,
  gallerySize: 0,
    
  init : function(obj,containerDiv) { 
    obj['IMAGE'] = '';
    this.parent(obj, containerDiv);
    
    
    this.galleryImg = AJS.IMG();
    this.boxDiv.appendChild(this.galleryImg);
    this.galleryImg.style.position = 'absolute';
    this.galleryImg.style.zIndex = -1;

    var images = this.obj['IMAGES'];
    if(this.gallery==null && images!=null){
      this.gallerySize = images.length;
      this.gallery = new FuguGallery(this.galleryImg, images, 3000, this.obj['fade_color']);
      this.gallery.name = this.name;
      this.gallery.play();
      this.gallery.pause();
    }
    
		if (this.gallerySize > 1) {
      this.galleryArea = AJS.getFirst(AJS.$bytc('div','gallery_buttons_area',this.div));
      this.galleryArea.style.zIndex = 10; 
      AJS.AEV(this.galleryArea, 'mouseover', AJS.bind(this.showButtons, this));
      AJS.AEV(this.galleryArea, 'mouseout', AJS.bind(this.hideButtons, this));
      AJS.AEV(this.galleryArea, 'mousemove', AJS.bind(this.showButtons, this));
      
      var b = AJS.getFirst(AJS.$bytc('div','button_previous',this.galleryArea));
      AJS.AEV(b, 'click', AJS.bind(this.navigateEvent, this, ['previous']));
      b = AJS.getFirst(AJS.$bytc('div','button_pause',this.galleryArea));
      AJS.AEV(b, 'click', AJS.bind(this.navigateEvent, this, ['pause']));
      b = AJS.getFirst(AJS.$bytc('div','button_next',this.galleryArea));
      AJS.AEV(b, 'click', AJS.bind(this.navigateEvent, this, ['next']));
      
      this.galleryPauseImg = AJS.getFirst(AJS.$bytc('img','gallery_pause',this.div));
		} 
    
    this.descriptionLink = AJS.getFirst(AJS.$bytc('a','description_link *',this.div));
    if(this.descriptionLink!=null){
      this.descriptionLink.href = 'javascript:void(0)';
      AJS.AEV(this.descriptionLink, 'click', AJS.bind(this.showDescriptionEvent, this, [true]));
    }
    this.descriptionPlus = AJS.getFirst(AJS.$bytc('a','description_plus',this.div));
    if(this.descriptionPlus!=null){
      this.descriptionPlus.href = 'javascript:void(0)';
      AJS.AEV(this.descriptionPlus, 'click', AJS.bind(this.showDescriptionEvent, this, [true]));
    }
    this.descriptionClose = AJS.getFirst(AJS.$bytc('a','description_close',this.div));
    if(this.descriptionClose!=null){
      this.descriptionClose.href = 'javascript:void(0)';
      this.descriptionClose.style.right = '0';
      AJS.AEV(this.descriptionClose, 'click', AJS.bind(this.showDescriptionEvent, this, [false]));
    }
        
    AJS.AEV(this.galleryImg, 'click', AJS.bind(this.showDescriptionEvent, this, [false, true]));
    
    this.descriptionDivs = AJS.$bytc('div','description',this.div);
    for(var i in this.descriptionDivs){
      this.descriptionDivs[i].style.zIndex = 10; 
    }
  },
  
  moveTo : function(newPosition,rightToLeft){
    this.parent(newPosition,rightToLeft);
    if(newPosition!='center'){
      this.showDescription(false,false);
      this.gallery.reset();
    }
  }, 
  
  moveToCompleted : function(){
    this.parent(); 
    if(this.position=='center' && !this.visible){
      try {
        if(this.gallery!=null && this.gallerySize>1){
          this.gallery.reset();
          this.gallery.play();
        }
      }
      catch(e){
      }
      this.handlePlayPause();
    }
  },
  
  // Show the description
  showDescriptionEvent : function(evt,show,restart) {
    var evt = evt || event;
    if (evt == null) {
      return null;
    }
    evt.element = evt.target || evt.srcElement;
    //IE 6 bug ?
    if(show && evt.element.className.indexOf('_close')!=-1){
      return; 
    }
    this.showDescription(show,restart && this.descriptionShow);
  },
	showDescription : function(show,restart) {
    if(this.descriptionDivs!=null){
      show = show == true;
      for(var i in this.descriptionDivs){
        this.descriptionDivs[i].style.display = show?'block':'none';
        AJS.setOpacity(this.descriptionDivs[i],0.8)
        this.descriptionDivs[i].style.zIndex = 10;
      }
      if(this.galleryArea!=null){
        this.galleryArea.style.display = show?'none':'block';
      }
      if(this.galleryPauseImg!=null){
        this.galleryPauseImg.src = urlPrefix+'img/nav/'+(show?'n_play.gif':'n_pause.gif');
      }
      if(this.descriptionLink!=null){
        this.descriptionLink.style.display = show?'none':'block';
      }
      if(this.descriptionPlus!=null && this.descriptionClose==null){
        this.descriptionPlus.style.display = show?'none':'block';
      }
      if(this.descriptionClose!=null){
        this.descriptionClose.style.display = show?'block':'none';
      }      
      
      if(this.gallery!=null){
        if(show){
          this.gallery.pause(); 
        }
        else if(restart){
          this.gallery.play();
        }
        this.handlePlayPause();
      }
      
      this.descriptionShow = show;
    }
	},
  
  navigateEvent : function(evt,what) {
    this.navigate(what);
	},
	navigate : function(what) {
    if(this.gallery==null)
      return;

    switch(what){
      case 'next':
        this.gallery.next();
      break;
      case 'previous':
        this.gallery.previous();
      break;
      case 'pause':
        if(this.gallery.isPaused()){
          this.gallery.play();
        }
        else {
          this.gallery.pause();
        }
      break;
    }
    this.handlePlayPause();
	},
  handlePlayPause : function(){
    if(this.gallery!=null && this.galleryPauseImg!=null){
      this.galleryPauseImg.src = urlPrefix+'img/nav/'+(this.gallery.isPaused()?'n_play.gif':'n_pause.gif');
    }
  },
  
  showButtons : function() {
	  try {
      clearTimeout(this.buttonTimer);
    } catch(ex) { }
    this.handleButtons(true);
	},
	hideButtons : function() {
	  this.buttonTimer = setTimeout(AJS.$b(this.handleButtonsEvent, this, [false]), 100);
	},
  handleButtonsEvent : function(e,show) {
     this.handleButtons(show);
  },
	// Show the buttons
	handleButtons : function(show) {
	  if (this.gallerySize > 0 && this.galleryArea) {
      if(this.showButtons==show){
        return;
      }
      
      this.showButtons = show;
      
  	  var ul = AJS.getFirst(this.galleryArea.getElementsByTagName('ul'));
      if(ul!=null){
        if (show) {
          ul.style.display = 'block';
          AJS.setOpacity(ul,1);
        } 
        else {
          AJS.fx.fadeOut(ul, {duration: 800, onComplete: AJS.$b(
              function(foo, elm) {
                try {
                  elm.style.display = 'none';
                } catch(ex1) {
                  try {
                    foo.style.display = 'none';
                    } catch(ex2) { }
                }
          }, this)});
        }
      }
	  }
	}
});
