function bewerten(id, klick)
{
	$.ajax({
		type: 'POST',
		url: 'bewertung.php',
		data: 'id=' + id + '&rate=' + klick,
		success: function(html) { $('#vote_' + id).html(html); }
		});
}
function bewerten_kommentar(id, klick)
{
	$.ajax({
		type: 'POST',
		url: 'bewertung.php',
		data: 'kommentar=' + id + '&rate=' + klick,
		success: function(html) { $('#vote_kommentar_' + id).html(html); }
		});
}
function gut(id, klick)
{
	if (klick == 1)
	{
		$.ajax({
			type: 'POST',
			url: 'ajax_gut.php',
			data: 'status=' + id + '&klick=1',
			success: function(html) { $('#' + id).html(html); }
			});
		$("#klick_" + id).hide();
	}
	else
	{
		$.ajax({
			type: 'POST',
			url: 'ajax_gut.php',
			data: 'status=' + id,
			success: function(html) { $('#' + id).html(html); }
			});
	}
}
function bewerten_bad(id, klick)
{
	$.ajax({
		type: 'POST',
		url: 'bewertung_bad.php',
		data: 'id=' + id + '&rate=' + klick,
		success: function(html) { $('#vote_bad_' + id).html(html); }
		});
}
function status_klappen(datum)
{
	klapptext = document.getElementById("klapptext_" + datum);
	klappfeld = document.getElementById("status_hide_" + datum);

	klapptext.style.display = "none";
	$("#status_hide_" + datum).slideDown("slow");
}
function blass(bild, wie)
{
	if (wie == "in")
	{
		$("#"+bild).fadeTo("fast",1);
	}
	if (wie == "out")
	{
		$("#"+bild).fadeTo("normal",0,4);
	}
}
function facebox_link(ziel)
{
	jQuery.facebox(function($){jQuery.get(ziel,function(data){jQuery.facebox(data);})});
}
function facebox_submit(ziel, feld)
{
	var inhalt = escpae(document.getElementById(feld).value);
	jQuery.facebox(function($){jQuery.get(ziel + "&" + feld + "=" + inhalt,function(data){jQuery.facebox(data);})});
}

function init_tinymce()
{
	tinyMCE.init({
	    theme : "advanced",
	    mode: "exact",
	    skin : "o2k7",
	    elements : "frm_inhalt",
	    plugins: "emotions",
	    theme_advanced_toolbar_location : "top",
	    theme_advanced_buttons1 : "forecolor,bold,italic,underline,strikethrough,charmap,separator,"
	    + "justifyleft,justifycenter,justifyright,justifyfull,"
	    + "bullist,numlist,outdent,indent,separator,link,unlink,image,separator,"
	    +"code,emotions",
	    theme_advanced_buttons2 : "",
	    theme_advanced_buttons3 : "",
	    height:"350px",
	    width:"480px"
	  });
	  
	tinyMCE.init({
	    theme : "advanced",
	    mode: "exact",
	    skin : "o2k7",
	    elements : "frm_kommentar",
	    plugins: "emotions",
	    theme_advanced_toolbar_location : "top",
	    theme_advanced_buttons1 : "forecolor,bold,italic,underline,strikethrough,charmap,separator,"
	    + "justifyleft,justifycenter,justifyright,justifyfull,"
	    + "bullist,numlist,outdent,indent,separator,link,unlink,image,separator,"
	    +"code,emotions",
	    theme_advanced_buttons2 : "",
	    theme_advanced_buttons3 : "",
	    height:"60px",
	    width:"480px"
	  });
}
init_tinymce();

