/*******************************************************************************

FILE: mud_FadeGallery.js
REQUIRES: prototype.js
AUTHOR: Takashi Okamoto mud(tm) - http://www.mudcorp.com/
VERSION: 2.1 - bug fixes
DATE: 01/07/2006

--------------------------------------------------------------------------------

This file is part of MudFadeGallery.

	MudFadeGallery is free for anyone to use, but this header MUST be
	included, and may not be modified.

*******************************************************************************/

var MudFadeGallery = Class.create();

MudFadeGallery.FADE_UP = new Array(0, 6, 11, 17, 23, 30, 38, 47, 56, 66, 75, 84, 92, 100);

MudFadeGallery.prototype = {
	
	/* ------------- CALLBACKS ------------- */
	/* ------------- EDITABLE -------------- */
	
	// runs right before fade begins
	onFadeStart: function() {
		var title = "...";
		var caption = "";
		var max = 100000;
		$(this.id).title = title;
		$(this.id).alt = caption;
		if (this.imgTotal>max)
		{
			var page=Math.ceil((this.imgCurrent+1)/max);
			var ret='';
			var imginit=(page*max)-max;
			var imgend=(( (page*max)>this.imgTotal )?this.imgTotal:page*max);
			var nslides=this.id.replace('imgDisplay','');
		}
    //$(this.id+(this.imgCurrent+1)+"_link").className = "cur";
		
		//$(this.id+"_title").innerHTML = title;
		//$(this.id+"_caption").innerHTML = caption;
		//$(this.id+"_number").innerHTML = (this.imgCurrent+1) + " of " + imgsGallery.length + " projects";
	},
	
	// runs right after fade completes
	onFadeEnd: function() {
		var title = (this.imgsArray[this.imgCurrent].title) ? this.imgsArray[this.imgCurrent].title : "";
		var caption = (this.imgsArray[this.imgCurrent].caption) ? this.imgsArray[this.imgCurrent].caption : "Image";
		$(this.id).title = title;
		$(this.id).alt = caption;
		//$(this.id+"_title").innerHTML = title;
		//$(this.id+"_caption").innerHTML = caption;
	},
	
	/* ------------- DON'T EDIT PAST HERE ------------- */
	
	initialize: function(thisObj, id, imgsArray, startNum, autoPlay) {
		this.id = id;
		this.thisObj = thisObj;
		this.imgsArray = imgsArray;
		this.imgTotal = imgsArray.length;
		this.opacity = 100;
		this.fadeFrame = 0;
		this.imgCurrent = (startNum) ? startNum : 0;
		this.timerID = null;
		this.apTimerID = null;
		this.imgsLoaded = new Array(this.imgTotal);
		this.autoplay = (autoPlay) ? autoPlay : 0;
		
		window.setTimeout(this.thisObj+'.preloadImgs()', 50);
		
    if (this.autoplay > 0) {
      var delay = this.autoplay * 1000 + 1000;
      this.apTimerID = window.setTimeout(this.thisObj + '.autoplayImgs(' + delay + ')', delay);
    }
	},
	
	changeImg: function(imgNum) {
		if (!this.imgsLoaded[imgNum]) {
			this.loadImgNumber(imgNum);
		}
		$(this.id).src = this.imgsLoaded[imgNum].src;
	},
	
	nextImg: function() {
		this.imgCurrent++;
		if (this.imgCurrent == this.imgTotal) {
			this.imgCurrent = 0;
		}
		this.doFade();
	},
	
	prevImg: function() {
		this.imgCurrent--;
		if (this.imgCurrent == -1) {
			this.imgCurrent = this.imgTotal - 1;
		}
		this.doFade();
	},
	
	showImg: function(imgNum) {
		if (this.imgCurrent != imgNum) {
			if (this.imgCurrent == -1) {
				this.imgCurrent = this.imgTotal - 1;
			}
			else if (this.imgCurrent > this.imgTotal-1) {
				this.imgCurrent = 0;
			}
			else this.imgCurrent = imgNum;
			this.doFade();
		}
	},
	
	doFade: function() {
		this.onFadeStart();
		Element.hide(this.id);
		this.changeImg(this.imgCurrent);
		if (!(/MSIE/.test(navigator.userAgent) && /Mac/.test(navigator.userAgent)))
			this.startFade();
		else {
			Element.show(this.id);
			this.onFadeEnd();
		}
	},
	
	startFade: function() {
		if (this.timerID) {
			window.clearTimeout(this.timerID);
			this.timerID = null;
		}
		// place delay before fade
		this.timerID = window.setTimeout(this.thisObj + ".fade()", 500);
	},
	
	preloadImgs: function() {
		for (var i = 0; i < this.imgTotal; i++) {
      this.loadImgNumber(i);
    }
	},
	
	loadImgNumber: function(imgNumber) {
		// check if already loaded
		if (!this.imgsLoaded[imgNumber]) {
			this.imgsLoaded[imgNumber] = new Image();
			this.imgsLoaded[imgNumber].src = this.imgsArray[imgNumber].image;
		}
	},
	
	autoplayImgs: function(delay) {
    if (this.apTimerID) {
    window.clearTimeout(this.apTimerID);
    this.apTimerID = null;
    }
    this.nextImg();
    this.apTimerID = window.setTimeout(this.thisObj + ".autoplayImgs(" + delay + ")", delay);
  },
  
  apStart: function(delay) {
    //this.nextImg();
    if (!delay || delay < 1) delay = 1;
    delay = delay * 1000 + 1000;
    this.autoplayImgs(delay);
  },

  apStop: function(delay) {
    if (this.apTimerID) {
    window.clearTimeout(this.apTimerID);
    this.apTimerID = null;
    }
  },
	
	fade: function() {
		if (this.timerID) {
			window.clearTimeout(this.timerID);
			this.timerID = null;
		}
		this.calcOpacity(this.fadeFrame);
		this.setOpacity(this.opacity);
		this.fadeFrame++;
		if ($(this.id).style.display == "none") Element.show(this.id);
		if (this.fadeFrame < MudFadeGallery.FADE_UP.length) {
			this.timerID = window.setTimeout(this.thisObj + ".fade()", 20);
		}
		else {
			this.fadeFrame = 0;
			this.onFadeEnd();
		}
	},
	
	calcOpacity: function(frameNumber) {
		this.opacity = MudFadeGallery.FADE_UP[frameNumber];
	},
	
	setOpacity: function(opacity) {
		var obj = $(this.id).style;
		// Fix for math error in some browsers
		opacity = (opacity == 100) ? 99.999 : opacity;
		// IE/Windows
		obj.filter = "alpha(opacity:"+opacity+")";
		// Safari < 1.2, Konqueror
		obj.KHTMLOpacity = opacity/100;	
		// Older Mozilla and Firefox
		obj.MozOpacity = opacity/100;
		// Safari 1.2, newer Firefox and Mozilla, CSS3
		obj.opacity = opacity/100;
	}
}