/*************************************************************************

  dw_viewport.js
  version date Nov 2003
  
  This code is from Dynamic Web Coding 
  at http://www.dyn-web.com/
  Copyright 2003 by Sharon Paine 
  See Terms of Use at http://www.dyn-web.com/bus/terms.html
  regarding conditions under which you may use this code.
  This notice must be retained in the code as is!

*************************************************************************/  
  
var viewport = {
  getWinWidth: function () {
    this.width = 0;
    if (window.innerWidth) this.width = window.innerWidth - 18;
    else if (document.documentElement && document.documentElement.clientWidth) 
      this.width = document.documentElement.clientWidth;
    else if (document.body && document.body.clientWidth) 
      this.width = document.body.clientWidth;
  },
  
  getWinHeight: function () {
    this.height = 0;
    if (window.innerHeight) this.height = window.innerHeight - 18;
    else if (document.documentElement && document.documentElement.clientHeight) 
      this.height = document.documentElement.clientHeight;
    else if (document.body && document.body.clientHeight) 
      this.height = document.body.clientHeight;
  },
  
  getScrollX: function () {
    this.scrollX = 0;
    if (typeof window.pageXOffset == "number") this.scrollX = window.pageXOffset;
    else if (document.documentElement && document.documentElement.scrollLeft)
      this.scrollX = document.documentElement.scrollLeft;
    else if (document.body && document.body.scrollLeft) 
      this.scrollX = document.body.scrollLeft; 
    else if (window.scrollX) this.scrollX = window.scrollX;
  },
  
  getScrollY: function () {
    this.scrollY = 0;    
    if (typeof window.pageYOffset == "number") this.scrollY = window.pageYOffset;
    else if (document.documentElement && document.documentElement.scrollTop)
      this.scrollY = document.documentElement.scrollTop;
    else if (document.body && document.body.scrollTop) 
      this.scrollY = document.body.scrollTop; 
    else if (window.scrollY) this.scrollY = window.scrollY;
  },
  
  getAll: function () {
    this.getWinWidth(); this.getWinHeight();
    this.getScrollX();  this.getScrollY();
  }
  
}


