$(function () {
	var bindContent = function() {
		// Generell - link (my link to different contents)
		$('#content a.link').click(function () {
			setContent($(this).attr('href'));
			return false;
		});
		// HomeAll - show hide
		$('.showHidePost').click(function () {
			$('#text_' + $(this).attr('id')).slideToggle('fast');
			if ($(this).hasClass('showPost'))
				$(this).removeClass('showPost').addClass('hidePost');
			else
				$(this).removeClass('hidePost').addClass('showPost');
		});
		// Home - show the form
		$('.leaveComment').click(function () {
			var id = $(this).attr('id').split('_')[1];
			if ($(this).html() == "Leave a comment") {
				$(this).html('Don\'t leave a comment');
				$('#leaveComment_input_' + id).slideDown('fast');
				if ($('#showComments_' + id).html() == "Hide comments") {
					$('#showComments_' + id).click();
				}
			} else {
				$(this).html('Leave a comment');
				$('#leaveComment_input_' + id).slideUp('fast');
			}
			return false;
		});
		// Home send the comment
		$('.sendComment').click(function () {
			var id = $(this).attr('id').split('_')[1];
			var okButton = $(this);
			$('#comment_reply_' + id).html("");
			okButton.hide();
			$('#comment_loader_' + id).show();
			var name = $('#comment_name_' + id);
			var message = $('#comment_message_' + id);
			$.post('scripts.php'
			, {act: 'add_comment', name: name.val(), message: message.val(), post_id: id}
			, function (data) {
				if (data.success) {
					name.val("");
					message.val("");
					$('#leaveComment_' + id).click();
					$('#showComments_' + id).click();
					$('#comment_count_' + id).html(parseInt(""+ $('#comment_count_' + id).html()) + 1);
				} else {
					$('#comment_reply_' + id).html(data.reply);
				}
				okButton.show();
				$('#comment_loader_' + id).hide();
			}, 'json');
			return false;
		});
		// Main show comments
		$('.showComments').click(function () {
			var link = $(this);
			var id = $(this).attr('id').split('_')[1];
			$('showcomments_loader_' + id).show();
			if ($(this).html() == "Hide comments") {
				$(this).html("Show comments");
				$('#comments_' + id).slideUp('fast');
			} else {
				$.post('scripts.php'
				, {act: 'get_comments', post_id: id}
				, function (data) {
					if (data.success) {
						link.html("Hide comments");
						if ($('#leaveComment_' + id).html() == "Don\'t leave a comment") {
							$('#leaveComment_' + id).click();
						}
						$('#comments_' + id).html(data.comments).slideDown('fast');
					}
					$('showcomments_loader_' + id).hide();
				}, 'json');
			}
			return false;
		});
	}
	var setContent = function(id) {
		$('#content').slideUp('fast', function() {
			$.post('content.php', {id: id}, function (data) {
				$('#content').html(data);
				bindContent();
				$('#loader').stop().fadeOut('fast', function () {
					$('#content').slideDown('fast');
				});
			});
			$('#loader').fadeIn('fast');
		});
	};
	
	$('#mainmenu a').click(function () {
		$('#mainmenu').children().removeClass('current');
		$(this).addClass('current');
		var x = $(this).attr('id').split('_');
		$('#submenu').children().hide(0);
		switch (x[1]) {
			case 'path':
			case 'text':
			case 'content':
				$('#submenu div').children().removeClass('current');
				setContent(x[2]);
				break;
			case 'sub':
				$('#submenu_' + x[2]).show(0);
				break;
		}
		return false;
	});
	$('#submenu a').click(function () {
		$('#submenu div').children().removeClass('current');
		$(this).addClass('current');
		var x = $(this).attr('id').split('_');
		setContent(x[1]);
		return false;
	});
	$('#content').hide();
	$('#menu_content_1').click();
});













