window.addEvent('domready',function(){
  var duration = 1000;
  var wait = 2000;
  
  if($chk(PAGE_HOME))
    redirect = PAGE_HOME;
  else
    redirect = false;
    
  if($chk($('intro')))
  {
    var right = false;
    var left = false;
    
    $$('.right').each(function(el) {
      new Fx.Morph(el,{
                  duration: duration,
                  transition: Fx.Transitions.Sine.easeIn,
                  onComplete: function() {
                    // timer = window.setTimeout('redirect',2000);
                    if($chk(redirect))
                      (function() { location.href = redirect }).delay(wait);
                  }.pass(redirect,wait)
                }).start({
                  opacity: [0,1]
                }); 
    })

    $$('.left').each(function(el) {
      new Fx.Morph(el,{
                  duration: duration,
                  transition: Fx.Transitions.Sine.easeIn
                }).start({
                  opacity: [0,1]
                }); 
    })
  }
  
  $$('.fadein').each(function(el) {
    new Fx.Morph(el,{
                duration: duration,
                transition: Fx.Transitions.Sine.easeIn
              }).start({
                opacity: [0,1]
              }); 
  });
  
  $$('.slide').each(function(el) {
  
    el.getFirst().setStyles({
      'position':'absolute',
      'right':el.getFirst().getSize().x*-1,
      'opacity':0
    });

    el.setStyles({
      'width':el.getSize().x,
      'height':el.getSize().y,
      'visibility':'visible',
      'overflow':'hidden'
    });

    new Fx.Morph(el.getFirst(),{
                duration: duration,
                transition: Fx.Transitions.Sine.easeOut
              }).start({
                'right': [el.getFirst().getStyle('right'),0],
                'opacity' : [el.getFirst().getStyle('opacity'),1]
              }); 
  });
  
  
  

  scrollField = {
    init : function(list)
    {
      if($chk(list))
      {
        new Element('div', {
          'class':'spacer',
          'style':'left:'+list.getScrollSize().x+'px'
        }).inject(list);

        this.obj = list;
        this.prevBtn.inject($('content')).list = this;
        this.nextBtn.inject($('content')).list = this;
        this.maxScroll = this.obj.getScrollSize().x - this.obj.getSize().x;
        
/*
        this.effect = new Fx.Tween(this.obj,{
          transition: Fx.Transitions.Sine.easeOut
        });
        

        this.effect = new Fx.Scroll(this.obj,{
          transition: Fx.Transitions.Sine.easeOut
        });
*/
        return this;
      }
    },
    effect : false,
    obj : false,
    timer : false,

    speed : 10,

    direction : 0,
    scrollerOn : false,
    scrollToPos : 0,

    doScroll : function(dir){
      if(typeof(dir)!='undefined')
      { 
        if((dir<=1 && this.obj.getScroll().x>0) || (dir>=1 && this.obj.getScroll().x<this.maxScroll))
        {
          this.direction = dir;
          this.scrollerOn = true;
          this.scroller();
        }
      }
    },
    stopScroll : function(){
      this.scrollerOn = false;
      this.direction = 0;
      this.timer = null;
    },
    scroller : function(){
      if(this.scrollerOn)
        this.scrollToPos+=this.direction * this.speed;

      if(this.scrollToPos>this.maxScroll)
      {
        this.scrollToPos = this.maxScroll;
        this.stopScroll();
//        this.nextBtn.setStyle('opacity',.5);
      }
      else if(this.scrollToPos<0)
      {
        this.scrollToPos = 0;
        this.stopScroll();
//        this.prevBtn.setStyle('opacity',.5);
      }
      
      this.prevBtn.setStyle('opacity',this.scrollToPos>0 ? .7 : .2);
      this.nextBtn.setStyle('opacity',this.scrollToPos<this.maxScroll ? .7 : .2);

       
      step = (this.scrollToPos - this.obj.getScroll().x) / 8;
      if(step>0)
        step = Math.ceil(step);
      else
        step = Math.floor(step);

      this.obj.scrollTo(this.obj.getScroll().x+step,0);
      
      if(this.obj.getScroll().x != this.scrollToPos)
        this.timer = this.scroller.delay(10,this);
    },
    prevBtn : new Element('a',{
      'href':'javascript:void(0)',
      'class':'prev'
    })
    .addEvent('mouseenter', function(){ this.list.doScroll(-1); })
    .addEvent('mouseleave', function(){ this.list.stopScroll(); })
    .setStyle('opacity',.5)
    .set('text','«'),
    nextBtn : new Element('a',{
      'href':'javascript:void(0)',
      'class':'next'
    })
    .addEvent('mouseenter', function(){ this.list.doScroll(1); })
    .addEvent('mouseleave', function(){ this.list.stopScroll(); })
    .setStyle('opacity',1)
    .set('text','»')
  };

  
  $$('#content .logolist').each(function(list){
    if(list.getSize().x < list.getScrollSize().x)
    {
      list.scroller = scrollField.init(list);
    }
  });

  $$('.image-list').each(function(list){

    if(list.getSize().x < list.getScrollSize().x)
    {
      list.scroller = scrollField.init(list);
    }
  });

  $$('.evelyn a').each(function(link,i){
    
    link.setStyles({
      'position':'absolute',
      'top': (i%2==0 ? 400 : -400),
      'opacity':0,
      'display':'block'
    });
    
    new Fx.Morph(link,{
      duration: duration*(i*.5 + 1),
      transition: Fx.Transitions.Sine.easeOut
    }).start({
      'top': [link.getStyle('top'),0],
      'opacity' : [link.getStyle('opacity'),1]
    }); 
  });

  if($chk($$('.modelogo')) && $$('.modelogo').length>0)
  {
    var modelogo = {
      logos : $$('.modelogo'),
      effect: false,
      currentlogo : false,
      container : new Element('div',{'class':'modelogo-stage'}).inject($('content')),
      hide : function() {
        if(typeof(this.container)=='object') this.container.empty();
      },
      show : function() {
        this.hide();
        
  /*
        tox = parseInt(this.currentlogo.getStyle('width'));
        toy = parseInt(this.currentlogo.getStyle('height'));
        fromx = tox/3;
        fromy = (fromx/tox) * toy;
  */
  
        this.currentlogo.inject(this.container)
          .setStyles({
            'opacity':0
          });
        
        tox = this.currentlogo.getSize().x;
        toy = this.currentlogo.getSize().y;
        fromx = tox/3;
        fromy = (fromx/tox) * toy;
  
        this.currentlogo.setStyles({
            'margin-left':fromx/-2,
            'margin-top':fromy/-2,
            'width':fromx,
            'height':fromy
          });
  
  
        this.effect = new Fx.Morph(this.currentlogo,{
          duration: duration,
          transition: Fx.Transitions.Sine.easeOut
        }).start({
          'margin-top': [fromy/-2,toy/-2],
          'margin-left' : [fromx/-2,tox/-2],
          'width' : [fromx,tox],
          'height' : [fromy,toy],
          'opacity': [0,1]
        }); 
      }
    };
    
    $$('.modelogo').each(function(logo){
      logo.addEvent('click',function(){
        // this.href = "javascript:void(0)";
        imagecls = this.get('class').split(' ');
        imagecls = imagecls[1].replace(/logo/g, "");
        images = $$('.bild'+imagecls)
        if($chk(images[0]))
          images[0].fireEvent('click',images[0]);          

        return false;
      });

      logo.addEvent('mouseenter',function(){
        modelogo.currentlogo = new Asset.image(this.href, {onload: modelogo.show.bind(modelogo)});
        this.target = '_self';
        
      })
      logo.addEvent('mouseleave',function(){
        modelogo.hide();
      })
    });
  }

});

