$(document).ready(
	function () {
		$(".friends").click( function() {
        	$("#friends_online").toggle("slow");
    	});
		getChat();
		$(".chat").click( function() {
        	$("#chat").toggle("slow");
			$(".chatinput").focus();
    	});
		$(".username").contextMenu({
					menu: 'username_menue'
				},
				function(action, el, pos) {
					if(action == "msg") {
						tb_show($(el).attr('id') + ' eine Nachricht senden', '/newmsg.php?height=200&width=400&id=' + $(el).attr('id'), '');
					}
					if(action == "goProfil") {
						window.location.href = "/profil/"+$(el).attr('id')+"/";
					}
					if(action == "makeFriends") {
						makeFriends($(el).attr('id'));
					}
		});
	
		var params = document.URL.split("/")[3];
		if(params != "") {
			$("#nav_"+params).addClass("current");
		} else {
			$("#nav_home").addClass("current");
		}
		$('a.closeEl').bind('click', toggleContent);
		$('.password').pstrength();
		$("#thumbnail").imgAreaSelect({ aspectRatio: "1:1", onSelectChange: preview });
		$("#save_thumb").click(function() {
			var x1 = $("#x1").val();
			var y1 = $("#y1").val();
			var x2 = $("#x2").val();
			var y2 = $("#y2").val();
			var w = $("#w").val();
			var h = $("#h").val();
			if(x1=="" || y1=="" || x2=="" || y2=="" || w=="" || h==""){
				alert("Du musst zuerst etwas auswaehlen!");
				return false;
			}else{
				return true;
			}
		});
		showFeeds();
	}

);
// Loading Image
var loading = "<div id=\"loading\" class=\"loading\">"
					+"<img src='/templates/img/ajax-loader.gif' /> Loading..."
				  +"</div>";

// Toggle
function toggle(id) {
	var targetContent = $('#'+id);
	if (targetContent.css('display') == 'none') {
		targetContent.toggle($('#'+id).css('display') == 'none');
	} else {
		targetContent.toggle();
	}
}

// Thumbnail Preview
function preview(img, selection) {
	var scaleX = 100 / selection.width;
	var scaleY = 100 / selection.height;
	$('#thumbnail + div > img').css({
		width: Math.round(scaleX * document.getElementById('thumbnail').offsetWidth) + 'px',
		height: Math.round(scaleY * document.getElementById('thumbnail').offsetHeight) + 'px',
		marginLeft: '-' + Math.round(scaleX * selection.x1) + 'px',
		marginTop: '-' + Math.round(scaleY * selection.y1) + 'px'
	});
	$('#x1').val(selection.x1);
	$('#y1').val(selection.y1);
	$('#x2').val(selection.x2);
	$('#y2').val(selection.y2);
	$('#w').val(selection.width);
	$('#h').val(selection.height);
}

// New Choice
var anz = 4;
function newChoice() {
	var color;
	if(anz%2 == 0) {
		color = 2;
	} else {
		color = 1;
	}
	$('#addChoice').before('<tr id="choice'+ anz +'">' +
						 	'<th class="form_col'+ color +'\"><label>Antwort '+ anz +':</label></th>' +
						 	'<td class="form_col'+ color +'"><input name="choice[]" type="text"  maxlength="150"  size="50" />' +
								'<a onclick="deleteChoice('+ anz +');" style="cursor:pointer;"><img src="/templates/default/img/icons/delete.png"/></a>' +
							'</td>' +
						 '</tr>');
	anz++;
}

// Delete Choise
function deleteChoice(num) {
	$('#choice'+num).remove();
	anz--;
}

// Feeds
function showFeeds() {
	var query_string = '';
	$("input[type=checkbox][name=feed]").each( 
    function() { 
        if (this.checked) { 
            query_string += "&feeds[]=" + this.value;
        }
		
    });
	$("#feeds").empty();
	$("#feeds").append(loading);
	$("#feeds").load("/module_feeds.php?id=1"+query_string);
}

