
/**
 * Serialize a list within <ul> or <ol> tags
 * @param	string	the base list element
 * @param	char	the joining caracter
 * @return	string	the serialized list
 */
function serializeList(source, join_char) {
	source = document.getElementById(source);
	if (!source)	return;
	var items = source.getElementsByTagName("li");
	var array = new Array();
	for (var i = 0, n = items.length; i < n; i++) {
		var item = items[i];

		array.push(ToolMan.junkdrawer()._identifier(item));
	}
	return array.join(join_char);
}


/**
 * Get a random integer between 0 and n-1
 * @param	int		max value obtained at random
 * @return	int		the random integer
 */
function random(n) {
	return (Math.floor(Math.random() * n));
}


/**
 * Transform numbers between decimal and hexadecimal format
 * @param	number	imput number
 * @return	mixed	converted number
 */
function dec2hex(d) {return d.toString(16);}
function hex2dec(h) {return parseInt(h,16);}


var STR_PAD_LEFT = 1;
var STR_PAD_RIGHT = 2;
var STR_PAD_BOTH = 3;
 
/**
 * Pad a string to the given length
 * @param	string	original string
 * @param	int		desired length
 * @param	string	character(s) to pad with
 * @param	const	padding direction
 * @return	string	modified string
 */
function str_pad(str, len, pad, dir) {
	if (typeof(len) == "undefined")	{	var len = 0;	}
	if (typeof(pad) == "undefined")	{	var pad = ' ';	}
	if (typeof(dir) == "undefined")	{	var dir = STR_PAD_RIGHT;	}
 
	if (len + 1 >= str.length) {
 		//Select the appropriate padding direction
		switch (dir) {
			case STR_PAD_LEFT:
				str = Array(len + 1 - str.length).join(pad) + str;
			break;
 
			case STR_PAD_BOTH:
				var RIGHT = Math.ceil((padlen = len - str.length) / 2);
				var left = padlen - right;
				str = Array(left+1).join(pad) + str + Array(right+1).join(pad);
			break;
 
			default:
				str = str + Array(len + 1 - str.length).join(pad);
			break;
		}
	}
 
	return str;
}


//Originally from Peter-Paul Koch, http://www.quirksmode.org/
//Returns the Internet Time at the given precision
function get_beat(precision) {
	var now = new Date();
	var offset = (now.getTimezoneOffset() + 60) * 60;
	var seconds = (now.getHours() * 3600) + (now.getMinutes() * 60) + now.getSeconds() + offset;
	var beat = Math.floor(Math.pow(10, precision) * seconds / 86.4) / Math.pow(10, precision);
	
	//Limit the beat between 0 and 999
	if (beat > 999) beat -= 1000;
	if (beat < 0) beat += 1000;
	
	beat = beat.toFixed(precision);
	
	//Pad the string as appropriate
	if (precision > 0)
		beat_length = 3 + 1 + precision;
	else
		beat_length = 3;
	beat = str_pad(beat, beat_length, '0', STR_PAD_LEFT);
	
	return beat;
}


/*****************
* Test functions *
*****************/

//Get a reference to a specific object
function getObject(obj) {
	var theObj;
	if (document.all) {
		if (typeof obj=='string') {
			return document.all(obj);
		} else {
			return obj.style;
		}
	}
	if (document.getElementById) {
		if (typeof obj=='string') {
			return document.getElementById(obj);
		} else {
			return obj.style;
		}
	}
	return null;
}

//Catch when Enter is hit in a form and call another function for processing
function catchEnterKey(callFunction, e) {
	var keycode;
	
	//Get the keycode based on each browser
	if (window.event) {
		keycode = window.event.keyCode;
	} else if (e) {
		keycode = e.which;
	} else {
		return true;
	}

	//If Enter was pressed, call the wanted function
	if (keycode == 13) {
		eval(callFunction);
		return false;
	}

	return true;
}

