﻿// http://www.google.com.ua/search?client=opera&rls=ru&q=setTimeout+with+opacity&sourceid=opera&ie=utf-8&oe=utf-8
// setTimeout with opacity

// constants
var opacityIE = 'filter';
var opacityMoz = 'MozOpacity';
var opacityKhtml = 'KhtmlOpacity';
var opacityOther = 'opacity';

var interval = 25;
var step = 5;
var iTimerID;

function fadeIn(objId)
{    
    if (document.getElementById)
    {
        //window.clearTimeout(iTimerID);
        var obj = document.getElementById(objId);
        var html = document.getElementsByTagName("html");
        if (html)
        {
            //alert(html.item(0).clientHeight);
            obj.top = html.item(0).clientHeight;
            obj.left = html.item(0).clientWidth;
        }
        var opacity = getOpacity(obj);
        if (opacity < 100)
        {
            if (opacity == 0)
            {
                obj.style.display = "block";
            }
            opacity += step;
            //		    var h = obj.style.height == 0 ? 0 : getHeight (obj);		       
            //		    h += step;		    
            //		    setHeight (obj, h);		    
            setOpacity(obj, opacity);
            iTimerID = window.setTimeout("fadeIn('" + objId + "')", interval);
        }
    }
}

function setHeight(obj, height)
{
    obj.style.height = height + "px";
    //alert (obj.style.height);
}

function getHeight(obj)
{
    return parseInt(obj.style.height.toString().substring(0, obj.style.height.toString().indexOf('px')));

}

function fadeOut(objId)
{
    if (document.getElementById)
    {
        //window.clearTimeout(iTimerID);        
        var obj = document.getElementById(objId);
        var opacity = getOpacity(obj);
        if (opacity > 0)
        {
            opacity -= step;
            //           var h = obj.style.height == 0 ? 0 : getHeight (obj);		       
            //		   h -= step;		    
            //		   setHeight (obj, h);	          
            setOpacity(obj, opacity);
            iTimerID = window.setTimeout("fadeOut('" + objId + "')", interval);
        }
        else
        {
            obj.style.display = "none";
        }
    }
}

function getOpacityProperty()
{
    if (typeof document.body.style.opacity == 'string') // CSS3 compliant (Moz 1.7+, Safari 1.2+, Opera 9)
        return opacityOther;
    else if (typeof document.body.style.MozOpacity == 'string') // Mozilla 1.6 и младше, Firefox 0.8 
        return opacityMoz;
    else if (typeof document.body.style.KhtmlOpacity == 'string') // Konqueror 3.1, Safari 1.1
        return opacityKhtml;
    else if (document.body.filters && navigator.appVersion.match(/MSIE ([\d.]+);/)[1] >= 5.5) // Internet Exploder 5.5+
        return opacityIE;

    return false; //нет прозрачности
}

function setOpacity(obj, opacity)
{
    //opacity = (opacity == 100)?99.999:opacity;
    var opacityProp = getOpacityProperty();
    // IE/Win
    if (opacityProp == "filter")
    {
        obj.style.filter = "alpha(opacity:" + opacity + ")";
    }
    // Safari<1.2, Konqueror
    else if (opacityProp == "KhtmlOpacity")
    {
        obj.style.KHTMLOpacity = opacity / 100;
    }
    // Older Mozilla and Firefox
    else if (opacityProp == "MozOpacity")
    {
        obj.style.MozOpacity = opacity / 100;
    }
    // Safari 1.2, newer Firefox and Mozilla, CSS3
    else if (opacityProp == "opacity")
    {
        obj.style.opacity = opacity / 100;
    }
}

function getOpacity(obj)
{
    var opacityProp = getOpacityProperty();
    //alert (opacityProp);
    // IE/Win
    if (opacityProp == "filter")
    {
        var opacityIE = obj.style.filter.match(/\d+/ig);
        if ((opacityIE != null) && (opacityIE.length > 0))
        {
            return parseInt(opacityIE[0]);
        }
        else
        {
            return 0;
        }
    }
    // Safari<1.2, Konqueror
    else if (opacityProp == "KhtmlOpacity")
    {
        //alert (obj.style.KHTMLOpacity);
        return obj.style.KHTMLOpacity * 100;
    }
    // Older Mozilla and Firefox
    else if (opacityProp == "MozOpacity")
    {
        //alert (obj.style.MozOpacity);
        return obj.style.MozOpacity * 100;
    }
    // Safari 1.2, newer Firefox and Mozilla, CSS3
    else if (opacityProp == "opacity")
    {
        //alert (obj.style.opacity * 100);
        return obj.style.opacity * 100;
    }
}
