function Stat() {
    this.content_height = 600;
    
    this.phase_out_length = 250;
    this.phase_in_length = 500;
    this.phase_in_wait = 500;
    
    this.last_sx = 0;
    this.last_mx = 0;
    this.menu_phase = 1;
    this.menu_base_phase = 1;
    this.menu_hide_start = false;
    this.menu_hide_end = false;
    this.menu_show_start = false;
    this.target_mx = 0;
    this.target_sx = 0;
    this.last_y = 0;
    
    this.resetViewport = function() {
        if ( typeof window.innerWidth != 'undefined' ) {
            this.vpwidth = window.innerWidth;
            this.vpheight = window.innerHeight;
        }
        else if ( typeof document.documentElement != 'undefined' &&
            typeof document.documentElement.clientWidth != 'undefined' &&
            document.documentElement.clientWidth != 0 ) {
            this.vpwidth = document.documentElement.clientWidth;
            this.vpheight = document.documentElement.clientHeight;
        }
        else {
            this.vpwidth = document.getElementsByTagName('body')[0].clientWidth;
            this.vpheight = document.getElementsByTagName('body')[0].clientHeight;
        };
	//this.vpwidth='990';
    }
    
    this.resetViewport();
}
function center(element) {
    alert('dd');
    		//if(document.menushowing!='')
		//	document.getElementById(document.menushowing).style.visibility='hidden';
    
    var w = element.offsetWidth;

    var x = 0;
    var el = element;
    while ( el ) {
        x += el.offsetLeft;
        el = el.offsetParent;
    }
    
    var sw = stat.vpwidth;
    var sx = Math.round( x + w / 2 - sw / 2 );
    
    var pw = document.body.scrollWidth;
    sx = ( sx + sw > pw) ? pw - sw : sx;
    sx = ( sx < 1 ) ? 0 : sx;
    
    stat.target_sx = sx;
    
    return false;
}
function moveResult( cx, tx) {
    if ( cx < tx ) return cx + Math.round( (tx - cx) / 5 + 0.5 );
    if ( cx > tx ) return cx - Math.round( (cx - tx) / 5 + 0.5 );
    return tx;
}
function moveLoop() {
    
    stat.resetViewport();

    var now = new Date();
    var sx = document.body.scrollLeft;
    // move target_sx if sx has changed (user moved scrollbar)
    if ( sx != stat.last_sx ) {
        stat.target_sx += sx - stat.last_sx;
        stat.last_sx = sx;
    }
    // move real scroll closer to target scroll
    if ( sx != stat.target_sx ) {
        var tsx = moveResult( sx, stat.target_sx );
        window.scroll( tsx, document.body.scrollTop );
        stat.last_sx = tsx;
    }
    // if menu is not in (or already moving to) the right place, start hiding the menu
    if ( ! stat.menu_hide_start && stat.target_sx != stat.target_mx ) {
        stat.menu_hide_start = now;
        stat.menu_base_phase = stat.menu_phase;
        stat.target_mx = stat.target_sx;
        stat.menu_show_start = false;
    }
    // if scroller in the right place, start countdown to show menu in the right place
    if ( ! stat.menu_show_start && sx == stat.target_sx && stat.menu_phase != 1 ) {
        stat.menu_show_start = now;
    }
    var new_phase = stat.menu_phase;
    // correct phase: first hide to the end, then show
    if ( stat.menu_hide_start ) {
        var time_passed = now.getTime() - stat.menu_hide_start.getTime();
        new_phase = 1 - ( ( time_passed ) / stat.phase_out_length );
        new_phase = new_phase - ( 1 - stat.menu_base_phase );
        if ( new_phase <= 0 ) {
            new_phase = 0;
            stat.menu_hide_start = false;
            stat.menu_hide_end = now;
        }
    }
    else if ( stat.menu_show_start ) {
        var time_passed = now.getTime() - stat.menu_show_start.getTime();
        // make sure we do not virtually reveal the menu before hide ends
        if ( stat.phase_in_wait < stat.phase_out_time && stat.menu_hide_end ) {
            var min_wait = stat.menu_hide_end.getTime() - stat.menu_show_start.getTime();
            var extra_wait = min_wait - stat.phase_in_wait;
            if ( extra_wait > 0 ) time_passed -= extra_wait;
        }
        if ( time_passed > stat.phase_in_wait ) {
            if ( stat.last_mx != stat.target_mx ) {
                var divs = new Array();
                divs.push( document.getElementById("rightheader") );
                divs.push( document.getElementById("header") );
                divs.push( document.getElementById("contact") );
                divs.push( document.getElementById("copyright") );
                
                for (var div in divs) {
                    divs[div].style.left = stat.target_mx + 'px';
                }
                stat.last_mx = stat.target_mx;
            }
            
            new_phase = ( time_passed - stat.phase_in_wait ) / stat.phase_in_length;
            if ( time_passed > stat.phase_in_wait + stat.phase_in_length ) {
                new_phase = 1;
                stat.menu_show_start = false;
            }
        }
    }
    

    // adjust menu phase
/*    if ( stat.menu_phase != new_phase ) {
        var divs = new Array();
        divs.push( document.getElementById("rightheader") );
        divs.push( document.getElementById("header") );
        divs.push( document.getElementById("contact") );
        divs.push( document.getElementById("copyright") );
        for (var div in divs) {
            divs[div].style.opacity = new_phase;
            divs[div].style.filter = 'alpha(opacity=' + Math.round( new_phase * 100 ) + ')';
        }
        stat.menu_phase = new_phase;
    }
    
 */
    // move content vertically to center of the page
    var dest = Math.round( ( stat.vpheight - stat.content_height ) / 2 );
    dest = ( dest < 0 ) ? 0 : dest;
    
    if ( stat.last_y != dest ) {
     //   document.getElementById("centering").style.height = dest + 'px';
        stat.last_y = dest;
    }
    setTimeout("moveLoop()", 20);
}

