/* Global Functions 
 * Benötigt: tweener.class
 * Letztes Update: 30.11.2011 8:22
 * */

function catchURL() {
	curl = document.URL;
	if ((/\.html/).test(curl)) {
		if ((/#/).test(curl)) {
			return(curl.slice(curl.indexOf('#')+1))
		} else {
			return('htmldefault');
		}
	} else if ((/\.php/).test(curl)) {
		if ((/\?/).test(curl)) {
			var cphp = curl.slice(curl.indexOf('?')+1);
			if ((/&/).test(cphp)) {
				return(cphp.split('&'));
			} else {
				return(cphp);
			}
		}
	} else {
		return(false);
	}
}

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) {
	if (typeof(element) === "undefined" || element === null) {
		return false;
	}
	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")) {
			if (neueOpacity == 100) {
				element.style.filter = "";
			} else {
				element.style.filter = "alpha(Opacity=" + neueOpacity + ")";
			}
		}

	}
}


function toggleElement(element, duration, startValue, endValue, attribute, tweeningFunction, callback, caller) {
	if (element) {
		if (typeof(element) != "object") {
			element = document.getElementById(element);
		}
		var aktValue = getStyle(element, attribute, true);
		if (element.open) {
			changeElement(element, new Date(), duration, aktValue, startValue, attribute, tweeningFunction, callback, caller);
			element.open = false;
		} else {
			changeElement(element, new Date(), duration, aktValue, endValue, attribute, tweeningFunction, callback, caller);
			element.open = true;
		}
	}
}

function changeElement(element, startTime, duration, startValue, endValue, attribute, tweeningFunction, callback, caller) {
	if (element) {
		if (typeof(element) != "object") {
			element = document.getElementById(element);
		}
		if (typeof(attribute) == "undefined") {
			attribute = "height";
		}
		if (typeof(element.timeoutId) !== "object" || element.timeoutId === null) {
			element.timeoutId = {};
		}
		window.clearTimeout(element.timeoutId[attribute]);
		element.timeoutId[attribute] = 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 (hasStyleProperty(attribute)) {
				element.style[attribute] = neueValue + "px";
			}
			if (typeof(callback) != "undefined") {
				if (typeof(caller) != "undefined") {
					caller[callback](element);
				} else if (typeof(callback) == "function") {
					callback(element);
				}
			}
		} else {
			if (hasStyleProperty(attribute)) {
				element.style[attribute] = neueValue + "px";
			}
			element.timeoutId[attribute] = setTimeout( function() {changeElement(element, startTime, duration / 1000, startValue, endValue, attribute, tweeningFunction,callback, caller);}, 20);
		}

	}
}

var debugInfo = setDebugFunction = function () {
	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(element && typeof(element.length) !== 'undefined') {
				type = 'array';
			}
			break;
		
		case 'number':
			if(element.toString().indexOf('.') >= 0) {
				type = 'float';
			}
			else {
				type = 'int';
			}
			break;
			
		default:
			type = typeof(element);
	}
	
	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);
	
	if(element !== document) {
		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';
		}
	}
	else {
		dump += getDumpTabspace(depth);
		dump += type + '(' + element.length + ')' + ' "' + '#' + element.toString() + '"\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;};
}



