//LD.SplashOverlay
LD.SO = {
    active: false,
    showProgressDisplayInterval: false,
    progressBegan: false,
    countWaiting: 0,
    runningProgressStream: [],
    
    check: function() {
        if(LD.BR.finished) {
            LD.SplashBox.allowClose = true;
            LD.SplashBox.close();
            return;
        }
        
        if(LD.BR.started || this.active) {
            this.progressBegan = true;
        }
        
        if(this.checkSplash() && !this.progressBegan) {
            this.countWaiting++;
            LD.SO.showProgressUser("Please wait(" + this.countWaiting + "s ago)...");
            setTimeout("LD.SO.check();", 1000);
        }
        else if(this.countWaiting > 0) {
            this.countWaiting = 0;
        }
    },
    
    checkSplash: function() {
        if(LD.ModalBoxes.instances > 0) {
            this.active = true;
            if(!this.showProgressDisplayInterval) {
                this.showProgressDisplayInterval = setInterval('LD.SO.showProgressDisplay()', 1200);
                setTimeout("LD.SO.showProgressDisplay()", 800);
            }
            return true;
        }
        return false;
    },
    
    update: function(params) {
        if(!this.active && !this.checkSplash()) {
            return false;
        }
        $.each(params, function(key, value){
            if(key == 'timer') {
                if(value != 10000) {
                    var pp = value.split('.');
                    $('#' + key).html('approximately ' + pp[0] + ':' + LD.SO.zeroPad(LD.SO.numberFormat((pp[1] * 0.60), 0), 2) + ' minutes left');
                }
            }
            else {
                if(typeof $('#' + key) !== undefined) {
                    $('#' + key).html(value);
                }
            }
        });
    },
    
    setProgress: function(perc) {
        if(!this.active && !this.checkSplash()) {
            return false;
        }
        
        this.progressBegan = true;
        
        try {
            document["LD_SplashDisplayStatusBar"].updatePercent(perc);
        }
        catch(e) { 
            if(perc > 45) {
                $('#flash_div').html(perc + '%');
            }
        }
        
        
        if(perc >= 34 && perc <= 35 && !LD.BR.Generate.used) {
            this.countWaiting++;
            if(this.countWaiting > 2) {
                this.showProgressUser("Waiting for all expected backlinks to return with proper addresses.. Waiting.." + this.countWaiting + "s..");
            }
        }
    },
    
    feedProgressUser: function(arr) {
        this.runningProgressStream.reverse();
        this.runningProgressStream = this.runningProgressStream.concat(arr);
        this.runningProgressStream.reverse();
        this.runningProgressStream = this.runningProgressStream.slice(0,10);
    },
    
    showProgressUser: function(str) {
        var strsize = this.runningProgressStream.length;
        if(strsize > 10) {
            this.runningProgressStream.pop();
        }
        this.runningProgressStream.unshift(str);
    },
    
    showProgressDisplay: function() {
        if(!this.checkSplash()) {
            clearInterval(this.showProgressDisplayInterval);
            return false;
        }
        
        $('#status_output').html(this.runningProgressStream.join("<br />" + "\n"));
    },
    
    numberFormat: function(number, decimals, dec_point, thousands_sep) {
        var n = number, prec = decimals;
    
        var toFixedFix = function (n,prec) {
            var k = Math.pow(10,prec);
            return (Math.round(n*k)/k).toString();
        };
    
        n = !isFinite(+n) ? 0 : +n;
        prec = !isFinite(+prec) ? 0 : Math.abs(prec);
        var sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
        var dec = (typeof dec_point === 'undefined') ? '.' : dec_point;
    
        var s = (prec > 0) ? toFixedFix(n, prec) : toFixedFix(Math.round(n), prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;
    
        var abs = toFixedFix(Math.abs(n), prec);
        var _, i;
    
        if (abs >= 1000) {
            _ = abs.split(/\D/);
            i = _[0].length % 3 || 3;
    
            _[0] = s.slice(0,i + (n < 0)) +
                  _[0].slice(i).replace(/(\d{3})/g, sep+'$1');
            s = _.join(dec);
        } else {
            s = s.replace('.', dec);
        }
    
        var decPos = s.indexOf(dec);
        if (prec >= 1 && decPos !== -1 && (s.length-decPos-1) < prec) {
            s += new Array(prec-(s.length-decPos-1)).join(0)+'0';
        }
        else if (prec >= 1 && decPos === -1) {
            s += dec+new Array(prec).join(0)+'0';
        }
        return s;
    },
    zeroPad: function(num,count) {
        var numZeropad = num + '';
        while(numZeropad.length < count) {    
            numZeropad = "0" + numZeropad; 
        }
        return numZeropad;
    }
};