//Display the key hit
function displayKeyCode(evt) {
	var textBox = document.getElementById('txtChar');
	var charCode = (evt.which) ? evt.which : event.keyCode
	textBox.value = String.fromCharCode(charCode);
	switch (charCode) {
		case 8:		textBox.value = "backspace";	break; //  backspace
		case 9:		textBox.value = "tab";		break; //  tab
		case 13:	textBox.value = "enter";	break; //  enter
		case 16:	textBox.value = "shift";	break; //  shift
		case 17:	textBox.value = "ctrl";		break;  //  ctrl
		case 18:	textBox.value = "alt";		break;  //  alt
		case 19:	textBox.value = "pause/break";	break;  //  pause/break
		case 20:	textBox.value = "caps lock";	break;  //  caps lock
		case 27:	textBox.value = "escape";	break;  //  escape
		case 32:	textBox.value = "space";	break;  //  space
		case 33:	textBox.value = "page up";	break;  // page up, to avoid displaying alternate character and confusing people	         
		case 34:	textBox.value = "page down";	break;  // page down
		case 35:	textBox.value = "end";		break;  // end
		case 36:	textBox.value = "home";		break;  // home
		case 37:	textBox.value = "left arrow";	break;  // left arrow
		case 38:	textBox.value = "up arrow";	break;  // up arrow
		case 39:	textBox.value = "right arrow";	break;  // right arrow
		case 40:	textBox.value = "down arrow";	break;  // down arrow
		case 45:	textBox.value = "insert";	break;  // insert
		case 46:	textBox.value = "delete";	break;  // delete
		case 91:	textBox.value = "left windows";	break;  // left windows
		case 92:	textBox.value = "right windows";	break;  // right windows
		case 93:	textBox.value = "select key";	break;  // select key
		case 96:	textBox.value = "numpad 0";	break;  // numpad 0
		case 97:	textBox.value = "numpad 1";	break;  // numpad 1
		case 98:	textBox.value = "numpad 2";	break;  // numpad 2
		case 99:	textBox.value = "numpad 3";	break;  // numpad 3
		case 100:	textBox.value = "numpad 4";	break;  // numpad 4
		case 101:	textBox.value = "numpad 5";	break;  // numpad 5
		case 102:	textBox.value = "numpad 6";	break;  // numpad 6
		case 103:	textBox.value = "numpad 7";	break;  // numpad 7
		case 104:	textBox.value = "numpad 8";	break;  // numpad 8
		case 105:	textBox.value = "numpad 9";	break;  // numpad 9
		case 106:	textBox.value = "multiply";	break;  // multiply
		case 107:	textBox.value = "add";		break;  // add
		case 109:	textBox.value = "subtract";	break;  // subtract
		case 110:	textBox.value = "decimal point";	break;  // decimal point
		case 111:	textBox.value = "divide";	break;  // divide
		case 112:	textBox.value = "F1";		break;  // F1
		case 113:	textBox.value = "F2";		break;  // F2
		case 114:	textBox.value = "F3";		break;  // F3
		case 115:	textBox.value = "F4";		break;  // F4
		case 116:	textBox.value = "F5";		break;  // F5
		case 117:	textBox.value = "F6";		break;  // F6
		case 118:	textBox.value = "F7";		break;  // F7
		case 119:	textBox.value = "F8";		break;  // F8
		case 120:	textBox.value = "F9";		break;  // F9
		case 121:	textBox.value = "F10";		break;  // F10
		case 122:	textBox.value = "F11";		break;  // F11
		case 123:	textBox.value = "F12";		break;  // F12
		case 144:	textBox.value = "num lock";	break;  // num lock
		case 145:	textBox.value = "scroll lock";	break;  // scroll lock
		case 186:	textBox.value = ";";		break;  // semi-colon
		case 187:	textBox.value = "=";		break;  // equal-sign
		case 188:	textBox.value = ",";		break;  // comma
		case 189:	textBox.value = "-";		break;  // dash
		case 190:	textBox.value = ".";		break;  // period
		case 191:	textBox.value = "/";		break;  // forward slash
		case 192:	textBox.value = "`";		break;  // grave accent
		case 219:	textBox.value = "[";		break;  // open bracket
		case 220:	textBox.value = "\\";		break;  // back slash
		case 221:	textBox.value = "]";		break;  // close bracket
		case 222:	textBox.value = "'";		break;  // single quote
	}

	var lblCharCode = document.getElementById('keycode');
	lblCharCode.innerHTML = 'KeyCode:  ' + charCode;

	return false;
}


/*********
* RANDOM *
*********/

/*
 * Button animation
 */
function ButtonGrowData (steps, speed) {
	this.steps = steps;
	this.speed = speed;
	this.current = 0;
	this.running = false;
}

function btnClickLaunch() {
	var attack = new ButtonGrowData(
		new Array('.', 'o', 'O', 'X'),
		500
	);

	if (attack.running == false) {
		attack.running = true;
		document.getElementById('btnClick').value = ' ';
		btnGrow('btnClick', attack);
	}
	document.getElementById('btnClick').blur();
}

function btnGrow (button, attack) {
	var valeur = ' ';
	current = attack.current;
	
	//If we reached the maximum value, exit
	if (current == attack.length) {
		btnGrowEnd(button);
	}
	
	attack.current++;
	
	if (attack.running) {
		document.getElementById(button).value = attack.steps[current];
		var t = setTimeout ("btnGrow('" + button + "', " + attack + ")", 500);
	}
}

function btnGrowEnd (button) {
	attack.running = false;
}

//Toggle an HTML element's visibility
function toggleDisplay(element_name) {
	var element = document.getElementById(element_name);
	
	if (element.style.display == "block")
		element.style.display = "none";
	else
		element.style.display = "block";
}