﻿var smsbox_currentpage = 0;
var smsbox_contentarray = [];	//PosterName,Time,Text,Blend,ScrollHeight,PosterUserID,PostID
var smsbox_startcolor = 240;
var smsbox_steps = 20;
var smsbox_busy = 0;

function smsbox_nextpage(page)
{
	if(smsbox_busy == 0)
	{
		smsbox_busy = 1;
		//alert(page + " " + smsbox_currentpage);
		smsbox_currentpage = smsbox_currentpage + (page*1);
		if(smsbox_currentpage < 0)
		{
			smsbox_currentpage = 0;
			smsbox_busy = 0;
		}
		else
		{
			if(smsbox_currentpage == 0)
			{document.getElementById('smsbox_nextpage').style.maxHeight = '0px';}
			else
			{document.getElementById('smsbox_nextpage').style.maxHeight = '10px';}
			
			if(typeof smsbox_contentarray[smsbox_currentpage+10] == "undefined")
			{
				//alert("Get Data" + smsbox_currentpage);
				var myAjax = new Ajax.Request(
					"/smsbox/worker.php",
					{
						method: 'post', 
						parameters: 'action=GetContent&page='+(smsbox_currentpage),
						onComplete: function(originalRequest) 
						{
							var temp_contentarray = originalRequest.responseText.split("#$#");
								
							var Counter = 0;
							for(Counter=0; Counter<15; Counter++)
							{
								var poster = temp_contentarray[2+(Counter*6)];
								var time = temp_contentarray[3+(Counter*6)];
								var text = temp_contentarray[4+(Counter*6)];
								var posterid = temp_contentarray[5+(Counter*6)];
								var postid = temp_contentarray[6+(Counter*6)];
								smsbox_contentarray[parseInt(temp_contentarray[1+(Counter*6)])] = [poster,time,text,0,0,posterid,postid];
							}
							smsbox_draw(page);
						}
					}
				);
			}
			else
			{
				smsbox_draw(page);
			}
		}
	}
}

function smsbox_draw(page)
{
	var smsbox_content_nr = 0;
	var Output = "";
	var Counter = 0;
	
	for(Counter=0; Counter<10; Counter++)
	{
		smsbox_content_nr = Counter+smsbox_currentpage;
		if(Counter==0 && page==-1)
		{
			color = 'rgb('+smsbox_startcolor+','+smsbox_startcolor+','+smsbox_startcolor+')';
			Output += '<div style="color: '+color+'; max-height: 0px; overflow: hidden;" id="smsbox_content_nr' + smsbox_content_nr + '">';
		}
		else if(Counter==0 && page==1)
		{
			Output += '<div id="smsbox_content_nr' + (smsbox_content_nr-1) + '">';
			Output += smsbox_contentarray[smsbox_content_nr-1][0]+"<br />";
			//Output += smsbox_contentarray[smsbox_content_nr-1][1]+"<br />";
			Output += smsbox_contentarray[smsbox_content_nr-1][2]+"<br />";
			Output += "</div>";
			Output += "<div id='smsbox_content_nr" + smsbox_content_nr + "'>";
		}
		else
		{Output += '<div id="smsbox_content_nr' + smsbox_content_nr + '">';}
		
		if((smsbox_contentarray[smsbox_content_nr][5] == smsbox_userid) && (smsbox_userid > 1))
		{
			Output += '<div style="background-color: #f5edc3;">';
			Output += '<div class="imghover_v20" style="width: 20px; cursor: pointer;" onClick="smsbox_delete_onclick('+smsbox_contentarray[smsbox_content_nr][6]+')"><span><img src="/pictures/icon_post_delete.gif" alt="Beitrag löschen" title="Beitrag löschen" /></span></div>';
		}
		
		Output += smsbox_contentarray[smsbox_content_nr][0];
		//Output += smsbox_contentarray[smsbox_content_nr][1]+"<br />";
		Output += smsbox_contentarray[smsbox_content_nr][2]+"<br />";	
		
		if((smsbox_contentarray[smsbox_content_nr][5] == smsbox_userid) && (smsbox_userid > 1))
		{
			Output += '</div>';
		}
		
		Output += '</div>';
	}
	
	document.getElementById('smsbox_content').innerHTML = Output;
	
	for(Counter=0; Counter<10; Counter++)
	{	
		smsbox_content_nr = Counter+smsbox_currentpage;
		smsbox_contentarray[smsbox_content_nr][4] = document.getElementById('smsbox_content_nr' + smsbox_content_nr).scrollHeight;	
		//alert(document.getElementById('smsbox_content_nr' + smsbox_content_nr).scrollHeight);
	}	
	
	if(page == 1)
	{
		//alert("blendout: ");
		setTimeout("smsbox_blendout("+(smsbox_currentpage-1)+");", 1);
	}
	else if(page == -1)
	{
		//alert("blend");
		setTimeout("smsbox_blendin("+smsbox_currentpage+");", 1);
	}
	else
	{smsbox_busy = 0;}
}

