var username;
var c_id;
var c_specialaction;
var loggedin;
var moa;
var chatframe;

var c_width_min = 240;
var c_width_start = 240;
var c_height_min = 140;
var c_height_start = 140;

var c_infoboxtimer;
var c_mouseoninfobox;
var c_mouseoninfolink;

var privatuser;



function c_checklogout() {
	var pt_txt = $('#pt').html();
	if (pt_txt != null) {
		document.location.reload();
	}
}

function c_createinfobox(t,tuser,twindow) {
	if (moa == 'mod' || moa == 'admin') {
		var scoutlink = '<a href="/scout/?uname='+tuser+'" onmouseover="c_infoboxstatus(\'scout\');" onmouseout="c_infoboxstatus();" onclick="return cf_link(this);"><img src="/images/user_edit.png" width="16" height="16" alt="'+tuser+'s Scoutadmin öffnen" /></a>';		
	} else {
		var scoutlink = '';
	}
	
	c_removeinfobox();
	$(t).addClass('infoboxlinkborder');

	var offset = $(t).position();
	var left = offset.left-10;
	var top = offset.top - 59;

	c_mouseoninfobox = 0;
	
	if (is_cf()) {
		var z = $('#chat');
		var cf_parent = 'window.parent.LH.';
		var privatchat = 'cf_smiley(\'/p '+tuser+'\');';
	} else {
		var z = $("#chat .chat[rel='"+twindow+"']");
		var cf_parent = '';
		var privatchat = 'c_create(\''+tuser+'\');';
	}
	
	z.append('<div class="infobox" onmouseover="c_infoboxmouse(\'1\');" onmouseout="c_infoboxmouse(\'0\');" style="left: '+left+'px;top: '+top+'px;"><b>Quickmen&uuml;:</b><br /><a href="javascript:void(0);" onclick="'+privatchat+'c_removeinfobox();" onmouseover="c_infoboxstatus(\'pchat\');" onmouseout="c_infoboxstatus();"><img src="/images/fluestern.png" width="16" height="16" alt="'+tuser+' anflüstern" /></a><a href="/profil/'+tuser+'/" onmouseover="c_infoboxstatus(\'profil\');" onmouseout="c_infoboxstatus();" onclick="return cf_link(this);"><img src="/images/profil.png" width="16" height="16" alt="'+tuser+'s Profil anschauen" /></a><a href="javascript:void(0);" onclick="'+cf_parent+'urltolayer(\'\',\'/anklopfen@'+tuser+'\',600,200);"onmouseover="c_infoboxstatus(\'anklopfen\');" onmouseout="c_infoboxstatus();"><img src="/images/icon_anklopfen.png" width="16" height="16" alt="Bei '+tuser+' anklopfen" /></a><a href="/msg@'+tuser+'" onmouseover="c_infoboxstatus(\'msg\');" onmouseout="c_infoboxstatus();" onclick="return cf_link(this);"><img src="/images/icon_msg_neu.png" width="16" height="16" alt="'+tuser+' eine Nachricht senden" /></a>'+scoutlink+'<br /><div class="infoboxstatus"></div></div>');
	if (moa == 'mod' || moa == 'admin') {$(".infobox").css("width","140px");}
	c_mouseoninfolink = 1;
	c_infoboxtimer = window.setInterval("c_chkremoveinfobox()", 250);

}

var debugcounter = 0;
var c_debug_aktiv = false;

function c_debug(txt) {
	if (c_debug_aktiv) {
		$("#chat .chat[rel='*DEBUG*'] .talk").append("<div><b>"+debugcounter+"</b>: "+txt+"</div>");
		$("#chat .chat[rel='*DEBUG*'] .talk").scrollTo({top:'100%',left: '0'},0);
		debugcounter++;
	}
}


function c_startdebug() {
	c_create('*DEBUG*');
	c_debug_aktiv=true;
}

function c_infoboxmouse(d) {
	if (d == '1') {
		c_mouseoninfobox=1;
		c_mouseoninfolink=0;
	} else if (d=='2') {
		c_mouseoninfobox=0;
		c_mouseoninfolink=1;	
	} else {
		c_mouseoninfobox=0;
		c_mouseoninfolink=0;
	}
}

