window.addEvent('domready', function(){
  $$('.rex-navi1 li').addEvent('mouseenter',function(){ this.addClass('hovered'); });
  $$('.rex-navi1 li').addEvent('mouseleave',function(){ this.removeClass('hovered'); });
  
  $$('.info-diagram').each(function(el,i){
    if(images = el.getElements('.theimage li'))
    {
      images.each(function(img,i){
        if(img.hasClass('active')) el.rel = 'item'+(i+1);
        img.addEvent('mouseenter',function(){
          this.getParent('.info-diagram').getElements('.explanation').removeClass('isactive');
          this.getParent('.info-diagram').getElements('li').removeClass('active');
          this.getParent('.info-diagram').getElements('li.'+this.get('class')).addClass('active');
          this.getParent('.info-diagram').getElements('.explanation').addClass('isactive');
        });
      });
      el.addEvent('mouseleave',function(){
        if(this.getParent('.info-diagram')) p = this.getParent('.info-diagram');
        else p = this;
        
        p.getElements('.explanation').removeClass('isactive');
        this.getElements('li').removeClass('active');
        this.getElements('li.'+this.rel).addClass('active');
        if(this.getElements('li.'+this.rel).length)
          p.getElements('.explanation').addClass('isactive');
      });
    }
  });
  
  if(Browser.ie && Browser.version=='6')
  {
    // alert($(document).getScrollSize().y+'px');
    h = _basics.getAbsoluteSize($$('#page .centerme')[0]).y < _basics.getAbsoluteSize($('page')).y ? _basics.getAbsoluteSize($('page')).y : _basics.getAbsoluteSize($$('#page .centerme')[0]).y;
    h = h+430;

    if($$('.mediaplayer>.video').length > 0)
      h+= $$('.mediaplayer>.video').length * 400;

    if($$('body')[0].hasClass('with-background')) h+=0;
    
    $('page').setStyle('height',(h-405)+'px');

    $$('body')[0].setStyle('height',h+'px');
    
    $('footer').setStyles({
      'bottom':'auto',
      'top':(h-$('footer').getSize().y)+'px'
    });

    if($('background'))
    {
      $('background').setStyles({
        'height':'1240px',
        'bottom':'0'
      });
    }
    
    $('footer').setStyles({'visibility':'visible'});
    
    $('language-menu').addEvent('mouseenter',function(){ this.addClass('hover'); });
    $('language-menu').addEvent('mouseleave',function(){ this.removeClass('hover'); });
  }
    
  $$('.imagegallery .media').each(function(el,i){
    if(tmp = __pager(el))
    {
      el.pager =   tmp.init();
      delete tmp;
    }
  });

  
});


var _basics = {
  _getBorderAndPadding: function(el){
    return {
      x : el.getStyle('paddingLeft').toInt() + el.getStyle('paddingRight').toInt() + el.getStyle('borderLeftWidth').toInt() + el.getStyle('borderRightWidth').toInt(),
      y : el.getStyle('paddingTop').toInt() + el.getStyle('paddingBottom').toInt() + el.getStyle('borderTopWidth').toInt() + el.getStyle('borderBottomWidth').toInt()
    };
  },
  
  getAbsoluteSize: function(el,debug){
    return {
      x : el.getSize().x - this._getBorderAndPadding(el).x,
      y : el.getSize().y - this._getBorderAndPadding(el).y
    };
  },
  
  getAbsoluteScrollSize: function(el){
    return {
      x : el.getScrollSize().x - this._getBorderAndPadding(el).x,
      y : el.getScrollSize().y - this._getBorderAndPadding(el).y
    };
  },
  
  debug : {
    win : null,
    msg : function(msg) {
      this.init();
      this.win.set('html',msg);
    },
    
    init : function()
    {
      if(!this.win)
      {
        this.win = new Element('div', {
          'style' : 'position:absolute;top:0;left:0;width:300px;z-index:99;background:#ff0000;color:#fff'
        })
        .inject($$('body')[0]);
      }
    }
  }
}

