// image toggler v1.0b

function imagetogg1(cont, x, y, bmode)
{
    this.contid = cont;
    this.cont = false;
    this.browser = navigator.appVersion.match(/MSIE/) ? 'ie' : 'object';
    this.image = new Image(x, y);//document.createElement('IMG');
    this.image.width = x;
    this.image.height = y;
    this.image.id = 'imagetogg1img1';
    this.image.name = this.image.id;
    this.image.contid = cont;
    this.image.blendmode = bmode;
    this.image.style.visibility = 'visible';
    this.image.style.position = 'absolute';
    this.image.style.zIndex = 2;
    this.image.style.left = '0';
    this.image.style.top = '0';
    this.image2 = new Image(x, y);//document.createElement('IMG');
    this.image2.width = x;
    this.image2.height = y;
    this.image2.id = 'imagetogg1img2';
    this.image2.fromid = this.image.id;
    this.image.fromid = this.image2.id;
    this.image2.name = this.image.id;
    this.image2.contid = cont;
    this.image2.blendmode = bmode;
    this.image2.style.visibility = 'hidden';
    this.image2.style.position = 'absolute';
    this.image2.style.zIndex = 1;
    this.image2.style.left = '0';
    this.image2.style.top = '0';
    this.images = new Array();
    this.index = -1;
    
    this.imgAdd = function(src) {
        var index = this.images.length;
        this.images[index] = new Image(25, 25);
        this.images[index].src = src;
    }
    
    this.toggle = function() {
        if (!this.cont && !(this.cont=getElement(this.contid)))
            return false;
        if (!this.images.length)
            return false;
        if (this.index==-1)
        {
            this.image.src = this.images[0].src;
            this.image.active = true;
            this.image2.active = false;
            this.cont.appendChild(this.image);
            this.cont.appendChild(this.image2);
            this.index = 0;
            return false;
        }
        if (this.images.length<=1)
            return false;
        this.index++;
        if (this.index>=this.images.length)
            this.index = 0;
        if (this.cont && (img=getElement(this.image.id)))
        {
            if (this.browser=='ie')
            {
                this.image2.onload = function() {
                    imageblends.go(this.id, this.fromid, this.blendmode);
                }
                this.image2.src = this.images[this.index].src;
            }
            else
            {
                if (this.image.active)
                {
                    this.image2.onload = function() {
                        imageblends.go(this.id, this.fromid, this.blendmode);
                    }
                    this.image2.src = this.images[this.index].src;
                    this.image.active = false;
                    this.image2.active = true;
                }
                else if(this.image2.active)
                {
                    this.image.onload = function() {
                        imageblends.go(this.id, this.fromid, this.blendmode);
                    }
                    this.image.src = this.images[this.index].src;
                    this.image.active = true;
                    this.image2.active = false;
                }
            }
        }
    }
}

imageblends = {

    go : function(toId, fromId, mode, speed) {
        if (toId && !(imgTo=getElement(toId)))
            return false;
        if (!speed)
            speed = 2.0; // sekund
        switch (mode)
        {
            case 'blend':
                if (fromId && !(imgFrom=getElement(fromId)))
                    return false;
                imgTo.onload = function() {}
                if (navigator.appVersion.match(/MSIE/))
                    this.blend_e2e_ie(imgTo, imgFrom, speed);
                else
                    this.blend_e2e(imgTo, imgFrom, speed);
                break;
            default:
        }
    },
    
    blend_e2e : function(imgTo, imgFrom, speed) {
        imgTo.style.zIndex = 1;
        imgFrom.style.zIndex = 2;
        this.changeOpacity(imgTo, 100);

        var timer = 0;
        var speed = Math.round(((speed)?(speed*1000):2000) / 100);
        for(i=100; i>=0; i--)
        {
            setTimeout("imageblends.changeOpacity('" + imgFrom.id + "', " + i + ")",(timer * speed));
            timer++;
        }
    },
    blend_e2e_ie : function(imgTo, imgFrom, speed) {
        if (!document.images)
            return;
        imgFrom.onload = function() {}
        imgFrom.style.filter = "blendTrans(duration="+speed+")";
        try {
            imgFrom.filters.blendTrans(duration=1.0).Apply();
            imgFrom.filters.blendTrans(duration=1.0).Play();
            imgFrom.src = imgTo.src;
        } catch(e) {
            imgFrom.src = imgTo.src;
        }
    },
    
    changeOpacity : function(element, opacity) {
        var object = (typeof(element)=='string') ? document.getElementById(element).style : element.style;
        if (opacity>0)
            object.visibility = 'visible';
        else
            object.visibility = 'hidden';
        object.opacity = (opacity / 100);
        object.MozOpacity = (opacity / 100);
        //object.KhtmlOpacity = (opacity / 100);
        object.filter = "alpha(opacity=" + opacity + ")";
    },
    
    src2bg : function src2bg(src, id) {
    if ((img=getElement(src)) && (cont=getElement(id)))
        cont.style.backgroundImage = "url("+img.src+")";
    }

}
