function initTiles()
{
	var tile = document.getElementsByClassName("tile") ;
	for ( i = 0 ; i < tile.length ; i++ )
	{
		tile[i].pos = i + 1 ;
		tile[i].tiles = tile ;
		tile[i].onclick = expand ;
	}
}
function expand()
{
	var parent = this.parentNode ;
	var split = this.href.split( "/" ) ;
	if ( split[6] == "new" )
	{
		var xhr = initXhr() ;
		xhr.new = this ;
		xhr.parent = parent ;
		xhr.entry = split[4] ;
		xhr.tounge = split[5] ;
		xhr.open( "POST" , "/lib/php/createNode.php" , true ) ;
		xhr.onreadystatechange = handleNewNode ;
		xhr.setRequestHeader( "Content-Type" , "application/x-www-form-urlencoded" ) ;
		xhr.send( "entry="+ split[4] ) ;
	}
	else
	{
		var oldDiv ;
		if ( oldDiv = parent.getElementsByTagName( "div" )[0] )
		{
			if ( oldDiv.interval )
				window.clearInterval( oldDiv.interval ) ;
			oldDiv.interval = window.setInterval( function() { shrink( oldDiv ) ; } , 1 ) ;
		}
		if ( this.clicked == 1 )
		{
			this.clicked = 2 ;
			return false ;
		}
		for ( i = 0 ; i < this.tiles.length ; i++ )
			this.tiles[i].clicked = 2 ;
		var pos = Math.floor( parent.offsetWidth / this.offsetWidth ) ;
		pos *= Math.ceil( this.pos / pos ) ;
		var div = document.createElement( "div" ) ;
		div.setAttribute( "class" , "extend" ) ;
		parent.insertBefore( div , this.tiles[pos] ) ;
		
		if ( split[3] == "verwaltung" )
		{
			split[3] = split[4] ;
			split[4] = split[5] ;
			split[5] = split[6] ;
			var type = "verwaltung" ;
		}
		else
			var type = "normal" ;
		var xhr = initXhr() ;
		xhr.div = div ;
		xhr.typ = type ;
		xhr.id = split[5] ;
		xhr.open( "POST" , "/lib/php/getNode.php?type="+type , true ) ;
		xhr.onreadystatechange = handleGetNode ;
		xhr.setRequestHeader( "Content-Type" , "application/x-www-form-urlencoded" ) ;
		xhr.send( "entry="+ split[3] +"&tounge="+ split[4] +"&id="+ split[5] ) ;
		this.clicked = 1 ;
	}
	return false ;
}
function grow( container )
{
	container.style.height = container.clientHeight + 10 +"px" ;
	if ( container.clientHeight >= container.origHeight )
		window.clearInterval( container.interval ) ;
}
function shrink( container )
{
	container.style.height = container.clientHeight - 10 +"px" ;
	if ( container.clientHeight - 10 <= 0 )
	{
		container.parentNode.removeChild( container ) ;
		window.clearInterval( container.interval ) ;
	}
}
function adaptSize( container )
{
	if ( container.newHeight < container.offsetHeight )
	{
		container.style.height = container.clientHeight - 10 +"px" ;
		if ( container.clientHeight <= container.newHeight )
			window.clearInterval( container.interval ) ;
	}
	else if ( container.newHeight > container.offsetHeight )
	{
		container.style.height = container.clientHeight + 10 +"px" ;
		if ( container.clientHeight >= container.newHeight )
			window.clearInterval( container.interval ) ;
	}
	else
		window.clearInterval( container.interval ) ;
}
function handleNewNode()
{
	if ( this.readyState == 1 )
		this.new.innerHTML = "<img src=\"/lib/css/loader.gif\">" ;
	else if ( this.readyState == 4 )
	{
		this.new.innerHTML = "neu" ;
		if ( this.responseText != "" )
		{
			var a = document.createElement( "a" ) ;
			a.setAttribute( "class" , "tile" ) ;
			a.innerHTML = "neues Element" ;
			a.href = this.entry +"/"+ this.tounge +"/"+ this.responseText ;
			this.parent.appendChild( a ) ;
			initTiles() ;
		}
	}
}
function handleGetNode()
{
	if ( this.readyState == 4 && this.responseText != "" )
	{
		var div = this.div ;
		var elements = responseCipher( this.responseText ) ;
		if ( this.typ == "normal" )
		{
			for ( var i = 0 ; i < 4 ; i++ )
				div.appendChild( elements[i] ) ;
			for ( var i = 0 ; i < elements[2].childNodes.length ; i++ )
				elements[2].childNodes[i].onclick = selectPicture ;
			div.textHeight = div.offsetHeight ;
			elements[0].firstChild.onload = function()
			{
				var div = this.parentNode.parentNode ;
				var gallery = this.parentNode.nextSibling ;
				gallery.replaceChild ( this , gallery.firstChild ) ;
				window.setTimeout( function()
				{
					div.origHeight = gallery.offsetHeight ;
					if ( div.origHeight < div.textHeight )
						div.origHeight = div.textHeight ;
					div.style.height = 0 ;
					div.interval = window.setInterval( function() { grow( div ) ; } , 1 ) ;
				 } , 2 ) ;
			}
		}
		if ( this.typ == "verwaltung" )
		{
			for ( var i = 0 ; i < 5 ; i++ )
				div.appendChild( elements[i] ) ;
			div.appendChild( elements[7] ) ;
			var id = this.id ;
			elements[8].onclick = function()
			{
				var con = elements[7] ;
				var span = document.createElement( "span" ) ; 
				var ja = document.createElement( "a" ) ;
				var nein = document.createElement( "a" ) ;
				span.appendChild( document.createTextNode( "Sicher? " ) ) ;
				ja.appendChild( document.createTextNode( "Ja " ) ) ;
				ja.href = "#" ;
				nein.appendChild( document.createTextNode( "Nein" ) ) ;
				nein.href = "#" ;
				var del = con.removeChild( this ) ;
				con.appendChild( span ) ;
				con.appendChild( ja ) ;
				con.appendChild( nein ) ;
				ja.onclick = function()
				{
					var xhr = initXhr() ;
					xhr.open( "POST" , "/lib/php/deleteTile.php" , true ) ;
					xhr.div = div ;
					xhr.id = id ;
					xhr.onreadystatechange = handleDeleteTile ;
					xhr.setRequestHeader( "Content-Type" , "application/x-www-form-urlencoded" ) ;
					xhr.send( "id="+ id ) ;
					return false ;
				}
				nein.onclick = function()
				{
					con.removeChild( span ) ;
					con.removeChild( ja ) ;
					con.removeChild( nein ) ;
					con.appendChild( del ) ;
					return false ;
				}
				return false ;
			}
			;
			div.origHeight = div.clientHeight ;
			initAutoUpload() ;
			initDragNDrop() ;
			initDeleter() ;
			div.style.height = 0 ;
			div.interval = window.setInterval( function() { grow( div ) ; } , 1 ) ;
		}
		// window.scrollTo( 0 , ( div.offsetTop + parent.offsetTop - 50 ) ) ;
	}
}
function handleDeleteTile()
{
	if ( this.readyState == 4 && this.responseText == 1 )
	{
		var tile = document.getElementById( this.id ) ;
		tile.parentNode.removeChild( tile ) ;
		this.div.parentNode.removeChild( this.div ) ;
		initTiles() ;
	}
	else if ( this.readyState == 4 && this.responseText != 1 )
		alert( this.responseText ) ;
}
function selectPicture()
{
	var div = this.parentNode.parentNode ;
	var preload = div.firstChild ;
	var gallery = preload.nextSibling ;
	for ( var i = 0 ; i < preload.childNodes.length ; i++ )
	{
		if ( preload.childNodes[i].name == this.name )
		{
			preload.appendChild( gallery.firstChild ) ;
			gallery.appendChild( preload.childNodes[i] ) ;
			div.newHeight = gallery.clientHeight ;
			if ( div.newHeight < div.textHeight )
				div.newHeight = parseInt( div.textHeight ) ;
			div.interval = window.setInterval( function() { adaptSize( div ) ; } , 1 ) ;
			break ;
		}
	}
	return false ;
}
function handleGetNode2()
{
	if ( this.readyState == 4 && this.responseText != "" )
	{
		var div = this.div ;
		div.innerHTML = this.responseText ;
		if ( this.typ == "normal" )
		{
			for ( var i = 0 ; i < div.firstChild.childNodes.length ; i++ )
				div.firstChild.appendChild( div.firstChild.childNodes[i] ) ;
			div.firstChild.firstChild.onload =
				function()
				{
					this.parentNode.nextSibling.appendChild ( this ) ;
					var div = this.parentNode.parentNode ;
					div.origHeight = div.clientHeight ;
					div.style.height = 0 ;
					div.interval = window.setInterval( function() { grow( div ) ; } , 20 ) ;
				}
		}
		if ( this.typ == "verwaltung" )
		{
			div.origHeight = div.clientHeight ;
			div.style.height = 0 ;
			div.interval = window.setInterval( function() { grow( div ) ; } , 20 ) ;
			initAutoUpload() ;
			initDragNDrop() ;
			initDeleter() ;
		}
		// window.scrollTo( 0 , ( div.offsetTop + parent.offsetTop - 50 ) ) ;
	}
}