_tooltips = {
  options : {
    duration : 200,
    width : 200,
    transition : Fx.Transitions.Sine.easeOut,
    hideOnMouseLeave : true,
    followMouse : true
  },
  
  getOptions : function(options)
  {
    options  = typeOf(options)!='object' ? {} : options;
    for(var prop in this.options)
    {
      if(typeof options[prop] != 'undefined')
      {
        if(typeOf(this.options[prop])!=typeOf(options[prop]))
        {
          if(typeOf(this.options[prop])=='string') options[prop] = options[prop]+'';
          else if(typeOf(this.options[prop])=='number') options[prop] = options[prop].toInt();
          else options[prop] = null;
        }
        this.options[prop] = options[prop];
      }
    }
  },
  
  
  init : function(elements,options)
  {
    this.getOptions(options);
    elements = typeOf(elements)=='string' ? $$(elements) : (typeOf(elements)!='array' ? new Array(elements) : elements);
    
    if(elements.length>0)
    {
      elements.each(function(el,i){
        if(typeOf(el)=='element')
        {
          el.addEvent('mouseenter', function(e) {
            if(typeof this.effect != 'undefined') this.effect.cancel();
            if(!this.tooltip)
            {
              if(this.getFirst('.tooltip-text')!=null)
                this.tiptext = this.getFirst('.tool-tip-text').get('html');
              else if(this.get('title')!=null)
                this.tiptext = this.get('title');
              else if(this.getElement('img')!=null && this.getElement('img').get('alt')!=null)
                this.tiptext = this.getElement('img').get('alt');
              else
                this.tiptext = ''

              if(this.tiptext!='')
              {
                this.tooltip = new Element('div', {
                  'class':'tooltip',
                  'style':'width:'+_tooltips.options.width+'px'
                });
                new Element('div',{'class':'tip'}).set('html',this.tiptext).inject(this.tooltip);
                new Element('div',{'class':'tip-bottom'}).inject(this.tooltip);
                if(_tooltips.options.hideOnMouseLeave) new Element('a',{'class':'tip-close'}).set('html','x').inject(this.tooltip);
                
                this.tooltip.inject($$('body')[0]).setStyles({
                  top: e.client.y - this.tooltip.getSize().y - 10,
                  left: e.client.x - Math.round(this.tooltip.getSize().x/2) + 15,
                  marginTop: -20,
                  opacity:0,
                  display:'block'
                });
                
    
                this.effect = new Fx.Morph(this.tooltip,{
                  duration: _tooltips.options.duration,
                  transition: _tooltips.options.transition,
                  onComplete: function(){
                    this.effect = null;
                  }.bind(this)
                }).start({
                  opacity: [this.tooltip.getStyle('opacity'),1],
                  marginTop: [this.tooltip.getStyle('margin-top'),0]
                }); 
              }
            }
            else
              this.tooltip = null;
          });
          
          if(!_tooltips.options.hideOnMouseLeave)
          {
            el.addEvent('mouseleave',function(){
              if(typeof this.effect != 'undefined') this.effect.cancel();
              if(this.tooltip!=null)
              {
                this.effect = new Fx.Morph(this.tooltip,{
                  duration: _tooltips.options.duration,
                  transition: _tooltips.options.transition,
                  onComplete: function(){ this.tooltip.destroy(); this.tooltip = false; }.bind(this)
                }).start({
                  opacity: [this.tooltip.getStyle('opacity'),0],
                  marginTop: [this.tooltip.getStyle('margin-top'),20]
                }); 
              }
            });
          }
          
          if(_tooltips.options.followMouse)
          {
            el.addEvent('mousemove',function(e){
              if(this.tooltip!=null)
              {
                posx = e.page.x - Math.round(this.tooltip.getSize().x/5) + 20;
                if((minus = $$('body')[0].getSize().x+$$('body')[0].getScroll().x - (posx + this.tooltip.getSize().x + 20)) < 0)
                  posx+=minus;
                else if(posx<10)
                  posx = 10;               
                
                this.tooltip.setStyles({
                  top: e.page.y - this.tooltip.getSize().y - 10,
                  left: posx
                });
              }
            });
          }
        }
      });
    }
  }
};


