﻿var Utils = {
    IsPageLoaded : false,
    OnloadHandlers : [], OnloadSet : false,
    AddWinOnloadHandler : function(handler, argsArray) { 
        this.OnloadHandlers.push( {'handler' : handler, 'args' : argsArray } );
        if (!this.OnloadSet) {
            this.OnloadSet = true;
            var _winOnLoad = window.onload;
            window.onload = function() {
                if (_winOnLoad) _winOnLoad();
                run();
            }
        }
        var handlers = this.OnloadHandlers;
        function run() {
            for (var i=0; i<handlers.length; i++) {
                handlers[i].handler(handlers[i].args);
            }
        }
    },
    GetQuery : function() {
        var params = window.location.search.substring(1); if (!params || params == '') return;
        params = params.split('&');
        var obj = {};
        for (var i=0; i<params.length; i++) {
            var p = params[i].split('=');
            if (p.length == 2) obj[p[0]] = p[1];            
        }
        return obj;
    },
    IsIE : function() {
        if (navigator.userAgent.indexOf('MSIE') != -1) return true;
        return false;
    },
    ChangeAlpha : function(elem, alpha) {
        if (this.IsIE())
            elem.style.filter = 'alpha(opacity='+alpha+')';
        else
            elem.style.opacity = alpha/100;
    },
    GetAlpha : function(elem) {
        var alpha = 0;
        if (this.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 {
            if (elem.style.opacity)
                alpha = elem.style.opacity * 100;
            else
                alpha = 100;
        }
        return Number(alpha);
    },
    GetPosition : function(elem) { 
        if (!elem) return null;
        var xx=elem.offsetLeft, 
            yy=elem.offsetTop, 
            offsetElem = elem.offsetParent; 
        while(offsetElem) {
            xx += offsetElem.offsetLeft;
            yy += offsetElem.offsetTop;
            offsetElem = offsetElem.offsetParent;
        }
        return {left:xx, top:yy};
    },
    FadeMgr : function(msToFadeObj) {
        var _interval = 10;
        msToFadeObj = (msToFadeObj <= 0) ? 10 : msToFadeObj;
        var _step = (100*_interval/msToFadeObj<1)?1:100*_interval/msToFadeObj;
        var IntervalsIn = [];
        var IntervalsOut = [];
        this.FadeOut = function(elem) { 
            if (IntervalsOut[elem.id]) return; 
            IntervalsOut[elem.id] = setInterval(fade, _interval);
            var _intervalsOut = IntervalsOut;
            function fade() {
                var alpha = Utils.GetAlpha(elem) - _step;
                if (alpha <= 0) { 
                    elem.style.display = 'none';
                    clearInterval(_intervalsOut[elem.id]); 
                    _intervalsOut[elem.id] = null;
                }
                else {
                    elem.style.display = 'block';
                    Utils.ChangeAlpha(elem, alpha);
                }
            }   
        }
        this.FadeIn = function(elem) { 
            if (IntervalsIn[elem.id]) return; 
            Utils.ChangeAlpha(elem, 0);
            IntervalsIn[elem.id] = setInterval(fade, _interval);
            var _intervalsIn = IntervalsIn;
            function fade() {
                var alpha = Utils.GetAlpha(elem) + _step;
                if (alpha >= 100) { 
                    elem.style.display = 'block';
                    clearInterval(_intervalsIn[elem.id]); 
                    _intervalsIn[elem.id] = null;
                }
                else {
                    elem.style.display = 'block';
                    Utils.ChangeAlpha(elem, alpha);
                }
            }   
        }
        this.Show = function(elem) {
            cancelFade(elem);
            elem.style.display = 'block';
            Utils.ChangeAlpha(elem, 100);
        }
        this.Hide = function(elem) {
            cancelFade(elem);
            elem.style.display = 'none';
            Utils.ChangeAlpha(elem, 100);
        }
        this.FadeStop = function(elem) { 
            cancelFade(elem);
        }
        function cancelFade(elem) { 
            if (IntervalsIn[elem.id]) { 
                clearInterval(IntervalsIn[elem.id]); 
                IntervalsIn[elem.id] = null;
            }
            if (IntervalsOut[elem.id]) { 
                clearInterval(IntervalsOut[elem.id]); 
                IntervalsOut[elem.id] = null;
            }
        }
    }
}
Utils.AddWinOnloadHandler(function() { Utils.IsPageLoaded = true; });

//Utils.AddWinOnloadHandler(function(){debugMessage = new DebugMessage();});
var debugMessage = null;
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';
    divMsg.style.zIndex = 10;
    document.body.appendChild(divMsg);
    var _int = null;
    this.print = function(msg, leaveMsg) { 
        if (_int) { clearTimeout(_int); _int = null; }
        divMsg.style.display = 'block'; divMsg.innerHTML = msg; 
        if (!leaveMsg) clear();
    }
    this.printObj = function(obj, leaveMsg) {
        var msg = '';
        if (typeof(obj) == 'object') {
            var sO = '<span style="font-size:8px;">';
            var sC = '</span>';
            for (var p in obj)  
                msg += p + sO + '(' + typeof(p) + ')' + sC + ':' + obj[p] + sO + '(' + typeof(obj[p]) + ')' + sC + '<br/>';
        }
        else
           msg = obj; 
        this.print(msg, leaveMsg);
    }
    function clear() { 
        _int = setTimeout(_clear, 1000);
        function _clear() { divMsg.style.display = 'none'; divMsg.innerHTML = ''; }
    }
}
