/* PTViewer.js  JavaScript Panorama Viewer
   Copyright (C) 2007 - Helmut Dersch  der@fh-furtwangen.de
   version 0.1             21.10.2007
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

/*------------------------------------------------------------*/

var PTViewer = {
	// Initialize PTViewer
	// Parameters: nver - Number of vertical   images
	//             nhor - Number of horizontal images
	//             base - Basename of images to load (PTViewer adds number + extension)
	init: function (nver, nhor, base) {
		this.nver = nver;
		this.nhor = nhor;
		this.base = base;
		this.idx  = Math.round(nhor/2);
		this.idy  = Math.floor(nver/2);
		this.stepx= 1;
		this.stepy= 1;
		this.msie = (navigator.appName=="Microsoft Internet Explorer");
		this.mx   = -1;
		this.my   = -1;
		this.aktiv= 0;
		this.cache= new Array(this.nhor); // Cache of images (one row)
		this.idyc = new Array(this.nhor); // pitch of cache images
		
		var i;
		for(i=0; i<this.nhor; i++){
			this.idyc[i] = -1;
		}

//		window.captureEvents(Event.MOUSEDOWN);
//		window.captureEvents(Event.MOUSEMOVE);
//		window.captureEvents(Event.MOUSEUP);
		window.onmousemove	= this.mmove;
		window.onmousedown	= this.mdown;
		window.onmouseup	= this.mup;
	},
	
	//   Construct filename for current image
	currImage: function () {
		return this.base+((this.idy*this.nhor)+this.idx)+".jpg";
	},
	
	// Mouse functions
	// Mousedown - save position to mx/my
	mdown: function (Event){
		this.mx = (this.msie?Event.offsetX:Event.pageX);
		this.my = (this.msie?Event.offsetX:Event.pageY);
	},

	// Mouseup - clear mx/my, stop panning
	mup: function (Event){
		this.mx = -1;
		this.my = -1;
		PTViewer.stop();
	},

	// Mousemove - Start panning
	mmove: function (Event){
		if(this.mx>=0 && this.my>=0){
			var dx =  Math.floor(((this.msie?Event.offsetX:Event.pageX)-this.mx)/30);
			var dy = -Math.floor(((this.msie?Event.offsetX:Event.pageY)-this.my)/30);
			if(dx!=0 || dy!=0)
				PTViewer.spin(dx, dy, 100);
			else
				PTViewer.stop();
		}
	},


	// Load next image, supply x/y-steps
	next: function(stepx, stepy){
		this.stop();
		this.stepx = stepx;
		this.stepy = stepy;
		this.nextImage();
	},
	
	

	// Load next image
	nextImage: function (){
		// Update index
		this.idx += this.stepx;
		while(this.idx<0)
			this.idx+=this.nhor;
		while(this.idx>=this.nhor)
			this.idx-=this.nhor;
		this.idy += this.stepy;
		if(this.idy<0)
			this.idy=0;
		if(this.idy>=this.nver)
			this.idy=this.nver-1;	
		
		if(this.idyc[this.idx] != this.idy){ // Image not cached
			this.cache[this.idx] 		= new Image();
			this.cache[this.idx].src 	= this.currImage();
			this.idyc[this.idx] 		= this.idy;
		}
		document.images[0].src = this.cache[this.idx].src;	
	},
	

	// Start spinning with steps x,y and timeintervall t (ms)
	spin: function(x,y,t){
		this.stop();
		this.stepx=x;
		this.stepy=y;
		this.aktiv=window.setInterval("PTViewer.nextImage()", t);
	},


	// Stop spinning
	stop: function (){
		window.clearInterval(this.aktiv);
	},

	// Zoom in
	zoomIn: function(){
		document.images[0].width  *= 1.05;
		document.images[0].height *= 1.05;
	},

	// Zoom out
	zoomOut: function(){
		document.images[0].width  /= 1.05;
		document.images[0].height /= 1.05;
	}
}


