/**
 * AMP common functions
 *
 * Copyright (c) 2007 Xavier 'Xr' Dalem <http://xr.i-jeuxvideo.com>
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */

function helpPopup(url) {
	window.open(url, 'help', 'status=1,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1,status=0,height=550,width=600');
}

/** Toggle the list of the tracks on the album view */
function toggleTracks(album_id) {
	$("div[@id=album"+album_id+"_show]").toggle();
	$("div[@id=album"+album_id+"_hide]").toggle();
	$("ol[@id=album"+album_id+"]").slideToggle("normal");
}

/** Toggle the detail of an event */
function toggleEvent(album_id) {
	$("div[@id=event"+album_id+"]").slideToggle("normal");
}

/** Append the value of a select field to an input field */
function selectToInput(select_id, input_id) {
	var select = document.getElementById(select_id);
	var input = document.getElementById(input_id);

	input.value += ' ' + select.value;
}

function getAJAXObject() {
	var axr; // Async XML Request
	if (window.XMLHttpRequest) // Firefox, Safari, Opera
		axr = new XMLHttpRequest();
	else if (window.ActiveXObject) { // IE
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				return null;
			}
		}
	}
	else {
		return null;
	}
	return axr;
}



/**
 * sprintf() for JavaScript v.0.1
 *
 * Copyright (c) 2007 Alexandru Marasteanu <http://alexei.417.ro/>, 
 * Xavier 'Xr' Dalem
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */

function str_repeat(i, m) { for (var o = []; m > 0; o[--m] = i); return(o.join('')); }

function sprintf() {
	var i = 0, f = arguments[i++], o = [], m, a, p;
	while (f) {
		if (m = /^[^\x25]+/.exec(f)) o.push(m[0]);
		else if (m = /^\x25{2}/.exec(f)) o.push('%');
		else if (m = /^\x25(\+)?(0|'[^$])?(-)?(\d+)?(\.\d+)?([b-fosuxX])/.exec(f)) {
			if (!(a = arguments[i++]) && a != '') throw("Too few arguments.");
			if (/[^s]/.test(m[6]) && (typeof(a) != 'number'))
				throw("Expecting number but found " + typeof(a));
			switch (m[6]) {
				case 'b': a = a.toString(2); break;
				case 'c': a = String.fromCharCode(a); break;
				case 'd': a = parseInt(a); break;
				case 'e': a = m[5] ? a.toExponential(m[5].charAt(1)) : a.toExponential(); break;
				case 'f': a = m[5] ? parseFloat(a).toFixed(m[5].charAt(1)) : parseFloat(a); break;
				case 'o': a = a.toString(8); break;
				case 's': a = ((a = String(a)) && m[5] ? a.substring(0, m[5].charAt(1)) : a); break;
				case 'u': a = Math.abs(a); break;
				case 'x': a = a.toString(16); break;
				case 'X': a = a.toString(16).toUpperCase(); break;
			}
			a = (/[def]/.test(m[6]) && m[1] && a > 0 ? '+' + a : a);
			p = m[4] ? str_repeat(m[2] ? m[2] == '0' ? '0' : m[2].charAt(1) : ' ', m[5] ? m[4] - String(a).length : m[4]) : '';
			o.push(m[3] ? a + p : p + a);
		} else throw("Huh ?");
		f = f.substring(m[0].length);
	}
	return(o.join(''));
}

