// JavaScript Document
// nova verzia 2

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.wPage = 0; // Width of the entire page
  this.hPage = 0; // Height of the entire page
  this.xMousePosMax = 0; // Width of the page
  this.yMousePosMax = 0; // Height of the page
  this.xScreenPos = 0;
  this.yScreenPos = 0;
  this.wScreen  = 0;
  this.hScreen = 0;
  this.xScreenWidth  = 0;
  this.yScreenHeight = 0;

  this.browserModel  = 0;
  this.xyp           = null;
  this.mouseFunc = null;
  this.resizeFunc = null;
  this.scrollFunc = null;
  this.clickFunc = null;
  this.funcs=new Object();
  this.measure=function(){
    this.wScreen=0;this.hScreen=0;
    if( typeof( window.innerWidth ) == 'number' ) {
      this.wScreen    = window.innerWidth;
      this.hScreen   = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
      this.wScreen    = document.documentElement.clientWidth;
      this.hScreen   = document.documentElement.clientHeight;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
      this.wScreen    = document.body.clientWidth;
      this.hScreen   = document.body.clientHeight;
    }
    if ((this.browserModel==1)||(this.browserModel==3)) {
        this.xScreenPos    = window.pageXOffset;
        this.yScreenPos    = window.pageYOffset;
    } else if ((this.browserModel==2)) {
        this.xScreenPos    = document.body.scrollLeft;
        this.yScreenPos    = document.body.scrollTop;
    }
    this.wPage  = this.wScreen+this.xScreenPos;
    this.hPage  = this.hScreen+this.yScreenPos;
    this.xScreenWidth=this.wScreen;
    this.yScreenHeight=this.hScreen;
    this.xMousePosMax=this.wPage;
    this.yMousePosMax=this.hPage;   
  },
  this.captureMousePosition=function(e){
    this.measure();
    if ((this.browserModel==1)||(this.browserModel==3)) {
        this.xMousePos     = e.pageX;
        this.yMousePos     = e.pageY;
    } else if ((this.browserModel==2)) {
        this.xMousePos     = window.event.x+this.xScreenPos;
        this.yMousePos     = window.event.y+this.yScreenPos;
    }
    this.xMouseViewportPos     = this.xMousePos-this.xScreenPos;
    this.yMouseViewportPos     = this.yMousePos-this.yScreenPos;
  },
  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.wPage<this.xyp.xx)||(this.yScreenPos>this.xyp.y)||(this.hPage<this.xyp.yy)){
      if(this.wScreen>(this.xyp.w+20)){xScroll=this.xyp.x-20;}else{
        xScroll=Math.floor((this.wScreen-this.xyp.w)/2);
      }
      if(this.hScreen>(this.xyp.h+20)){yScroll=this.xyp.y-20;}else{
        yScroll=Math.floor((this.hScreen-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;this.measure();
    if(this.xyp.w<this.wScreen){x=Math.round((this.wScreen-this.xyp.w)/2);};
    if(this.xyp.h<this.hScreen){y=Math.round((this.hScreen-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';
    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.wPage>this.xyp.xx)){
      if((this.yScreenPos<this.xyp.y)&&(this.hPage>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.captureKey=function(e){
    this.keynum = 0;
    this.keychar = '';
    if(window.event){this.keynum = window.event.keyCode}else if(e.which){this.keynum = e.which};
    this.keychar = String.fromCharCode(this.keynum);
  },
  this.startCapture=function(ne,f){
    if(typeof(f)=='string')this.funcs[ne]=Function(f);
    else if(typeof(f)=='function')this.funcs[ne]=f;
    var that=this;
    if(ne=='onclick'){document.onclick = function(e){that.captureMousePosition(e);if(that.funcs['onclick'])that.funcs['onclick']();};};
    if(ne=='ondblclick'){document.ondblclick = function(e){that.captureMousePosition(e);if(that.funcs['ondblclick'])that.funcs['ondblclick']();};};
    if(ne=='onmouseup'){document.onmouseup = function(e){that.captureMousePosition(e);if(that.funcs['onmouseup'])that.funcs['onmouseup']();};};
    if(ne=='onmousedown'){document.onmousedown = function(e){that.captureMousePosition(e);if(that.funcs['onmousedown'])that.funcs['onmousedown']();};};
    if(ne=='onmousemove'){document.onmousemove = function(e){that.captureMousePosition(e);if(that.funcs['onmousemove'])that.funcs['onmousemove']();};};
    if(ne=='onkeydown'){document.onkeydown = function(e){that.captureKey(e);if(that.funcs['onkeydown'])that.funcs['onkeydown']();};};
    if(ne=='onkeypress'){document.onkeypress = function(e){that.captureKey(e);if(that.funcs['onkeypress'])that.funcs['onkeypress']();};};
    if(ne=='onkeyup'){document.onkeyup = function(e){that.captureKey(e);if(that.funcs['onkeyup'])that.funcs['onkeyup']();};};    
    if(ne=='onresize'){window.onresize = function(e){that.measure();if(that.funcs['onresize'])that.funcs['onresize']();};};
    if(ne=='onscroll'){window.onscroll = function(e){that.measure();if(that.funcs['onscroll'])that.funcs['onscroll']();};};
  },
  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;
    }
  }
  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.scrollOnScreen=function(){
    if(!viewport)return false;
    viewport.scrollOnScreen(this.p);
    this.update();return false;  
  },
  this.centerOnScreen=function(){
    if(!viewport)return false;
    viewport.centerOnScreen(this.p);
    this.update();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);
}