document.nextRight= new Array();
document.nextRight[0]=0;
document.nextRight[1]='undefined';
function moveRight(){
    alert('moveright');
    var alltags=document.getElementsByTagName('img');	
    document.nextRight[0]=0;
    for (i=0; i<alltags.length; i++) {
	
    var test = '' + alltags[i].id;

        if (test.match(/^A.*/)) { //väli pois
	    
	    var x = 0;
	    var el = alltags[i];
	    while ( el ) {
                x += el.offsetLeft;
                el = el.offsetParent;
            }
	    
	    if ((document.body.scrollLeft + Math.round(stat.vpwidth/2))<(x+Math.round(alltags[i].offsetWidth/2))&&((x+Math.round(alltags[i].offsetWidth/2))<document.nextRight[0]||document.nextRight[0]==0)) {
		
		document.nextRight[0]=x;
		document.nextRight[1]=Math.round((alltags[i].offsetWidth/2)+x)-Math.round(stat.vpwidth/2);
		
	    }

	}

    }
    alert(stat.vpwidth);
    stat.target_sx = document.nextRight[1] + stat.vpwidth/2;
   
    
}




document.nextLeft= new Array();
document.nextLeft[0]=0;
document.nextLeft[1]='undefined';
function moveLeft(){
    
    var alltags=document.getElementsByTagName('img');	
    document.nextLeft[0]=0;
    for (i=0; i<alltags.length; i++) {
	
    var test = '' + alltags[i].id;

        if (test.match(/^A.*/)) { //väli pois
	    
	    var x = 0;
	    var el = alltags[i];
	    while ( el ) {
                x += el.offsetLeft;
                el = el.offsetParent;
            }
	    
	    if ((document.body.scrollLeft + Math.round(stat.vpwidth/2))>(x+Math.round(alltags[i].offsetWidth/2))&&((x+Math.round(alltags[i].offsetWidth/2))>document.nextLeft[0])) {
		
		document.nextLeft[0]=x;
		document.nextLeft[1]=Math.round((alltags[i].offsetWidth/2)+x)-Math.round(stat.vpwidth/2);
		
	    }

	}

    }
    
    stat.target_sx = document.nextLeft[1];
   
    
}


