/* viewport v 3.0 */
var viewport=new Viewport();
function Viewport(){
  this.xMousePos = 0; 
  this.yMousePos = 0;
  this.wPage = 0;
  this.hPage = 0;
  this.xMousePosMax = 0;
  this.yMousePosMax = 0;
  this.xScreenPos = 0;
  this.yScreenPos = 0;
  this.wScreen  = 0;
  this.hScreen = 0;
  this.xScreenWidth  = 0;
  this.yScreenHeight = 0;
  this.minimized=false;
  this.browserModel  = 0;
  this.xyp           = null;
  this.mouseFunc = null;
  this.resizeFunc = null;
  this.scrollFunc = null;
  this.clickFunc = null;
  this.funcs=new Object();
  this.measurePage=function(){
	if(this.isIE()){  
	var p=document.createElement('div');
	p.style.display='block';
	p.style.clear='both';p.style.height='1px';
	p.style.overflow='hidden';
	document.body.appendChild(p);
	var pp=p;
    var x=0;
    var y=0;
    if(!p)return false;
    var h=p.offsetHeight;
    var w=p.offsetWidth;
    while(p && p.offsetParent)
      {
        x += p.offsetLeft;
        y += p.offsetTop;
        p = p.offsetParent;
      }
    this.wPage=Number(x+w);
    this.hPage=Number(y+h);
    pp.parentNode.removeChild(pp);
    }else{
	  this.wPage=document.body.offsetWidth;  
	  this.hPage=document.body.offsetHeight;		
	}
  },  
  this.measure=function(){
    this.wScreen=0;this.hScreen=0;
    this.wWindow=0;this.hWindow=0;this.minimized=false;
    if( typeof( window.outerWidth ) != 'undefined' ) {
	  this.wWindow=window.outerWidth;
	  this.hWindow=window.outerHeight;
	}else if( typeof(document.body.offsetWidth) != 'undefined' ) {
	  this.wWindow=document.body.offsetWidth;
	  this.hWindow=document.body.offsetHeight;
	}
	if(typeof(window.screenTop)=='number'){
		if(window.screenTop<-32000)this.minimized=true;
		this.tWindow=window.screenTop;	
	}else if(typeof(window.screenY)=='number'){
		if(window.screenY<-32000)this.minimized=true;
		this.tWindow=window.screenY;
	}
	if(!this.isIE())
	  if((this.hWindow<100)&&(this.hWindow!=0))
	    this.minimized=true;

	
    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 + document.documentElement.scrollLeft;
        this.yScreenPos    = document.body.scrollTop + document.documentElement.scrollTop;
    }
    
    this.wPage  = this.wScreen+this.xScreenPos;
    this.hPage  = this.hScreen+this.yScreenPos;
    this.measurePage();
    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;
    return true;
  },
  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.centerOnScreenTop=function(o,ypos){
    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);};
    //y=85;
    y=108;if(typeof(ypos)!='undefined')y=ypos;
    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.centerOnScreenInPopup=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);};    
    y=0;
    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.scrollTo=function(x,y){
	window.scrollTo(x,y);
  },
  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.doFuncs=function(ne){
	for(var i=0;i<this.funcs[ne].length;i++){
	  	this.funcs[ne][i]();
	}  
  },
  this.startCapture=function(ne,f){
    if(!this.funcs[ne])this.funcs[ne]=[];
    if(typeof(f)=='string')this.funcs[ne][this.funcs[ne].length]=Function(f);
    else if(typeof(f)=='function')this.funcs[ne][this.funcs[ne].length]=f;
    if(ne=='onresize'){
		if(window.onresize){
			
		  this.funcs[ne][this.funcs[ne].length]=window.onresize;	
		}
	}
    if(ne=='onscroll'){
		if(window.onscroll){

		  this.funcs[ne][this.funcs[ne].length]=window.onscroll;	
		}
	}

    var that=this;
    if(ne=='onclick'){document.onclick = function(e){that.captureMousePosition(e);if(that.funcs['onclick'])that.doFuncs('onclick');};};
    if(ne=='ondblclick'){document.ondblclick = function(e){that.captureMousePosition(e);if(that.funcs['ondblclick'])that.doFuncs('ondblclick');};};
    if(ne=='onmouseup'){document.onmouseup = function(e){that.captureMousePosition(e);if(that.funcs['onmouseup'])that.doFuncs('onmouseup');};};
    if(ne=='onmousedown'){document.onmousedown = function(e){that.captureMousePosition(e);if(that.funcs['onmousedown'])return that.doFuncs('onmousedown');};};
    if(ne=='onmousemove'){document.onmousemove = function(e){that.captureMousePosition(e);if(that.funcs['onmousemove'])return that.doFuncs('onmousemove');};};
    if(ne=='onkeydown'){document.onkeydown = function(e){that.captureKey(e);if(that.funcs['onkeydown'])that.doFuncs('onkeydown');};};
    if(ne=='onkeypress'){document.onkeypress = function(e){that.captureKey(e);if(that.funcs['onkeypress'])that.doFuncs('onkeypress');};};
    if(ne=='onkeyup'){document.onkeyup = function(e){that.captureKey(e);if(that.funcs['onkeyup'])that.doFuncs('onkeyup');};};    
    if(ne=='onresize'){window.onresize = function(e){that.measure();if(that.funcs['onresize'])that.doFuncs('onresize');};};
    if(ne=='onscroll'){window.onscroll = function(e){that.measure();if(that.funcs['onscroll'])that.doFuncs('onscroll');};};
  },
  this.setOpac=function(o,t) {
  t=(t==100)?99.999:t;
  if(typeof o.style.opacity !== 'undefined'){// W3C
    o.style.opacity=t/100;
  }else if (typeof o.style.MozOpacity !== 'undefined') {// Older Mozilla
    o.style.MozOpacity=t/100;
  }else if (typeof o.style.filter != 'undefined') {// IE 
    o.style.filter='alpha(opacity:'+t+')';
  }else if (typeof o.style.KHTMLOpacity != 'undefined') {// Older KHTML Based Browsers 
    o.style.KHTMLOpacity=t/100;
  }
 },
  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._getInternetExplorerVersion=function(){
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer'){
      var ua = navigator.userAgent;
      var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
      if (re.exec(ua) != null)
        rv = parseFloat( RegExp.$1 );
      }
    return rv;
  },
  this.isIE=function(){
	if(typeof(this.browser)!='undefined')return (this.browser=='IE');
    var ver = this._getInternetExplorerVersion();
    if ( ver > -1 )
    {
      if ( ver >= 8.0 ){this.browser='IE';return true;}
      else {this.browser='IE';return true;};
    }
    return false;
  },
  this.isChrome=function(){
	if(typeof(this.browser)!='undefined')return (this.browser=='CHROME');
	if ( navigator.userAgent.toLowerCase().indexOf('chrome') > -1 )
	{this.browser='CHROME';return true;}else{return false;};  
  },
  this.on=function(n,f){
    if(this.isIE()){
	  if(this.browserModel==2){	
		  if(n=='focus')return this.onD('focusin',f);
		  if(n=='blur')return this.onD('focusout',f);
		  if(n=='mousemove')return this.onD(n,f);
		  if(n=='beforeunload'){window.onbeforeunload=f;return true;};
		  if(n=='unload'){window.onunload=f;return true;};
	    }
		window.attachEvent('on'+n,f);
	}
    else {window.addEventListener(n,f,false);};	  
  },
  this.no=function(n,f){
    if(this.isIE()){
	  if(this.browserModel==2){	
		  if(n=='focus')return this.noD('focusin',f);
		  if(n=='blur')return this.noD('focusout',f);
		  if(n=='mousemove')return this.noD(n,f);
		  if(n=='beforeunload'){window.onbeforeunload=null;return true;};
		  if(n=='unload'){window.onunload=null;return true;};
	    }
	    window.detachEvent('on'+n,f);		
	}
    else {window.removeEventListener(n,f,false);};	  
  },
  this.onD=function(n,f){
    if(this.isIE()){document.attachEvent('on'+n,f);}
    else {document.addEventListener(n,f,false);};	  
  },
  this.noD=function(n,f){
    if(this.isIE()){document.detachEvent('on'+n,f);}
    else {document.removeEventListener(n,f,false);};	  
  },
  this.onB=function(n,f){
    if(this.isIE()){document.body.attachEvent('on'+n,f);}
    else {document.body.addEventListener(n,f,false);};	  
  },
  this.noB=function(n,f){
    if(this.isIE()){document.body.detachEvent('on'+n,f);}
    else {document.body.removeEventListener(n,f,false);};	  
  }
  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;
    if(!p)return false;
    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.ready=true;
  },  
  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.hasMouseAround=function(marg){
    if(!viewport)return false;
    if(!viewport.xMousePos)return false;if(!viewport.yMousePos)return false;
    if((Number(viewport.xMousePos)>(this.x-marg))&&(Number(viewport.xMousePos)<Number(this.x+this.w+marg))){
      if((Number(viewport.yMousePos)>this.y-marg)&&(Number(viewport.yMousePos)<Number(this.y+this.h+marg))){
        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.overKick=function(o,x,y){
    o.style.position='absolute';
    o.style.left=Number(this.x+x)+'px';
    o.style.top=Number(this.y+y)+'px';
  },
  this.restoreAbsolute=function(x,y){
    this.p.style.position='absolute';
    if(typeof(x)=='undefined')x=0;if(typeof(y)=='undefined')y=0;
    this.p.style.left=Number(this.x+x)+'px';
    this.p.style.top=Number(this.y+y)+'px';
  }
  this.update();  
}

function chmeasure(o){
  if(!viewport.onScreen(o)) viewport.bringOnScreen(o);
}


