var mouse_left = false;
var mouse_right = false;
var mouse_middle = false;
var IE = document.all ? true : false;

function sprintf () {
	var format = arguments[0];
	var pattern = /%(s|d)/;
	var match = format.search(pattern);
	var count = 1;
	while (match >= 0) {
		var key = format.substr(match, 2);
		var value = arguments[count];
		if (key == '%d') { value = parseInt(value); }
		else if (key == '%s') { value = value.toString(); }
		format = format.substr(0, match) + value + format.substr(match+2);
		match = format.search(pattern);
		count++;
	}
	return format;
}

function getEvent (e) {
	return (typeof e == 'undefined') ? window.event : e;
}   
    
function getTarget (e) {
	var target;
	if (e.target) { target = e.target; }
	else if (e.srcElement) { target = e.srcElement; }
	else if (target && target.nodeType == 3) { target = target.parentNode; }
	return target;
}

function getMouseClick (e) {
	// button 1 (left), button 2 (middle), button 3 (right)
	var button = 0;
	if (e.which) { button = e.which; }
	else {
		if (e.button == 2) { button = 3; }
		else if (e.button == 4) { button = 2; }
		else { button = e.button; }
	}

	if (button == 1) { mouse_left = true; }
	else if (button == 2) { mouse_middle = true; }
	else if (button == 3) { mouse_right = true; }
}

function setMouseClear () {
	mouse_left = false;
	mouse_middle = false;
	mouse_right = false;
}

function screenWidth () {
	return document.all ? document.body.offsetWidth : window.innerWidth;
}

function screenHeight () {
	return document.all ? document.body.offsetHeight : window.innerHeight;
}

function removeChildren (obj) {
	while (obj.firstChild) { obj.removeChild(obj.firstChild); }
}

function sec2time (seconds) {
	var min = Math.floor(seconds / 60);
	var sec = seconds % 60;
	if (min < 10) { min = '0' + min; }
	if (sec < 10) { sec = '0' + sec; }
	return '' + min + ':' + sec;
}

function stripWhiteSpace (node) {
	if (!node) { return; }
	for (var i=0; i<node.childNodes.length; i++) {
		// Stripping the white spaces and repeat for each sub node
		if (node.childNodes[i].nodeName == '#text' && node.childNodes[i].nodeValue.match(/^\s*$/)) {
			node.removeChild(node.childNodes[i]);
		}
		stripWhiteSpace(node.childNodes[i]);
	} 
} 

function backToMain () {
	this.location = '/index.php';
}

function createGame () {
	this.location = '/game_create.php';
}

function refreshList () {
	this.location = '/game_join.php';
}