function smsbox_blendin(smsbox_currentpage)
{
	if(smsbox_contentarray[smsbox_currentpage][3]<smsbox_steps)
	{
		if(smsbox_contentarray[smsbox_currentpage][3] < (smsbox_steps/2))
		{
			document.getElementById('smsbox_content_nr' + smsbox_currentpage).style.maxHeight = smsbox_contentarray[smsbox_currentpage][4] * (smsbox_contentarray[smsbox_currentpage][3]/((smsbox_steps/2)-1)) + "px";
		}
		else
		{
			var color = smsbox_startcolor-(smsbox_startcolor/((smsbox_steps/2)-1))*(smsbox_contentarray[smsbox_currentpage][3]-(smsbox_steps/2));
			color = 'rgb('+color+','+color+','+color+')';
			document.getElementById('smsbox_content_nr' + smsbox_currentpage).style.color = color;
		}
		smsbox_contentarray[smsbox_currentpage][3]++;
		setTimeout("smsbox_blendin("+smsbox_currentpage+");", 20);
	}
	else
	{
		smsbox_contentarray[smsbox_currentpage][3] = 0;
		smsbox_busy = 0;
	}
}

function smsbox_blendout(smsbox_currentpage)
{
	if(smsbox_contentarray[smsbox_currentpage][3]<smsbox_steps)
	{
		if(smsbox_contentarray[smsbox_currentpage][3] < (smsbox_steps/2))
		{
			var color = (smsbox_startcolor/((smsbox_steps/2)-1))*smsbox_contentarray[smsbox_currentpage][3];
			color = 'rgb('+color+','+color+','+color+')';
			document.getElementById('smsbox_content_nr' + smsbox_currentpage).style.color = color;
		}
		else
		{
			document.getElementById('smsbox_content_nr' + smsbox_currentpage).style.maxHeight = (smsbox_contentarray[smsbox_currentpage][4] * (1-((smsbox_contentarray[smsbox_currentpage][3]-(smsbox_steps/2))/((smsbox_steps/2)-1)))) + "px";
		}	
		smsbox_contentarray[smsbox_currentpage][3]++;
		setTimeout("smsbox_blendout("+smsbox_currentpage+");", 20);
	}
	else
	{
		document.getElementById('smsbox_content_nr' + smsbox_currentpage).innerHTML = '';
		smsbox_contentarray[smsbox_currentpage][3] = 0;
		smsbox_busy = 0;
	}
}

function smsbox_input_onfocus()
{
	//alert("InputBox");
	if(document.getElementById('smsbox_input').value == 'Schreibe eine Nachricht...')
	{document.getElementById('smsbox_input').value = ''}
	document.getElementById('smsbox_input').rows = 10;
	
	var SendButtonHTML = '<input type="submit" value="Absenden" onClick="smsbox_absenden_onclick()"/>';
	
	document.getElementById('smsbox_sendbutton').innerHTML = SendButtonHTML;
	
	document.getElementById('smsbox_input').style.color = "#000000";
	document.getElementById('smsbox_input').style.borderColor="#D5AF07";
}

