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(!$chk(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]);
      }
    }
  }
}

/*
var _ie6fix = {
  init : function(){
    if(Browser.Engine.trident && Browser.Engine.version.toInt()<=4)
    {
      $$('.rex-navi1>li').each(function(li,i){
        li.addEvents({
          'mouseenter' : function(){ this.addClass('hover'); }.bind(li),
          'mouseleave' : function(){ this.removeClass('hover'); }.bind(li)
          
        });
      })
    }
  }
}
*/

_tooltips = {
  duration : 200,
  
  getContainerId : function(classstring){
    classstring = classstring.indexOf(' ')>-1 ? classstring.split(' ') : new Array(classstring);
    name = false;
    for(i=0; i<classstring.length; i++)
    {
      if(classstring[i].substr(0,10)=='container-')
      {
        name = classstring[i];
        break;
      }
    }
    return name;
  },
  
  init : function(elements)
  {
    if($type(elements)=='string') elements = $$(elements);
    else if($type(elements)!='array') elements = new Array(elements);
    
    if(elements.length>0)
    {
      elements.each(function(el,i){
        if($type(el)=='element')
        {
          el.addEvent('mouseenter',function(e) {
            if($chk(this.effect)) this.effect.cancel();
            if(!$chk(this.tooltip))
            {
              if($$('.'+_tooltips.getContainerId(this.get('class'))+'.tooltip-text').length>0)
                this.tiptext = $$('.'+_tooltips.getContainerId(this.get('class'))+'.tooltip-text')[0].get('html');
              else
                this.tiptext = $chk(this.getFirst('.tooltip-text')) ? this.getFirst('.tool-tip-text').get('html') : ($chk(this.title) ? this.title : '')

              if(this.tiptext!='')
              {
                this.tooltip = new Element('div', {
                  'class':'tooltip',
                  'style':'width:'+_tooltips.width+'px'
                });
                new Element('div',{'class':'tip'}).set('html',this.tiptext).inject(this.tooltip);
                new Element('div',{'class':'tip-bottom'}).inject(this.tooltip);
                
                this.tooltip.inject($$('body')[0]).setStyles({
                  // top:this.getPosition($$('#content-01 .centerme')[0]).y + Math.round(this.getSize().y/3) - this.tooltip.getSize().y,
                  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.duration,
                  transition: Fx.Transitions.Sine.easeOut,
                  onComplete: function(){
                    $empty(this.tipeffect);
                    
                  }.bind(this)
                }).start({
                  opacity: [this.tooltip.getStyle('opacity'),1],
                  marginTop: [this.tooltip.getStyle('margin-top'),0]
                }); 
              }
            }
          });
          el.addEvent('mouseleave',function(){
            if($chk(this.effect)) this.effect.cancel();
            if($chk(this.tooltip))
            {
              this.effect = new Fx.Morph(this.tooltip,{
                duration: _tooltips.duration,
                transition: Fx.Transitions.Sine.easeOut,
                onComplete: function(){ this.tooltip.destroy(); this.tooltip = false; }.bind(this)
              }).start({
                opacity: [this.tooltip.getStyle('opacity'),0],
                marginTop: [this.tooltip.getStyle('margin-top'),20]
              }); 
            }
          });
          el.addEvent('mousemove',function(e){
            if($chk(this.tooltip))
            {
              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
              });
            }
          });

        }
      })
    }
  }
};

_raster = {
  init : function()
  {
    if($$('#content.raster').length>0)
    {
      this.container = $$('#content.raster')[0];
      
      new Element('div',{'id':'raster-onoff'})
      .setStyles({
        'position':'absolute',
        'top':'450px',
        'left':0,
        'background':'#e0e0e0',
        'width':'10px',
        'height':'10px',
        'z-index':90      
      })
      .addEvent('click',function(){
        if(this.container.hasClass('raster')) this.container.removeClass('raster');
        else this.container.addClass('raster');
      }.bind(this))
      .inject(this.container);
      
      this.container.removeClass('raster');
    }
  }
}
window.addEvent('domready', function(){
  _tooltips.init('.tool-tip');
  _raster.init();
});
