/* Global Functions 
 * Benötigt: tweener.class
 * Letztes Update: 16.08.2011 11:18
 * */
function removeEvent(element, event, callback) {
	var checkForAddListener = false,
			checkForAttach = false,
			multiEvents = [];

	if (typeof(event) === 'object') {
		multiEvents = event;
	}

	checkForAddListener = window.addEventListener;
	checkForAttach = window.attachEvent;

	if (checkForAddListener) {
		if (multiEvents.length > 0) {
			event = multiEvents[0];
		}

		element.removeEventListener(event, callback, false);
	}
	else if (checkForAttach) {
		if (multiEvents.length > 0) {
			event = multiEvents[1];
		}

		element.detachEvent('on' + event, callback);
	}
	else {
		if (multiEvents.length > 0) {
			event = multiEvents[2];
		}

		element['on' + event] = null;
	}
}

function addEvent(element, event, callback) {
	var checkForAddListener = false,
			checkForAttach = false,
			multiEvents = [],
			attachCallback = null;

	if (typeof(event) === 'object') {
		multiEvents = event;
	}

	checkForAddListener = window.addEventListener;
	checkForAttach = window.attachEvent;

	if (checkForAddListener) {
		if (multiEvents.length > 0) {
			event = multiEvents[0];
		}

		element.addEventListener(event, callback, false);
	}
	else if (checkForAttach) {
		if (multiEvents.length > 0) {
			event = multiEvents[1];
		}

		attachCallback = function () {
			callback.call(element);
		};

		element.attachEvent('on' + event, attachCallback);
	}
	else {
		if (multiEvents.length > 0) {
			event = multiEvents[2];
		}

		element['on' + event] = callback;
	}
}

function getStyle(element, property, asInt) {
	var propValue = '';

	if (typeof(asInt) === 'undefined') {
		asInt = false;
	}

	if (element.currentStyle) {
		propValue = element.currentStyle[property];
	}
	else if (document.defaultView && document.defaultView.getComputedStyle) {
		propValue = document.defaultView.getComputedStyle(element, "")[property];
	}
	else {
		propValue = element.style[property];
	}

	if (asInt) {
		propValue = parseInt(propValue, 10);

		if (isNaN(propValue)) {
			propValue = 0;
		}
	}

	return propValue;
}

function setStyle(element, property, value) {
	if (element && element.style) {
		element.style[property] = value;
	}
}

function hasStyleProperty(property) {
	var propValue = false;

	if (property && property in document.body.style) {
		propValue = true;
	}

	return propValue;
}




function fadeElement(element, startTime, duration, startOpacity, endOpacity, tweeningFunction, callback) {
	if (element) {
		if (typeof(element) != "object") {
			element = document.getElementById(element);
		}
		window.clearTimeout(element.timeoutId);
		element.timeoutId = null;

		if (typeof(tweeningFunction) == "undefined" || tweeningFunction == "" || !tweeningFunction) {
			tweeningFunction = "easeNone";
		}
		duration *= 1000;
		var currentTime = new Date();
		var timerDifference = currentTime.getTime() - startTime.getTime();
		var tweener = JSTweener.easingFunctions;
		var neueOpacity = tweener[tweeningFunction](timerDifference, startOpacity, endOpacity - startOpacity, duration);
		if (timerDifference >= duration) {
			neueOpacity = tweener[tweeningFunction](duration, startOpacity, endOpacity - startOpacity, duration);
			if (typeof(callback) != "undefined" && typeof(callback) == "function") {
				callback(element);
			}
		} else {
			element.timeoutId = setTimeout( function() {fadeElement(element, startTime, duration / 1000, startOpacity, endOpacity, tweeningFunction,callback);}, 20);
		}
		
		if (hasStyleProperty("opacity")) {
			element.style.opacity = neueOpacity / 100;
		}
		else if (hasStyleProperty("filter")) {
			element.style.filter = "alpha(Opacity=" + neueOpacity + ")";
		}

	}
}


