﻿/*
	"AlphaBlender 1.1"
	by Diego Costa (foxy_dcc@hotmail.com)
	2009 -All Rights Reserved.

              version 1.1 - 18-05-2011 :    AlphaChange structure added.
                                            makeAlphaBlending() method modified to work together with processAlphaBlending and AlphaChange structure.
                                            processAlphaBlending method added to work with AlphaChange structure and avoid flickering when multithreaded processing calls work on the same object.
                                            blendTrans method modified to work with both img.src and obj.style.backgroundImage.
*/

function AlphaChange(currentOpacity, increment, finalOpacity, callbackFunction)
{
    this.currentOpacity=currentOpacity;
    this.increment=increment;
    this.finalOpacity=finalOpacity;
    this.callbackFunction=callbackFunction;
}

function AlphaBlender()
{

}

AlphaBlender.prototype.changeOpacity=function(obj,iNewOpacity)
{
    obj.style.display='';
	obj.style.opacity=iNewOpacity/100;
	obj.style.MozOpacity=iNewOpacity/100;
	obj.style.KhtmlOpacity=iNewOpacity/100;
	obj.style.filter="alpha(opacity=" + iNewOpacity + ")";
    return(obj)	
}

//Hace una transcición entre una imagen y otra:
AlphaBlender.prototype.blendTrans=function(sId,sDestImg)
{
	var img1;
	img1=document.getElementById(sId);
	
	if(img1.filters)
	{
	    img1.style.filter="blendTrans(duration=2)";
	    img1.filters.blendTrans.Apply();
	}
	
	if(img1.src && img1.src!="")
	{
	    img1.src=sDestImg;
	}
	else
	{
	    img1.style.backgroundImage="url(" + sDestImg + ")";
	}
	
	if(img1.filters)
	{
	    img1.filters.blendTrans.Play();
	}
	img1=null;
}


//Hace un fade in/out de un objeto según se le pase un incremento positivo o negativo:
/*
AlphaBlender.prototype.makeAlphaBlending=function(iCurrentOpacity,iIncrement,sIdObject,callbackFunction,iFinalOpacity)
{
    var object=document.getElementById(sIdObject);
    
    iCurrentOpacity+=iIncrement;
    			
	if(iIncrement>0)
	{
	    if (iFinalOpacity == null)
	    {
	        iFinalOpacity = 156;
	    }

	    if (iCurrentOpacity < iFinalOpacity)
		{
		    object = AlphaBlender.prototype.changeOpacity(object, iCurrentOpacity);

			window.setTimeout(function() { AlphaBlender.prototype.makeAlphaBlending(iCurrentOpacity, iIncrement, sIdObject, callbackFunction, iFinalOpacity); }, 10);
		}
		else
		{
		    if(callbackFunction)
		    {
		        callbackFunction();
		    }
		}
	}
	else
	{
	    if (iFinalOpacity == null)
	    {
	        iFinalOpacity = 0;
	    }

	    if (iCurrentOpacity > iFinalOpacity)
		{
		    object = AlphaBlender.prototype.changeOpacity(object, iCurrentOpacity);
		    window.setTimeout(function() { AlphaBlender.prototype.makeAlphaBlending(iCurrentOpacity, iIncrement, sIdObject, callbackFunction, iFinalOpacity); }, 10);
		}
		else
		{
		    object.style.display = "none";
			if(callbackFunction)
		    {
			    callbackFunction();
			}
		}
	}			
	object=null;
}
*/

AlphaBlender.prototype.processAlphaBlending=function(obj)
{
    var alphaChange=obj.getAttribute("alphaChange");
    if(alphaChange)
    {
        alphaChange.currentOpacity+=alphaChange.increment;
        			
	    if(alphaChange.increment>0)
	    {
	        if (alphaChange.finalOpacity == null)
	        {
	            alphaChange.finalOpacity = 156;
	        }

	        if (alphaChange.currentOpacity < alphaChange.finalOpacity)
		    {
		        obj = AlphaBlender.prototype.changeOpacity(obj, alphaChange.currentOpacity);
                obj.setAttribute("alphaChange",alphaChange);
			    window.setTimeout(function() { AlphaBlender.prototype.processAlphaBlending(obj); }, 10);
		    }
		    else
		    {
		        obj.setAttribute("alphaChange",null);
		        if(alphaChange.callbackFunction)
		        {
		            alphaChange.callbackFunction();
		        }
		    }
	    }
	    else
	    {
	        if (alphaChange.finalOpacity == null)
	        {
	            alphaChange.finalOpacity = 0;
	        }

	        if (alphaChange.currentOpacity > alphaChange.finalOpacity)
		    {
		        obj = AlphaBlender.prototype.changeOpacity(obj, alphaChange.currentOpacity);
		        obj.setAttribute("alphaChange",alphaChange);
		        window.setTimeout(function() { AlphaBlender.prototype.processAlphaBlending(obj); }, 10);
		    }
		    else
		    {
		        obj.setAttribute("alphaChange",null);
		        obj.style.display = "none";
			    if(alphaChange.callbackFunction)
		        {
			        alphaChange.callbackFunction();
			    }
		    }
	    }			
	    alphaChange=null;
	}
}

AlphaBlender.prototype.makeAlphaBlending=function(iCurrentOpacity,iIncrement,sIdObject,callbackFunction,iFinalOpacity)
{
    var object=document.getElementById(sIdObject);
    var alphaChange=new AlphaChange(iCurrentOpacity, iIncrement, iFinalOpacity, callbackFunction);
    object.setAttribute("alphaChange",alphaChange);
    AlphaBlender.prototype.processAlphaBlending(object);
}



AlphaBlender.prototype.getOpacity = function(obj)
{
    var iOpacity=-1;
    if(obj.style.opacity)
    {
        iOpacity=obj.style.opacity;
    }
    else
    {
        if(obj.style.MozOpacity)
        {
            iOpacity = obj.style.MozOpacity;
        }
        else
        {
            if(obj.style.KhtmlOpacity)
            {
                iOpacity = obj.style.KhtmlOpacity;
            }
        }
    }
    return(iOpacity);     
}
