// JavaScript Document
// JavaScript Document

var viewport=new Viewport();
function Viewport(){
  this.xMousePos = 0; // Horizontal position of the mouse on the screen
  this.yMousePos = 0; // Vertical position of the mouse on the screen
  this.xMousePosMax = 0; // Width of the page
  this.yMousePosMax = 0; // Height of the page
  this.xScreenPos = 0;
  this.yScreenPos = 0;
  this.xScreenWidth  = 0;
  this.yScreenHeight = 0;
  this.browserModel  = 0;
  this.xyp           = null;
  this.captureMousePosition=function(e){
    if ((this.browserModel==1)||(this.browserModel==3)) {
        this.xMousePos     = e.pageX;
        this.yMousePos     = e.pageY;
        this.xMousePosMax  = window.innerWidth+window.pageXOffset;
        this.yMousePosMax  = window.innerHeight+window.pageYOffset;
        this.xScreenPos    = window.pageXOffset;
        this.yScreenPos    = window.pageYOffset;
        this.xScreenWidth  = window.innerWidth;
        this.yScreenHeight = window.innerHeight;
    } else if ((this.browserModel==2)) {
        this.xMousePos     = window.event.x+document.body.scrollLeft;
        this.yMousePos     = window.event.y+document.body.scrollTop;
        this.xMousePosMax  = document.body.clientWidth+document.body.scrollLeft;
        this.yMousePosMax  = document.body.clientHeight+document.body.scrollTop;
        this.xScreenPos    = document.body.scrollLeft;
        this.yScreenPos    = document.body.scrollTop;
        this.xScreenWidth  = document.body.clientWidth;
        this.yScreenHeight = document.body.clientHeight;
    }
  },
  this.bringOnScreen=function(o){
    var xScroll=0;var yScroll=0;
    if(typeof(o)=='object')this.xyp=new xyPos(o);if(!this.xyp)return false;
    if((this.xScreenPos>this.xyp.x)||(this.xMousePosMax<this.xyp.xx)||(this.yScreenPos>this.xyp.y)||(this.yMousePosMax<this.xyp.yy)){
      if(this.xScreenWidth>(this.xyp.w+20)){xScroll=this.xyp.x-20;}else{
        xScroll=Math.floor((this.xScreenWidth-this.xyp.w)/2);
      }
      if(this.yScreenHeight>(this.xyp.h+20)){yScroll=this.xyp.y-20;}else{
        yScroll=Math.floor((this.yScreenHeight-this.xyp.h)/2);
      }        
      if(xScroll<0)xScroll=0;if(yScroll<0)yScroll=0;
      window.scrollTo(xScroll,yScroll);
    }
    return false;
  },
  this.scrollOnScreen=function(o){
    return this.bringOnScreen(o); 
  },
  this.centerOnScreen=function(o){
    if(typeof(o)=='object')this.xyp=new xyPos(o);if(!this.xyp)return false;
    var x=0;var y=0;
    if(this.xyp.w<this.xScreenWidth){x=Math.round((this.xScreenWidth-this.xyp.w)/2);};
    if(this.xyp.h<this.yScreenHeight){y=Math.round((this.yScreenHeight-this.xyp.h)/2);};
    x=x+Number(this.xScreenPos);y=y+Number(this.yScreenPos);//v kazdom pripade tam, kde sme my
    o.style.position='absolute';
    o.style.left = x+'px';
    o.style.top =  y+'px';
    this.xyp.update();
    return false;
  },
  this.onScreen=function(o){
    if(typeof(o)=='object')this.xyp=new xyPos(o);if(!this.xyp)return false;
    if((this.xScreenPos<this.xyp.x)&&(this.xMousePosMax>this.xyp.xx)){
      if((this.yScreenPos<this.xyp.y)&&(this.yMousePosMax>this.xyp.yy)){
        return true;
      }
    }
    return false;
  },
  this.mouseOn=function(o){
    if(typeof(o)=='object')this.xyp=new xyPos(o);if(!this.xyp)return false;
    if((this.xMousePos>this.xyp.x)&&(this.xMousePos<this.xyp.xx)){
      if((this.yMousePos<this.xyp.y)&&(this.yMousePos<this.xyp.yy)){
        return true;
      }
    }
    return false;
  },
  this.init=function(){
    if (document.layers) { // Netscape
      this.browserModel=1;
      document.captureEvents(Event.MOUSEMOVE);
    } else if (document.all) { // Internet Explorer
      this.browserModel=2;
    } else if (document.getElementById) { // Netcsape 6
      this.browserModel=3;
    }
    var that=this;//document.onmousemove = captureMousePosition;
    document.onmousemove = function(e){that.captureMousePosition(e);};
  }
  this.init();
}

function xyPos(p){
  this.x=0;
  this.y=0;
  this.xx=0;
  this.yy=0;
  this.h=0;
  this.w=0;
  this.p=p;
  this.update=function(){
    p=this.p;
    this.x=0;
    this.y=0;
    this.h=p.offsetHeight;
    this.w=p.offsetWidth;
    while(p && p.offsetParent)
      {
        this.x += p.offsetLeft;
        this.y += p.offsetTop;
        p = p.offsetParent;
      }
    this.xx=Number(this.x+this.w);
    this.yy=Number(this.y+this.h);
  },  
  this.hasMouse=function(){
    if(!viewport)return false;
    if(!viewport.xMousePos)return false;if(!viewport.yMousePos)return false;
    if((Number(viewport.xMousePos)>this.x)&&(Number(viewport.xMousePos)<Number(this.x+this.w))){
      if((Number(viewport.yMousePos)>this.y)&&(Number(viewport.yMousePos)<Number(this.y+this.h))){
        return true;
      }  
    }
    return false;  
  },
  this.underKick=function(o,x,y){
    o.style.position='absolute';
    //alert(this.yy);
    o.style.left=Number(this.x+x)+'px';
    o.style.top=Number(this.yy+y)+'px';
  }
  this.update();  
}

function chmeasure(o){
  if(!viewport.onScreen(o)) viewport.bringOnScreen(o);
}