var position = {
  init : function (sender, tipID) {
     this.sender = sender;
     this.tipID = tipID;
     viewport.getAll();
     this.getSenderTop();
     this.getSenderLeft();
     this.getSenderHeight();
     this.getSenderWidth();
     this.getTipHeight();
     this.getTipWidth();
     this.arrW = 55;
     this.arrH = 101;
     this.tipW = 255;

  },

  compare : function () {
     this.is_hidden_top = (viewport.scrollY>this.SenderY);
     this.is_hidden_bottom = ((this.SenderY+this.SenderH) > (viewport.scrollY+viewport.height));
     if (this.is_hidden_top && this.is_hidden_bottom) this.is_hidden_bottom = false;
     
     this.left_just = false;
     this.right_just = false;
     l_space = this.SenderX - viewport.scrollX; 
     r_space = viewport.width - (this.SenderX + this.SenderW - viewport.scrollX);
     if (l_space>r_space) {
       this.left_just = true
     }else {            
       this.right_just = true
     }

     if (this.is_hidden_top || this.is_hidden_bottom) {
        if (this.is_hidden_top) {
           this.image_name = "up";
           this.arrY = viewport.scrollY + 25;
           this.tipY = viewport.scrollY + 5;
        }
        if (this.is_hidden_bottom) {
           this.image_name = "lower";
           this.arrY = viewport.scrollY + viewport.height - this.arrH + 25;
           this.tipY = viewport.scrollY + viewport.height - this.TipH;
        }
     } else {


          if ((this.SenderH/2 + this.SenderY) < (viewport.height/2 + viewport.scrollY)) {
             this.image_name = "up";
             if ((this.SenderY + this.SenderH/2 - this.TipH/2) < (viewport.scrollY)) {
               this.tipY = viewport.scrollY + 5;
             } else {
               this.tipY = this.SenderY + (this.SenderH/2) - (this.TipH/2);
             }

          } else {
             this.image_name = "lower";
             if ((this.SenderY + this.SenderH/2 + this.TipH/2) > (viewport.scrollY+viewport.height)) {
               this.tipY = viewport.scrollY + viewport.height - this.TipH;
             } else {
               this.tipY = this.SenderY + (this.SenderH/2) - (this.TipH/2);
             }
          }
          this.arrY = this.SenderY + (this.SenderH - this.arrH)/2 + 20;
          
          
      }


     if (this.left_just) {
       this.image_name = this.image_name + '-right-combo.gif';
       this.arrX = this.SenderX + 28 - this.arrW;
       this.tipX = this.SenderX + 30 - this.arrW  - this.tipW;
     }
     if (this.right_just) {
       this.image_name = this.image_name + '-left-combo.gif';
       this.arrX = this.SenderX + this.SenderW - 28;
       this.tipX = this.SenderX + this.SenderW - 44 + this.arrW;
     }
     
     
  },

  compare_exp : function () {
     this.is_hidden_top = (viewport.scrollY>this.SenderY);
     this.is_hidden_bottom = ((this.SenderY+this.SenderH) > (viewport.scrollY+viewport.height));
     if (this.is_hidden_top && this.is_hidden_bottom) this.is_hidden_bottom = false;
     
     this.left_just = false;
     this.right_just = false;
     l_space = this.SenderX - viewport.scrollX; 
     r_space = viewport.width - (this.SenderX + this.SenderW - viewport.scrollX);
     if (l_space>r_space) {
       this.left_just = true
     }else {            
       this.right_just = true
     }

     if (this.is_hidden_top || this.is_hidden_bottom) {    
        if (this.is_hidden_top) {
           this.image_name = "up";
           this.arrY = viewport.scrollY + 25;
           this.tipY = viewport.scrollY + 5;
        }
        if (this.is_hidden_bottom) {
           this.image_name = "lower";
           this.arrY = viewport.scrollY + viewport.height - this.arrH + 25;
           this.tipY = viewport.scrollY + viewport.height - this.TipH;
        }
     } else {                                  
             

          if ((this.SenderH/2 + this.SenderY) < (viewport.height/2 + viewport.scrollY)) {  
             this.image_name = "up";
             if ((this.SenderY + this.SenderH/2 - this.TipH/2) < (viewport.scrollY)) {
               this.tipY = viewport.scrollY + 15;
             } else {
               this.tipY = this.SenderY + (this.SenderH/2) - (this.TipH/2) +10;
             }

             this.arrY = this.SenderY + (this.SenderH - this.arrH)/2 + 40;

          } else {                    
             this.image_name = "lower";
             if ((this.SenderY + this.SenderH/2 + this.TipH/2) > (viewport.scrollY+viewport.height)) {
               this.tipY = viewport.scrollY + viewport.height - this.TipH -  10;
             } else {
               this.tipY = this.SenderY + (this.SenderH/2) - (this.TipH/2) - 10 ;
             }

             this.arrY = this.SenderY + (this.SenderH - this.arrH)/2 + 20;
          }
          
          
          
      }


     if (this.left_just) {
       this.image_name = this.image_name + '-right-combo.gif';
       this.arrX = this.SenderX + 28 - this.arrW;
       this.tipX = this.SenderX + 30 - this.arrW  - this.tipW;
     }
     if (this.right_just) {
       this.image_name = this.image_name + '-left-combo.gif';
       this.arrX = this.SenderX + this.SenderW - 28;
       this.tipX = this.SenderX + this.SenderW - 44 + this.arrW;
     }
     
     
  },

  compareA : function (x, y) {
     this.left_just = false;
     this.right_just = false;
     l_space = x - viewport.scrollX; 
     r_space = viewport.width - (x - viewport.scrollX);
     if (l_space>r_space) {
       this.left_just = true
     }else {            
       this.right_just = true
     }
     if (y < (viewport.height/2 + viewport.scrollY)) {
             this.image_name = "up";
             if ((y - this.TipH/2) < (viewport.scrollY)) {
               this.tipY = viewport.scrollY + 5;
             } else {
               this.tipY = y - (this.TipH/2);
             }
             this.arrY = this.SenderY +this.SenderH/2 - this.arrH/2 + 50;

          } else {
             this.image_name = "lower";
             if ((y + this.TipH/2) > (viewport.scrollY+viewport.height)) {
               this.tipY = viewport.scrollY + viewport.height - this.TipH;
             } else {
               this.tipY = y - (this.TipH/2);
             }
             this.arrY = this.SenderY + this.SenderH/2 - this.arrH/2;
          }

     if (this.left_just) {
       this.image_name = this.image_name + '-right-combo.gif';
       this.arrX = x  - this.arrW;
       this.tipX = x + 2 - this.arrW  - this.tipW;
     }
     if (this.right_just) {
       this.image_name = this.image_name + '-left-combo.gif';
       this.arrX = x - 10;
       this.tipX = x - 26 + this.arrW;
     }
  },

  getSenderTop : function (){
     yPos = document.getElementById(this.sender).offsetTop;
     tempEl = document.getElementById(this.sender).offsetParent;
       while (tempEl != null) {
        yPos += tempEl.offsetTop;
        tempEl = tempEl.offsetParent;
     }
     this.SenderY = yPos;
  },

  getSenderLeft : function (){
     xPos = document.getElementById(this.sender).offsetLeft;
     tempEl = document.getElementById(this.sender).offsetParent;
       while (tempEl != null) {
        xPos += tempEl.offsetLeft;
        tempEl = tempEl.offsetParent;
      }
     this.SenderX = xPos;
  },

  getSenderHeight : function (){
    if (document.getElementById(this.sender).height) {
     this.SenderH = document.getElementById(this.sender).height; 
    } else {
      if (document.getElementById(this.sender).offsetHeight) {
       this.SenderH = document.getElementById(this.sender).offsetHeight; 
      } 
    }
  },

  getSenderWidth : function (){
    if (document.getElementById(this.sender).width) {
     this.SenderW = document.getElementById(this.sender).width;
    } else {
      if (document.getElementById(this.sender).offsetWidth) {
       this.SenderW = document.getElementById(this.sender).offsetWidth; 
      } 
    }
  },
  getTipHeight : function (){
     this.TipH = document.getElementById(this.tipID).offsetHeight; 
  },
  getTipWidth : function (){
     this.TipW = document.getElementById(this.tipID).offsetWidth;
  }
}
