/* ------------------------------------------------------------------------------------------------

  Глобальные объекты данной библиотеки:
    Body

  Классы даной библиотеки:
    -> XBody

    require 'libs/prototype/prototype'
    require 'itools/utils/global'
    
--------------------------------------------------------------------------------------------------- */

// Класс для упрощения работы с DOM-элементом BODY
//    !! инициализируется глобальный объект Body
    
//    Body.append( elementOrID ) - добавление в документ DOM-элемента (допускается передача только ID)

//    Body.width()          - полная ширина, с учетом scroll-области
//    Body.height()         - полная высота, с учетом scroll-области
//    Body.visibleWidth()   - видимая ширина, ширина без учета scroll-области
//    Body.visibleHeight()  - видимая высота, высота без учета scroll-области
//    Body.scrollWidth()    - ширина только scroll-области
//    Body.scrollHeight()   - высота только scroll-области


XBody = Class.create({

  initialize : function() {
    this.htmlObj = window.document.lastChild;
  },
  
  append: function( elementOrID ) {
    var element = $( elementOrID ); // для случая, если elementOrID - это ID
    
    if ( element ) {
      document.body.appendChild( element );
      return element;
    } else {
      return false;
    }
    
  },
  
  width: function() {
    if ( document.documentElement )
      return document.documentElement.scrollWidth;
    
    return 0;
  }, // width
  
  height: function() {
    if ( Browser.opera && document.body ) {
      if ( document.body.scrollHeight == window.innerHeight ) {
        return document.body.clientHeight;
      } else {
        return document.body.scrollHeight;
      } // if
    } else if ( document.documentElement ) {
      var res = document.documentElement.scrollHeight;
      
      if ( res < document.documentElement.clientHeight ) {
        return document.documentElement.clientHeight;
      } else {
        return res;
      } // if
    } // if
    
    return 0;
  },
  
  visibleWidth: function() {
    if ( document.documentElement )
      return document.documentElement.clientWidth;
    
    return 0;
  }, // visibleWidth
  
  visibleHeight: function() {
    if ( Browser.opera && document.body ) {
      return document.body.clientHeight;
    } else if ( document.documentElement ) {
      return document.documentElement.clientHeight;
    }
    
    return 0;
  },
  
  scrollWidth: function() {
    return ( this.width() - this.visibleWidth() );
  },
  
  scrollHeight: function() {
    return ( this.height() - this.visibleHeight() );
  },
  
  scrollLeft: function() {
    return document.lastChild.scrollLeft;
  },
  
  scrollTop: function() {
    return document.lastChild.scrollTop;
  },
  
  hasScrolling: function() {
    return ( this.scrollWidth() > 0 || this.scrollHeight() > 0 );
  }, // hasScrolling

  autoSizing: function() {
    /*var fullWidth;
    var fullHeight;
    
    if ( window.innerWidth ) {
      var dW = window.outerWidth - window.innerWidth;
      var dH = window.outerHeight - window.innerHeight;
    
      fullWidth = this.width()+dW;
      fullHeight = this.height()+dH;
    } else {
      var dW = this.htmlObj.offsetWidth - this.visibleWidth() + 10;
      var dH = this.htmlObj.offsetHeight - this.visibleHeight() + 35;
      
      fullWidth = this.width()+dW;
      fullHeight = this.height()+dH;
    }
    
    //alert(this.width() + ' x ' + this.height());
    //alert(fullWidth + ' x ' + fullHeight);

    if ( fullWidth && fullHeight ) {
      window.resizeTo( fullWidth, fullHeight );
    }
    
    return;
   /**/
 
   var str = ''; 
   var ddiv = $('debugDiv');
   
   
    //var body = window.document.lastChild;
    
    /*str += '<b>Document:</b><br>';
    str += 'width: '  + document.width + '<br>';
    str += 'height: ' + document.height + '<br><br>';
    
    var body = document.body;
    str += '<b>Body:</b><br>';
    str += 'clientWidth: '  + body.clientWidth + '<br>';
    str += 'clientHeight: ' + body.clientHeight + '<br>';
    str += 'scrollWidth: '  + body.scrollWidth + '<br>';
    str += 'scrollHeight: ' + body.scrollHeight + '<br>';
    str += 'offsetWidth: '  + body.offsetWidth + '<br>';
    str += 'offsetHeight: ' + body.offsetHeight + '<br>';
    str += 'scrollTop: '    + body.offsetWidth + '<br>';
    str += 'scrollLeft: '   + body.offsetHeight + '<br><br>';
    

    var html = document.documentElement;
    str += '<b>HTML:</b><br>';
    str += 'clientWidth: '  + html.clientWidth + '<br>';
    str += 'clientHeight: ' + html.clientHeight + '<br>';
    str += 'scrollWidth: '  + html.scrollWidth + '<br>';
    str += 'scrollHeight: ' + html.scrollHeight + '<br>';
    str += 'offsetWidth: '  + html.offsetWidth + '<br>';
    str += 'offsetHeight: ' + html.offsetHeight + '<br>';
    str += 'scrollTop: '    + html.offsetWidth + '<br>';
    str += 'scrollLeft: '   + html.offsetHeight + '<br><br>';

    str += '<b>Window:</b><br>';
    str += 'innerWidth: '  + window.innerWidth + '<br>';
    str += 'innerHeight: ' + window.innerHeight + '<br>';
    str += 'outerWidth: '  + window.outerWidth + '<br>';
    str += 'outerHeight: ' + window.outerHeight + '<br>';
    str += 'pageXOffset: ' + window.pageXOffset + '<br>';
    str += 'pageYOffset: ' + window.pageYOffset + '<br>';

    ddiv.innerHTML = str;
    
    /**/
    
    /*alert('docWidth=' + document.width);
    alert('docHeight=' + document.height);
    alert('clientWidth=' + body.clientWidth);
    alert('clientHeight=' + body.clientHeight);
    alert('scrollWidth=' + body.scrollWidth);
    alert('scrollHeight=' + body.scrollHeight);
    alert('offsetWidth=' + body.offsetWidth);
    alert('offsetHeight=' + body.offsetHeight);
    alert('innerWidth=' + window.innerWidth);
    alert('innerHeight=' + window.innerHeight);
    alert('outerWidth=' + window.outerWidth);
    alert('outerHeight=' + window.outerHeight);/**/
    
    str += 'visibleWidth: '   + this.visibleWidth() + '<br>';
    str += 'visibleHeight: '  + this.visibleHeight() + '<br>';
    str += 'width: '          + this.width() + '<br>';
    str += 'height: '         + this.height() + '<br>';

    ddiv.innerHTML = str;
    
    /*alert( 'visibleWidth=' + this.visibleWidth() );
    alert( 'visibleHeight=' + this.visibleHeight() );
    alert( 'width=' + this.width() );
    alert( 'height=' + this.height() );
    alert( 'scrollWidth=' + this.scrollWidth() );
    alert( 'scrollHeight=' + this.scrollHeight() );
    /**/
    
  
    return;
  
    var sHeight;
    var sWidth;
    var wHeight;
    var wWidth;
    
    if ( Browser.msie ) {
      var body = this.htmlObj.lastChild;
      
      wWidth  = body.offsetWidth;
      wHeight = body.offsetHeight;
      
      sWidth  = this.scrollWidth();
      sHeight = this.scrollHeight();
    } else {
      wWidth  = window.outerWidth;
      wHeight = window.outerHeight;
      
      sWidth  = this.scrollWidth();
      sHeight = this.scrollHeight();
    } // if
    
    alert(wWidth);
    alert(wHeight);
    alert(this.width());
    alert(this.height());
    alert(this.scrollWidth());
    alert(this.scrollHeight());
    
    
    
    //window.resizeTo( wWidth + sWidth, wHeight + sHeight );
    
    //if ( sWidth != 0 || sHeight != 0 || || wWidth == 0 || wHeight == 0 ) {
    //  window.resizeTo( wWidth + sWidth, wHeight + sHeight );
    //} // if
  } // autoSizing
});

Body = new XBody();