function c_chkremoveinfobox() {
	// wo ist der mauszeiger
	if (c_mouseoninfobox =='0' && c_mouseoninfolink=='0') {
		window.clearInterval(c_infoboxtimer);
		c_removeinfobox();
	}
}



function c_removeinfobox() {
	$("#chat a.writer").removeClass('infoboxlinkborder');
	$('.infobox').fadeOut(200,function() {
		$(this).remove();
	});
}

function c_infoboxstatus(action) {
	if (action == null) {
		$('.infoboxstatus').html('').css('left','0px');
	} else if (action == 'pchat') {
		$('.infoboxstatus').html('Privatchat');
	} else if (action == 'anklopfen') {
		$('.infoboxstatus').html('Anklopfen').css('left','45px');
	} else if (action == 'msg') {
		$('.infoboxstatus').html('Nachricht').css('left','75px');
	} else if (action == 'scout') {
		$('.infoboxstatus').html('Scout').css('left','110px');
	} else if (action == 'profil') {
		$('.infoboxstatus').html('Profil').css('left','28px');
	} else {
		$('.infoboxstatus').html('').css('left','0px');
	}
}


function c_checksize() {
	c_debug('c_checksize');
	getSize();
	$("#chat .chat").each(function(){
		var user = $(this).attr('rel');
		var left = parseInt($(this).css("left"));
		var width = parseInt($(this).css("width"));
		var height = parseInt($(this).css("height"));
		
		if ((left + width + 25) > myWidth) {
			var newwidth = myWidth - left - 25;
			$(this).animate({"width":newwidth+"px"},function(){
				c_sync_inputwidth();
				c_conf(user,'resize',$(this).css('width')+'|'+$(this).css('height'));
			});
		}
		
		if ((height+25) > myHeight) {
			var newheight = myHeight-25;
			$(this).animate({"height":newheight+"px"},function(){
				c_conf(user,'resize',$(this).css('width')+'|'+$(this).css('height'));
			}).find(".talk").animate({"height":newheight-52+"px"});
		}
	});
}



var c_counter = 0;
c_id = "init";
function c_get() {
	var g_id = "?cc="+c_counter;
	c_counter++;
	
	if (c_id) {
		g_id += "&id="+c_id; 
	}
	
	if (chatframe == 1) {
		g_id +="&chatframe=1";
	}
	
	if (c_specialaction) {
		for (i=0;i<c_specialaction.length;i++) {
			g_id += "&specialaction[]="+c_specialaction[i];
		}
	}
	

	
	c_debug('c_get('+g_id+')');
	
	$.ajax({
  	url: "/chat/get.php"+g_id,
  	cache: false,
  	success: function(html){
  		eval(html);
  	}
	});
}

function c_post(to,msg) {
	c_debug('c_post('+to+','+msg+')');
	if (to == '*DEBUG*') { eval(msg);}
	if (to===undefined || to=='' || to == '*'){to=0;}
	$.ajax({
  	url: "/chat/post.php?to="+encodeURIComponent(to)+"&msg="+encodeURIComponent(msg),
  	cache: false,
  	success: function(html){
  		eval(html);
  		c_get();
  	}
	});
}

function is_cf() {
	if (window.name != "" || chatframe == 1) {
		return true;
	}
}


function c_check(aktiv) {
	if (is_cf() && aktiv == 0) {
		c_popin();	
	} else if (!is_cf() && aktiv == 1) {
		c_popout();
	}
}

function c_popout() {
	var siteurl  = '#/'+document.location.href.substring(document.location.href.indexOf('de/')+3, document.location.href.length);
	top.document.location.href="http://www.livinghandy.de/chat/"+siteurl;
	return false;
}

function c_popin() {
	$.ajax({
	  url: '/chat/conf.php?key=popin',
	  cache: false,
	  async: false
	});

	top.document.location.href = window.parent.LH.document.location.href;
}



function cf_scroll() {
	var elem = $('#chatscroll');
	var inner = $('#chat');
	if (elem.height() + elem.offset().top + Math.abs(inner.offset().top) >= inner.outerHeight() - 100) {
			$("#chatscroll").scrollTo({top:'100%',left: '0'},0);
	}
}

