﻿function DCC_FadingAPI_find(id) {
    var elms = jQuery("*");
    if (elms == null) return;
    for (var i = 0; i < elms.length; i++) {
        var elm = elms[i];
        if (jQuery(elm).attr("uniqueID") == id)
            return elm;
    }
}
function DCC_FadingAPI_animateFade(lastTick, timeLeftToFade, id) {
    var elm = DCC_FadingAPI_find(id);
    if (elm == null) return;
    var curTick = new Date().getTime();
    var elapsedTicks = curTick - lastTick;
    var elememt = document.getElementById(id);
    if (elm.FadeTimeLeft <= elapsedTicks) {
        elm.style.opacity = elm.FadeState == 1 ? '1' : '0';
        elm.style.filter = 'alpha(opacity = ' + (elm.FadeState == 1 ? '100' : '0') + ')';
        elm.FadeState = elm.FadeState == 1 ? 2 : -2;
        if (elm.callback)
            return elm.callback();
        return;
    }
    elm.FadeTimeLeft -= elapsedTicks;
    var newOpVal = elm.FadeTimeLeft / timeLeftToFade;
    if (elm.FadeState == 1) newOpVal = 1 - newOpVal;
    elm.style.opacity = newOpVal;
    elm.style.filter = 'alpha(opacity = ' + (newOpVal * 100) + ')';
    setTimeout("DCC_FadingAPI_animateFade(" + curTick + "," + timeLeftToFade + ",'" + id + "')", 33);
}
(function($) {
    function DCC_FadingAPI(elmt, time, callback) {
        var obj = {
            getTime: function(parm) {
                var value = parseInt(parm);
                if (value >= 0) {
                    return value;
                }
                switch (parm) {
                    case 'fast':
                        return 200;
                        break;
                    case 'normal':
                        return 400;
                        break;
                    case 'slow':
                        return 600;
                        break;
                    default:
                        return 400;
                }
            },
            fadein: function() {
                var elm = this.elem;
                if (elm == null) return;
                if (!elm.uniqueID)
                    elm.uniqueID = (new Date()).getTime();
                elm.FadeState = 1;
                elm.FadeTimeLeft = this.TimeToFade;
                elm.callback = obj.callback;
                setTimeout("DCC_FadingAPI_animateFade(" + new Date().getTime() + "," + this.TimeToFade + ",'" + $(elm).attr("uniqueID") + "')", 33);
            },
            fadeout: function() {
                var elm = this.elem;
                if (elm == null) return;
                if (!elm.uniqueID)
                    elm.uniqueID = (new Date()).getTime();
                elm.FadeState = -1;
                elm.FadeTimeLeft = this.TimeToFade;
                elm.callback = obj.callback;
                setTimeout("DCC_FadingAPI_animateFade(" + new Date().getTime() + "," + this.TimeToFade + ",'" + $(elm).attr("uniqueID") + "')", 33);
            }
        };
        obj.elem = elmt;
        obj.TimeToFade = obj.getTime(time);
        obj.callback = callback;
        return obj;
    }
    $.fn.DCCFadeIn = function(time, callback) {
        return this.each(function() {
            var fapi = new DCC_FadingAPI(this, time, callback);
            fapi.fadein();
            return this;
        });
    };
    $.fn.DCCFadeOut = function(time, callback) {
        return this.each(function() {
            var fapi = new DCC_FadingAPI(this, time, callback);
            fapi.fadeout();
            return this;
        });
    };
})(jQuery);
