var SBOXTIMER = null;
function loadSbox(){
	$('#sboxContent').load('prepareJS.php?jsaction=sbox');
}
function showChat(link){
	window.open(link, "Cordless-Radio Chatrooms", "width=900,height=600,left=50,top=100,scrollbars=no,location=no");
	return false;
}
function closeLayer(){
	$('#pageLayer').fadeOut(700, function(){
		$('#pLInner').html('');
		$('#pageLayer').css('left', -1000);
	});
}
function hideLayer(){
	$('#pageLayer').animate({
		left:-1000,
	}, 'fast');
	$('#chatShow').css('display','block');
	$('#chatShow').click(function(){
		showHidden();
	});
	
}
function showHidden(){
	var layer = $('#pageLayer');
	$('#chatShow').css('display','none');
	layer.animate({
		left:0,
	}, 'fast');
	return false;
}
function showPn(thisElement){
	var parent = thisElement.parents('.js_MsgParent');
	var msgbody = parent.find('.js_msgBody');
	msgbody.css('height', 'auto');
	msgbody.removeClass('closed').addClass('open');
	msgbody.slideDown('fast');
	thisElement.unbind('click');
	$.post("/prepareJS.php?jsaction=pn&markread=true", parent.find('.read').serialize(), function(data){
		parent.removeClass('MsgEntryNew').addClass('MsgEntry');
	});
	thisElement.text('[ einklappen ]');
	thisElement.click(function(){
		hidePn($(this));
	});
}
function hidePn(thisElement){
	var parent = thisElement.parents('.js_MsgParent');
	var msgbody = parent.find('.js_msgBody');
	var replyBody =  parent.find('.js_msgReplyWrap'); 
	msgbody.slideUp('fast');
	replyBody.slideUp('fast');
	msgbody.removeClass('open').addClass('closed');
	thisElement.text('[ ausklappen ]');
	thisElement.unbind('click');
	thisElement.click(function(){
		showPn($(this));
	});
}
function delMsg(thisElement){
	var link = thisElement.attr('href');
	var parent = thisElement.parents('.js_MsgParent');
	$.get(link, function(data){
		if(data==1){
			parent.remove();
		}else{
			alert('es gab ein Problem beim löschen');
		}
	});
}
function sendMsg(thisElement){
	$.post("/prepareJS.php?jsaction=pn&answer=true", thisElement.serialize(), function(data){
		if(data == '0'){
			alert('Es ist ein Fehler aufgetreten\Sollte das öfter passieren melde dich bitte\bei einem Admin...!');
		}else{
			thisElement.parents('.js_msgReplyWrap').append('<div>'+data+'</div>');
			thisElement.remove();
		}
		
	});
}
$(document).ready(function(){
	$.ajaxSetup({
	  cache: false
	});
	SBOXTIMER = window.setInterval(loadSbox, 10000);	
	if ( window.attachEvent && !window.addEventListener ) {
		window.attachEvent("onunload", function() {
		   for ( var id in jQuery.cache ) {
				if ( jQuery.cache[ id ].handle ) {
					try {
						jQuery.event.remove( jQuery.cache[ id ].handle.elem );
					} catch(e) {}
				}
			}
		});
	}
	$('.js_chat').click(function(){
		showChat($(this).attr('href'));
		return false;
	});
	$('#js_layerHide').click(function(){
		 hideLayer();
		return false;
	});
	$('#js_layerClose').click(function(){
		 closeLayer();
		return false;
	});
	$('#chatShow').click(function(){
		showHidden();
	});
	$(window).keypress(function(e) { 
		if(e.keyCode == 27) { 
			closeLayer();
			return true;
		} 
	});
	$('.js_msgBody, .js_msgReplyWrap').slideUp(0).addClass('closed');
	$('.js_msgAction').click(function(){
		showPn($(this));
	});
	$('.js_replyAction').click(function(){
		var thisElement = $(this);
		var rel = thisElement.attr('rel');
		$('#'+rel).slideDown('fast');
		return false;
	});
	$('.js_DelMsg').click(function(){
		var thisElement = $(this);
		delMsg(thisElement);
		return false;
	});
	
	$('.reply').submit(function(){
		var thisElement = $(this);
		sendMsg(thisElement);
		return false;
	});
	
	
});