function cf_key(t,e) {
	var code = e.keyCode;
	if (code == 13) { 
		c_debug('cf_key(enter)');
		c_post('*',$(t).attr('value'));
		$(t).attr('value','');
		return false;
	} else if (code == 9) {
		if (privatuser) {
			$('#input input').val('/p '+privatuser+' ');
		}
		return false;
	}
}


function cf_size() {
	var viewportwidth;
 	var viewportheight;
	
	if (typeof window.innerWidth != 'undefined') {
      viewportwidth = window.innerWidth,
      viewportheight = window.innerHeight
	} else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
		viewportwidth = document.documentElement.clientWidth,
		viewportheight = document.documentElement.clientHeight
	} else {
		viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
		viewportheight = document.getElementsByTagName('body')[0].clientHeight
	}
	
	var chatheight = viewportheight - 93 - 6 - 20;
	$('#chatscroll').css('height',chatheight+"px");
	
	$('#chatscroll').css('width',viewportwidth-2+"px");
	$('#header').css('width',viewportwidth-2+"px");
	$('#input input').css('width',viewportwidth-8+"px");
	//alert(viewportwidth);
	
	
	cf_scroll();
}


	
function c_do(id,writer,age,sex,msg,time,receiver,chat_init,scoutrang) {

	c_debug('c_do('+id+','+writer+','+age+','+sex+','+msg+','+time+','+receiver+','+chat_init+','+scoutrang+')');
	if (id <= c_id) {
		// Die Nachricht wurde bereits angezeigt - also nicht nochmal anzeigen!
		// passiert u.a. weil nach post direkt neu geladen wird - da können sich
		// evtl. zwei gets überschneiden
	} else {
		
		c_id = id;
		
		if (receiver=='0') {
			//message den chat
			ziel = '*';
			var privtext = '';
		} else {
			//private message
			
			if (is_cf()) {
					msg = "<b>"+msg+"</b>";
			}

			if (receiver==username) {
				// empfangene nachricht
				ziel = writer;
				var privtext = '(privat&nbsp;an&nbsp;dich)';
				privatuser = writer;
			} else {
				// gesendete nachricht
				ziel = receiver;
				var privtext = '(privat&nbsp;an&nbsp;'+receiver+')';
			}
		}
		
		
		if (!is_cf()) {		
			var privtext = '';
			
			// existiert zielfenster oder ist versteckt?
			if ($("#chat .chat[rel='"+ziel+"']").html() == null || $("#chat .chat[rel='"+ziel+"']").css('display') == 'none') {
				c_create(ziel);
			}
			
			// zielfenster minimiert?
			if ($("#chat .chat[rel='"+ziel+"'] .inhalt").css('display') == 'none') {
				c_addmsganz(ziel);
			}
		}
		
		if (sex == 'w') {
			var sexicon = '<img src="http://img.livinghandy.de/images/icon_female.gif" width="5" height="8" alt="weiblich" title="weiblich" />';
		} else {
			var sexicon = '<img src="http://img.livinghandy.de/images/icon_male.gif" width="8" height="8" alt="männlich" title="männlich" />';
		}

		if (is_cf()) {
			sexicon = '<a href="javascript:void(0);" onclick="insert_smiley(\'#input input\',\'@'+writer+'\');">'+sexicon+'</a>';
		} else {
			sexicon = '<a href="javascript:void(0);" onclick="insert_smiley(\'#chat .chat[rel='+ziel+'] input\',\'@'+writer+'\');">'+sexicon+'</a>';
		}
		
		var agensex = "<span class=\"small\">"+age+", "+sexicon+"</span>";
		var pretext = ":  ";
		
		if (msg.substring(0,4) == '/me ') {
			msg = msg.split("/me ").join("");
			agensex = "";
			pretext = "";		
		}
		
		var scoutclass='';
		if (scoutrang == 'mod') {
			scoutclass = ' moderator';
		} else if (scoutrang == 'admin') {
			scoutclass = ' admin';
		}
	
		if (scoutrang == 'ace') {
			msg = "<div id=\"chat_"+id+"\">"+msg+"</div>";
		} else {
			msg = "<div id=\"chat_"+id+"\"><span class=\"smaller\">"+time+"</span><a href=\"/profil/"+writer+"/\" onmouseout=\"c_infoboxmouse('0');\" onmouseover=\"c_infoboxmouse('2');\" onclick=\"c_createinfobox(this,'"+writer+"','"+ziel+"');return false;\" class=\"writer"+scoutclass+"\"><b>"+writer+"</b></a>"+agensex+"&nbsp;"+privtext+pretext+"<span class=\"chatmsg\">"+msg+"</span></div>";
		}
		
		
		if (!is_cf()) {
			$("#chat .chat[rel='"+ziel+"'] .talk").append(msg);
		} else {
			$("#chat").append(msg);
		}
		
		if (c_getfirstpost(ziel) == id) {
			c_conf(ziel,'open',id);
		}
		
		if (receiver==username && chat_init != "1" && !is_cf()) {
			AudioPlayer.embed("audioplayer", { soundFile: "/sound/pmsg.mp3"});
		}
		
		if (!is_cf()) {
			c_scroll(ziel);
		} else {
			cf_scroll();
			cf_linkmaker(id);
		}
	}
}

