﻿/*slide projector
	version 3  1/22/07 dc
*/ 

	
/* 
Slide object
*/
	function slide(){
		this.imageURL='';
		this.imageOBJ=0;
		this.caption='';

		this.downloadSlide = downloadSlide;
		
		function downloadSlide (   ){
			if (!this.imageObj){this.imageOBJ=new Image();}
			this.imageOBJ.src= this.imageURL;
		};
	}

/*slide projector

	MAIN slideProjector OBJECT
	 startup sequence:
	 
	 	start (called on page load) ->loadTextFile (via XMLHTTP object)
	 	PosFileReady (callback)
	 	initProjector...
	 
	*/ 

function slideProjector (   ) {
	
 	this.supported =   ( document.getElementById || document.all  );
 	if  ( !this.supported  ){return false;}

	this.screenID="";// the ID of the image tag on the page.
	
	this.slideShowImageDirectory="";// this can be left empty if you supply full filename and path for each slide.
	
	// the list of slide names  ( or complete urls, if you leave the 
	this.slideListURL="";//must be a text file
	
	// for parsing out the images from the file
	this.slideDelimiter='\n';
	this.fieldDeliniter="\t";
	
	this.random_adv=false; //  random advance - note, this will start on first slide (use random start for full random)
	this.random_start=false;// allows start at random slide, but regular advance
	this.dwellTime=1500;//milliseconds, can be overruled by the slideDwellTime of an individual slide  ( if 

	this.slideList=new Array (   );//will be filled up with slide objects
	this.ImageNum=0;
	this.next_number = 1;//used if slideshow is to be random
	this.timerID = 0;
	
	this.remove = remove;
	function remove(){
	
		window.clearTimeout(this.timerID);
		
	
	}
	
	
	this.getNextSlideNumber = getNextSlideNumber;
	function getNextSlideNumber (   ) {
		
		if  ( this.random_adv  ) {
			this.ImageNum = this.next_number;
			this.next_number = randNum ( 0, this.number_of_slides-1  );
		}
		else {
			this.ImageNum =  ( this.ImageNum+1  ) %  ( this.number_of_slides  );
			this.next_number =  ( this.ImageNum+1  ) %  ( this.number_of_slides  );
		}
		
	};
	
	this.showSlide = showSlide;
		function showSlide ( whichOne ){
		document.images[ this.screenID ].src = this.slideList[ whichOne ].imageURL;
		if(!this.captionID==''){
		document.getElementById ( this.captionID ).innerHTML=this.slideList[ whichOne ].caption;	
		}
		
	};


// fill up the slideList with new slide objects
	this.initProjector = initProjector;
	function initProjector ( theTexttoParse  ){


		
		var NameArray = new Array (   );
		NameArray=theTexttoParse.split ( this.slideDelimiter );
       
		this.number_of_slides = NameArray.length;
		
		this.ImageNum=0;
		if(this.random_start){this.ImageNum = randNum ( 0, this.number_of_slides-1  );}

       	for  ( var i=0; i<this.number_of_slides;i++  ){

					ParamArray=NameArray[ i ].split ( this.fieldDeliniter );					
		
		 			this.slideList[ i ] = new slide (   );
		 			this.slideList[ i ].imageURL=this.slideShowImageDirectory+ParamArray[ 0 ];
		 			if(ParamArray.length>1){this.slideList[ i ].caption=ParamArray[ 1 ];}//caption
		}
       
		this.slideList[ this.ImageNum ].downloadSlide();// for next time
		
		this.ShowCurrentAndPrepareNext (   );
	};
//
//////////// slideProjector //////////////// xxxxxxxxxxxxxxxxx ///////////////////////////////////////////
//
	this.ShowCurrentAndPrepareNext=ShowCurrentAndPrepareNext;
	function ShowCurrentAndPrepareNext (   ) {

		this.showSlide ( this.ImageNum );
	
		this.getNextSlideNumber (   );
	 	this.slideList[ this.next_number ].downloadSlide ();
	 	var _this=this;
		var recur_call = function (   ){_this.ShowCurrentAndPrepareNext ();};
		this.timerID = window.setTimeout ( recur_call, this.dwellTime  );
		
	};

//
//////////// slideProjector //////////////// xxxxxxxxxxxxxxxxx ///////////////////////////////////////////
//
    this.start = start;
    function start(){
    	this.loadTextFile ( this.slideListURL  );
    };

	function randNum ( x, y  ) {
		var range = y - x + 1;
		return Math.floor ( Math.random() * range  ) + x;
	};
	
	////////////////////////////// UTILITY FUNCTIONS TO GET THE FILE /////////////////////////////	
	// function to get the list from the server
	this.loadTextFile=loadTextFile;
	function loadTextFile ( theURL  ) {
		var _this = this;
		if  ( window.XMLHttpRequest  ) { // code for Mozilla, Safari, etc 
	      this.xmlhttpObj=new XMLHttpRequest (   );
	      this.xmlhttpObj.onreadystatechange=function (   ) {_this.postFileReady (   )};
	      this.xmlhttpObj.open ( "GET", theURL, true  );
	      this.xmlhttpObj.send ( null  );
		} 
	    else if  ( window.ActiveXObject  ) { 
	  		this.xmlhttpObj=false;
		 	try {
		 		this.xmlhttpObj = new ActiveXObject( "Msxml2.XMLHTTP");
		 	} catch (er) {
		 		try {
		    		this.xmlhttpObj = new ActiveXObject("Microsoft.XMLHTTP");
		 		} catch (er) {
		   			this.xmlhttpObj = false;
		 			}
				}
		if (!this.xmlhttpObj && typeof XMLHttpRequest!='undefined') {
			try {
				this.xmlhttpObj = new XMLHttpRequest();
			} catch (er) {
				this.xmlhttpObj=false;
			}
		}
		if (!this.mlhttp && window.createRequest) {
			try {
				this.xmlhttpObj = window.createRequest();
			} catch (er) {
				this.xmlhttpObj=false;
			}
		}
		

      if  ( this.xmlhttpObj  ) {
       	this.xmlhttpObj.onreadystatechange=function (   ) {_this.postFileReady (   );};
       	this.xmlhttpObj.open ( 'GET', theURL, true  );
	    this.xmlhttpObj.send (   );
	   }
   }
  	  

};

// function to handle asynchronous call - loads the slideList
this.postFileReady = postFileReady;
function postFileReady (   ) {
   if  ( this.xmlhttpObj.readyState==4  ) { 
      if  ( this.xmlhttpObj.status==200  ) { 
         this.initProjector  (  this.xmlhttpObj.responseText   );
      }
  }
};

}// end of slide projector