getObjectSize = function(obj)
{
  r = {x:0,y:0};
  if(obj)
  {
    r.x+=obj.getSize().x;
    for(var t in {'left':0,'right':0})
    {
      if(ml = obj.getStyle('magin-'+t))
      {
        if(ml.indexOf('em')>-1) r.x+=ml.toInt()*10;
        else r.x+=ml.toInt();
      }
      if(ml = obj.getStyle('padding-'+t))
      {
        if(ml.indexOf('em')>-1) r.x+=ml.toInt()*10;
        else r.x+=ml.toInt();
      }
/*
      if(ml = obj.getStyle('border-'+t+'-width'))
      {
        if(ml.indexOf('em')>-1) r.x+=ml.toInt()*10;
        else r.x+=ml.toInt();
      }
*/
    }

    r.y+=obj.getSize().y;
    for(var t in {'top':0,'bottom':0})
    {
      if(ml = obj.getStyle('magin-'+t))
      {
        if(ml.indexOf('em')>-1) r.y+=ml.toInt()*10;
        else r.y+=ml.toInt();
      }
      if(ml = obj.getStyle('padding-'+t))
      {
        if(ml.indexOf('em')>-1) r.y+=ml.toInt()*10;
        else r.y+=ml.toInt();
      }
/*
      if(ml = obj.getStyle('border-'+t+'-width'))
      {
        if(ml.indexOf('em')>-1) r.y+=ml.toInt()*10;
        else r.y+=ml.toInt();
      }
*/
    }

  }
  return r;
}

