pano_width = 1;
pano_height = 2;
pano_image = 3;
pano_start_speed = 4;
pano_start_direct = 5;
pano_round = 6;
pano_step = 7;

function Panoram(containerId, panoId){
    this.st_x = 0;
    this.st_y = 0;
    this.defaultSpeed = 0;

    this.index = panoId;

    this.width=0;
    this.height=0;

    this.can_recalc = true;

    this.round = 1;

    this.div1 = null;
    this.div2 = null;

    this.containerX = 0;
    this.containerY = 0;

    this.img1 = new Image();
    this.img2 = new Image();
    
//    this.step = panoram_step;
    this.direct = -1;

    this.container = document.getElementById(containerId);

    if (this.container == null){
         alert('Achtung!');
         return null;
    }

    this.container.objId = panoId;
    this.container.isPano = 1;

    this.container.style.pasition = 'relative';
}

Panoram.prototype.setOpt = function(opt, val){
	switch(opt){
		case pano_width:
			this.width = parseInt(val);		
			break;
		case pano_height:
			this.height = parseInt(val);
			break;
		case pano_image:
			this.img1.src = val;
				break;
		case pano_start_speed:
			this.defaultSpeed = parseInt(val);
			break;
		case pano_start_direct:
			this.direct = val;
			break;
		case pano_round:
			this.round = val;
			break;
		case pano_step:
			this.step = val;
			break;
	}
}

Panoram.prototype.doMove = function(){
	if (this.round){
        var old_left= parseInt(this.div1.style.left);
        if (isNaN(old_left)) {
            old_left = 0;
        }
        var new_left = old_left + this.direct * this.step;
	  	this.div1.style.left = new_left + 'px';
    		if (new_left >  this.width - this.step - parseInt(this.div1.style.width)){
			this.div2.style.left = new_left - parseInt(this.div2.style.width);
    		}

    		if (new_left < this.width + this.step - parseInt(this.div1.style.width))
		{                                                                                	
			this.div2.style.left = new_left + parseInt(this.div1.style.width);
		}
	
		if ((new_left +  parseInt(this.div1.style.width) < 0)||(parseInt(this.div1.style.left) > this.width)){
			dtmp = this.div1;
			this.div1 = this.div2;
			this.div2 = dtmp;
		}
	}else{
        var old_left = parseInt(this.div1.style.left);
        if (isNaN(old_left)) {
            old_left = 0;
        }
		new_pos = old_left + this.direct * this.step;
		if ((new_pos < 0 )&&(new_pos > this.width - parseInt(this.div1.style.width))){
			this.div1.style.left = new_pos;
		}	
	}	


}
        
Panoram.prototype.setSpeed = function(screenX, screenY){
      delta = this.width/2 - (screenX - this.containerX);
      if (delta > 0){this.direct = -1;}
      else {this.direct = 1;}
      this.speed = Math.round(Math.abs(200*(delta/this.width)));
}


Panoram.prototype.init = function(){
    this.div1 = document.createElement('div');


    k = 1;
    this.img2.src = this.img1.src;
    if (this.img1.height != this.height){
         k = this.height / this.img1.height;
         this.img1.height = this.img1.height * k;
         this.img1.width  = this.img1.width * k;
         this.img2.height = this.img2.height * k;     
         this.img2.width  = this.img2.width * k;
      }
    this.container.style.width = this.width;
    this.container.style.height = this.height;
    this.container.style.overflow = 'hidden';
    this.container.style.position = 'relative';
    this.div1.style.width = this.img1.width;
    this.div1.style.position = 'absolute';
    this.div1.style.visibility = 'visible';
    this.div1.style.display = 'inline';

    ob = this.container;

    x=0; y=0;
    while(ob) {
      x+=ob.offsetLeft;
      y+=ob.offsetTop;
      ob=ob.offsetParent;
    }
    this.containerX = x;
    this.containerY = y;


    this.div1.style.left =  - (parseInt(this.div1.style.width) - this.width)/2;

    this.div1.style.top = 0;

    this.speed = 0;

    this.div1.appendChild(this.img1);
    this.container.appendChild(this.div1);

    if (this.round){
    	    this.div2 = document.createElement('div');
	    this.div2.style.width = this.img1.width;
	    this.div2.style.position = 'absolute';
	    this.div2.style.visibility = 'visible';
	    this.div2.style.display = 'inline';
	    this.div2.appendChild(this.img2);
	    this.div2.style.left =  - parseInt(this.div2.style.width);
	    this.div2.style.top = 0;
	    this.container.appendChild(this.div2);
    }

    this.container.onmousemove = onPanoMouseMove;
    this.container.onmouseover = onPanoMouseOver;
    this.container.onmouseout = onPanoMouseOut;

}

Panoram.prototype.setImage = function(imgName){
      this.img1.src = imgName;
      this.img2.src = imgName;
}


Panoram.prototype.setDefaultSpeed = function(){
	this.speed = this.defaultSpeed
}

function pano_init(container){
	window.panorams[window.pano_length] = new Panoram(container, window.pano_length);
	window.pano_length++;
	return window.panorams[window.pano_length-1];
}

function pano_setopt(pano, opt, value){
	pano.setOpt(opt, value);
}

function pano_exec(pano){
	if (pano.img1 == null){
		alert('Image not set!');
		return;
	}

	if (!pano.img1.complete){
		setTimeout('pano_exec(window.panorams['+pano.index+'])', 100);
		return;
	}
	pano.init();
	intervals[pano.index] = setInterval("movePanoram('"+pano.index+"')", 100 - pano.speed);

}

function onPanoMouseOver(evt){
	evt = (evt)?evt:(window.event)?window.event:"";
	if (evt){
		t = (evt.target)?evt.target:evt.srcElement;
		while (t&&!t.isPano){
			t = t.parentNode;
		}
		if (t){
			pano = window.panorams[t.objId];
			if (pano){
				pano.setSpeed(evt.screenX, evt.screenY);
				clearInterval(intervals[pano.index]);
				intervals[pano.index] = setInterval("movePanoram('"+pano.index+"')", 100 - pano.speed);
			}
		}

	}

}

function onPanoMouseMove(evt){
	evt = (evt)?evt:(window.event)?window.event:"";
	if (evt){
		t = (evt.target)?evt.target:evt.srcElement;
		while (t&&!t.isPano){
			t = t.parentNode;
		}
		if (t){
			pano = window.panorams[t.objId];
			if (pano){
				if (pano.can_recalc){
					pano.can_recalc = false;
					pano.setSpeed(evt.screenX, evt.screenY);
					clearInterval(intervals[pano.index]);
					intervals[pano.index] = setInterval("movePanoram('"+pano.index+"')", 100 - pano.speed);
					setTimeout("panorams["+pano.index+"].can_recalc = true", 100 - pano.speed+1);

				}
			}
		}

	}

}

function onPanoMouseOut(evt){
	evt = (evt)?evt:(window.event)?window.event:"";
	if (evt){
		t = (evt.target)?evt.target:evt.srcElement;
		while (t&&!t.isPano){
			t = t.parentNode;
		}
		if (t){
			pano = window.panorams[t.objId];
			pano.setDefaultSpeed();
			clearInterval(intervals[pano.index]);
			if (pano.speed){
				intervals[pano.index] = setInterval("movePanoram('"+pano.index+"')", 100 - pano.speed);
				setTimeout("panorams["+pano.index+"].can_recalc = true", 100 - pano.speed+1);
			}	
		}
	}

}

function movePanoram(index){
	window.panorams[parseInt(index)].doMove();
}


window.panorams = new Array();

intervals = new Array();
window.pano_length = 0;