// Search
function showSearch() {
	var query_string = '';
	$("input[type='checkbox'][name='content']").each( 
    function() { 
        if (this.checked) { 
            query_string += "&contents[]=" + this.value;
        }
		
    });
	var suche = $("input[type='text'][name='search']").val();
	suche = suche.replace(/ /g, "-");
	var art;
	$("input[type='radio'][name='art']").each(
		function() { 
        if (this.checked) { 
            art = this.value;
        }
    });
	$("#results").empty();
	$("#results").append(loading);
	$("#results").load("/module_search.php?art="+art+"&search="+suche+""+query_string);
}

// Chat Message
function sendChat() {
	var msg = $("input[type='text'][name='chat_message']").val();
	$("input[type='text'][name='chat_message']").val("");
	$.post("/addChat.php", { message: msg});
}

// Get Chat
function getChat() {
	var objDiv = document.getElementById("chatfenster");
	if(objDiv != null) {
		$("#chatfenster").load("/getChat.php");
		objDiv.scrollTop = objDiv.scrollHeight;
		setTimeout('getChat()', 1000);
	}
}

// Send Message
function sendMsg() {
   	$.post("/savemsg.php", 
			{ empf: $("#empf").val(), text: $("#text").val() },
			function(data){
				if(confirm(unescape(data + "%0A%0AWillst du eine neue Nachricht schreiben?"))) {
					$("#text").attr("value","");
				} else {
					tb_remove();
				}
  			}
	)
}

// Delete Message
function delMsg(id) {
	if(confirm(unescape('Willst du diese Nachricht wirklich l%F6schen?'))) {
		window.location.href = "/delmsg.php?id="+id;
	}
}

// Make Friends
function makeFriends(nick) {
	tb_show(nick + ' eine Freundschaftsanfrage senden', '/newfriend.php?height=50&width=400&id=' + nick, '');
}

// Dismiss Friend
function dismissFriend(id) {
	if(confirm(unescape('Willst du diesen User ablehnen?'))) {
		window.location.href = "/dismissfriend.php?id="+id;
	}
}

// Delete Friend
function delFriend(id) {
	if(confirm(unescape('Willst du diesen Freund wirklich l%F6schen?'))) {
		window.location.href = "/delfriend.php?id="+id;
	}
}

// Delete Thread
function delThread(id) {
	if(confirm(unescape('Willst du diesen Thread wirklich l%F6schen?'))) {
		window.location.href = "/delthread.php?id="+id;
	}
}

// Delete Post
function delPost(id) {
	if(confirm(unescape('Willst du diesen Post wirklich l%F6schen?'))) {
		window.location.href = "/delpost.php?id="+id;
	}
}

// Delete Comment
function delComment(id) {
	if(confirm(unescape('Willst du diesen Kommentar wirklich l%F6schen?'))) {
		window.location.href = "/delcomment.php?id="+id;
	}
}

// Toggle
var toggleContent = function(e) {
	var targetContent = $('div.itemContent', this.parentNode.parentNode);
	if (targetContent.css('display') == 'none') {
		targetContent.slideDown(300);
		$(this).removeClass('openEl');
	} else {
		targetContent.slideUp(300);
		$(this).addClass('openEl');
	}
	return false;
};
(function() {

function $(id) {
	return document.getElementById(id);
}

function setStyleDisplay(id, status) {
	$(id).style.display = status;
}

function goTop(acceleration, time) {

	acceleration = acceleration || 0.1;
	time = time || 16;

	var dx = 0;
	var dy = 0;
	var bx = 0;
	var by = 0;
	var wx = 0;
	var wy = 0;

	if (document.documentElement) {
		dx = document.documentElement.scrollLeft || 0;
		dy = document.documentElement.scrollTop || 0;
	}
	if (document.body) {
		bx = document.body.scrollLeft || 0;
		by = document.body.scrollTop || 0;
	}
	var wx = window.scrollX || 0;
	var wy = window.scrollY || 0;

	var x = Math.max(wx, Math.max(bx, dx));
	var y = Math.max(wy, Math.max(by, dy));

	var speed = 1 + acceleration;
	window.scrollTo(Math.floor(x / speed), Math.floor(y / speed));
	if(x > 0 || y > 0) {
		var invokeFunction = "pJ.goTop(" + acceleration + ", " + time + ")"
		window.setTimeout(invokeFunction, time);
	}
}

window['pJ'] = {};
window['pJ']['$'] = $;
window['pJ']['setStyleDisplay'] = setStyleDisplay;
window['pJ']['goTop'] = goTop;

})();
