// JavaScript Document

//	runtime Variables
var globalHost = "http://www.jugendbahnhof.org/";

var	animation	=	Object();
	animation.	pos		=	0;
	animation.	dest	=	0;
	animation.	dist	=	0;
	animation.	step	=	0;
	animation.	hndl	=	false;
	animation.	busy	=	false;


var backgroundImage = false;
var	backgroundHandle = false;

//hideContent();

//	runtime functions

function	Initialize()
	{
		//	load contents		
		loadNews('news', 0);
		loadBetreuer('karin');
		loadProjectDesc(0);
		loadBlog();
		
		//	show content when loaded
		showContent();
		
		//	replace background image when loaded
		backgroundHandle = window.setInterval("replaceBackground()", 50);
	}
	
function	replaceBackground()
	{
		if (backgroundImage == false)
			{
				backgroundImage = new Image();
				backgroundImage.src = "images/background.png";
			}
		if (backgroundImage.complete)
			{
				var juba = document.getElementById('juba');
				setStyleAttribute(juba, 'backgroundImage', 'url('+relPath()+'images/background.png)');
				window.clearInterval(backgroundHandle);
			}
	}
	
function	relPath()
	{
		tempReq 	= String(window.location);				// Number(tempReq.length) - Number(globalHost.length)
		req		= tempReq.substr(globalHost.length, Number(tempReq.length) - Number(globalHost.length));
		//req 		= req.substr(0, req.length-1);
		paths		= req.split('/');
		relPre 	= '';
		for (i = 0; i < Number(paths.length); i++)
			{
				if (paths[i].length > 0 && paths[i] != 'index.php')
					{
						relPre = relPre+'../';
					}
			}
		return relPre;
	}

function	preloadImages()
	{
//		alert(document.images.length);
		imgCount 	=  document.images.length;
		imgLoaded	=	false;
		while(imgLoaded == false)
			{
				tempFlag = true;
				for (i = 0; i < imgCount; i++)	
					{
						if (tempFlag == true && document.images[i].complete == false)
							{
								tempFlag = false;
							}
					}
				imgLoaded = tempFlag;
			}
	}

//	generate ajax variable
function prepareAjax()
	{	
		var temp_ajax = false;
		
		if (typeof XMLHttpRequest != 'undefined')	{ temp_ajax = new XMLHttpRequest(); }	
		if (!temp_ajax) 
			{
				try { temp_ajax = new ActiveXObject("Msxml2.XMLHTTP"); }
				catch(e) 
			{
			  try { temp_ajax = new ActiveXObject("Microsoft.XMLHTTP"); }
			  catch(e) { temp_ajax = null; }
			}
		}
		
		return temp_ajax;
	}


function	getPosition()
	{
		if (navigator.appName == 'Microsoft Internet Explorer')
			{
				if (document.documentElement.scrollTop)
					{
						return Number(document.documentElement.scrollTop);
					}
				else
					{
						return document.body.scrollTop;
					}
			}
		else
		{
			return Number(window.pageYOffset);
		}
	}

function scrollWindow (id, offset)
	{
		if (!animation.busy)
			{
				var elem 	= 	document.getElementById(id);
				var pos		=	String(getStyleAttribute(elem, 'top'));
				
				animation.	pos		= 	getPosition(); //Number(window.pageYOffset);
				animation.	dest	=	Number(pos.replace(/px/, "")) - Number(offset);
				animation.	dist	=	Number(animation.dest - animation.pos);
				animation.	step	=	0;
				animation.	hndl	=	window.setInterval("doScroll()", 10);
				animation.	busy	=	true;
			}
		else
			{
				animation.busy = false;
				window.clearInterval(animation.hndl);
				scrollWindow(id, offset);
			}
	}

function	doScroll()
	{
		//alert("distance :"+animation.dist+"\n"+"position :"+animation.pos+"\n"+"step :"+animation.step);
		

		if (Math.abs(animation.dist) > 1)
			{
				
				animation.step 	= 	Number(animation.dist / 10.0);//.toFixed(0);
				animation.dist	=	animation.dist - animation.step;
				animation.pos	=	Number(animation.pos) + Number(animation.step);
				window.scrollTo(0, animation.pos);
			}
		else
			{
				
				window.clearInterval(animation.hndl);
				window.scrollTo(0, animation.dest);
				animation.busy = false;
			}
	}

function	showContent()
	{
		//	show div.content
		var elem = Array();	elem = getElementsByClassName('div', 'content');

		for (i = 0; i < elem.length; i++)
			{
				setStyleAttribute(elem[i], 'visibility', 'visible');
			}
		
		//	show Menu
		setStyleAttribute(document.getElementById('menu'), 'visibility', 'visible');
	}
	
function	hideContent()
	{
		//	show div.content
		var elem = Array();	elem = getElementsByClassName('div', 'content');

		for (i = 0; i < elem.length; i++)
			{
				setStyleAttribute(elem[i], 'visibility', 'hidden');
			}
		
		//	show Menu
		setStyleAttribute(document.getElementById('menu'), 'visibility', 'hidden');
	}
	
	
function nl2br (str, is_xhtml) {
    // Converts newlines to HTML line breaks  
    // 
    // version: 1006.1915
    // discuss at: http://phpjs.org/functions/nl2br
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Philip Peterson
    // +   improved by: Onno Marsman
    // +   improved by: Atli Þór
    // +   bugfixed by: Onno Marsman
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   improved by: Maximusya
    // *     example 1: nl2br('Kevin\nvan\nZonneveld');
    // *     returns 1: 'Kevin\nvan\nZonneveld'
    // *     example 2: nl2br("\nOne\nTwo\n\nThree\n", false);
    // *     returns 2: '<br>\nOne<br>\nTwo<br>\n<br>\nThree<br>\n'
    // *     example 3: nl2br("\nOne\nTwo\n\nThree\n", true);
    // *     returns 3: '\nOne\nTwo\n\nThree\n'
    var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '' : '<br />';
 
    return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');
}

function addslashes(str) {
str=str.replace(/\n/g, '<br />');
str=str.replace(/\\/g,'\\\\');
str=str.replace(/\'/g,'\\\'');
str=str.replace(/\"/g,'\\"');
str=str.replace(/\0/g,'\\0');
return str;
}
function stripslashes(str) {
str=str.replace(/\\'/g,'\'');
str=str.replace(/\\"/g,'"');
str=str.replace(/\\0/g,'\0');
str=str.replace(/\\\\/g,'\\');
return str;
}