function c_addtxt() {

}

function c_key(t,e) {
	var code = e.keyCode;
	if (code == 13) { 
		c_debug('c_key(enter)');
		c_post($(t).parent().parent().parent().attr('rel'),$(t).attr('value'));
		$(t).attr('value','');
		return false;
	}
}


var c_intval = false;
var c_zIndex = 500;

function c_scroll(user) {
	c_debug('c_scroll('+user+')');
	if ($("#chat .chat[rel='"+user+"'] .talk div").html() != ''){
		$("#chat .chat[rel='"+user+"'] .talk").scrollTo({top:'100%',left: '0'},0);
	}
}

function c_create(user,max,left,width,height) {
	c_debug('c_create('+user+','+max+','+left+','+width+','+height+')');

if (user != "") {
	// existiert es schon?
	if ($("#chat .chat[rel='"+user+"']").html() == null) {
		if (max == null) { var max = 1; var slide_display='none';if (user == '*') {var left = 770;slide_display='block';}} else {var slide_display = 'block';}
		
		if (left == null) { var left=10;}
		if (height == null) { var height = c_height_start;}
		if (width == null) { var width = c_width_start;}
		var input_width = width - 10;
		getSize();
		var c_height_max = myHeight - 10;
		var c_width_max = myWidth-left-30;
		
		if (width > c_width_max) {
			width = c_width_max;
			if (c_width_min > width) {
				left=0;
			}
		} 


		if (width < c_width_min) {
			width = c_width_min;
		}
		
		if (height > c_height_max) {
			height = c_height_max;
		}
		
		if (height < c_height_min) {
			height = c_height_min;
		}
		
		var theight = height - 52;
		
		if (max == '1') {
			var bottom = '0';
			var icon_minmax = '/css/img/c_minimize.png';
		} else {
			var bottom= height - 25;
			var icon_minmax = '/css/img/c_maximize.png';
		}
		
		var close;

		if (user == '*') {
			user_chatmit = 'LivingHandy.de';
			close = '<a href="/chat/" style="margin-right: 3px;" onclick="return c_popout();"><img src="/css/img/c_popout.png" width="7" height="7" alt="Chat im Frame öffnen" /></a>';
		} else { 
			user_chatmit = user;
			close = '<a href="javascript:void(0);" style="margin-right: 3px;" onclick="return c_remove(\''+user+'\');"><img src="/css/img/c_close.png" width="7" height="7" alt="Close" /></a>';
		}
		
		
		
		
		
		
		var c_smileys = "<div style=\"display: inline;position: relative; margin-right:2px;\"><a href=\"#\" onclick=\"smiley(this,'#chat .chat[rel="+user+"] input');return false;\"><img src=\"/css/img/c_smiley.png\" width=\"7\" height=\"7\" alt=\"Smileys\" /></a><div class=\"smiley\" style=\"top:20px;\"></div></div>";
		
		var c = '<div class="chat community" rel="'+user+'" style="display: '+slide_display+';left: '+left+'px;width: '+width+'px;height: '+height+'px;bottom:-'+bottom+'px">	<div class="topbg community"><span><div class="left"><b>Chat mit '+user_chatmit+'</b></div><div class="right">'+c_smileys+'<a href="javascript:void(0);" onclick="return c_toggle(\''+user+'\');" class="c_minmax"><img src="'+icon_minmax+'" width="7" height="7" alt="MinMax" /></a>'+close+'</div></span></div>	<div class="inhalt">		<div class="talk" style="height: '+theight+'px;"></div>		<div class="input"><input type="text" name="message" value="" autocomplete="off" id="message" maxlength="160" rel="" onkeydown="return c_key(this,event);" style="width:'+input_width+'px" /></div></div></div>';
		$("#chat").append(c);
	
		$("#chat .chat[rel='"+user+"']")
		.mousedown(function() {
			c_totop(user);
		});
	
		$("#chat .chat[rel='"+user+"']").draggable({ 
			axis: 'x', 
			cursor: 'e-resize', 
			handle: '.topbg',
			containment: "body",
			stop: function() {
				c_conf(user,'left',$(this).css('left'));
			}
		});
		
		if (slide_display == 'none') {
			$("#chat .chat[rel='"+user+"']").slideDown('fast',function() {
					$("#chat .chat[rel='"+user+"'] input").focus();
			});
		}
		
		if (max == '1') {
			c_resize(user,true);
		}
		
		if (!loggedin) {
			$("#chat .chat[rel='"+user+"'] input").val('Bitte einloggen...').attr('disabled','disabled');
		}
		
		c_sync_inputwidth();

		// trägt es in die DB ein...
		c_conf(user,'left',left);
		
	} else if ($("#chat .chat[rel='"+user+"']").css('display')=='none') {
		// existiert, aber ist versteckt
		c_toggle(user);
		$("#chat .chat[rel='"+user+"']").slideDown('fast');
	}
	// in jedem fall genau dieses neue fenster in den vordergrund bringen...
	c_totop(user);
	
	// und focus?
	$("#chat .chat[rel='"+user+"'] input").focus();
}
}