function changeElement(element, startTime, duration, startValue, endValue, attribute, tweeningFunction, callback) {
	if (element) {
		if (typeof(element) != "object") {
			element = document.getElementById(element);
		}
		window.clearTimeout(element.timeoutId);
		element.timeoutId = null;

		if (typeof(tweeningFunction) == "undefined" || tweeningFunction == "" || !tweeningFunction) {
			tweeningFunction = "easeNone";
		}
		duration *= 1000;
		var currentTime = new Date();
		var timerDifference = currentTime.getTime() - startTime.getTime();
		var tweener = JSTweener.easingFunctions;
		var neueValue = tweener[tweeningFunction](timerDifference, startValue, endValue - startValue, duration);
		if (timerDifference >= duration) {
			neueValue = tweener[tweeningFunction](duration, startValue, endValue - startValue, duration);
			if (typeof(callback) != "undefined" && typeof(callback) == "function") {
				callback(element);
			}
		} else {
			element.timeoutId = setTimeout( function() {changeElement(element, startTime, duration / 1000, startValue, endValue, attribute, tweeningFunction,callback);}, 20);
		}
		if (typeof(attribute) == "undefined") {
			attribute = "height";
		}
		if (hasStyleProperty(attribute)) {
			element.style[attribute] = neueValue + "px";
		}

	}
}

function setDebugFunction() {
	var useFunction = null,
			ieIndex = 0;

	if (typeof(console) !== 'undefined') {
		if (typeof(console.info) === 'object' || typeof(console.info) === 'function') {
			useFunction = 'info';
			
			if (console[useFunction].apply) {
				console[useFunction].apply(console, arguments);
			}
			else {
				for (ieIndex = 0; ieIndex < arguments.length;) {
					console[useFunction](arguments[ieIndex]);
					ieIndex += 1;
				}
			}
		}
		else if (typeof(console.debug) === 'function') {
			useFunction = 'debug';
			console[useFunction].apply(console, arguments);
		}
	}
}

function parseScript(stringToParse) {
	var scriptSource = "",
		scriptCheck = "",
		scriptTags = "";
		
	scriptSource = stringToParse.replace(/\s+/g, " ");	
	scriptSource = scriptSource.replace(/[ ]+/g, " ");
	
	scriptCheck = /<script[^>]*>(.*?)<\/script>/gi;
	
	while(scriptTags = scriptCheck.exec(scriptSource)) {
		if (scriptTags.length > 1) {
			scriptTags = scriptTags.pop();
			scriptTags = scriptTags.replace(/(<!--|\/\/-->)/g, '');			
			eval(scriptTags);
		}
	}
}

function numberFormat(number, decimals, decimalPoint, thousandsSeparator) {
	return number_format(number, decimals, decimalPoint, thousandsSeparator);
}

function number_format(number, decimals, decimalPoint, thousandsSeparator) {
	var decimalMultiplier = 1,
		index = 0,
		fullNumber = '';
	
	if (typeof(decimals) === 'undefined') {
		decimals = 0;
	}

	if (typeof(decimalPoint) === 'undefined') {
		decimalPoint = '.';
	}

	if (typeof(thousandsSeparator) === 'undefined') {
		thousandsSeparator = '';
	}
	
	decimalMultiplier = Math.pow(10, decimals);
	
	number *= decimalMultiplier;
	number = Math.round(number);
	number /= decimalMultiplier;
	
	if ((decimalPoint !== '.' && decimalPoint !== '') || thousandsSeparator !== '') {
		number = number.toString();

		number = number.split('.');
		
		if(thousandsSeparator !== '') {
			fullNumber = number[0];
			fullNumber = fullNumber.split('');
			
			fullNumber.reverse();
			
			for (index = 3; index < fullNumber.length; index += 3) {
				fullNumber[index] = fullNumber[index] + thousandsSeparator;
			}
			
			fullNumber.reverse();
			
			fullNumber = fullNumber.join('');
			number[0] = fullNumber;
		}
		
		number = number.join(decimalPoint);
	}
	
	return number;
}

