var running = false;
var visibleForm = false;
var changeTitle = true;

function hideThread(event)
{
	if (!running)
	{
		running = true;
		this.hide();
		this.next('div.text_area').down('p.text').hide();
		if (this.up('div.item').next('ul')) this.up('div.item').next('ul').slideUp({'duration' : '.2', 'afterFinish' : function() {running = false;}});
		else running = false;
		this.next('a.plus').show();
	}
	event.stop();
}

function showThread(event)
{
	if (!running)
	{
		running = true;
		this.next('div.text_area').down('p.text').show();
		if (this.up('div.item').next('ul')) this.up('div.item').next('ul').slideDown({'duration' : '.2', 'afterFinish' : function() {running = false;}});
		else running = false;
		this.hide();
		this.previous('a.minus').show();
	}
	event.stop();
}

function showAddForm(event)
{
	if (!visibleForm)
	{
		this.hide();
		visibleForm = true;
		this.stopObserving('click');
		Event.observe(this, 'click', function(event) {event.stop();});
		$('post_form').slideDown({'duration' : '.2'});
	}
	event.stop();
}

function reactTo(event)
{
	this.insert({'bottom' : $('post_form')});
	//$('post_form').down().childElements().invoke('removeClassName', 'input_text');
	if (!visibleForm)
	{
		visibleForm = true;
		$('post_form').slideDown({'duration' : '.2'});
	}
	$('post_form').down('input[name="parent"]').value = this.up().id.sub('post-', '');
	$('post_form').down('fieldset').removeClassName('add_prispevok').addClassName('reakcia_form');
	titleInput = $('post_form').down('input[name="title"]');
	if (changeTitle) titleInput.value = 'Re: ' + this.down('h4').innerHTML.unescapeHTML();
	event.stop();
}

function reactToUnlogged(event)
{
	this.insert({'bottom' : $('login_form_discussion')});

	if (!visibleForm)
	{
		visibleForm = true;
		$('login_form_discussion').slideDown({'duration' : '.2'});
	}
	$('login_form_discussion').down('span').setStyle("color:red;");
	event.stop();
}


function addNotification(event)
{
	event.stop();
	new Ajax.Request('services/DiscussionSubscribe.html', {
		parameters: {'post_id': post_id},
		onSuccess: function(response) {
			alert(response.responseJSON.message);
			if(response.responseJSON.notif == 1) {
				event.element().innerHTML = 'Odhlásiť odoberanie diskusie mailom';
			} else {
				event.element().innerHTML = 'Posielať nové príspevky mailom';
			}
		}
	});
}

Event.observe(document, 'dom:loaded', function() {

	if ($('post_form')!=null)
	{
		if (submitted) scrollTo(0, $('post_form').positionedOffset()['top']);
		else $('post_form').hide();
	
		$$('div.diskusia div.zoznam div.item a.minus').each(function (s) {
		s.show();
		Event.observe(s, 'click', hideThread.bindAsEventListener(s));
		});
		
		$$('div.diskusia div.zoznam div.item a.plus').each(function (s) {
			Event.observe(s, 'click', showThread.bindAsEventListener(s));
		});
		
		$$('div.diskusia div.zoznam li.main p.date a').each(function (s) {
			Event.observe(s, 'click', reactTo.bindAsEventListener(s.up('div.text_area')));
		});
		
		$('add-post').show();
		Event.observe($('add-post'), 'click', showAddForm.bindAsEventListener($('add-post')));
		$('add-new-post-caption').observe('click', showAddForm.bindAsEventListener($('add-post')));

		notificationButton = $$('div.diskusia div.bot_links a.aler')[0];
		if (notificationButton)
		{
			notificationButton.show();
			Event.observe(notificationButton, 'click', addNotification.bindAsEventListener(notificationButton));
		}
		
		titleInput = $('post_form').down('input[name="title"]');
		Event.observe(titleInput, 'change', function() {changeTitle = false; this.stopObserving('change');});
	
	}
	else
	{
		$$('div.diskusia div.zoznam li.main p.date a').each(function (s) {
			Event.observe(s, 'click', reactToUnlogged.bindAsEventListener(s.up('div.text_area')));
		});
	
	}
	
});