__pager = function(el) {
  if(typeOf(el)=='element')
  {
    returnObj = {
      duration : 500,
      transition : Fx.Transitions.Sine.easeOut,
      container : el,
      items : null,
      viewsize : {x:0,y:0},
      completesize : {x:0,y:0},
      space : 7,
      
      navigation : null, // the element containing the navigation items,
      stage      : null, // the element where the sliding is done
      wrapper    : null, // the element containing the items - this one is sliding left and right
      
      prevButton : null,
      nextButton : null,
      pagerButtons : null,
      
      current : 0,
      upcoming : 0,
      ismoving : false,
      
      mousedownandup : true,
      
      init : function(options)
      {
        if(typeOf(options) == 'object')
        {
          if(options.mousedownandup===true) this.mousedownandup = true;
        }
      
        this.items = this.container.getChildren().setStyles({'display':'block'});
        if(this.items.length > 1)
        {
          this.wrapper = new Element('div.wrapper');
          
          newitems = new Array();
          
          for(i=0; i<this.items.length; i++)
          {
            if(this.items[i].hasClass('active') && !this.current) this.current = i;
            
            newitems.push(this.items[i]
            .clone().cloneEvents(this.items[i])
            .setStyles({
              'position' : 'absolute',
              'top' : 0,
              'left' : this.completesize.x+'px',
              'width' : this.items[i].getSize().x - this.items[i].getStyle('border-left-width').toInt() - this.items[i].getStyle('border-right-width').toInt() 
            })
            .inject(this.wrapper));
            

            this.completesize.x+= getObjectSize(this.items[i]).x + this.space;
            
            if(this.completesize.y < getObjectSize(this.items[i]).y) this.completesize.y = getObjectSize(this.items[i]).y
            this.viewsize.y = this.completesize.y +5;
          }
          
          this.completesize.x-=this.space;
          
          if(this.completesize.x <= this.container.getSize().x) return false;
          
          this.wrapper.setStyles({
            'position' : 'absolute',
            'top' : 0,
            'left' : 0,
            'display' : 'block',
            'width' : this.completesize.x+'px',
            'height' : this.completesize.y+'px'
          });
          
          this.stage = new Element('div.stage').setStyles({
            'position' : 'relative',
            'width' : '100%',
            'height' : this.viewsize.y+'px',
            'overflow' : 'hidden'
          });
          
          this.wrapper.inject(this.stage);
          
          this.navigation = new Element('div.navigation');
          
          this.container.empty();
          this.navigation.inject(this.container);
          this.stage.inject(this.container);

          this.insertButtons();

          this.items = newitems;
          
          this.container.addClass('pager');

          return this;
        }
        return null;
      },
      
      insertButtons : function(){
        if(!this.prevButton)
        {
          this.prevButton = new Element('a.prev',{'href':'javascript:void(0)','html':'&lt;'});

          if(!this.mousedownandup)
          {
            this.prevButton.addEvent('click',function(){ if(!this.prevButton.hasClass('inactive')) this.prev(); }.bind(this));
          }
          else
          {
            this.prevButton.addEvent('mousedown',function(){ this.constantClick(-1); }.bind(this))
            this.prevButton.addEvent('mouseup',function(){ this.constantClick(0); }.bind(this))
          }
          
          this.prevButton.inject(this.navigation);
        }
          
        if(!this.pagerButtons)
        {
          this.pagerButtons = new Array();
          for(i=0; i<this.items.length; i++)
          {
            l = new Element('a.page',{'href':'javascript:void(0)','html':i+1,'rel':i})
                .addEvent('click',function(){
                  this.show(this.rel);
                })
                .inject(this.navigation);
            l.show = function(num){
              this.show(num);
            }.bind(this);

            this.pagerButtons.push(l);
          }
        }

        if(!this.nextButton)
        {
          this.nextButton = new Element('a.next',{'href':'javascript:void(0)','html':'&gt;'});

          if(!this.mousedownandup)
          {
            this.nextButton.addEvent('click',function(){ if(!this.nextButton.hasClass('inactive')) this.next(); }.bind(this));
          }
          else
          {
            this.nextButton.addEvent('mousedown',function(){ this.constantClick(1); }.bind(this))
            this.nextButton.addEvent('mouseup',function(){ this.constantClick(0); }.bind(this))
          }
          
          this.nextButton.inject(this.navigation);
        }
        
        if(this.current == 0) this.prevButton.addClass('inactive');
        else this.prevButton.removeClass('inactive');

        if(this.current >= this.items.length-1 || this.isScrolledMax()) this.nextButton.addClass('inactive');
        else this.nextButton.removeClass('inactive');
        
        this.pagerButtons.each(function(el){ el.removeClass('inactive').removeClass('current'); });
        this.pagerButtons[this.current].addClass('inactive').addClass('current');
        
      },
      
      constantClick : function(dir)
      {
        if(dir!=1 && dir!=-1) dir = 0;
        
        if(dir===1)
        {
          if(!this.nextButton.hasClass('inactive'))
          { 
            this.next();
            this.constantClicker = (function() { this.constantClick(1); }.bind(this)).delay(100);
          }
          else
            window.clearTimeout(this.constantClicker);;
        }
        else if(dir===-1)
        {
          if(!this.prevButton.hasClass('inactive'))
          {
            this.prev();
            this.constantClicker = (function() { this.constantClick(-1); }.bind(this)).delay(100);
          }
          else
            window.clearTimeout(this.constantClicker);;
        }
        else
        { 
          window.clearTimeout(this.constantClicker);
        }
        
      },
      
      prev : function() {
        if(this.current>0) this.show(this.current-1);
      },
      
      next : function() {
        if(this.current<this.items.length-1) this.show(this.current+1);
      },
      
      show : function(num) {
        if(num != this.current && !this.ismoving)
        {
          this.ismoving = true;
          this.upcoming = num;

          maxtox = getObjectSize(this.wrapper).x - getObjectSize(this.stage).x;
          tox = (this.items[this.upcoming].getPosition(this.wrapper).x - this.items[this.upcoming].getStyle('border-left-width').toInt()) * -1;
          if(Browser.ie) tox-= this.items[this.upcoming].getStyle('border-left-width').toInt();
          
          if(maxtox + tox < 0)
          {
            tox = (maxtox * -1) - this.items[0].getStyle('border-left-width').toInt() - this.items[this.upcoming].getStyle('border-left-width').toInt() - this.items[this.upcoming].getStyle('border-right-width').toInt();
            for(i=0; i<this.items.length; i++)
            {
              tmptox = this.items[i].getPosition(this.wrapper).x * -1;
              if(maxtox + tmptox < 0)
              {
                this.upcoming = i;
                break;
              }
            }
          }
          
          this.effect = new Fx.Morph(this.wrapper, {
            duration : this.duration,
            transition : this.transition,
            onComplete : function(){
              this.current = this.upcoming.toInt();

              this.insertButtons();
              this.ismoving = false;
            }.bind(this)
          })
          .start({
            'left' : tox+'px'
          });
        }
      },
      
      isScrolledMax : function()
      {
        if(getObjectSize(this.wrapper).x <= getObjectSize(this.stage).x) return true;
        
        if(this.wrapper.getStyle('left').toInt()*-1 >= getObjectSize(this.wrapper).x - getObjectSize(this.stage).x)
          return true;
        else
          return false;
      }
      
    };
    return returnObj;
  }
  return null;
};