function getDumpType(element) {
	var type = '';

	type = typeof(element);
	
	switch (type) {
		case 'object':
			if(typeof(element.length) !== 'undefined') {
				type = 'array';
			}
			break;
		
		case 'number':
			if(element.toString().indexOf('.') >= 0) {
				type = 'float';
			}
			else {
				type = 'int';
			}
			break;
			
		default:
			type = 'string';
	}
	
	return type;
}

function getDumpTabspace(depth) {
	var tabspace = '',
		index = 0;

	for (index = 0; index < depth;) {
		tabspace += '    ';
		
		index += 1;
	}

	return tabspace
}

function getObjectSize(object) {
    var size = 0,
		index = null;
	
    for (index in object) {
        if (object.hasOwnProperty(index)) {
			size += 1;
		}
    }

    return size;
}

function varDump(element, useConsole, depth) {
	return var_dump(element, useConsole, depth);
}

function var_dump(element, useConsole, depth) {
	var dump = '',
		type = '',
		index = 0,
		alert = function () {};

	if (typeof(useConsole) === 'undefined') {
		useConsole = -1;
	}

	if (useConsole > 0) {
		alert = window.setDebugFunction;
	}
	else if (useConsole < 0) {
		alert = window.alert;
	}

	if (typeof(depth) === 'undefined') {
		depth = 0;
	}

	type = getDumpType(element);
	
	switch (type) {
		case 'array':
			dump += getDumpTabspace(depth);
			dump += type + '(' + element.length + ') {\n';

			for (index = 0; index < element.length;) {
				dump += getDumpTabspace(depth + 1);
				dump += '[' + index + ']=>\n';
				dump += var_dump(element[index], useConsole, depth + 1);

				index += 1;
			}

			dump += getDumpTabspace(depth);
			dump += '}\n';
			break;
		
		case 'object':
			dump += getDumpTabspace(depth);
			dump += type + '(' + getObjectSize(element) + ') {\n';

			for (index in element) {
				dump += getDumpTabspace(depth + 1);
				
				if (getDumpType(index) === 'string') {
				dump += '["' + index + '"]=>\n';
				}
				else {
				dump += '[' + index + ']=>\n';
				}
				
				dump += var_dump(element[index], useConsole, depth + 1);
			}

			dump += getDumpTabspace(depth);
			dump += '}\n';
			break;
		
		case 'string':
			dump += getDumpTabspace(depth);
			dump += type + '(' + element.length + ')' + ' "' + element + '"\n';
			break;
	
		default:
			dump += getDumpTabspace(depth);
			dump += type + '(' + element + ')\n';
	}

	if (depth < 1) {
		alert(dump);
		
		if (useConsole > 0 || useConsole < 0) {
			dump = '';
		}
	}
	
	return dump;
}

function getElementsByClassName(myName) {
  var tags = ["div", "span"];
  var result = [];
  var searchExpression = new RegExp( "\\b" + myName + "\\b" );
  for (var i = 0; i < tags.length; i++ ) {
    var objects = document.getElementsByTagName( tags[ i ] );
    for (var j = 0; j < objects.length; j++ )
    if ( objects[ j ].className.match( searchExpression ) )
      result.push( objects[ j ] );
    }
  return result;
}

function getXMLChildrenByTagName(element, name) {
	var list = [],
		i = 0;

	if (typeof(element) === 'object' && typeof(name) === 'string') {
		name = name.toUpperCase();
	
		for (i = 0; i < element.childNodes.length; i++) {
			if (element.childNodes[i].nodeName.toUpperCase() == name) {
				list.push(element.childNodes[i]);
			}
		}
	}
	
	return list;
}

function makeNoSelection() {
	document.body.onselectstart = function () {return false;};
	document.body.ondrag        = function () {return false;};
	document.onmousedown        = function () {return false;};
}

function resetAllValues() {
	document.body.onselectstart = function () {return true;};
	document.body.ondrag        = function () {return true;};
	document.onmousedown        = function () {return true;};
}