/* *****************************************
POPUP FUNKTIONEN
***************************************** */
function popup_einladen()
{
	var email = document.getElementById("einladen_email").value;
	var name = document.getElementById("einladen_name").value;
	facebox_link("einladen.php?einladen_email=" + email + "&einladen_name=" + name);
}
function popup_login()
{
	var user = document.getElementById("login_user").value;
	var pass = document.getElementById("login_pass").value;
	facebox_link("login.php?login_user=" + escape(user) + "&login_pass=" + escape(pass));
}
function popup_status()
{
	var s_u = document.getElementById("s_u").value;
	var s_d = document.getElementById("s_d").value;
	var status = document.getElementById("status_inhalt").value;
	facebox_link("statuskommentar.php?s_u=" + s_u + "&s_d=" + s_d + "&status_inhalt=" + escape(status));
}
function popup_melden()
{
	var id = document.getElementById("melden_id").value;
	var melden = document.getElementById("melden_inhalt").innerHTML;
	$.ajax({
		type: 'POST',
		url: 'melden.php',
		data: "id=" + id + "&melden_inhalt=" + escape(melden),
		success: function(html) { jQuery.facebox("Vielen Dank, deine Meldung wurde weiter geleitet. Du kannst dieses Fenster jetzt schliessen."); }
		});
}
function popup_media_edit()
{
	var id = document.getElementById("media_id").value;
	var seite = document.getElementById("media_seite").value;
	var media = tinyMCE.activeEditor.getContent();
	
	$.ajax({
		type: 'POST',
		url: 'media_edit.php',
		data: 'id=' + id + '&media_inhalt=' + escape(media),
		success: function(html) { parent.location.href = "media.php?id=" + seite; }
		});
}
function popup_forum_edit()
{
	var id = document.getElementById("forum_id").value;
	var seite = document.getElementById("forum_thread").value;
	var titel = document.getElementById("forum_titel").value;
	var media = tinyMCE.activeEditor.getContent();
	$.ajax({
		type: 'POST',
		url: 'forum_edit.php',
		data: 'id=' + id + '&forum_inhalt=' + escape(media) + "&forum_titel=" + escape(titel),
		success: function(html) { parent.location.href = "forum.php?id=" + seite + "&edit_done=1"; }
		});
	
}

/*
jQUERY PLUGINS
*/

/**
 * Edit-in-place with contentEditable property (FF2 is not supported)
 * Project page - http://valums.com/edit-in-place/
 * Copyright (c) 2008 Andris Valums, http://valums.com
 * Licensed under the MIT license (http://valums.com/mit-license/)
 * Version 0.4 (27.02.2009)
 */
(function(){
var d = document, w = window;

/**
 * Get element by id
 */	
function $(element){
	if (typeof element == "string")
		element = d.getElementById(element);
	return element;
}

/**
 * Attaches event to a dom element
 */
function addEvent(el, type, fn){
	if (w.addEventListener){
		el.addEventListener(type, fn, false);
	} else if (w.attachEvent){
		var f = function(){
		  fn.call(el, w.event);
		};			
		el.attachEvent('on' + type, f)
	}
}


if (jQuery){
	jQuery.fn.editable = function(onChange){
		return this.each(function(){
			editableAreas.add(this, onChange);
		});
	};
}

editableAreas = {
	enterDisabled : false
	,instances : []
	,active : null
	,inited : false
	,init : function(){
		var self = this;
		// attach enter keypress capturer to document
		addEvent(d, 'keypress', function(e){
			// find which key was pressed (code from jQuery library)
			if ( !e.which && ((e.charCode || e.charCode === 0) ? e.charCode : e.keyCode)){
				e.which = e.charCode || e.keyCode;	
			}

			if (self.enterDisabled && (e.which == 13)) {
				if (e.preventDefault) e.preventDefault();				
				else e.returnValue = false;
			}
		});
		
		addEvent(d, 'click', function(e){
			var target = e.target ? e.target : e.srcElement || document;

			while (target.nodeName != "HTML"
				&& target.nodeName != "BODY"
				&& target.contentEditable != true && target.contentEditable != 'true')
				// contentEditable is boolean in Opera
			{										
				target = target.parentNode;				
			}

			if (self.active && (self.active.el !== target)){
				// User clicked outside of editable area
				self.enterDisabled = false;
				self.active.onChange.call(self.active.el, self.active.el);
				self.active = null;												
			}
			
			if ( ! self.active){				
				var i = self.indexOfEditable(target);
				if (i !== -1){
					if (target.nodeName != 'DIV'){						
						//disable line breaks for h1..h5,p,etc ..
						self.enterDisabled = true;
					}
					self.active = self.instances[i];
				}			
			}
		});
		
		this.inited = true;				
	}
	,add : function(el, onChange){
		el = $(el);
		onChange = onChange ||	function(){};
		// FF2 doesn't support contentEditable
		el.contentEditable = true;
		this.instances.push({el:el, onChange:onChange});
		
		if (!this.inited) this.init();		
	}
	,indexOfEditable : function(el){
		for (var i=0, length = this.instances.length; i < length; i++){				
			if (this.instances[i].el === el){
				return i;
			}	
		}				
		return -1;
	}
	
	};
})();