function c_conf(user,key,value){
	c_debug('c_conf('+user+','+key+','+value+')');
	$.ajax({
	  url: '/chat/conf.php?user='+user+'&key='+key+'&value='+value,
	  cache: false
	});
}


function c_sync_inputwidth() {
	c_debug('c_sync_inputwidth()');
	$("#chat .chat").each(function(){
		var inputwidth = parseInt($(this).css("width")) - 10;
		$(this).find(".input input").css("width",inputwidth+"px")
	});
}


function c_resize(user,action) {
	c_debug('c_resize('+user+','+action+')');
	if (action) {
		$("#chat .chat[rel='"+user+"']").resizable({
			minHeight: c_height_min,
			minWidth: c_width_min,
			handles: 'ne,n,e,',
			alsoResize: "#chat .chat[rel='"+user+"'] .talk",
			start: function () {
				getSize();
				var left = parseInt($(this).css("left"));
				$(this).resizable('option', 'maxWidth', myWidth-left-30);
				$(this).resizable('option', 'maxHeight', myHeight-10);
			},
			resize: function(a,b) {
				c_sync_inputwidth();
			},
			stop: function() {
				// magnet
				$("#chat .chat[rel='"+user+"']").css({position:'fixed',top:''});
				c_conf(user,'resize',$(this).css('width')+'|'+$(this).css('height'));
				c_scroll(user);
			}
		});

		$("#chat .chat[rel='"+user+"'] .topbg span").css("background","url(/css/img/topbg_right_resize.gif) no-repeat top right");

		
	} else {
		$("#chat .chat[rel='"+user+"']").resizable('destroy');
		$("#chat .chat[rel='"+user+"'] .topbg span").css("background","url(/css/img/topbg_right.gif) no-repeat top right");
	}

}

function c_aktivieren() {
	c_debug('c_aktivieren()');
	if (!c_intval) {
		c_get(false);
		c_intval = window.setInterval("c_get(true)", 3000);
	}
}