function getScrollXY() { // aktuelle Scrollposition auslesen
    var scrOfX = 0, scrOfY = 0;
 
    if( typeof( window.pageYOffset ) == 'number' ) {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    return [ scrOfX, scrOfY ];
}


function moveCursorDiv(e, element) {
	if (!e) {
		e = window.event;
	}
	if (typeof(element) !== "object") {
		element = document.getElementById(element);
	}
	
	var scrollxy = getScrollXY();
	
	element.style.display = "block";
	element.style.top = (e.clientY + scrollxy[1] + 5) + "px";
	element.style.left = (e.clientX + scrollxy[0] + 5) + "px";
	
}
function hideCursorDiv(element) {
	if (typeof(element) !== "object") {
		element = document.getElementById(element);
	}
	element.style.display = "none";
}



function imagePreloader() {
	var preloaded = [];
	if (preloadImages.length > 0) {
		for (i=0;i<preloadImages.length;++i) {
			if (preloadImages[i] != "") {
				preloaded[i] = new Image();
				preloaded[i].src = preloadImages[i];
			}
		}
	}
}



function openPopup(popupId) {
	/* Beispielconfig */
	/*
		var popupConfig = new Object();
		popupConfig[1] = {
				width: "580",
				height: "335",
				effect: "'fade",
				direction: "left",
				tweening: "easeNone",
				speed: "0.8"
		};
	*/
	var popup = document.getElementById("popup_" + popupId),
		overlay = document.getElementById("popup_overlay_" + popupId);
		
	if (popup) {
		switch (popupConfig[popupId].effect) {
			case "ohne":
				popup.style.display = "block";
				popup.style.opacity = 1;
				popup.style.filter = "alpha(opacity=100)";
				break;
			case "slide":
				resetPopup(popup);
				popup.style.visibility = "hidden";
				popup.style.display = "block";
				popup.style.opacity = "1";
				popup.style.filter = "alpha(opacity=100)";
				
				var direction = popupConfig[popupId].direction;
				var offsetLeftStart = -(popup.offsetLeft + parseInt(popupConfig[popupId].width));
				var offsetTopStart = -(popup.offsetTop  + parseInt(popupConfig[popupId].height));
				var offsetLeft = popup.offsetLeft;
				var offsetTop = popup.offsetTop;
				
				if (direction == "left" || direction == "both") {
					popup.style.left = offsetLeft + "px"; 
					popup.style.marginLeft = "0px";
					popup.style.visibility = "visible";
					changeElement(popup, new Date(), popupConfig[popupId].speed, offsetLeftStart, offsetLeft, "left", popupConfig[popupId].tweening, resetPopupLeft);
				} 
				if (direction == "top" || direction == "both") {
					popup.style.top = offsetTop + "px"; 
					popup.style.marginTop = "0px";
					popup.style.visibility = "visible";
					changeElement(popup, new Date(), popupConfig[popupId].speed, offsetTopStart, offsetTop, "top", popupConfig[popupId].tweening, resetPopupTop);
				} 
				break;
			case "fade":
			default:
				popup.style.display = "block";
				fadeElement(popup, new Date(), popupConfig[popupId].speed, 0, 100);
				break;
		} 
	}
	
	// overlay einblenden
	if (overlay) {
		overlay.style.display = "block";
		fadeElement(overlay, new Date(), popupConfig[popupId].speed, 0, 70);
	}
}

function closePopup(popupId) {
	var popup = document.getElementById("popup_" + popupId),
		overlay = document.getElementById("popup_overlay_" + popupId);
		
	if (popup) {
		var popup_opacity = popup.style.opacity;
		switch (popupConfig[popupId].effect) {
			case "ohne":
				popup.style.opacity = 0;
				popup.style.filter = "alpha(opacity=0)";
				hideDiv(popup);
				break;
				
			case "slide":
				resetPopup(popup);
				//popup.style.visibility = "hidden";
				popup.style.display = "block";
				popup.style.opacity = "1";
				popup.style.filter = "alpha(opacity=100)";
				
				var direction = popupConfig[popupId].direction;
				var offsetLeftStart = -(popup.offsetLeft + parseInt(popupConfig[popupId].width));
				var offsetTopStart = -(popup.offsetTop  + parseInt(popupConfig[popupId].height));
				var offsetLeft = popup.offsetLeft;
				var offsetTop = popup.offsetTop;
				if (direction == "left" || direction == "both") {
					popup.style.left = offsetLeftStart + "px"; 
					popup.style.marginLeft = "0px";
					popup.style.visibility = "visible";
					changeElement(popup, new Date(), popupConfig[popupId].speed, offsetLeft, offsetLeftStart, "left", popupConfig[popupId].tweening, hideDiv);
				} 
				if (direction == "top" || direction == "both") {
					popup.style.top = offsetTopStart + "px"; 
					popup.style.marginTop = "0px";
					popup.style.visibility = "visible";
					changeElement(popup, new Date(), popupConfig[popupId].speed, offsetTop, offsetTopStart, "top", popupConfig[popupId].tweening, hideDiv);
				} 
				break;
			case "fade":
			default:
				if (typeof(popup_opacity) === "undefined" || popup_opacity == "") {
					popup_opacity = 0;
				}
				fadeElement(popup, new Date(), 0.8, popup_opacity * 100, 0,"easeNone",hideDiv);
				break;
		} 
	}
	
	// overlay ausblenden
	if (overlay) {
		var overlay_opacity = overlay.style.opacity;
		if (typeof(overlay_opacity) === "undefined" || overlay_opacity == "") {
			overlay_opacity = 0;
		}
		fadeElement(overlay, new Date(), 0.8, overlay_opacity * 100, 0,"easeNone",hideDiv);
	}
}

function resetPopup(element) {
	resetPopupLeft(element);
	resetPopupTop(element);
}
function resetPopupLeft(element) {
	element.style.marginLeft = "";
	element.style.left = "";
}
function resetPopupTop(element) {
	element.style.marginTop = "";
	element.style.top = "";
}
function hideDiv(element) {
	//alert(element);
	if (typeof(element) !== "undefined") {
		element.style.display = "none";
	}
}