function chatStart(user, name) {
	// Existiert das Fenster bereits?
	if ($("#chat_user_" + user).attr("id"))
	{
		$("#chat_user_" + user).css("background", "#c00");
	}
	else
	{
		if (user > 0)
		{
			$("#chat_tabs").append(
				"<div id='chat_user_" + user + "' rel='chat_child' style='background: #777;'><input type='hidden' id='chat_last_" + user + "'><div class='chat_sidebar_name'> \
						<img src='img/chat_close.png' align='right' id='chat_close_" + user + "' style='margin-top: 3px; cursor: pointer;' alt='Schliessen' title='Schliessen'> \
						<img src='avatar.php?id=" + user + "&size=22' style='margin-bottom: -5px'> \
						" + name + " \
						<div class='clear'></div> \
						<div class='chat_window'> \
							<div class='chat_input'> \
								<input type='text' id='input_user_" + user + "'> \
							</div> \
							<div class='chat_output' id='chat_output_" + user + "'></div> \
							<div class='clear'></div> \
						</div> \
				</div></div>"
			);
		}
		else
		{
			$("#chat_tabs").append(
				"<div id='chat_user_" + user + "' rel='chat_child' style='background: #777;'><input type='hidden' id='chat_last_" + user + "'><div class='chat_sidebar_name'> \
						<img src='img/chat_home.png' style='margin-bottom: -5px'> \
						" + name + " \
						<div class='clear'></div> \
						<div class='chat_window'> \
							<div class='chat_input'> \
								<input type='text' id='input_user_" + user + "'> \
							</div> \
							<div class='chat_output' id='chat_output_" + user + "'></div> \
							<div class='clear'></div> \
						</div> \
				</div></div>"
			);
		}
		$("#chat_user_" + user).bind("mouseover", function() {
			$("#chat_user_" + user + " .chat_window").show();
			$("#chat_user_" + user).css("background", "#333");
		});
		$("#chat_user_" + user).bind("mouseout", function() {
			$("#chat_user_" + user + " .chat_window").hide();
			$("#chat_user_" + user).css("background", "#777");
		});
		
		// Schliessen
		$("#chat_close_" + user).bind("mouseover", function() {
			$(this).attr("src", "img/chat_close_.png");
		});
		$("#chat_close_" + user).bind("mouseout", function() {
			$(this).attr("src", "img/chat_close.png");
		});
		$("#chat_close_" + user).bind("click", function() {
			$.ajax({
				type: "POST",
				url: "ajax_chat.php",
				data: "was=close&an=" + user,
				success: function(html) {
					$("#chat_user_" + user).remove();
				}
			});
		});
		
		// Input
		$("#input_user_" + user).bind("keypress", function(e) {
			var id = "#input_user_" + user;
			if (e.which == 13)
			{
				$.ajax({
					type: "POST",
					url: "ajax_chat.php",
					data: "was=msg&an=" + user + "&msg=" + escape($(id).val()),
					success: function() {
						$(id).val("");
					}
				});
			}
		});
	}
}

function chatUpdate() {
	var debug = "";
	$("[rel=chat_child]").each(function() {
		var id = $(this).attr("id");
		var user = id.split("_");
		
		$.ajax({
			type: "POST",
			url: "ajax_chat.php",
			data: "was=get&last=" + $("#chat_last_" + user[2]).val() +"&chat=" + user[2],
			success: function(html) {
				if (html > "")
				{
					$("#chat_output_" + user[2]).prepend(html);
					var last = $("#chat_output_" + user[2] + " div:first-child").attr("id");
					$("#chat_last_" + user[2]).val(last);
					var von = last.split("_");
					if (von[1] != $("#chatter").val())
					{
						$("#chat_user_" + user[2]).css("background", "#f90");
					}
				}
			}
		});
		
		debug = debug + user[2] + ", ";
	});
	
	// $("#chat_debug").html(debug);
	
	setTimeout("chatUpdate()", 4000);
}