
/*  common/js/MCounter.js  */
var counterdata;
function MCounter(options) {
    this['mcounter_' + options.cid] = {};
    var counter = this['mcounter_' + options.cid];
    counter.setCurrency = function () {
        $('.progress-currency' + counter.mckey + ', .progress-currency-no-key').each(function () {
            $(this).html(counter.currency_value);
        });
    };
    counter.setTarget = function () {
        var target = counter.number_format(counter.target, 0, '.', options.thousand_separator),
            h = '',
            k = '';
        $('.progress-target' + counter.mckey).each(function () {
            if (counter.is_target) {
                $(this).html(target);
            } else {
                $(this).html('');
            }
        });
        counter.text = counter.text.replace('{target}', target);
        if (counter.is_target) {
            if ('' !== counter.currency_value) {
                h = ' ';
            } else {
                k = ' ';
            }
            $('.progress-target-additional' + counter.mckey).html(k + counter.number_format(counter.target, 0, '.', options.thousand_separator));
            $('.progress-currency-target' + counter.mckey).html(h + counter.currency_value);
        }
    };
    counter.setText = function () {
        if (counter.in_array(counter.counter_type, ['countries_donate', 'donates'])) {
            counter.setAdditionalCurrency();
        }
        $('.progress-text' + counter.mckey).html(' ' + counter.text);

    };
    counter.setAdditionalCurrency = function () {
        if ('euro_dollar' === counter.currency) {
            $('.progress-additional-currency' + counter.mckey).html('($' + counter.number_format(counterdata.USD) + ')');
        } else if ('dollar_euro' === counter.currency) {
            $('.progress-additional-currency' + counter.mckey).html('(&euro;' + counter.number_format(counterdata.EUR) + ')');
        }
    };
    counter.setCountryCount = function () {
        if ('countries' === counter.counter_type) {
            $('.progress-country' + counter.mckey).html(' ' + counterdata.countries_cnt + ' ' + counter.country_text);
        } else if ('countries_donate' === counter.counter_type) {
            $('.progress-country' + counter.mckey).html(' ' + counterdata.donate_countries_cnt + ' ' + counter.country_text);
        }
    };
    counter.setData = function (num) {
        var togo = counter.target - num;
        if (counter.default_max > counter.target) {
            togo = counter.default_max - num;
        }
        $('.progress-data' + counter.mckey).each(function () {
            $(this).html(counter.number_format(num, 0, '.', options.thousand_separator));
        });

        $('.progress-data-nearby' + counter.mckey).each(function () {
            $(this).html(counter.number_format(counter.max_nearby, 0, '.', options.thousand_separator));
        });

        $('.progress-data-togo' + counter.mckey + ', .progress-data-togo-no-key').each(function () {
            if (0 > togo) {
                togo = 0;
            }
            $(this).html(counter.number_format(togo, 0, '.', options.thousand_separator)).change();
        });
        $('.progress-data-goal' + counter.mckey + ', .progress-data-goal-no-key').each(function () {
            $(this).html(counter.number_format(counter.target, 0, '.', options.thousand_separator)).change();
        });
    };
    counter.showCounter = function () {
        var px,
            iw;
        counter.setData(counter.num);
        if (counter.max > counter.default_max) {
            counter.default_max = parseInt(counter.max, 10);
            counter.proc = counter.progress_bar_width / counter.max;
        }
        px = parseInt(counter.proc * counter.num, 10);
        iw = $('.progress-inline' + counter.mckey).width();
        if (0 !== iw && (iw + 10) > px) {
            px = iw + 10;
        }
        $('.progress-move' + counter.mckey).width(px + 'px');
    };

    counter.showNearbyCounter = function () {
        var px,
            iw;
        counter.setData(counter.num);
        if (counter.nearby_max > counter.default_max) {
            counter.default_max = parseInt(counter.nearby_max, 10);
            counter.proc = counter.progress_bar_width / counter.nearby_max;
        }
        px = parseInt(counter.proc * counter.nearby_max, 10);
        iw = $('.progress-inline' + counter.mckey).width();
        if (0 !== iw && (iw + 10) > px) {
            px = iw + 10;
        }
        $('.progress-move' + counter.mckey).width(px + 'px');
    };

    counter.getSumm = function (nnm, nnm2) {
        var rand, timeout;
        if (nnm2 < 1) {
            rand = nnm + nnm2;
        } else if (nnm2 < 10) {
            rand = nnm + 1;
            timeout = 100;
        } 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 = 50;
        } else if (nnm2 < 10000) {
            rand = nnm + Math.floor((Math.random() * 901) + 100);
            timeout = 50;
        } else if (nnm2 < 100000) {
            rand = nnm + Math.floor((Math.random() * 2001) + 1000);
            timeout = 50;
        } else if (nnm2 < 1000000) {
            rand = nnm + Math.floor((Math.random() * 20001) + 5000);
            timeout = 50;
        } else if (nnm2 >= 1000000) {
            rand = nnm + Math.floor((Math.random() * nnm2 / 100) + nnm2 / 100);
            timeout = 50;
        } else {
            rand = nnm + counter.plus;
        }
        counter.timeout = timeout;
        return rand;
    };

    counter.startCounter = function () {
        var rand, num2;

        if (counter.franchise_cid && undefined != counter.franchise_cid){
            counter.showNearbyCounter();
        } else {
            counter.showCounter();
        }

        if (counter.timerId) {
            clearTimeout(counter.timerId);
        }
        if (counter.num < counter.max) {
            rand = counter.getSumm(counter.num, counter.max);
            if (rand > counter.max) {
                num2 = counter.max - counter.num;
                counter.num = counter.getSumm(counter.num, num2);
            } else {
                counter.num = rand;
            }
            counter.timerId = setTimeout(counter.startCounter, counter.timeout);
        } else if (true === counter.ajax_data) {
            counter.ajaxCounter();
        }

    };
    counter.addCommas = function (nStr) {
        var rgx, x, x1, x2;
        nStr += '';
        x = nStr.split('.');
        x1 = x[0];
        x2 = x.length > 1 ? '.' + x[1] : '';
        rgx = /(\d+)(\d{3})/;
        while (rgx.test(x1)) {
            x1 = x1.replace(rgx, '$1' + ',' + '$2');
        }
        return x1 + x2;
    };
    counter.ajaxCounter = function () {
        if (counter.ajax_counter_timer) {
            clearTimeout(counter.ajax_counter_timer);
        }
        $.ajax({
            url: '/act/ajax_counter.php?cid=' + counter.cid + '&type=' + counter.template +
                (undefined !== counter.petition_id ? '&petition_id=' + counter.petition_id : '') +
                (counter.franchise_cid && undefined !== counter.franchise_cid ? '&franchise_cid=' + counter.franchise_cid : '') +
                (counter.petition_latitude && undefined !== counter.petition_latitude ? '&petition_latitude=' + counter.petition_latitude : '') +
                (counter.petition_longitude && undefined !== counter.petition_longitude ? '&petition_longitude=' + counter.petition_longitude : '') +
                (counter.petition_distance && undefined !== counter.petition_distance ? '&distance=' + counter.petition_distance : ''),
            dataType: 'json',
            type: 'GET',
            success: function (json) {
                if (json && 0 == json.paused) {
                    switch (counter.counter_type) {
                    case 'countries_donate':
                    case 'donates':
                        if (counter.in_array(counter.currency, ['euro', 'euro_dollar'])) {
                            counter.max = json.EUR;
                        } else if (counter.in_array(counter.currency, ['other'])) {
                            counter.max = json.OTHER;
                        } else {
                            counter.max = json.USD;
                        }
                        counterdata.USD = json.USD;
                        counterdata.EUR = json.EUR;
                        counterdata.OTHER = json.OTHER;
                        counter.setAdditionalCurrency();
                        break;
                    case 'x_number':
                        counter.max = json.donations;
                        break;
                    case 'ct_x_unique_donors':
                        counter.max = json.donors;
                        break;

                    case 'sign':
                        counter.max = json.count;
                        if (undefined !== counter.franchise_cid){
                            counter.max_nearby = json.count_nearby;
                        }
                        break;
                    }
                }
                counter.ajax_data = false;
                counter.startCounter();
                counter.ajax_counter_timer = setTimeout(counter.ajaxCounter, 20000);
            }
        });
    };
    counter.in_array = function (needle, haystack, argStrict) {
        var key = '', strict = !!argStrict;
        if (strict) {
            for (key in haystack) {
                if (haystack[key] === needle) {
                    return true;
                }
            }
        } else {
            for (key in haystack) {
                if (haystack[key] == needle) {
                    return true;
                }
            }
        }
        return false;
    };
    counter.number_format = function (number, decimals, dec_point, thousands_sep) {
        var n = number, prec = decimals, toFixedFix, sep, dec, s, abs, v, i, decPos;

        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);
        sep = (undefined === thousands_sep) ? ',' : thousands_sep;
        dec = (undefined === dec_point) ? '.' : dec_point;

        s = (prec > 0) ? toFixedFix(n, prec) : toFixedFix(Math.round(n), prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;

        abs = toFixedFix(Math.abs(n), prec);

        if (abs >= 1000) {
            v = abs.split(/\D/);
            i = v[0].length % 3 || 3;

            v[0] = s.slice(0, i + (n < 0)) +
                v[0].slice(i).replace(/(\d{3})/g, sep + '$1');
            s = v.join(dec);
        } else {
            s = s.replace('.', dec);
        }

        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;
    };

    counter.cid = options.cid;
    counter.mckey = options.key;

    if (undefined !== options.petition_id) {
        counter.petition_id = options.petition_id;
    }

    counter.progress_bar_width = options.autodect_width ? $(options.progress_container).width() : options.max_width;
    counter.counter_type = options.subtype;
    counter.currency_value = options.current_currency_value;
    counter.currency = options.current_currency;
    counter.text = options.text;
    counter.country_text = options.country_text;
    counter.is_target = options.is_target;
    counter.franchise_cid = false;
    counter.petition_latitude = false;
    counter.petition_longitude = false;
    counter.petition_distance = false;

    if (options.franchise_cid && 'undefined' !== options.franchise_cid){
        counter.franchise_cid = options.franchise_cid;

        counter.petition_latitude  = options.petition_latitude;
        counter.petition_longitude = options.petition_longitude;
        counter.petition_distance = options.petition_distance;
    }

    counter.is_progress_target = ('undefined' !== typeof options.is_progress_target) ? options.is_progress_target : true;
    counter.template = '';

    if (undefined === window.counterdata) {
        window.counterdata = {target: 0, target_donate: 0, EUR: 0, OTHER: 0, USD: 0, sign: 0, sign_nearby: 0, donations: 0};
    }

    switch (counter.counter_type) {
    case 'donates':
        counter.target = counterdata.target_donate;
        if (counter.in_array(counter.currency, ['euro', 'euro_dollar'])) {
            counter.current_value = counterdata.EUR;
        } else if (counter.in_array(counter.currency, ['dollar', 'dollar_euro'])) {
            counter.current_value = counterdata.USD;
        } else if (counter.in_array(counter.currency, ['other'])) {
            counter.current_value = counterdata.OTHER;
        }
        counter.template = 'donates';
        break;
    case 'countries_donate':
        counter.target = counterdata.target_donate;
        if (counter.in_array(counter.currency, ['euro', 'euro_dollar'])) {
            counter.current_value = counterdata.EUR;
        } else if (counter.in_array(counter.currency, ['dollar', 'dollar_euro'])) {
            counter.current_value = counterdata.USD;
        }
        counter.template = 'donates';
        break;
    case 'sign':
        counter.currency_value = '';
        counter.target = counterdata.target;
        counter.current_value = counterdata.sign;
        if (false !== counter.franchise_cid){
            counter.current_nearby_value = counterdata.sign_nearby;
        }
        counter.template = 'sign';
        break;
    case 'x_number':
        counter.currency_value = '';
        counter.target = counterdata.target;
        counter.current_value = counterdata.donations;
        counter.template = 'donations';
        break;

    case 'ct_x_unique_donors':
        counter.currency_value = '';
        counter.target = counterdata.target;
        counter.current_value = counterdata.donors;
        counter.template = 'donors';
        break;

    case 'countries':
        counter.currency_value = '';
        counter.target = counterdata.target;
        counter.current_value = counterdata.sign;
        counter.template = 'sign';
        break;
    }

    counter.max2 = counter.target;
    counter.max = counter.current_value;
    counter.nearby_max = counter.current_nearby_value;
    counter.default_max = parseInt(counter.max2, 10);
    counter.num = 0;//arguments[5]?arguments[5]:0;
    counter.timerId = null;
    counter.plus = undefined !== options.plus ? options.plus : 1;
    counter.timeout = 0;
    counter.proc = counter.progress_bar_width / counter.max2;
    counter.ajax_counter_timer = null;
    counter.ajax_data = options.ajax;
    counter.aj_counter_flag = false;
    if (counter.max > counter.max2) {
        counter.proc = counter.progress_bar_width / counter.max;
        counter.default_max = parseInt(counter.max, 10);
    }
    counter.setText();
    counter.setCurrency();
    counter.setTarget();
    counter.setCountryCount();
    counter.startCounter();
}

/*  new/common/js/async_done.js  */
function async_load_function() {
    if (typeof(async_load) != 'undefined' && async_load) {
	var func = async_load.pop();
	while (func) {
	    func();
	    func = async_load.pop();
	};
    }
    setTimeout(async_load_function,1000);
};
async_load_function();