function smsbox_input_onblur()
{
	//alert(document.getElementById('smsbox_input').value);
	if((document.getElementById('smsbox_input').value == 'Schreibe eine Nachricht...') || (document.getElementById('smsbox_input').value == ''))
	{
		document.getElementById('smsbox_input').rows = 2;
		document.getElementById('smsbox_input').value = 'Schreibe eine Nachricht...';
		document.getElementById('smsbox_sendbutton').innerHTML = '';
	}
	document.getElementById('smsbox_input').style.color = "#A0A0A0";
	document.getElementById('smsbox_input').style.borderColor="#E0E0E0";
}

function smsbox_name_onfocus()
{
	if(document.getElementById('smsbox_name').value == 'Dein Name')
	{document.getElementById('smsbox_name').value = ''}
		
	var SendButtonHTML = '<input type="submit" value="Absenden" onClick="smsbox_absenden_onclick()"/>';
	
	document.getElementById('smsbox_sendbutton').innerHTML = SendButtonHTML;
	
	document.getElementById('smsbox_name').style.color = "#000000";
	document.getElementById('smsbox_name').style.borderColor="#D5AF07";
}

function smsbox_name_onblur()
{
	//alert(document.getElementById('smsbox_input').value);
	if((document.getElementById('smsbox_name').value == 'Dein Name') || (document.getElementById('smsbox_name').value == ''))
	{
		document.getElementById('smsbox_name').value = 'Dein Name'
		document.getElementById('smsbox_sendbutton').innerHTML = '';
	}
	document.getElementById('smsbox_name').style.color = "#A0A0A0";
	document.getElementById('smsbox_name').style.borderColor="#E0E0E0";
}

function smsbox_absenden_onclick()
{
	if((document.getElementById('smsbox_input').value == '') || (document.getElementById('smsbox_input').value == 'Schreibe eine Nachricht...'))
	{alert("Bitte gib einen Text ein.");}
	else
	{
		if(smsbox_userid < 2)
		{Data = '&absender='+encodeURIComponent(document.getElementById('smsbox_name').value);}
		else
		{Data = '';}
		var myAjax = new Ajax.Request(
			"/smsbox/worker.php",
			{
				method: 'post', 
				parameters: 'action=PostContent&text='+encodeURIComponent(document.getElementById('smsbox_input').value)+Data,
				onComplete: function(originalRequest) 
				{
					var Antwort = originalRequest.responseText;
					//alert('Eingetragen: '+Antwort);	
					if(Antwort == 'Erfolgreich eingetragen...')
					{
						document.getElementById('smsbox_input').rows = 2;
						document.getElementById('smsbox_input').value = 'Schreibe eine Nachricht...'
						document.getElementById('smsbox_sendbutton').innerHTML = '';
						
						smsbox_contentarray = [];
						smsbox_currentpage = 1;
						smsbox_nextpage(-1);
					}
					else if(Antwort == 'Du musst mindestens 30 Sekunden warten.')
					{alert('Du musst mindestens 30 Sekunden warten.');}
					else
					{alert('Es ist ein Fehler aufgetreten: '+Antwort);}
				}
			}
		);	
	}
}

function smsbox_delete_onclick(post_id)
{
	var myAjax = new Ajax.Request(
		"/smsbox/worker.php",
		{
			method: 'post', 
			parameters: 'action=DeleteContent&deletepostid='+post_id,
			onComplete: function(originalRequest) 
			{
				var Antwort = originalRequest.responseText;
				if(Antwort == 'Beitrag wurde erfolgreich enfernt.')
				{
					alert('Beitrag wurde erfolgreich enfernt.');
					smsbox_contentarray = [];
					smsbox_currentpage = 0;
					smsbox_nextpage(0);
				}
				else if(Antwort == 'Du bist nicht Verfasser dieses Beitrags')
				{alert('Du bist nicht Verfasser dieses Beitrags');}
				else
				{alert('Es ist ein Fehler aufgetreten');}
			}
		}
	);
}
