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

FILE: mud_FadeGallery.js
REQUIRES: mud_API.js
AUTHOR: Takashi Okamoto mud(tm) - http://www.mudcorp.com/
VERSION: 1.0 - initial public release
DATE: 07/22/2005

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

This file is part of MudFadeGallery.

	MudFadeGallery is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.
	
	MudFadeGallery is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with Foobar; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

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

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

// CONSTRUCTOR
function MudFadeGallery(thisObj, id, imgsArray) {
	this.id = id;
	this.thisObj = thisObj;
	this.imgsArray = imgsArray;
	this.imgTotal = imgsArray.length;
	this.opacity = 100;
	this.fadeFrame = 0;
	this.imgCurrent = 0;
	this.timerID = null;
	// this keeps loaded images
	this.imgsLoaded = new Array(this.imgTotal);
}

// img change/control methods
MudFadeGallery.prototype.changeImg = function(imgNum) {
	// loadImage
	if (!this.imgsLoaded[imgNum]) {
		this.loadImgNumber(imgNum);
	}
	// set image
	getRawObject(this.id).src = this.imgsLoaded[imgNum].src;
}

MudFadeGallery.prototype.nextImg = function() {
	this.imgCurrent++;
	if (this.imgCurrent == this.imgTotal) {
		this.imgCurrent = 0;
	}
	// hide image
	hide(this.id);
	this.changeImg(this.imgCurrent);
	if (!isIEMac) this.startFade();
	else show(this.id);
}

MudFadeGallery.prototype.prevImg = function() {
	this.imgCurrent--;
	if (this.imgCurrent == -1) {
		this.imgCurrent = this.imgTotal - 1;
	}
	// hide image
	hide(this.id);
	this.changeImg(this.imgCurrent);
	if (!isIEMac) this.startFade();
	else show(this.id);
}

MudFadeGallery.prototype.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;
		// hide image
		hide(this.id);
		this.changeImg(this.imgCurrent);
		if (!isIEMac) this.startFade();
		else show(this.id);
	}
}

// start fade
MudFadeGallery.prototype.startFade = function() {
	// clear settimeout
	if (this.timerID) {
		window.clearTimeout(this.timerID);
		this.timerID = null;
	}
	// place delay before fade
	this.timerID = window.setTimeout(this.thisObj + ".fade()", 500);
}

// returns boolean
MudFadeGallery.prototype.loadImgNumber = function(imgNumber) {
	// check if already loaded
	if (!this.imgsLoaded[imgNumber]) {
		// load image
		this.imgsLoaded[imgNumber] = new Image();
		this.imgsLoaded[imgNumber].src = this.imgsArray[imgNumber];
	}
}

// img fade methods
MudFadeGallery.prototype.fade = function() {
	// clear settimeout
	if (this.timerID) {
		window.clearTimeout(this.timerID);
		this.timerID = null;
	}
	this.calcOpacity(this.fadeFrame);
	this.setOpacity(this.opacity);
	this.fadeFrame++;
	//alert(getRawObject(this.id).src+"\n"+this.opacity);
	if (getObject(this.id).visibility == "hidden") show(this.id);
	if (this.fadeFrame < MudFadeGallery.FADE_UP.length) {
		this.timerID = window.setTimeout(this.thisObj + ".fade()", 20);
	}
	else {
		this.fadeFrame = 0;
	}
}

MudFadeGallery.prototype.calcOpacity = function(frameNumber) {
	this.opacity = MudFadeGallery.FADE_UP[frameNumber];
}

MudFadeGallery.prototype.setOpacity = function(opacity) {
	var obj = getObject(this.id);
	// 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;
}