function Panorama(div,src2) {
    this.div = div;
    div.scrollLeft=0;
    this.pSpeed = 0;
    this.pSpeedMax = 2;
    this.startAcc = 0.05;
    this.stopAcc = 0.5;
    this.pAcc = 0;
    this.pVal = 0;
    var me = this;
    var onloadfn=window.onload;
    window.onload=function() {
	me.init(src2);
	if (onloadfn) onloadfn();
    };
};
    
Panorama.prototype.init=function(src2) {
    var img = this.div.getElementsByTagName('img')[0];
    var me=this;
    if (!src2) src2 = img.src;
    img.style.top = '0px';
    img.style.left = '0px';
    img.style.position='absolute';
    var img2 = document.createElement('img');
    img2.src = src2;
    img2.style.top = '0px';
    img2.style.left = img.offsetWidth + 'px';
    img2.style.position='absolute';
    this.div.appendChild(img2);
    img2.onload=function() {
	me.panWidth = img.offsetWidth + img2.offsetWidth;
	me.init2(img,img2);
    }
};

Panorama.prototype.init2=function(img,img2) {
    var me=this;
    var img3 = document.createElement('img');
    img3.src = img.src;
    img3.style.top = '0px';
    img3.style.left = me.panWidth + 'px';
    img3.style.position='absolute';
    this.div.appendChild(img3);
    this.div.onmousedown=function(e) {
	me.stop();
	me.mouseX=(e || event).clientX;
	this.style.cursor = 'move';
	return false;
    };
    this.div.onmouseup=function(e) {
	me.mouseX=null;
	me.start();
	this.style.cursor = '';
	return false;
    };
    this.div.onmouseout=function(e) {
	me.mouseTm=window.setTimeout(function() {
	    me.mouseX=null;
	    me.start();
	    me.div.style.cursor = '';
	},100);
	return false;
    };
    this.div.onmouseover=function(e) {
	window.clearTimeout(me.mouseTm);
    };
    this.div.onmousemove=function(e) {
	if (me.mouseX==null) return;
	var x=(e || event).clientX;
	me.pVal = me.mouseX-x;
	me.mouseX=x;
	me.step();
    };
    this.start();
}

Panorama.prototype.start=function() {
    if (this.started || !this.panWidth) return;
    this.reset();
    this.started=true;
    this.pAcc = this.startAcc;
    var me=this;
    this.pTmout=window.setInterval(function() { (function() {
      this.pSpeed+=this.pAcc;
      if (this.pSpeed>this.pSpeedMax) this.pSpeed=this.pSpeedMax;
      if (this.pSpeed<=0) return this.reset();
      this.pVal += this.pSpeed;
      this.step();
    }).call(me);
    },50);
};

Panorama.prototype.stop=function() {
    this.pAcc=-this.stopAcc;
    this.started=false;
};

Panorama.prototype.reset=function() {
    window.clearInterval(this.pTmout);
    this.pSpeed=0;
    this.pAcc=0;
    this.started=false;
};

Panorama.prototype.step=function() {
    if (this.pVal>=1 || this.pVal<0) {
	var v = Math.floor(this.pVal);
	this.pVal -= v;
	var sc = this.div.scrollLeft + v;
	if (sc>=this.panWidth) sc-=this.panWidth;
	else if (sc<0) sc+=this.panWidth;
	this.div.scrollLeft = sc;
    }
};