function initHeight() {
         document.getElementById('arrowRight').style.left=document.body.scrollLeft + stat.vpwidth - 54;
         if(stat.vpheight<630) height = stat.vpheight-130;
         else height=500;
         var alltags=document.getElementsByTagName('img');	

    for (i=0; i<alltags.length; i++) {
	
    var test = '' + alltags[i].id;

        if (test.match(/^A.*/)) { //väli pois
	 
                  alltags[i].height=height;
        }
    }
    
    moveLeft();
         
}


function moveWindowEffect() {
    
    this.msie = navigator.appVersion.indexOf("MSIE") != -1;

    if(this.msie) {
	this.windowPositionX=window.screenLeft;
	this.windowPositionY=window.screenTop;
	
	this.contentPositionX=window.screenLeft;
	this.contentPositionY=window.screenTop;
    }
    else {
	this.windowPositionX = window.screenX;
	this.windowPositionY = window.screenY;
	
	this.contentPositionX = window.screenX;
	this.contentPositionY = window.screenY;
    }

    this.speedX=0;
    this.speedY=0;
    
    this.move = function() {
	
	
	
	if(this.windowPositionX!=this.contentPositionX||
	   this.windowPositionY!=this.contentPositionY||
	   this.speedX!=0||
	   this.speedY!=0) {
	    
	    document.body.style.overflow='hidden';
	    
	    
	    this.oldSpeedX=this.speedX;
	    this.oldSpeedY=this.speedY;
	    
	    if(this.windowPositionX-this.contentPositionX>0)
	       this.speedX = Math.ceil(this.speedX + Math.ceil(((this.windowPositionX-this.contentPositionX) / 10)));
	    else
	       this.speedX = Math.floor(this.speedX + Math.floor(((this.windowPositionX-this.contentPositionX) / 10)));
	       
	    if(this.windowPositionY-this.contentPositionY>0)
	       this.speedY = Math.ceil(this.speedY + Math.ceil(((this.windowPositionY-this.contentPositionY) / 10)));
	    else
	       this.speedY = Math.floor(this.speedY + Math.floor(((this.windowPositionY-this.contentPositionY) / 10)));
	       
	       
	    if(Math.abs(this.speedX)<Math.abs(this.oldSpeedX)) {
		if(this.speedX>0)
		    this.speedX =Math.ceil(Math.ceil(this.speedX*0.3));
		else
		    this.speedX =Math.floor(Math.floor(this.speedX*0.3));
		
	    }
	    
	    if(Math.abs(this.speedY)<Math.abs(this.oldSpeedY)) {
		if(this.speedY>0)
		    this.speedY =Math.ceil(Math.ceil(this.speedY*0.3));
		else
		    this.speedY =Math.floor(Math.floor(this.speedY*0.3));
		
		
	    }
	    

	    
	    document.getElementById('pageid').style.left = (this.contentPositionX-this.windowPositionX + this.speedX) +'px';
	    document.getElementById('pageid').style.top = (this.contentPositionY-this.windowPositionY + this.speedY) +'px';
	    
	    
	    this.contentPositionX = this.contentPositionX + this.speedX;
	    this.contentPositionY = this.contentPositionY + this.speedY;
	    
	    if(Math.abs(this.contentPositionX-this.windowPositionX)<3&&this.speedX<3) {
		this.speedX = 0;
		this.contentPositionX=this.windowPositionX;
		document.getElementById('pageid').style.left = (this.contentPositionX-this.windowPositionX + this.speedX) +'px';
	    
	    }
	     if(Math.abs(this.contentPositionY-this.windowPositionY)<3&&this.speedY<3) {
		this.speedY = 0;
		this.contentPositionY=this.windowPositionY;
		document.getElementById('pageid').style.top = (this.contentPositionY-this.windowPositionY + this.speedY) +'px';
	    }
	
	    
	    
	}
	else document.body.style.overflow='auto'; 
	
    
    }
    
    this.run= function(){
	if(this.msie) {
		this.windowPositionX=window.screenLeft;
		this.windowPositionY=window.screenTop;
	    }
	    else {
		this.windowPositionX = window.screenX;
		this.windowPositionY = window.screenY;
	    }
	this.move();
	setTimeout("moveWindowEffect.run()", 20);
    }
    
    
    
}