function c_deaktivieren() {
	c_debug('c_deaktivieren()');
	window.clearInterval(c_intval);
}

function c_totop(user) {
	c_debug('c_totop('+user+')');
	c_zIndex++;
	$("#chat .chat[rel='"+user+"']").css('zIndex',c_zIndex);
	//c_scroll(user);
}

function c_addmsganz(user) {
	c_debug('c_addmsganz('+user+')');
	var anz = $("#chat .chat[rel='"+user+"'] .inhalt").data('newmsg');
	if (anz) {
		anz++;
	} else {
		anz = 1;
	}
	c_bounce(user);
	$("#chat .chat[rel='"+user+"'] .inhalt").data('newmsg',anz);
	if (user=='*') {showuser = 'LivingHandy.de';} else {showuser = user;}
	$("#chat .chat[rel='"+user+"'] .topbg .left").html('<b>Chat mit '+showuser+'</b> ('+anz+')');
}

function c_removemsganz(user) {
	c_debug('c_removemsganz('+user+')');
	$("#chat .chat[rel='"+user+"'] .inhalt").removeData('newmsg');
	if (user=='*') {showuser = 'LivingHandy.de';} else {showuser = user;}
	$("#chat .chat[rel='"+user+"'] .topbg .left").html('<b>Chat mit '+showuser+'</b>');
}

function c_toggle(user) {
	c_debug('c_toggle('+user+')');
	//$("#chat .chat[rel='"+user+"'] .inhalt").slideToggle();
	if (parseInt($("#chat .chat[rel='"+user+"']").css('bottom'))== 0) {
		var runter = parseInt($("#chat .chat[rel='"+user+"']").css('height'));
		runter = runter - 25;
		$("#chat .chat[rel='"+user+"']").animate({bottom:"-"+runter+"px"},function(){
			$("#chat .chat[rel='"+user+"'] a.c_minmax img").attr('src','/css/img/c_maximize.png');
			var lastpost = c_getlastpost(user);
			c_conf(user,'minimize',lastpost);
		});
		
		c_resize(user,false);
		
	} else {
		$("#chat .chat[rel='"+user+"']").animate({bottom:"0px"},function(){
			$("#chat .chat[rel='"+user+"'] a.c_minmax img").attr('src','/css/img/c_minimize.png');
			c_removemsganz(user);
			c_resize(user,true);
			c_conf(user,'maximize',0);
			c_scroll(user);
		});
	}
	return false;
}

function c_remove(user) {
	c_debug('c_remove('+user+')');
	$("#chat .chat[rel='"+user+"']").slideUp('fast',function() {
		$("#chat .chat[rel='"+user+"']").remove();
		c_conf(user,'action','remove');
	});
	return false;
}

function c_bounce(user) {
	c_debug('c_bounce('+user+')');
	//$("#chat .chat[rel='"+user+"']").effect('pulsate','',200);
}

function c_getlastpost(user) {
	c_debug('c_getlastpost('+user+')');
	var lastid = $("#chat .chat[rel='"+user+"'] .talk div:last").attr('id');
	if (lastid != null) {
		return lastid.substr(5);
	} else {
		return 0;
	}
}

function c_getfirstpost(user) {
	c_debug('c_getfirstpost('+user+')');
	var lastid = $("#chat .chat[rel='"+user+"'] .talk div:first").attr('id');
	if (lastid != null) {
		return lastid.substr(5);
	} else {
		return 0;
	}
}

function status(msg) {
	c_debug('status('+msg+')');
	if (msg == "" && $("#status").css("display") != "none") {
		$("#status").slideUp(function(){
			$(this).html("");
		});
	} else if (msg != "") {
		$("#status").html(msg);
		if ($("#status").css("display") == "none") {
			$("#status").slideDown();
		}
	}
}

