//	JavaScript for controlling interactive image stacks for reviewing radiology images
//	Written by Daniel Cornfeld, M.D. 
//	January 2007


var currentimage = 0;			 // which image in the cine is to be displayed	
var cine = new Array ();
var xpos = 0;					// x location of the mouse
var ypos = 0;					// y location of the mouse
var oldxpos = 0;				// previous x location of the mouse
var oldypos = 0;				// previous y location of the mouse
var threshold = 10;		// how far the mouse moves before the next image is displayed
var scrollobject = 0 ;		// this contains the object of the cine to scroll through


document.onmousemove = mouseMove;
document.onmouseup = mouseUp;


function mouseUp(ev)
  {
   scrollobject = 0;
   return
  };


function preparetoscroll(ev)         // updates mouse position so that old values for
				     // oldxpos, oldypos are not used when determining
 				     // if threshold values are met for updating the image

  {
	scrollobject = 1;
	if (!ev) var ev = window.event;
      getMousePosition(ev);		//  calls function to get xpos and ypos updated relative to document
	oldxpos = xpos;
	oldypos = ypos;
	return true;
   };


function getMousePosition(ev)               
 {

   if (ev.pageX || ev.pageY)		// basically if Safari or Firefox which use absolute coordinates
	{xpos = ev.pageX;
   	 ypos = ev.pageY;
	}
     else
	{xpos = ev.clientX + document.body.scrollLeft - document.body.clientLeft;
         ypos = ev.clientY + document.body.scrollTop - document.body.clientTop;
        }
   
	  return true;
  };	


function mouseMove(ev)
 {

 	if (scrollobject == 1) 
 	{
 	if (!ev) var ev = window.event;
      getMousePosition(ev);			//  calls function to get xpos and ypos updated relative to document
	
	if (Math.abs( oldypos - ypos ) > threshold)  
	{ 
	
	  if (oldypos < ypos) {advancecine(); }
	  if (oldypos > ypos) {backcine(); }
  
	  oldxpos = xpos;				//  set the old position before updating the new one
	  oldypos = ypos; 			  	//  set the old position before updating the new one
  	
  	};						// end if threshold is met 
  	
  	};						// end if (scrollobect)	
 	return true;
 };


function advancecine()				        // scroll forward in the cine by 1 image
  {  
  	if (currentimage == lastimage)
  	 {
  		currentimage = currentimage;
  	 }
  	 else
  	 {
  	 	currentimage = currentimage + 1
  	 };		  	
  	 document.cineframe.src = cine[currentimage].src;
 
  	 ;
  };


function backcine()					 // scroll backwards in the cine by 1 image
{  
	if (currentimage == 0)
  	 {
  		currentimage = currentimage;
  	 }
  	 else
  	 {
  	 	currentimage = currentimage - 1;
  	 };		  	
  	 document.cineframe.src = cine[currentimage].src;
  };		



for (var i = 0; i< movielength; i++)   	// preload all the images into an array

   {
	
   	var x = i.toString();
	var Pathtoimage = foldername + x + '.jpg'  ;
	cine[i] = new Image();
	cine[i].src = Pathtoimage ;	
   } ;  	


  		
  		
