// PictureCycle Object // Duncan Littlefield // Feel free to use this code. // I do not assume any responsibility for the accuracy of this code. // STATE_INIT = 0 // STATE_IDLE = 1 // STATE_CYCLE = 2 // STATE_ENDING_CYCLE = 3 // STATE_DISPLAY_1_PIC = 4 //**************************************************** // PictureDisplay object definition / constructor // --------------------------------------------------- function PictureDisplay (sPictureName) { //==================================== // Member variables and initialization this.sPictureName = sPictureName; // The name of the picture on the web page this.iPictureIndex = 0; this.iPictureCount = 0; this.aPictureList = new Array(); this.aTimeoutList = new Array(); this.aFullSizeList = new Array(); this.iState = 0; eval(this.oSelf + "=this"); //==================================== // Public Member methods this.CyclePictureList = CyclePictureList; this.StopCycling = StopCycling; this.AddPicture = AddPicture; this.DisplayPicture = DisplayPicture; this.ShowFullSize = ShowFullSize; //==================================== // Private Member methods this.PrivateCycleList = PrivateCycleList; } //**************************************************** // AddPicture // --------------------------------------------------- function AddPicture (a_sPicture, a_iTimeout, a_sFullSizeName) { this.aTimeoutList[this.iPictureCount] = a_iTimeout; this.aPictureList[this.iPictureCount] = new Image(); this.aPictureList[this.iPictureCount].src = a_sPicture; this.aFullSizeList[this.iPictureCount] = a_sFullSizeName; this.iPictureCount++; } //**************************************************** // CyclePicture // --------------------------------------------------- function CyclePictureList() { if ((this.iState != 2) && (this.iState != 3) && (this.aPictureList.length > 0)) { this.iState = 2; this.PrivateCycleList (); } } function PrivateCycleList () { //================================================== // Enter cycle as long as we have not been told to stop if ((this.iState == 2) && (this.aPictureList.length > 0)) { //------------------------------------------------------ // Calculate next picture this.iPictureIndex++; if (this.iPictureIndex >= this.aPictureList.length) { this.iPictureIndex = 0; } //------------------------------------------------------ // Display next picture document.images[this.sPictureName].src = this.aPictureList[this.iPictureIndex].src; //------------------------------------------------------ // Wait for some time, then display the next picture // This call is non-blocking - it does not sleep, so this is not bad recursion setTimeout(this.oSelf+".PrivateCycleList()", this.aTimeoutList[this.iPictureIndex]) } //================================================== // If the stop flag has been set, then go back to idle if (this.iState == 3) { this.iState = 1; } } //**************************************************** // Stop Cycling // Set flag so that loop will stop. I use a flag so that I do not get // several loops running at the same time. // --------------------------------------------------- function StopCycling() { if (this.iState == 2) this.iState = 3; // Set so that loop stops } //**************************************************** // DisplayPicture // --------------------------------------------------- function DisplayPicture (a_iPictureIndex) { //==================================== // Stop any cycling this.StopCycling(); //==================================== // Validate the picture index if (a_iPictureIndex < this.iPictureCount) { this.iPictureIndex = a_iPictureIndex; } //==================================== // Display selected picture this.iState = 4; document.images[this.sPictureName].src = this.aPictureList[this.iPictureIndex].src; } //**************************************************** // ShowFullSize // --------------------------------------------------- function ShowFullSize () { //==================================== // Stop any cycling this.StopCycling(); //==================================== // Display selected picture in a new window if (this.iPictureIndex < this.iPictureCount) { this.iState = 4; window.location.href = this.aFullSizeList[this.iPictureIndex]; } }