//Das Objekt, das gerade bewegt wird.
var dragobjekt = false;
var openChatTimeouter = true;
var openChatTimeouter;
var chatRefresher = true;
var makeTrue;

document.onmousemove = drag;
document.onmouseup = dragstop;

// Position, an der das Objekt angeklickt wurde.
// relativ zur oberen, rechten Ecke des Objekts
var dragx = 0;
var dragy = 0;

// Mausposition
// relativ zur oberen, rechten Ecke des Fensters
var posx = 0;
var posy = 0;

function dragstart(element) {
	//Wird aufgerufen, wenn ein Objekt bewegt werden soll.
	//Parameter element: Das zu bewegende Objekt.

	dragobjekt = $(element);
	dragx = posx - dragobjekt.offsetLeft;
	dragy = posy - dragobjekt.offsetTop;
}


function dragstop() {
	//Wird aufgerufen, wenn ein Objekt nicht mehr bewegt werden soll.
	dragobjekt.style.opacity = '1';
	
	dragobjekt=null;
}


function drag(ereignis) {
	//Wird aufgerufen, wenn die Maus bewegt wird und bewegt bei Bedarf das Objekt.
	posx = document.all ? window.event.clientX : ereignis.pageX;
	posy = document.all ? window.event.clientY : ereignis.pageY;
	
	if (dragobjekt != null) {
		if ((posx - dragx) < 1000 && (posy - dragy) < 1000) {
			dragobjekt.style.left = (posx - dragx) + "px";
			dragobjekt.style.top = (posy - dragy) + "px";
		}
	}
	
	dragobjekt.style.opacity = '0.5';
}

function chatClose(path, chat_id) {

	new Ajax.Request(
		path + 'ajax_works.php?mode=chatClose',
		{
			method: 'post',
			parameters: 'chat_id=' + chat_id,
			onComplete: function (x) {
				if (x.responseText == 'no error') {
					$('boxChat' + chat_id).style.display = 'none';
				} else {
					alert(x.responseText);
				}
			}
		}
	);

}

