﻿var Effects = {
    GetPosition : function(elem) {
        var xx = elem.offsetLeft, yy = elem.offsetTop;
        var parent = elem;
        while (parent = parent.offsetParent) {
            xx += parent.offsetLeft; yy += parent.offsetTop;
        }
        return {x: xx, y: yy};
    }
    ,IsIE : function() {
        if (navigator.userAgent.indexOf('MSIE') != -1) return true;
        return false;
    }
    ,GetWindowSize : function() {
        return { height: document.documentElement.clientHeight, width: document.documentElement.clientWidth };
    }
    ,SetAlpha : function(elem, alpha) {
        if (Effects.IsIE())
            elem.style.filter = 'alpha(opacity='+alpha+')';
        else
            elem.style.opacity = alpha/100;
    }
    ,GetAlpha : function(elem) {
        var alpha = '';
        if (Effects.IsIE()) {
            var filter = elem.style.filter;
            if (filter && filter.indexOf('opacity') != -1) {
                var opacity = filter.substr(filter.indexOf('opacity') + 8, filter.length);
                do {
                    var temp = Number(opacity.substr(0, 1));
                    opacity = opacity.substr(1, opacity.length);
                    if (temp == 0 || temp) alpha = alpha + '' + temp;
                }
                while(temp == 0 || temp)
            }
            else
                alpha = 100;
        }
        else {
            alpha = elem.style.opacity * 100;
        }
        return alpha;
    }
    ,FadeIn : function(elem, speedMS, beforeFadeMS, onFinishHandler) {
        if (beforeFadeMS) setTimeout(_fadeIn, beforeFadeMS); else _fadeIn();
        function _fadeIn() {
            var alpha = 0;
            var fadeInInt = setInterval(__fadeIn, speedMS);
            function __fadeIn() {
                Effects.SetAlpha(elem, alpha);
                if (alpha == 0) elem.style.display = 'block';
                if (alpha >= 100) { clearInterval(fadeInInt); Effects.SetAlpha(elem, 100); fadeInInt = null; if(onFinishHandler) onFinishHandler(); }
                alpha += 3;
            }
        }
    }
    ,FadeOut : function(elem, speedMS, beforeFadeMS, onFinishHandler) {
        if (beforeFadeMS > 0) setTimeout(_fadeOut, beforeFadeMS); else _fadeOut();
        function _fadeOut() {
            var alpha = 100;
            var fadeOutInt = setInterval(__fadeOut, speedMS);
            function __fadeOut() {
                Effects.SetAlpha(elem, alpha);
                if (alpha <= 0) { clearInterval(fadeOutInt); fadeOutInt = null; elem.style.display = 'none';  if(onFinishHandler) onFinishHandler(); }
                alpha -= 3;
            }
        }
    }
    ,SlideH : function(elem, step, speed, onStepHandler) {
        var _int = setInterval(_slide, speed);
        function _slide() {
            elem.style.left = (elem.style.left.replace('px','') - step) + 'px';
            if (onStepHandler) onStepHandler();
        }
        return _int;
    }
    ,SlideV : function(elem, step, speed, onStepHandler) {
        var _int = setInterval(_slide, speed);
        function _slide() {
            elem.style.top = (elem.style.top.replace('px','') - step) + 'px';
            if (onStepHandler) onStepHandler();
        }
        return _int;
    }
    ,SlideStop : function(intId) {
        if (intId) clearInterval(intId);
        intId = null;
        return intId; 
    }
}

function DebugMessage() {
    var divMsg = document.createElement('DIV');
    divMsg.style.backgroundColor = '#FFEEEE';
    divMsg.style.color = '#000000';
    divMsg.style.border = 'solid 1px #FFAAAA';
    divMsg.style.padding = '3px';
    divMsg.style.position = 'absolute';
    divMsg.style.right = '20px';
    divMsg.style.top = '10px';
    divMsg.style.display = 'none';
    document.body.appendChild(divMsg);
    var _int = null;
    this.print = function(msg) { 
        if (_int) { clearTimeout(_int); _int = null; }
        divMsg.style.display = 'block'; divMsg.innerHTML = msg; 
        clear();
    }
    function clear() { 
        _int = setTimeout(_clear, 700);
        function _clear() { divMsg.style.display = 'none'; divMsg.innerHTML = ''; }
    }
}