// --------------------------------------------------------
// Dir di primo livello

var CRoot = Class.create({
	initialize: function() {
		this.entries = this.active = null;

		Event.observe(document, 'dom:loaded', this.observe.bindAsEventListener(this));
	},

	activate: function(el)
	{
		if (this.active) Observer.deactivate(this.active);
		if (el = $(el)) Observer.activate(el);
		this.active = el;
	},

	activateId: function(id)
	{
		var el = $('root_' + id);
		if (el) this.activate(el);
	},

	activateOnEvent: function(e) {
		if (!Ubk.isOpenPage(e))
			this.activate(e.element());
	},

	observe: function() {
		var f = function() {
			this.entries = $$('#forum-roots a');
			this.entries.invoke('observe', 'click', this.activateOnEvent.bindAsEventListener(this));
		};

		f.bind(this).defer();
	}
});

// --------------------------------------------------------
// Dir laterale


var CDir = Class.create({
	initialize: function() {
		this.dir   = null;
		this.forum = null;
	},

	// attiva una entry (foglia o nodo)
	activate: function(entry)
	{
		if (l = $('lnf_' + this.forum)) {
			Observer.deactivate(l);
		}
		// e' un forum
		if (l = $('lnf_' + entry)) {
			Observer.activate(l);
			this.forum = entry;
		// e' una dir
		} else if (l = $('dir_' + entry)) {
			Observer.forceActive(l, '');
			this.forum = null;
		}
	},

	// mostra il contenuto di una directory
	showDirectory: function( directory, after )
	{
		if (this.dir == null || this.dir != directory) {
			this.dir = directory;

			pars = 'AZIONE=mostra_riga&TABELLA=t_a_forum'+
					'&FILE=hp/forum/links/dir-list.xml&CAMPO-ID=i_forum_id'+
					'&ID=' + directory +'&PAGE=0';

			var onComplete = function() {
				Ubk.tryThis(after);
			}

			Ubk.follow('dir-content', 'general.php', pars, true, true, onComplete);
		} else {
			Ubk.tryThis(after);
		}
	},

	// nasconde tutto il menu
	hide: function(after)
	{
		Ubk.hide('dir-content');
		this.dir   = null;
		this.forum = null;
	},

	// sincronizza il percorso
	// deve essere ordinato dalla root a scendere
	setPath: function(path) {
		// dove vado a finire
		var end = path[path.length - 1];
		// se finisco su un forum, lo tolgo dalla coda di controllo
		if (end[0] == 'H') path.pop();

		// arrivati qua, path contiene per forza almeno una dir (la root)

		// l'ultima cosa da fare, in ogni caso, è attivare le entry
		var after = function() {
			this.activate(end.substr(1));
			Root.activateId(path[0].substr(1));
		};
		// la dir che devo aprire
		if ( path.length > 0 )
		{
			var lastDir = path[path.length - 1];
			// la apro e attivo le entry
			this.showDirectory(lastDir.substr(1), after.bind(this));
		}
		else
			this.hide( after.bind(this) );
	},

	// mette gli observer sulle entry
	observe: function()
	{
		var entries = $$('#dir-path div:not([observed])');

		entries.invoke('observe', 'mouseover', Observer.over)
				.invoke('observe', 'mouseout', Observer.out)
				.invoke('observe', 'click', Forum.openAtomOnEvent.bindAsEventListener(Forum))
				.invoke('writeAttribute', 'observed');
			;
	}

});

var CForumReaders = Class.create({
	initialize: function() {
	},

	load: function(forum)
	{
		var onComplete = function() {
			var button = $('readers');
			if (button) {
				button.writeAttribute('hidden');
				sibs = button.siblings().findAll(Ubk.isVisible);
				sibs.each(function(e){
					e.toggle().writeAttribute('hidden');
				});
			}
		};
		var params = 'FILE=hp/user/readers.xml'+
				'&AZIONE=mostra&TABELLA=t_a_user'+
				'&FORUM='+forum+'&ESEGUI-CARICA=false'+
				'&MODE=R&PAGE-SIZE=0';

		Ubk.follow('forumcontent', 'general.php', params, true, true, onComplete.bind(this));
	},
	close: function(forum)
	{
		Forum.openAtom(forum, null, null, null);
	},

	toggle: function(forum)
	{
		if ($('readers').hasAttribute('hidden'))
			this.close(forum);
		else
			this.load(forum);
	}
});

var CForumFeed = Class.create({
	initialize: function() {
	},

	open: function(id, page)
	{
		Ubk.hide('content');
		Ubk.follow('content', 'feed.php', 'TABELLA=t_s_user_feed&AZIONE=getFeed&ID='+id+'&XSL=system/xsl/user-feed-home.xsl&PAGE='+page, true, true);
	},
	openForum: function(forum, page)
	{
		Ubk.hide('content');
		Ubk.follow('content', 'feed.php', 'TABELLA=t_a_forum&AZIONE=getFeed&FORUM='+forum+'&XSL=system/xsl/feed-home.xsl&PAGE='+page, true, true);
	}
});

// --------------------------------------------------------
// Forum (contenuto centrale)
var CForum = Class.create({
	initialize: function(rootObj, dirObj) {
		this.Readers = new CForumReaders(this);
		this.Feed = new CForumFeed(this);
	},

	// apre il content: def-home o posts
	// nella pagina viene poi chiamato il metodo per sincronizzare la directory
	openAtom: function (forum, topic, page, post)
	{
		var fOpen = function(file, table, idField, idValue, order, page, post) {

			var pars = 'AZIONE=mostra_riga&TABELLA='+table+
					'&FILE='+file+'&CAMPO-ID='+idField+
					'&ID=' + idValue+'&ORDINE=' + order+
					'&PAGE='+page;

			var onComplete = function() {
				if (post != undefined) {
					Element.scrollTo('post_' + post);
				}
				User.Menu.activate(null);
			};
			Ubk.follow('content', 'general.php', pars, true, true, onComplete);
		};

		Ubk.hide('content');

		if (topic == null || topic == undefined) {
			fOpen('hp/forum/homes/def-home.xml', 't_a_forum', 'i_forum_id', forum, '', page || 0);
		} else {
			Topic.currentPage = page || 0;
			fOpen('hp/forum/inside/topic-body.xml', 't_s_post', 'i_topic_id', topic, 'r_insert asc', page || 0, post);
		}
	},

	openAtomOnEvent: function(event)
	{
		if ((event.isLeftClick() && event.ctrlKey) || event.isMiddleClick()) return;

		var id = event.element().id;
		id = id.substr(id.lastIndexOf('_')+1);

		this.openAtom(id, null, 0, null);
	}
});
