MultiImageRotator.restartDelay = 3000; // delay onmouseout before call to rotate
MultiImageRotator.col = []; 

// arguments: image name, rotation speed, path to images (optional), 
// target, i.e. name of window to direct url's to onclick (optional)
function MultiImageRotator(name, speed, path, tgt) {
    this.name = name; 
	this.speed = speed || 4500; // default speed of rotation
    this.path = path || ""; 
    //this.imgext = imgext || ".gif";
	this.tgt = tgt;
    this.ctr = 0; 
	this.timer = 0; 
	//this.fit = 0;
	//this.fot = 0;
	this.imgsMain = []; 
	this.imgsOutter = [];
	this.imgsOutterSelected = [];
	this.actions = [];
    this.index = MultiImageRotator.col.length; 
	MultiImageRotator.col[this.index] = this;
    this.animString = "MultiImageRotator.col[" + this.index + "]";
	this.imageNameOrder = new Array();
}

// preloads images
MultiImageRotator.prototype.addImages = function() { 
    var img;
    var sname;
    for (var i=0; arguments[i]; i++) {
        //sname = removeExt(arguments[i]);
        sname = arguments[i];
		this.imageNameOrder["ctl00_"+sname]=i;
		//load main center images
        img = new Image();
        img.src = this.path + arguments[i] + ".jpg"; // + this.imgext;
		img.name = sname;
        this.imgsMain[this.imgsMain.length] = img;
		//load outter selected images
		img2 = new Image();
        img2.src = this.path + "outterselected_" + arguments[i] + ".gif"; // + this.imgext;
		img2.name = sname;
        this.imgsOutterSelected[this.imgsOutterSelected.length] = img2;
		//load outter images
		img3 = new Image();
        img3.src = this.path + "outter_" + arguments[i] + ".gif"; // + this.imgext;
		img3.name = sname;
        this.imgsOutter[this.imgsOutter.length] = img3;
    }
}

MultiImageRotator.prototype.addActions = function() {
    //in case an argument's value is null
    var len = arguments.length; 
    for (var i=0; i < len; i++) 
        this.actions[this.actions.length] = arguments[i]; 
}

MultiImageRotator.prototype.rotate = function() {
	//clearTimeout(this.fit);
	//clearTimeout(this.fot);
	clearTimeout(this.timer);
	this.timer = null;
	var pimgid = 0;
	var imgObjout; 
	
    if (this.ctr < this.imgsMain.length-1) {
		pimgid = this.ctr;
		this.ctr++;
	} else { 
		pimgid = this.imgsMain.length-1;
		this.ctr = 0;
	}
	
	//reset all the images to unselected state
	for (var i=0; i<this.imgsOutter.length; i++) {
		imgObjout = document.getElementById("ctl00_"+this.imgsMain[i].name); 
		imgObjout.src = this.imgsOutter[i].src;
	}
	
	//main center image
    var imgObj = document.images[this.name]; 
	//surrounding images
	var imgObjext = document.getElementById("ctl00_"+this.imgsMain[this.ctr].name); 

    if (imgObj) {
		//change the center image
        imgObj.src = this.imgsMain[this.ctr].src;
		//change the corresponding outter image to selected state
		imgObjext.src = this.imgsOutterSelected[this.ctr].src;
        this.timer = setTimeout( this.animString + ".rotate()", this.speed);
		//this.fot = setTimeout("currentOpac('main', 0, " + MultiImageRotator.fadeOutTime + ")",this.speed-MultiImageRotator.fadeOutTime);
		//this.fit = setTimeout("currentOpac('main', 100, " + MultiImageRotator.fadeInTime + ")",MultiImageRotator.fadeInTime);
    }
}

// Start rotation for all instances 
MultiImageRotator.start = function() {
    var len = MultiImageRotator.col.length, obj;
    for (var i=0; i<len; i++) {
        obj = MultiImageRotator.col[i];
        if (obj && obj.name )
            obj.timer = setTimeout( obj.animString + ".rotate()", obj.speed);
			//obj.fot = setTimeout("currentOpac('main', 0, " + MultiImageRotator.fadeOutTime + ")",obj.speed-MultiImageRotator.fadeOutTime);
			//obj.fit = setTimeout("currentOpac('main', 100, " + MultiImageRotator.fadeInTime + ")",MultiImageRotator.fadeInTime);
    }
}

// called onclick of images
MultiImageRotator.doClick = function(n) {
    var obj = MultiImageRotator.col[0]; 
	if ( !document.images || !obj ) return true;
    if ( obj.actions && obj.actions[obj.ctr] ) {
        // url
        if ( typeof obj.actions[obj.ctr] == "string" ) { 
            // open in separate window
            if ( obj.tgt ) { 
                // add features here if you want, i.e., chrome, size, position, ...
                //UNCOMMENT WHEN USING LIVE
                //alert("A");
                window.location = obj.actions[obj.ctr];
                //var win = window.open(obj.actions[obj.ctr], obj.tgt);
                //if ( win && !win.closed ) win.focus();
            } else {
                //UNCOMMENT WHEN USING LIVE
				//window.location = obj.actions[obj.ctr];
				//alert(obj.actions[obj.ctr]);
            }
        // execute user defined function
        } else { 
            obj.actions[obj.ctr](); 
        }
    }
    return false;
}

// for stopping/starting onmouseover/out
MultiImageRotator.pause = function(n) {	
	if (n != "ctl00_main") {
		var imgObjext; 
		var obj = MultiImageRotator.col[0];
		
		var txa = obj.imageNameOrder[n];
		var txb = parseInt(txa);
		var txc = obj.imgsMain[txb];
		var txd = txc.name;
		
		document.getElementById("ctl00_"+obj.imgsMain[parseInt(obj.imageNameOrder[n])].name).src = obj.imgsOutterSelected[parseInt(obj.imageNameOrder[n])].src;
		document.getElementById("ctl00_main").src = obj.imgsMain[parseInt(obj.imageNameOrder[n])].src;
		//set the ctr to the id of the image that was rolled over so we can obtain the correct action to take if the user clicks
		MultiImageRotator.col[0].ctr = parseInt(obj.imageNameOrder[n]);
		for (var i=0; i<obj.imgsMain.length; i++) {
			if (i != parseInt(obj.imageNameOrder[n])) { 
				imgObjext = document.getElementById("ctl00_"+obj.imgsMain[i].name); 
				imgObjext.src = obj.imgsOutter[i].src;
			}
		}
	}
    MultiImageRotator.clearTimers(0);
}

MultiImageRotator.clearTimers = function(n) {
    var obj = MultiImageRotator.col[0]; 
    if ( obj ) {
        clearTimeout( obj.timer );
		//clearTimeout( obj.fit );
		//clearTimeout( obj.fot );
		obj.timer = null;
		//obj.fit = null;
		//obj.fot = null;
    }
}

MultiImageRotator.resume = function(n) {
    MultiImageRotator.clearTimers(0);
    var obj = MultiImageRotator.col[0]; 
    if ( obj ) {
        obj.timer = setTimeout( obj.animString + ".rotate()", MultiImageRotator.restartDelay );
		//obj.fot = setTimeout("currentOpac('main', 0, " + MultiImageRotator.fadeOutTime + ")",MultiImageRotator.restartDelay-MultiImageRotator.fadeOutTime);
		//obj.fit = setTimeout("currentOpac('main', 100, " + MultiImageRotator.fadeInTime + ")",MultiImageRotator.fadeInTime);
    }
}

function removeExt(s) {
    return s.slice(0, s.indexOf("."));
}