/**
*
*  Javascript sprintf
*  http://www.webtoolkit.info/
*
*
**/

sprintfWrapper = {

	init : function () {

		if (typeof arguments == "undefined") { return null; }
		if (arguments.length < 1) { return null; }
		if (typeof arguments[0] != "string") { return null; }
		if (typeof RegExp == "undefined") { return null; }

		var string = arguments[0];
		var exp = new RegExp(/(%([%]|(\-)?(\+|\x20)?(0)?(\d+)?(\.(\d)?)?([bcdfosxX])))/g);
		var matches = new Array();
		var strings = new Array();
		var convCount = 0;
		var stringPosStart = 0;
		var stringPosEnd = 0;
		var matchPosEnd = 0;
		var newString = '';
		var match = null;

		while (match = exp.exec(string)) {
			if (match[9]) { convCount += 1; }

			stringPosStart = matchPosEnd;
			stringPosEnd = exp.lastIndex - match[0].length;
			strings[strings.length] = string.substring(stringPosStart, stringPosEnd);

			matchPosEnd = exp.lastIndex;
			matches[matches.length] = {
				match: match[0],
				left: match[3] ? true : false,
				sign: match[4] || '',
				pad: match[5] || ' ',
				min: match[6] || 0,
				precision: match[8],
				code: match[9] || '%',
				negative: parseInt(arguments[convCount]) < 0 ? true : false,
				argument: String(arguments[convCount])
			};
		}
		strings[strings.length] = string.substring(matchPosEnd);

		if (matches.length == 0) { return string; }
		if ((arguments.length - 1) < convCount) { return null; }

		var code = null;
		var match = null;
		var i = null;

		for (i=0; i<matches.length; i++) {

			if (matches[i].code == '%') { substitution = '%' }
			else if (matches[i].code == 'b') {
				matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(2));
				substitution = sprintfWrapper.convert(matches[i], true);
			}
			else if (matches[i].code == 'c') {
				matches[i].argument = String(String.fromCharCode(parseInt(Math.abs(parseInt(matches[i].argument)))));
				substitution = sprintfWrapper.convert(matches[i], true);
			}
			else if (matches[i].code == 'd') {
				matches[i].argument = String(Math.abs(parseInt(matches[i].argument)));
				substitution = sprintfWrapper.convert(matches[i]);
			}
			else if (matches[i].code == 'f') {
				matches[i].argument = String(Math.abs(parseFloat(matches[i].argument)).toFixed(matches[i].precision ? matches[i].precision : 6));
				substitution = sprintfWrapper.convert(matches[i]);
			}
			else if (matches[i].code == 'o') {
				matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(8));
				substitution = sprintfWrapper.convert(matches[i]);
			}
			else if (matches[i].code == 's') {
				matches[i].argument = matches[i].argument.substring(0, matches[i].precision ? matches[i].precision : matches[i].argument.length)
				substitution = sprintfWrapper.convert(matches[i], true);
			}
			else if (matches[i].code == 'x') {
				matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(16));
				substitution = sprintfWrapper.convert(matches[i]);
			}
			else if (matches[i].code == 'X') {
				matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(16));
				substitution = sprintfWrapper.convert(matches[i]).toUpperCase();
			}
			else {
				substitution = matches[i].match;
			}

			newString += strings[i];
			newString += substitution;

		}
		newString += strings[i];

		return newString;

	},

	convert : function(match, nosign){
		if (nosign) {
			match.sign = '';
		} else {
			match.sign = match.negative ? '-' : match.sign;
		}
		var l = match.min - match.argument.length + 1 - match.sign.length;
		var pad = new Array(l < 0 ? 0 : l).join(match.pad);
		if (!match.left) {
			if (match.pad == "0" || nosign) {
				return match.sign + pad + match.argument;
			} else {
				return pad + match.sign + match.argument;
			}
		} else {
			if (match.pad == "0" || nosign) {
				return match.sign + match.argument + pad.replace(/0/g, ' ');
			} else {
				return match.sign + match.argument + pad;
			}
		}
	}
}
sprintf = sprintfWrapper.init;

var writeDomElemDebug = /^http(s)?:\/\/localhost:8080\/.*/i.test(self.location.href);

function debug_getReporter(id)
{
	if (top.document.body==null)
		return null;
	if (top.document.getElementById(id)==null) {
		r = top.document.createElement('div');
		r.setAttribute('id', id);
		r.style.height = '280px';
		r.style.position = 'absolute';
		r.style.bottom = '0px';
		r.style.border = '1px dotted red';
		r.style.overflow = 'auto';
		r.style.padding = '5px';
		r.style.display = 'none';
		r.style.backgroundColor = '#CCEEDD';
		r.ondblclick = function(){this.innerHTML='';};
		r.linesNumber = 0;
		top.document.body.appendChild(r);
	}
	return top.document.getElementById(id);
}

function debug_getTimeStamp()
{
	var currentTime = new Date();
	return sprintf('%02d:%02d:%02d.%03d', currentTime.getHours(), currentTime.getMinutes(), currentTime.getSeconds(), currentTime.getMilliseconds());
}

function debug_write(m, s)
{
	return false; // ATTENTION: Does not work in IE, as appendChild throws errer as prototype.js not pat of a body
	if (writeDomElemDebug) {
		var jsdebugger = debug_getReporter('domElemDebug');
		if (jsdebugger) {
			if (jsdebugger.linesNumber > 100) {
				jsdebugger.linesNumber = 0;
				jsdebugger.innerHTML = '';
			}
			jsdebugger.innerHTML = '[' + debug_getTimeStamp() + ']&nbsp;<span class="jsdebuggerMethodName">&lt;' + m + '&gt;</span>&nbsp;<span class="jsdebuggerMethodMsg">' + s + '</span><br />' + jsdebugger.innerHTML;
			jsdebugger.linesNumber++;
		}
	}
}


/* we done use that?
String.prototype.trim = function(chars)
{
	chars = chars || "\\s";
	return this.ltrim(chars).rtrim(chars);
}

String.prototype.ltrim = function(chars)
{
	chars = chars || "\\s";
	return this.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

String.prototype.rtrim = function(chars)
{
	chars = chars || "\\s";
	return this.replace(new RegExp("[" + chars + "]+$", "g"), "");
}*/


/* fixed in newer version?
if (Prototype.Browser.WebKit) {
	  $A = Array.from = function(iterable) {
	    if (!iterable) return [];
	    if (!(typeof iterable == 'function' && iterable == '[object NodeList]') &&
	      iterable.toArray) {
	      return iterable.toArray();
	    } else {
	      var results = [];
	      for (var i = 0, length = iterable.length; i < length; i++)
	        results.push(iterable[i]);
	      return results;
	    }
	  }
	}
*/


/**
 * Ajax.Request.abort
 * extend the prototype.js Ajax.Request object so that it supports an abort method
 */
Ajax.Request.prototype.abort = function()
{
	// prevent and state change callbacks from being issued
	this.transport.onreadystatechange = Prototype.emptyFunction;
	// abort the XHR
	this.transport.abort();
};


//function $(element) {
//	  if (arguments.length > 1) {
//	    for (var i = 0, elements = [], length = arguments.length; i < length; i++)
//	      elements.push($(arguments[i]));
//	    return elements;
//	  }
//	  if (Object.isString(element))
//		  if(document.getElementById(element)==null){
//			  debug_write('$', 'ID not found: ' + element);
//			  alert(element);
//			  //return null; TODO figure out if this is okay?
//		  }
//	    element = document.getElementById(element);
//	  return Element.extend(element);
//}