function bbcodeChat(string) {

	string = string.replace(/\+(.*)\+/, "<strong>$1</strong>");
	string = string.replace(/\_(.*)\_/, "<strong>$1</strong>");
	string = string.replace(/\#(.*)\#/, "<em>$1</em>");
	string = string.replace(/\*(.*)\*/, "<em>$1</em>");
	
	return string;

}

function smileImage(path, filename) {

	return '<img alt="' + filename + '" src="' + path + 'images/smiles/' + filename + '" />';

}

function smiles(path, string) {

	/*
		Smiles
	*/
	string = string.replace(/:-\)/gi, smileImage(path, 'icon_smile.gif'));
	string = string.replace(/:-\(/gi, smileImage(path, 'icon_sad.gif'));
	string = string.replace(/:-\'\(/gi, smileImage(path, 'icon_cry.gif'));
	//string = string.replace(/:-|/gi, smileImage(path, 'icon_neutral.gif'));
	string = string.replace(/:D/gi, smileImage(path, 'icon_biggrin.gif'));
	string = string.replace(/:oo/gi, smileImage(path, 'icon_rolleyes.gif'));
	string = string.replace(/;-\)/gi, smileImage(path, 'icon_wink.gif'));
	string = string.replace(/:R/g, smileImage(path, 'icon_razz.gif'));
	string = string.replace(/xD/gi, smileImage(path, 'icon_mrgreen.gif'));
	string = string.replace(/:redface:/gi, smileImage(path, 'icon_redface.gif'));
	string = string.replace(/:mad:/gi, smileImage(path, 'icon_mad.gif'));
	string = string.replace(/:lol:/gi, smileImage(path, 'icon_lol.gif'));
	string = string.replace(/:eek:/gi, smileImage(path, 'icon_eek.gif'));
	
	/*
		':-)' => 'icon_smile.gif',
		':-(' => 'icon_sad.gif',
		':-\'(' => 'icon_cry.gif',
		':-|' => 'icon_neutral.gif',
		':D' => 'icon_biggrin.gif',
		':oo' => 'icon_rolleyes.gif',
		';-)' => 'icon_wink.gif',
		':R' => 'icon_razz.gif',
		'xD' => 'icon_mrgreen.gif',
		':redface:' => 'icon_redface.gif',
		':mad:' => 'icon_mad.gif',
		':lol:' => 'icon_lol.gif',
		':eek:' => 'icon_eek.gif'
	*/
	
	return string;

}

function floodMsgOff() {

	$('postTextopenChat').disabled = false;
	$('postTextopenChat').style.color = '#0E516E';
	$('postTextopenChat').style.fontWeight = 'normal';
	$('postTextopenChat').style.cursor = 'text';
	$('postTextopenChat').value = '';
	$('postTextopenChat').focus();

}

function startChatData(path, chat_id) {
	chatData(path, chat_id);
}

function chatData(path, chat_id) {

	new Ajax.Request(
		path + 'ajax_works.php?mode=chatData',
		{
			method: 'post',
			parameters: 'chat_id=' + chat_id,
			onComplete: function (x) {
				$('chat' + chat_id).innerHTML = x.responseText.replace(/CHAT_ROOT_PATH/g, path);
				$('chat' + chat_id).scrollTop = 9999999;

				window.setTimeout("chatData('" + path + "', '" + chat_id + "');", 1000);
			}
		}
	);
}

function chatPostText(path, chat_id) {

	if (trim($('postText' + chat_id).value) != '') {	
		
		var chatAddData = '<div class="chatUsername"><strong><a href="' + path + 'user/' + $('myUsername').value + '">' + $('myUsername').value + '</a></strong></div><div class="chatText">' + smiles(path, bbcodeChat($('postText' + chat_id).value)) + '</div><hr class="chatBorder" />';
		
		$('chat' + chat_id).innerHTML = $('chat' + chat_id).innerHTML + chatAddData;
		$('chat' + chat_id).scrollTop = 9999999;		
		
		var text = $('chatForm' + chat_id).serialize();
		
		$('postText' + chat_id).value = '';
		
		new Ajax.Request(
			path + 'ajax_works.php?mode=chatPostText',
			{
				method: 'post',
				parameters: text + '&chat_id=' + chat_id,
				onComplete: function (x) {
					if (x.responseText != 'no error') {
						alert(x.responseText);
					} else {
						$('postText' + chat_id).value = '';
					}
				}
			}
		);
		
	}

}

function openChatPostText(path, chatName) {

	if (trim($('postTextopenChat').value) != '') {
		
		var theTime = new Date();
		
		if ($('antiFlooder').value != '-1' && parseInt($('antiFlooder').value) + 500 > theTime.getTime()) {
		
			$('postTextopenChat').disabled = true;
			$('postTextopenChat').style.color = '#D70000';
			$('postTextopenChat').style.fontWeight = 'bold';
			$('postTextopenChat').style.cursor = 'default';
			$('postTextopenChat').value = 'Bitte floode nicht, du zerstörst ansonsten die Übersichtlichkeit und belästigst andere Chatter!';
			
			window.setTimeout("floodMsgOff()", 3000);
			
		} else {
		
			/*
				Schimpfwörter
			*/
			$('postTextopenChat').value = $('postTextopenChat').value.replace(/ficken/gi, 'f*cken');
			$('postTextopenChat').value = $('postTextopenChat').value.replace(/ficken/gi, 'f*cken');
			$('postTextopenChat').value = $('postTextopenChat').value.replace(/sau/gi, 'weibliches Schwein (Sus scrofa domestica)');
			$('postTextopenChat').value = $('postTextopenChat').value.replace(/schlampe/gi, 'Schl*mpe');
			$('postTextopenChat').value = $('postTextopenChat').value.replace(/fotze/gi, 'Vagina');
			$('postTextopenChat').value = $('postTextopenChat').value.replace(/arsch/gi, 'Gesäß');
			
			if ($('is_admin').value == 'yes') {
				var adminImage = '<img src="' + path + 'style/imageset/admin.png" alt="" title="Administrator" style="vertical-align: -2px; margin-right: 4px;" />';
			} else {
				var adminImage = '';
			}			
			
			if ($('postTextopenChat').value.match(/^\/me (.*?)$/)) {
				$('openChatTemp').innerHTML = $('openChatTemp').innerHTML + '<div class="openChatMsg">' + $('myUsername').value + ' ' + bbcodeChat($('postTextopenChat').value.replace(/\/me/, '')) + '</div>';
			} else if ($('postTextopenChat').value.match(/^\/f (.*?) (.*?)$/)) {
				$('openChatTemp').innerHTML = $('openChatTemp').innerHTML + '<div class="openChatMsg">' + bbcodeChat($('postTextopenChat').value.replace(/\/f (.*?) (.*?)/, 'Flüstere zu $1: $2')) + '</div>';
			} else if ($('postTextopenChat').value.match(/^\/sepnew (.*?)$/)) {
				if ($('postTextopenChat').value.match(/^\/sepnew [a-z|A-Z|0-9]+$/)) {
					location.href = path + 'chats/' + $('postTextopenChat').value.replace(/\/sepnew (.*?)/, '$1');
				} else {
					alert('Bitte benutze für Raumnamen nur Buchstaben und Zahlen!');
				}
			} else if ($('postTextopenChat').value.match(/^\/join (.*?)$/)) {
				if ($('postTextopenChat').value.match(/^\/join [a-z|A-Z|0-9]+$/)) {
					location.href = path + 'chats/' + $('postTextopenChat').value.replace(/\/join (.*?)/, '$1');
				} else {
					alert('Bitte benutze für Raumnamen nur Buchstaben und Zahlen!');
				}
			} else if ($('postTextopenChat').value.match(/^\^+(.*?)$/)) {
				if ($('is_admin').value == 'yes') {
					$('openChatTemp').innerHTML = $('openChatTemp').innerHTML + '<div class="openChatLine"><div class="openChatUsername">' + adminImage + '<strong><a style="color: ' + $('myUserColor').value + ';" target="_blank" href="' + path + 'user/' + $('myUsername').value + '">' + $('myUsername').value + '</a></strong><img src="' + path + 'style/imageset/icons/chat.png" style="margin-left: 8px; vertical-align: -4px; cursor: pointer;" alt="chat" style="width: 16px; height: 16px;" title="Privatchat" onclick="javascript:startPrivateChat(\'' + path + '\', \'44\');" /></div><div class="openChatDoublePoint">:</div><div class="openChatText" style="color: #FF0000;">' + smiles(path, bbcodeChat($('postTextopenChat').value.replace(/\^+/, ''))) + '</div></div><div style="clear: both;"></div>';
				} else {
					$('openChatTemp').innerHTML = $('openChatTemp').innerHTML + '<div class="openChatLine"><div class="openChatUsername"><strong><a style="color: ' + $('myUserColor').value + ';" target="_blank" href="' + path + 'user/' + $('myUsername').value + '">' + $('myUsername').value + '</a></strong><img src="' + path + 'style/imageset/icons/chat.png" style="margin-left: 8px; vertical-align: -4px; cursor: pointer;" alt="chat" style="width: 16px; height: 16px;" title="Privatchat" onclick="javascript:startPrivateChat(\'' + path + '\', \'44\');" /></div><div class="openChatDoublePoint">:</div><div class="openChatText">' + smiles(path, bbcodeChat($('postTextopenChat').value.replace(/\^+/, ''))) + '</div></div><div style="clear: both;"></div>';
				}
			} else if (!$('postTextopenChat').value.match(/^\/(.*?)$/)) {
				$('openChatTemp').innerHTML = $('openChatTemp').innerHTML + '<div class="openChatLine"><div class="openChatUsername">' + adminImage + '<strong><a style="color: ' + $('myUserColor').value + ';" target="_blank" href="' + path + 'user/' + $('myUsername').value + '">' + $('myUsername').value + '</a></strong><img src="' + path + 'style/imageset/icons/chat.png" style="margin-left: 8px; vertical-align: -4px; cursor: pointer;" alt="chat" style="width: 16px; height: 16px;" title="Privatchat" onclick="javascript:startPrivateChat(\'' + path + '\', \'44\');" /></div><div class="openChatDoublePoint">:</div><div class="openChatText">' + smiles(path, bbcodeChat($('postTextopenChat').value)) + '</div></div><div style="clear: both;"></div>';
			}
			
			$('openChatScroller').scrollTop = $('openChatScroller').scrollHeight;
			
			var text = $('openChatForm').serialize();

			$('postTextopenChat').value = '';
			
			new Ajax.Request(
				path + 'ajax_works.php?mode=openChatPostText',
				{
					method: 'post',
					parameters: text + '&chatName=' + chatName,
					onComplete: function (x) {
						if (x.responseText != 'no error') {
							alert(x.responseText);
						} else {
							theTime = new Date();
							
							$('antiFlooder').value = theTime.getTime();
							
							clearTimeout(makeTrue);
							makeTrue = window.setTimeout("chatRefresher = true; startOpenChatData('" + path + "', '" + chatName + "');", 1500);
						}
					}
				}
			);
			
		}
	}

}

function openChatData(path, chatName) {

	if (chatRefresher == true) {

		new Ajax.Request(
			path + 'ajax_works.php?mode=openChatData',
			{
				method: 'post',
				parameters: 'chatName=' + chatName,
				onComplete: function (x) {
				
					if (x.responseText == 'kicked') {
					
						$('openChat').innerHTML = '<div class="openChatMsg"><img src="' + path + 'style/imageset/icons/error.png" alt="kicked" /> Du wurdest aus diesem Chat von einem Administrator gekickt.</div>';
						$('openChatScroller').scrollTop = $('openChatScroller').scrollHeight;			
						$('postTextopenChat').disabled = true;
						
					} else if (x.responseText == 'banned') {
					
						$('openChat').innerHTML = '<div class="openChatMsg"><img src="' + path + 'style/imageset/icons/error.png" alt="banned" /> Du wurdest aus diesem Chat von einem Administrator gebannt.</div>';
						$('openChatScroller').scrollTop = $('openChatScroller').scrollHeight;			
						$('postTextopenChat').disabled = true;
					
					} else {
						var data = x.responseText.replace(/CHAT_ROOT_PATH/g, path);
						
						$('openChat').innerHTML = data;
						$('openChatTemp').innerHTML = '';
						$('openChatScroller').scrollTop = $('openChatScroller').scrollHeight;
					}
				}
			}
		);
	
	}
}

function startOpenChatData(path, chatName) {
	if (chatRefresher == true) {
		openChatData(path, chatName);
	
		window.setTimeout("startOpenChatData('" + path + "', '" + chatName + "')", 2500);
	}
}


function startPrivateChat(path, user_id) {

	new Ajax.Request(
		path + 'ajax_works.php?mode=startPrivateChat',
		{
			method: 'post',
			parameters: 'user_id=' + user_id,
			onComplete: function (x) {

				var id = x.responseText;
			
				location.href = location.href;
			}
		}
	);
}

function markMove(id) {
	//$(id).style.opacity = '0.9';
}

function unmarkMove(id) {
	//$(id).style.opacity = '1';
}

var slidingExport;

function slideIn(id, height) {

	var styleHeight = parseInt($(id).style.height.replace(/px/, ''));
	
	if (styleHeight < height && slidingExport) {
		window.setTimeout("slideIn('" + id + "', '" + height + "')", 1);
		
		d_block(id);
		$(id).style.overflow = 'hidden';
		$(id).style.height = styleHeight + 5 + 'px';
	}
}

function slideOut(id, height) {

	var styleHeight = parseInt($(id).style.height.replace(/px/, ''));
	
	if (styleHeight > 0 && !slidingExport) {
		window.setTimeout("slideOut('" + id + "', '" + height + "')", 1);
		
		$(id).style.overflow = 'hidden';
		$(id).style.height = styleHeight - 5 + 'px';
	}
}

function colorPalette(path, dir, width, height, mode, tName)
{
	var r = 0, g = 0, b = 0;
	var numberList = new Array(6);
	var color = '';

	numberList[0] = '00';
	numberList[1] = '40';
	numberList[2] = '80';
	numberList[3] = 'BF';
	numberList[4] = 'FF';

	document.writeln('<table cellspacing="1" cellpadding="0" border="0">');

	for (r = 0; r < 5; r++)
	{
		if (dir == 'h')
		{
			document.writeln('<tr>');
		}

		for (g = 0; g < 5; g++)
		{
			if (dir == 'v')
			{
				document.writeln('<tr>');
			}
			
			for (b = 0; b < 5; b++)
			{
				color = String(numberList[r]) + String(numberList[g]) + String(numberList[b]);
				document.write('<td bgcolor="#' + color + '" style="width: ' + width + 'px; height: ' + height + 'px;">');
				
				if (mode == 0) {
					document.write('<span style="cursor: pointer;" onclick="javascript:openChatChangeUserColor(\'' + path + '\', \'' + color + '\');"><img src="' + path + 'style/imageset/spacer.gif" width="' + width + '" height="' + height + '" alt="#' + color + '" title="#' + color + '" /></span>');
				} else {
					document.write('<span style="cursor: pointer;" onclick="javascript:insert_bbcode(\'[color=#' + color + ']\', \'[/color]\', \'' + tName + '\');"><img src="' + path + 'style/imageset/spacer.gif" width="' + width + '" height="' + height + '" alt="#' + color + '" title="#' + color + '" /></span>');
				}
				
				document.writeln('</td>');
			}

			if (dir == 'v')
			{
				document.writeln('</tr>');
			}
		}

		if (dir == 'h')
		{
			document.writeln('</tr>');
		}
	}
	document.writeln('</table>');
}

function change_palette()
{
	dE('colour_palette');
	e = document.getElementById('colour_palette');
	
	if (e.style.display == 'block')
	{
		document.getElementById('bbpalette').value = 'Schriftfarbe ausblenden';
	}
	else
	{
		document.getElementById('bbpalette').value = 'Schriftfarbe';
	}
}

function openChatShowCP() {
	$('openChatBoxAll').style.height = 640 + 'px';
	$('openChatData').style.height = 610 + 'px';
	d_block('colorPallette');
}

function openChatChangeUserColor(path, color) {

	new Ajax.Request(
		path + 'ajax_works.php?mode=openChatChangeUserColor',
		{
			method: 'post',
			parameters: 'color=' + color,
			onComplete: function (x) {
				if (x.responseText != 'no error') {
					alert(x.responseText);
				} else {
					$('openChatBoxAll').style.height = 550 + 'px';
					$('openChatData').style.height = 520 + 'px';
					d_none('colorPallette');
					
					$('myUserColor').value = '#' + color;
				}
			}
		}
	);

}

function firstStart(path, chatName) {

	new Ajax.Request(
		path + 'ajax_works.php?mode=firstStart',
		{
			method: 'post',
			parameters: 'chatName=' + chatName,
			onComplete: function (x) {
				if (x.responseText != 'no error') {
					alert('Error when starting: ' + x.responseText);
				}
			}
		}
	);

}

function openChatOnlineList(path, chatName) {

	new Ajax.Request(
		path + 'ajax_works.php?mode=openChatOnlineList',
		{
			method: 'post',
			parameters: 'chatName=' + chatName,
			onComplete: function (x) {
				$('onlineListChatUL').innerHTML = x.responseText;
			}
		}
	);
	
	window.setTimeout("openChatOnlineList('" + path + "', '" + chatName + "')", 8000);
	
}
