/*************************************************************************
  This code is from Dynamic Web Coding  at http://www.dyn-web.com/
  Copyright 2001-3 by Sharon Paine 
  See Terms of Use at http://www.dyn-web.com/bus/terms.html
  Permission granted to use this code as long as this entire notice is included.
*************************************************************************/

// if you want to open sub-window onclick of images set variables here
var subWin;
var subWinWd = 520;	// width of sub-window
var subWinHt = 450;	// height of sub-window
// add/remove window chrome as needed (no spaces between them!)
// list of common chrome elements: "menubar,location,toolbar,status,scrollbars,resizable"
var subWinChrome = "resizable";
// for centering sub-window on screen
var subWinTop = Math.round( (screen.availHeight - subWinHt)/2 );
var subWinLeft = Math.round( (screen.availWidth - subWinWd)/2 );

function initImgRotation() {
  // create rotating image objects here 
  // arguments: image name, rotation speed (milliseconds)
  var rotator1 = new rotateImgObj('img1',2500);
  // add the images to rotate into that image object
  rotator1.addImages("1.jpg","2.jpg","3.jpg","4.jpg","5.jpg","6.jpg","7.jpg","8.jpg","9.jpg","10.jpg","11.jpg","12.jpg","13.jpg");
  // add the corresponding actions to take onclick of those images (last one shows how to call another function onclick)
  rotator1.addActions("/gallery/1.html", "/gallery/2.html", "/gallery/3.html", "/gallery/4.html", "/gallery/5.html", "/gallery/6.html", "/gallery/7.html", "/gallery/8.html", "/gallery/9.html","/gallery/10.html","/gallery/11.html","/gallery/12.html","/gallery/13.html");
  
  // starts rotation for all defined rotateImgObjs
  for (var i=0; i<rotateImgObjs.length; i++) 
    rotateImgObjs[i].timer = setInterval(rotateImgObjs[i].animString + ".rotate()", rotateImgObjs[i].speed);
}

// for example function call onclick  
var msg1 = "You could call another function onclick of the images, or set" + "\n" + "the window.location property to load another document.";
function doMsg(msg) { alert(msg); }

// If all the images you wish to display are in the same location, you can specify the path here 
rotateImgObj.imagesPath = "/images/rotation/";

rotateImgObjs = []; // holds all rotating image objects defined
// constructor 
function rotateImgObj(nm,s) {
  this.speed=s; this.ctr=0; this.timer=0;  
  this.imgObj = document.images[nm]; // get reference to the image object
  this.index = rotateImgObjs.length; rotateImgObjs[this.index] = this;
  this.animString = "rotateImgObjs[" + this.index + "]";

  this.addImages = addRotatingImages; this.addActions = addClickActions;
  this.rotate = rotateImg;
}

function addRotatingImages() {
  this.imgObj.imgs = [];
  for (var i=0; i<arguments.length; i++) {
    this.imgObj.imgs[i] = new Image();
    this.imgObj.imgs[i].src = rotateImgObj.imagesPath + arguments[i];
  }
}

function addClickActions() {
  this.actions = [];
  for (var i=0; i<arguments.length; i++) { this.actions[i] = arguments[i]; }
}

// called onclick of images
function doImgClick(n) {
	if ( document.images && rotateImgObjs[n] ) {
    var obj = rotateImgObjs[n]; // shorten reference 
    if ( obj.actions && obj.actions[obj.ctr] ) {
  		if ( obj.actions[obj.ctr].indexOf('javascript:') != -1 ) eval( obj.actions[obj.ctr] );
  		else {
  			if ( subWin && !subWin.closed ) subWin.focus();
  			subWin = window.open( obj.actions[obj.ctr], "subWin",
           subWinChrome+",height="+subWinHt+",width="+subWinWd+",top="+subWinTop+",left="+subWinLeft); 
  		}
    } 
	}
}

// automatically closes sub-window when document unloads
function closeWin() {	if (subWin && !subWin.closed) subWin.close(); }
window.onunload = closeWin;


// controls rotation
function rotateImg() {
  if (this.ctr < this.imgObj.imgs.length-1) this.ctr++;
  else this.ctr = 0;
  this.imgObj.src = this.imgObj.imgs[this.ctr].src;
}

// for stopping/starting onmouseover/out
function stopRotation(n) {	
  if (rotateImgObjs[n]) clearInterval(rotateImgObjs[n].timer); 
}

function restartRotation(n) {
  if ( rotateImgObjs[n] ) {
    var obj = rotateImgObjs[n]; // shorten reference 
    obj.rotate(); // rotate now and resume repeated calls
    obj.timer = setInterval( obj.animString + ".rotate()", obj.speed );
  }
}
