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

FILE: mud_FadeGallery.js
REQUIRES: mud_API.js
AUTHOR: Takashi Okamoto mud(tm) - http://www.mudcorp.com/
VERSION: 2.0
DATE: 09/24/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 MudFadeGallery; if not, write to the Free Software
        Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

--------------------------------------------------------------------------------
NOTES:
        added support for captions.
        fixed image preload bug.

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

MudFadeGallery.FADE_UP = new Array(6, 12, 24, 48, 99, 100);
var sideshow=false;
// CONSTRUCTOR
function MudFadeGallery(thisObj, id, imgsArray, startNum) {
        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 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);
        }
        if (!this.imgsLoaded[imgNum].complete) {
                return false;
        }
        // set image
        getRawObject(this.id).src = this.imgsLoaded[imgNum].src;
        return true;
}

MudFadeGallery.prototype.showImg = function(imgNum) {
        this.imgCurrent = parseInt(imgNum);
        if (this.imgCurrent >= this.imgTotal) {
                this.imgCurrent = 0;
        }
        else if (this.imgCurrent < 0) {
                this.imgCurrent = 0;
        }
        this.onStart();
        // hide image
        hide(this.id);
        if (!this.changeImg(this.imgCurrent)) {
                // we need to give time for image to load
                if (this.timerID) {
                        window.clearTimeout(this.timerID);
                        this.timerID = null;
                }
                this.timerID = window.setTimeout(this.thisObj + ".showImg(" + imgNum + ")", 100);
                return;
        }
        if (!isIEMac) this.startFade();
        else {
                show(this.id);
                this.onFadeStart();
                this.onFadeEnd();
        }
}

MudFadeGallery.prototype.nextImg = function() {
        this.imgCurrent++;
        if (this.imgCurrent >= this.imgTotal) {
                this.imgCurrent = 0;
        }
        this.onStart();
        // hide image
        hide(this.id);
        if (!this.changeImg(this.imgCurrent)) {
                this.imgCurrent--;
                if (this.imgCurrent < 0) this.imgCurrent = this.imgTotal - 1;
                // we need to give time for image to load
                if (this.timerID) {
                        window.clearTimeout(this.timerID);
                        this.timerID = null;
                }
                this.timerID = window.setTimeout(this.thisObj + ".nextImg()", 100);
                return;
        }
        if (!isIEMac) this.startFade();
        else {
                show(this.id);
                this.onFadeStart();
                this.onFadeEnd();
        }
}
MudFadeGallery.prototype.prevImg = function() {
        this.imgCurrent--;
        if (this.imgCurrent == -1) {
                this.imgCurrent = this.imgTotal - 1;
        }
        this.onStart();
        // hide image
        hide(this.id);
        if (!this.changeImg(this.imgCurrent)) {
                this.imgCurrent++;
                if (this.imgCurrent >= this.imgTotal) this.imgCurrent = 0;
                // we need to give time for image to load
                if (this.timerID) {
                        window.clearTimeout(this.timerID);
                        this.timerID = null;
                }
                this.timerID = window.setTimeout(this.thisObj + ".prevImg()", 100);
                return;
        }
        if (!isIEMac) this.startFade();
        else {
                show(this.id);
                this.onFadeStart();
                this.onFadeEnd();
        }
}

// start fade
MudFadeGallery.prototype.startFade = function() {
        this.onFadeStart();
        // 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) {
        // load image
        this.imgsLoaded[imgNumber] = new Image();
        this.imgsLoaded[imgNumber].src = this.imgsArray[imgNumber].image;
}

// img fade methods
MudFadeGallery.prototype.fade = function() {
        // clear settimeout
        show(this.id);
        if (this.timerID) {
                window.clearTimeout(this.timerID);
                this.timerID = null;
        }
        this.calcOpacity(this.fadeFrame);
        this.setOpacity(this.opacity);
        this.fadeFrame++;
        if (this.fadeFrame < MudFadeGallery.FADE_UP.length) {
                this.timerID = window.setTimeout(this.thisObj + ".fade()", 20);
        }
        else {
                this.onFadeEnd();
                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;
}

MudFadeGallery.prototype.onStart = function() {
        // show title and caption
        var title = "cargando...";
        var caption = "cargando...";
        // replace with innerHTML
        if (getRawObject("proj_title")) getRawObject("proj_title").innerHTML = title;
        if (getRawObject("proj_caption")) getRawObject("proj_caption").innerHTML = caption;
        if (getRawObject("proj_number")) getRawObject("proj_number").innerHTML = "(" +(this.imgCurrent+1) + " de " + imgsGallery.length + ")";

}

MudFadeGallery.prototype.onFadeStart = function() {
        if (getRawObject("image")) getRawObject("image").style.backgroundImage="none";

}

MudFadeGallery.prototype.onFadeEnd = function() {
        // show title and caption
        var colors = (this.imgsArray[this.imgCurrent].colors) ? this.imgsArray[this.imgCurrent].colors : "";
        var title = (this.imgsArray[this.imgCurrent].title) ? this.imgsArray[this.imgCurrent].title : "Sin titulo";
        var caption = (this.imgsArray[this.imgCurrent].caption) ? this.imgsArray[this.imgCurrent].caption : "";
        // replace with innerHTML
        if (getRawObject("proj_title")) getRawObject("proj_title").innerHTML = title;
        if (getRawObject("proj_caption")) getRawObject("proj_caption").innerHTML = caption;
        if (getRawObject("image")) getRawObject("image").style.backgroundImage="url('fondo.gif')";
}
