var captions = {
    16: 'ont envoyé le message',
    36: 'mensagens enviadas ',
    11: 'Mensajes enviados',
    6: 'Ergebnisse der Petition'
	//,	
    //'=6': "have signed the petition"
};

	
function counter(target, number, progress, vel) {
     if (document.getElementById(target)) {
		document.getElementById(target).innerHTML = addCommas(progress + vel);
		if (progress + vel < number) {
			setTimeout('counter("' + target + '", ' + number + ', ' + (progress + vel) + ', ' + Math.round(vel * 1.25) + ');', 75);
		} else {
	
				document.getElementById(target).innerHTML = addCommas(number);			
		}
	}
}

event_add(window, 'load', function() {
	if (document.getElementById('signers_caption')) {
		http({
		   // url: 'moveon-count.php',
			// url: 'newCount.cfm',
			//url: 'count.php',
			url: 'count.txt',
			callback: function(response) {
				var num = response.responseText.match(/document\.write\('([^']+)'\)/);
				var moveon_num = parseInt(num[1].replace(/,/, ''));
				var id = location.href.substr(location.href.length - 2, 2);

				if (captions[id]) {
					document.getElementById('signers_caption').innerHTML = captions[id];
				}
				document.getElementById('signers_caption').style.display = 'block';
			   
				counter('signers', moveon_num + 300, 0, 11);
			}
		});
	}
	
});

// Thanks to http://www.mredkj.com/javascript/numberFormat.html
function addCommas(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;
}


function http(config) {
    
    if (window.XMLHttpRequest) {
        var request = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        var request = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        return false;
    }
    
    if (!config) {
        return true;
    }
    
    if (!config.url && config.form) {
        config.url = config.form.getAttribute('action');
    } else if (!config.url) {
        return true;
    }
    
    
    if (!config.method) {
        if (config.form && config.form.getAttribute('method')) {
            config.method = config.form.getAttribute('method').toUpperCase();
        } else if (config.form) {
            config.method = 'GET';
        } else if (!config.content) {
            config.method = 'GET';
        } else {
            config.method = 'POST';
        }
    }
    
    if (!config.async && config.callback) {
        config.async = true;
    } else if (!config.async) {
        config.async = false;
    }
    
    if (!config.user_agent) {
        config.user_agent = 'XMLHttpRequest';
    }
    
    if (config.mime_type) {
        request.overrideMimeType(config.mime_type);
    }
    
    if (config.form) {
        
        if (config.content == null) {
            config.content = '';
        } else {
            config.content += '&';
        }
        
        var inputs = config.form.getElementsByTagName('input');
        for (var i = 0; i < inputs.length; i++) { 
            var type = inputs[i].getAttribute('type');
            var name = inputs[i].getAttribute('name');
            if (type == 'text' || type == 'password' || type == 'hidden') {
                config.content += name + '=' + escape(inputs[i].value) + '&';
            } else if (type == 'radio' && inputs[i].checked) {
                config.content += name + '=' + escape(inputs[i].value) + '&';
            } else if (type == 'checkbox' && inputs[i].checked) {
                if (!inputs[i].value) {
                    inputs[i].value = '1';
                }
                config.content += name + '=' + inputs[i].value + '&';
            }
        }
        
        var selects = config.form.getElementsByTagName('select');
        for (var i = 0; i < selects.length; i++) {
            var name = selects[i].getAttribute('name');
            var value = selects[i].options[selects[i].selectedIndex].value;
            config.content += name + '=' + escape(value) + '&';
        }
        
        var textareas = config.form.getElementsByTagName('textarea');
        for (var i = 0; i < textareas.length; i++) {
            var name = textareas[i].getAttribute('name');
            config.content += name + '=' + escape(textareas[i].value) + '&';
        }
        
        if (config.content.length > 0 &&
            config.content.lastIndexOf('&') == config.content.length - 1) {
            config.content = config.content.substr(0, config.content.length - 1);
        }
        
    }
    
    if (config.method == 'GET' && config.form) {
        if (config.url.indexOf('?') == -1) {
            config.url += '?';
        }
        config.url += config.content;
        config.content = null;
    }
    
    window.http_arguments = http.arguments;
    
    if (!config.proxy) {
        request.open(config.method, config.url, config.async);
    } else {
        request.open(config.method,
                     config.proxy.replace(/\$url/, escape(config.url)),
                     config.async);
    }
    
    if (config.async) {
        request.onreadystatechange = function() {
            if (request.readyState == 4) {
                var args = '';
                for (var i = 1; i < window.http_arguments.length; i++) {
                    args += ', window.http_arguments[' + i + ']';
                }
                eval('config.callback(request' + args + ');');
            }
        }
    }
    
    request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    request.setRequestHeader('Referer', window.location);
    request.setRequestHeader('User-Agent', config.user_agent);
    
    if (config.headers) {
        for (header in config.headers) {
            request.setRequestHeader(header, config.headers[header]);
        }
    }
    
    request.send(config.content);
    
    if (!config.async) {
        return request;
    }
}
function find_pos(target) {
    // Thanks to PPK & Quirksmode
	var x = 0;
    var y = 0;
	while (target.offsetParent) {
		x += target.offsetLeft;
        y += target.offsetTop;
		target = target.offsetParent;
    }
	return [x, y];
}

function window_dimensions() {
    // Thanks to PPK & Quirksmode
    if (self.innerWidth) {
		w = self.innerWidth;
		h = self.innerHeight;
	} else if (document.documentElement &&
               document.documentElement.clientWidth) {
		w = document.documentElement.clientWidth;
		h = document.documentElement.clientHeight;
	}
	else if (document.body) {
		w = document.body.clientWidth;
		h = document.body.clientHeight;
	}
    return [w, h];
}

function event_pos(e) {
    // Thanks to PPK & Quirksmode
    var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY)
	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		posx = e.clientX + document.body.scrollLeft;
		posy = e.clientY + document.body.scrollTop;
	}
    return [posx, posy];
}


function event_add(target, type, func) {
    // Thanks to John Resig
    if (target.attachEvent) {
        target['e' + type + func] = func;
        target[type + func] = function() {
            target['e' + type + func](window.event);
        }
        target.attachEvent('on' + type, target[type + func]);
    } else {
        target.addEventListener(type, func, false);
    }
}

function event_remove(target, type, func) {
    // Thanks to John Resig
    if (target.detachEvent) {
        target.detachEvent('on' + type, target[type + func]);
        target[type + func] = null;
    } else {
        target.removeEventListener(type, func, false);
    }
}

function set_opacity(target, value) {
    target.style.opacity = value;
    target.style.filter = 'alpha(opacity=' + parseInt(100 * value) + ')';
}
