
function FrontCounter(id,countt, type, cid, undef) {
    type = (type === undef ? 'total' : type);
    this['frontcounter_' + type] = {};
    var fc  = this['frontcounter_' + type];
    fc.id   = id;
    fc.type = type;
    fc.cid = (cid === undef ? 0 : parseInt(cid));
    fc.num  = 0;
    fc.max  = countt;
    fc.timerId;
    fc.plus = 1;
    fc.timeout = 0;
    fc.first_ajax = true;
    fc.ajax_counter_timer;

    fc.showCounter = function() {
        document.getElementById(fc.id).innerHTML = '' + fc.addCommas(fc.num);
    }
    
	fc.getSumm = function (nnm, nnm2){
        var rand;

        if(nnm2<10){
            rand = nnm + 1
            timeout = 1200;
        }

        else if(nnm2<100){
            rand = nnm + Math.floor((Math.random()*9)+1);
            timeout = 0;
        }

        else if(nnm2<1000){
            rand  = nnm + Math.floor((Math.random()*91)+10);
            timeout = 100;
        }

        else if(nnm2 < 10000){
            rand  = nnm + Math.floor((Math.random()*901)+100);
            timeout = 100;
        }

        else if(nnm2 < 100000){
            rand  = nnm + Math.floor((Math.random()*2001)+1000);
            timeout = 100;
        }

        else if(nnm2 < 1000000){
            rand  = nnm + Math.floor((Math.random()*10001)+1000);
            timeout = 100;
        }

        else if(nnm2 <= 100000000){
            rand  = nnm + Math.floor((Math.random()*100000)+10000);
            timeout = 100;
        }

        else{
            rand = nnm + fc.plus;
        }

        return rand;

    }



    fc.startCounter = function(){

        var rand;

        if(fc.timerId){
            clearTimeout(fc.timerId);
        }

        if (fc.num <= fc.max){
            fc.showCounter();
            rand = fc.getSumm(fc.num, fc.max);

            if(rand > fc.max){
                var num2 = fc.max - fc.num;
                fc.num = fc.getSumm(fc.num, num2);
            }

            else{
                fc.num = rand;
            }

            if (timeout > 10){
				fc.timerId = setTimeout(fc.startCounter, timeout);
			}
			else {

			 fc.timerId = false;

			 fc.startCounter();

			}

            fc.timerId = setTimeout(fc.startCounter, timeout);

        } else if (fc.first_ajax) {

            fc.ajaxCounter();

        }



    }

    fc.addCommas = function(nStr)

    {
		/*
        nStr += '';

        x = nStr.split('.');

        x1 = x[0];

        x2 = x.length > 1 ? '.' + x[1] : '';

        var rgx = /(\d+)(\d{3})/;

        while (rgx.test(x1)) {

            x1 = x1.replace(rgx, '$1' + ',' + '$2');

        }

        return x1 + x2;
		*/
		return nStr;
    }

    fc.ajaxCounter = function() {

        if(fc.ajax_counter_timer) {

            clearTimeout(fc.ajax_counter_timer);

        }
		/*
        $.ajax({

            url: '/stat/act/ajax_counter.php/type=' + fc.type + (fc.cid ? '&cid=' + fc.cid : '') + (fc.lang ? '&lang=' + fc.lang : ''),

            dataType: 'json',

            type: 'GET',

            success: function (json) {

                var slowdown = false;

                if (json) {

                    if (0 == json.paused) {

                        if(Math.abs(fc.max - json.total) < 10) {

                            slowdown = true;

                        }

                        fc.max = json.total;

                    }

                }

                fc.first_ajax = false;

                fc.startCounter();

                fc.ajax_counter_timer = setTimeout(fc.ajaxCounter, slowdown ? 40000 : 20000);

            }

        });
		*/

    };

    fc.startCounter();

}
