function OSP_Forum(forum_id)
{
	var current_selected_topic_id;
	var base_html_id = 'forum_topic__id__';

	
	function Init(forum_id)
	{
		GetTopics(forum_id);

		$('#forum_posts_container').data('initial_top_offset', $('#forum_posts_container').offset().top);
		$('#forum_posts_container').data('initial_top_margin', parseInt($('#forum_posts_container').css('margin-top')));
	}


	function PostReply(base_url)
	{
		if ( !current_selected_topic_id )
		{
			// todo: translate
			alert("Vous n'avez sélectionnez aucune question.");
		}
		else
		{
			window.location.href = base_url + '&topic_id=' + current_selected_topic_id;
		}
	}


	function GetTopics(forum_id)
	{
		var url = OSP_PUBLIC_PATH + '/index.php5?plugin=cms&object=forum&action=get_topics&forum_id=' + forum_id;

		byId('forum_topics_container').innerHTML = '';
		OSP_AppendLoader(byId('forum_topics_container'));

		dojo.xhrGet({
			url: url,
			load: function(response){ GetTopics_OK(response); },
			error: function(response){ GetTopics_Error(response); }
		});
	}


	function GetTopics_OK(response)
	{
		eval('var response_data = ' + response);

		byId('forum_topics_container').innerHTML = '';

		for ( var i = 0; i < response_data['topics'].length; i++ )
		{
			$('#forum_topics_container').append(response_data['topics'][i]['html']);
			var topic = $('#' + base_html_id + response_data['topics'][i]['id']);
			topic.data('id', response_data['topics'][i]['id']);

			topic.click(function()
			{
				if ( !$(this).hasClass('selected') )
				{
					GetPosts($(this).data('id'));
				}
			});

			if ( !current_selected_topic_id )
			{
				topic.click();
			}
		}
	}


	function GetTopics_Error(response)
	{
		alert('GetTopics_Error ' + response);
	}


	function GetPosts(topic_id)
	{
		if ( current_selected_topic_id )
		{
			$('#' + base_html_id + current_selected_topic_id).removeClass('selected');
		}
		$('#' + base_html_id + topic_id).addClass('selected');

		current_selected_topic_id = topic_id;

		// Replace topic_id in "Post reply" link's query string
		$('.menu_item.post_reply a').each(function(){ $(this).attr('href', $(this).attr('href').replace(/topic_id=[0-9]+/, 'topic_id=' + topic_id)); });

		var url = OSP_PUBLIC_PATH + '/index.php5?plugin=cms&object=forum&action=get_posts&topic_id=' + topic_id;

		byId('forum_posts_container').innerHTML = '';

		// Align posts to topic
		$('#forum_posts_container').css('margin-top', $('#' + base_html_id + current_selected_topic_id).offset().top - $('#forum_posts_container').data('initial_top_offset') + $('#forum_posts_container').data('initial_top_margin'));
		
		OSP_AppendLoader(byId('forum_posts_container'));

		dojo.xhrGet({
			url: url,
			load: function(response){ GetPosts_OK(response); },
			error: function(response){ GetPosts_Error(response); }
		});
	}


	function GetPosts_OK(response)
	{
		eval('var response_data = ' + response);

		byId('forum_posts_container').innerHTML = '';

		for ( var i = 0; i < response_data['posts'].length; i++ )
		{
			byId('forum_posts_container').innerHTML += response_data['posts'][i]['html'];
		}

		if ( !inView($('#forum_posts_container')) )
		{
			$(window).scrollTo('#forum_posts_container', 400, {'over':-0.25});
		}
	}


	function inView(elem)
	{
		var view_bounds = {
			'top' : $(window).scrollTop(),
			'bottom' : $(window).scrollTop() + $(window).height()
		};
		var elem_bounds = {
			'top' : elem.offset().top,
			'bottom' : elem.offset().top + elem.outerHeight()
		};

		return ( elem_bounds.top > view_bounds.top && elem_bounds.bottom < view_bounds.bottom );
	}


	function GetPosts_Error(response)
	{
		alert('GetPosts_Error ' + response);
	}


	Init(forum_id);
}