/* smileys */
function smiley(showarea,smileyziel) {
	var smileyarray = new Array(
'<a href="#" onclick="insert_smiley(\''+smileyziel+'\',\':)\');return false;"><img src="/images/smileys/smiley_happy.gif" border="0" /></a>',
'<a href="#" onclick="insert_smiley(\''+smileyziel+'\',\';)\');return false;"><img src="/images/smileys/smiley_wink.gif" border="0" /></a>',
'<a href="#" onclick="insert_smiley(\''+smileyziel+'\',\'(-:\');return false;"><img src="/images/smileys/smiley_andersrum.gif" border="0" /></a>',
'<a href="#" onclick="insert_smiley(\''+smileyziel+'\',\':I\');return false;"><img src="/images/smileys/smiley_naja.gif" border="0" /></a>',
'<a href="#" onclick="insert_smiley(\''+smileyziel+'\',\':D\');return false;"><img src="/images/smileys/smiley_fies.gif" border="0" /></a>',
'<a href="#" onclick="insert_smiley(\''+smileyziel+'\',\':))\');return false;"><img src="/images/smileys/smiley_lach.gif" border="0" /></a>',
'<a href="#" onclick="insert_smiley(\''+smileyziel+'\',\'B-)\');return false;"><img src="/images/smileys/smiley_cool.png" border="0" /></a>',
'<a href="#" onclick="insert_smiley(\''+smileyziel+'\',\':(\');return false;"><img src="/images/smileys/smiley_sad.gif" border="0" /></a>',
'<a href="#" onclick="insert_smiley(\''+smileyziel+'\',\']:)\');return false;"><img src="/images/smileys/smiley_devil.png" border="0" /></a>',
'<a href="#" onclick="insert_smiley(\''+smileyziel+'\',\'x(\');return false;"><img src="/images/smileys/smiley_angry.png" border="0"></a>',
'<a href="#" onclick="insert_smiley(\''+smileyziel+'\',\'8-)\');return false;"><img src="/images/smileys/smiley_clever.png" border="0" /></a>',
'<a href="#" onclick="insert_smiley(\''+smileyziel+'\',\':-o\');return false;"><img src="/images/smileys/smiley_shock.png" border="0" /></a>',
'<a href="#" onclick="insert_smiley(\''+smileyziel+'\',\':-P\');return false;"><img src="/images/smileys/smiley_yum.png" border="0" /></a>',
'<a href="#" onclick="insert_smiley(\''+smileyziel+'\',\':-§\');return false;"><img src="/images/smileys/smiley_blush.png" border="0" /></a>',
'<a href="#" onclick="insert_smiley(\''+smileyziel+'\',\':\\\')\');return false;"><img src="/images/smileys/smiley_cry.gif" border="0" /></a>',
'<a href="#" onclick="insert_smiley(\''+smileyziel+'\',\'I-)\');return false;"><img src="/images/smileys/smiley_sleep.png" border="0" /></a>',
'<a href="#" onclick="insert_smiley(\''+smileyziel+'\',\':8)\');return false;"><img src="/images/smileys/smiley_search.gif" border="0" /></a>',
'<a href="#" onclick="insert_smiley(\''+smileyziel+'\',\'^^\');return false;"><img src="/images/smileys/smiley_augenbrauen.gif" border="0" /></a>',
'<a href="#" onclick="insert_smiley(\''+smileyziel+'\',\'>.<\');return false;"><img src="/images/smileys/smiley_halfdead.gif" border="0" /></a>',
'<a href="#" onclick="insert_smiley(\''+smileyziel+'\',\'x.x\');return false;"><img src="/images/smileys/smiley_dead.gif" border="0" /></a>',
'<a href="#" onclick="insert_smiley(\''+smileyziel+'\',\':-S\');return false;"><img src="/images/smileys/smiley_well.png" border="0" /></a>',
'<a href="#" onclick="insert_smiley(\''+smileyziel+'\',\':-*\');return false;"><img src="/images/smileys/smiley_kuss.gif" border="0" /></a>',
'<a href="#" onclick="insert_smiley(\''+smileyziel+'\',\'8>)\');return false;"><img src="/images/smileys/smiley_love.gif" border="0" /></a>',
'<a href="#" onclick="insert_smiley(\''+smileyziel+'\',\':-Y\');return false;"><img src="/images/smileys/smiley_pfeif.gif" border="0" /></a>',
'<a href="#" onclick="insert_smiley(\''+smileyziel+'\',\'*<->*\');return false;"><img src="/images/smileys/smiley_augendreher.gif" border="0" /></a>',
'<a href="#" onclick="insert_smiley(\''+smileyziel+'\',\'O:-)\');return false;"><img src="/images/smileys/smiley_engel.gif" border="0" /></a>',
'<a href="#" onclick="insert_smiley(\''+smileyziel+'\',\'c(_)\');return false;"><img src="/images/smileys/smiley_kaffee_warm.gif" border="0" /></a>',
'<a href="#" onclick="insert_smiley(\''+smileyziel+'\',\'(zZ)\');return false;"><img src="/images/smileys/smiley_schlafen.gif" border="0" /></a>'
);

var smileys = smileyarray.join("");


	if (!$(showarea).parent().find('.smiley').html()) {
		$(showarea).parent().find('.smiley').html(smileys);
	}
	
	$(showarea).parent().find('.smiley').slideToggle();

}

