var CTopicProperties = Class.create({
	initialize: function(btnSuffix, action) {
		this.suffix = btnSuffix;
		this.action = action;
	},

	show: function(topic)
	{
		var onComplete = function() {
			this.toggleButtonBar('top-'+this.suffix, 0);
			this.toggleButtonBar('bot-'+this.suffix, 0);
		};
		var action = this.action.replace('#topic#', topic);

		Topic.closeNew();
		Ubk.follow('posts', 'general.php', action, true, true, onComplete.bind(this));
	},

	hide: function(topic)
	{
		var onComplete = function() {
			this.toggleButtonBar('top-'+this.suffix, 1);
			this.toggleButtonBar('bot-'+this.suffix, 1);
		};
		// ritorno all'ultima pagina visualizzata del topic
		Topic.openPage(topic, Topic.currentPage, onComplete.bind(this));
	},

	toggle: function(topic)
	{
		if ($('top-'+this.suffix).hasAttribute('hidden')) {
			this.hide(topic);
		} else {
			this.show(topic);
		}
	},

	toggleButtonBar: function(button, mode)
	{
		button = $(button);
		// mostro
		if (mode && button) {
			button.removeAttribute('hidden');
			sibs = button.up(1).select('a,img').filter(function(e){return e.hasAttribute('hidden');});
			sibs.each(function(e){
				e.toggle().removeAttribute('hidden');
			});
		// nascondo
		} else if (!mode && button) {
			button.writeAttribute('hidden');
			sibs = button.up(1).select('a,img').findAll(Ubk.isVisible);
			sibs.each(function(e){
				e.toggle().writeAttribute('hidden');
			});
		}
	}

});

/*new Draggable('tmove', {
	handle: '.btn'
	,onStart: function(d, evt) { Element.setStyle(d.element, { cursor: 'move' }); }
	,onEnd: function(d, evt) { Element.setStyle(d.element, { cursor: 'default' }); }
});*/

var CTopicModeration = Class.create({
	initialize: function() {
		this.target = 'moderation';
		this.container = null;
		this.Rights = new CTopicProperties('rights', 'FILE=hp/mod/topic-rights.xml&AZIONE=mostra_riga&TABELLA=t_s_topic&ID=#topic#&MODE=RW&PAGE=0');

		var setup = function() {
			this.container = $(this.target);
		};
		Event.observe(document, 'dom:loaded', setup.bindAsEventListener(this));
	},

	load: function(topic, file)
	{
		var onComplete = function() {
			this.container.writeAttribute('file', file);
			this.container.select('form').invoke('reset');
			this.container.select('form input[type=checkbox]').each(function(e){ Check.setChecked(e, false);});
		};
		Ubk.follow(this.target, 'forum.php', 'FILE='+file+'&AZIONE=mostra_riga&TABELLA=t_s_topic&MODE=W&ID=' + topic, true, true, onComplete.bind(this));
	},

	hide: function()
	{
		Ubk.hide(this.target);
	},

	move: function(topic)
	{
		this.load(topic, 'hp/mod/move-topic.xml');
	},

	merge: function(topic)
	{
		this.load(topic, 'hp/mod/merge-topic.xml');
	},

	movePosts: function(topic)
	{
		this.load(topic, 'hp/mod/move-posts.xml');
	},


	publish: function(topic)
	{
		var makeSortable = function()
		{
			Sortable.create('sortable-news', {
				tag: 'div'
				//,handle: '.top'
				,overlap: 'vertical'
				,constraint: 'vertical'
				//,onUpdate: this.updateOrder
			});
		};

		var file = 'hp/mod/publish.xml';

		var onComplete = function() {
			this.container.writeAttribute('file', file);
			makeSortable.bind( this )();
		};

		Ubk.follow(this.target, 'forum.php', 'FILE='+file+'&AZIONE=mostra&TABELLA=t_s_topic_news&MODE=R&ORDINE=i_order&ID=' + topic, true, true, onComplete.bind(this));
	},

	updateNews: function()
	{
		var childs = $('sortable-news').select('div');
		var ids = childs.pluck('id').without('').collect(function(id) { return id.substr(id.lastIndexOf('_') + 1); });

		var onComplete = function() {
			Ubk.hide(this.target);
		};
		Ubk.follow(null, 'forum.php', 'TABELLA=t_s_topic_news&AZIONE=updateNews&IDS='+ids.join('|'), false, true, onComplete.bind(this));
	},

	deleteNews: function(topic)
	{
		$('news_' + topic).remove();
	}
});



var CTopic = Class.create({
	initialize: function() {
		this.currentPage = 0;
		this.target = 'new_topic';
		this.Readers = new CTopicProperties('readers', 'FILE=hp/user/readers.xml&AZIONE=mostra&TABELLA=t_a_user&TOPIC=#topic#&ESEGUI-CARICA=false&MODE=R&PAGE-SIZE=0');
		this.Mod = new CTopicModeration;
	},
	// apre / chiude il box per il nuovo post
	toggleNew: function()
	{
		if (Ubk.isVisible(this.target)) {
			// reset del form
			$(this.target).up('form').reset();
			Emo.hide();
			Post.Preview.hide();
			new Effect.BlindUp(this.target);
		} else {
			new Effect.BlindDown(this.target);
			Emo.hide(true);
		}
	},
	closeNew: function() {
		if (Ubk.isVisible(this.target)) {
			// reset del form
			$(this.target).up('form').reset();
			Emo.hide();
			Post.Preview.hide();
			Element.hide(this.target);
		}
	},
	// rimuove, dall'elenco
	remove: function(topic)
	{
		var onOk = function() {
			var onComplete = function() {
				Ubk.working(false);
				Element.remove('topic_'+topic);
			};
			Ubk.follow(null
				, 'general.php'
				, 'AZIONE=elimina&TABELLA=t_s_topic&CAMPO-ID=i_topic_id&ID='+topic
				, false
				, true
				, onComplete.bind(this));
		};
		Ubk.confirm(Locale.string(3032), onOk.bind(this));
	},
	// apre una pagina successiva (cambia solo il contenuto, elenco post)
	openPage: function(topic, page, onComplete)
	{
		this.currentPage = page;

		Ubk.hide('posts');
		pars = 'AZIONE=mostra_riga&TABELLA=t_s_post'+
				'&FILE=hp/forum/inside/posts.xml&CAMPO-ID=i_topic_id'+
				'&ID='+topic+'&ORDINE=r_insert asc'+
				'&PAGE='+page;
		Ubk.follow('posts', 'general.php', pars, true, true, onComplete);
	},
	openLastPost: function(topic, page, post)
	{
		if (this.currentPage == page) {
			Element.scrollTo('post_' + post);
		} else {
			var onComplete = function(request) {
				Element.update('posts', request.responseText);
				Element.scrollTo('post_' + post);
				Ubk.wait.hide();
				new Effect.Appear('posts', {from: 0.4, to:1.0, duration: 0.2 });
			};
			this.currentPage = page;

			Ubk.hide('posts');
			pars = 'AZIONE=mostra_riga&TABELLA=t_s_post'+
					'&FILE=hp/forum/inside/posts.xml&CAMPO-ID=i_topic_id'+
					'&ID='+topic+'&ORDINE=r_insert asc'+
					'&PAGE='+page;
			Ubk.follow('posts-last', 'general.php', pars, true, true, onComplete);
		}
	},
	// imposta la pagina quando si fa click sul paginatore
	setPageOnClick: function(event) {
		this.currentPage = parseInt(event.element().innerHTML) - 1;
	}

});