function cf_linkmaker(id) {
	$("#chat_"+id+" .chatmsg a").click(function() {
		window.parent.LH.document.location.href = $(this).attr("href");
		return false;
	});
}

function cf_link(t) {
	window.parent.LH.document.location.href = t.href;
	return false;
}

function cf_smiley(txt) {
	insert_smiley('#input input',txt);
}

function insert_smiley(ziel, text) {
	insert_text(ziel, text, true);
	$('.smiley').slideUp();
}

function insert_text(ziel, text, spaces)
{
	var textarea;

	textarea = $(ziel).get()[0];
	
	if (spaces) {
		if ($(ziel).val() == '') {
			text = text+" ";
		} else {
			text = ' ' + text + ' ';
		}
	}
	
	if (!isNaN(textarea.selectionStart))
	{
		var sel_start = textarea.selectionStart;
		var sel_end = textarea.selectionEnd;

		mozWrap(textarea, text, '')
		textarea.selectionStart = sel_start + text.length;
		textarea.selectionEnd = sel_end + text.length;
	}	
	
	else if (textarea.createTextRange && textarea.caretPos)
	{
		if (baseHeight != textarea.caretPos.boundingHeight) 
		{
			textarea.focus();
			storeCaret(textarea);
		}		
		var caret_pos = textarea.caretPos;
		caret_pos.text = caret_pos.text.charAt(caret_pos.text.length - 1) == ' ' ? caret_pos.text + text + ' ' : caret_pos.text + text;
		
	}
	else
	{
		textarea.value = textarea.value + text;
	}
	
	textarea.focus();

}

function mozWrap(txtarea, open, close)
{
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	var scrollTop = txtarea.scrollTop;

	if (selEnd == 1 || selEnd == 2) 
	{
		selEnd = selLength;
	}

	var s1 = (txtarea.value).substring(0,selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd)
	var s3 = (txtarea.value).substring(selEnd, selLength);

	txtarea.value = s1 + open + s2 + close + s3;
	txtarea.selectionStart = selEnd + open.length + close.length;
	txtarea.selectionEnd = txtarea.selectionStart;
	txtarea.focus();
	txtarea.scrollTop = scrollTop;

	return;
}




/*
 * Konami-JS
 * http://snaptortoise.com/konami-js
 * Copyright (c) 2009 George Mandis (georgemandis.com)
 * Version: 1.0 (04/27/2009)
 * Licensed under the Artistic License/GPL
 * http://dev.perl.org/licenses/
 * Tested in: Safari 4, Firefox 3, IE7
 */

var konami = {
        input:"",
        clear:setTimeout('konami.clear_input()',2000),
        load: function(link) {
                window.document.onkeyup = function(e) {
                        konami.input+= e ? e.keyCode : event.keyCode         
                        if (konami.input == "38384040373937396665") {
                                konami.code(link)
                                clearTimeout(konami.clear)
                                }
                        clearTimeout(konami.clear)
                        konami.clear = setTimeout("konami.clear_input()",2000)
                        }
        },
        code: function(link) { window.location=link},
        clear_input: function() {
                konami.input="";
                clearTimeout(konami.clear);
        }
}
konami.code = function() {
	urltolayer("haxx0r Tetris","/onlinespiele/JsTetris/",350,330);
}

konami.load()

