var f = null;

window.addEvent('load', function() {
	init();
});

window.addEvent('unload', function() {
	f = null;
});

function init()
{
	f = new Engine();
	f.init( document );
}

var Engine = new Class({
	
	init: function( obj )
	{
		this.menu();
		this.tree();
		this.comments();
		this.relabels();
	},

	menu: function( )
	{
		var items = $$('#posts-categories li a');
		items.each( function(item, i) { item.set('html', '<span>' + item.get('html') + '</span>') } );
	},
	
	tree: function( )
	{
		$$('#tree li ul').setStyle('display', 'none');
		var items = $$('#tree li');
		for (var i=0; i<items.length; i++)
		{
			if (items[i].getElement('ul'))
				items[i].getElement('a').addClass('current-has-sub');
		}
		var current = $$('#tree .current-cat')[0];
		if (current)
		{
			current.getElement('a').addClass('current-cat-link');
			var sub = current.getElement('ul');
			if (sub)
			{
				sub.setStyle('display', 'block');
			}
			this.treeShow( current );
			this.treeParent( current );
		}
		
		var parents = $$('#tree .current-cat-parent');
		for (var i=0; i<parents.length; i++)
		{
			parents[i].getElement('a').addClass('current-cat-parent-link');
		}
		
		var root = document.getElement('#tree');
		this.treeLevel(root, 0);
	},
	
	treeParent: function (current)
	{
		var parent = current.getParent('li');
		if (parent)
		{
			parent.addClass('current-cat-parent');
			this.treeParent(parent);
		}
	},
	
	treeShow: function( obj )
	{
		if (!obj) return;
		var parent = obj.getParent();
		if (parent.get('tag') == 'ul')
		{
			parent.setStyle('display', 'block');
			this.treeShow( parent.getParent('li') );
		}
		return;
	},
	
	treeLevel: function( node, level )
	{
		var subtree = node.getElement('ul');
		if (!subtree) return false;
		var subs = subtree.getChildren('li');
		//console.log(subs.length);
		if (subs.length > 0)
		{
			level++;
			subs.addClass('level-' + level);
			for (var i=0; i<subs.length; i++)
			{
				subs[i].addClass('level-' + level);
				subs[i].getElement('a').addClass('link-level-' + level);
				this.treeLevel( subs[i], level );
			}
		}
	},
	
	comments: function()
	{
		this.page = 0;
		this.list = $('commentlist');
		this.post = $('post-details-content');
		this.form = $('respond');
		if (!this.list) return false;
		
		this.hScreen = this.post.getSize().y - this.form.getSize().y;
		this.list.setStyle('height', this.hScreen);
		this.hTotal = this.list.getScrollSize().y;
		this.nPages = Math.ceil( this.hTotal / this.hScreen );
		
		this.nav = new Element('div', {'class': 'comments-nav'} ).inject( this.list, 'after' );
		this.nav.pages = new Element('span', {'class': 'comments-nav-pages'} ).inject( this.nav );
		this.nav.prevnext = new Element('span', {'class': 'comments-nav-prevnext'} ).inject( this.nav );
		this.nav.prev = new Element('a', {'class': 'comments-nav-prev', 'href': 'javascript:;', 'html': '<span>&lt;</span>'} ).addEvent('click', this.commentsGo.pass(-1, this)).inject( this.nav.prevnext );
		this.nav.next = new Element('a', {'class': 'comments-nav-next', 'href': 'javascript:;', 'html': '<span>&gt;</span>'} ).addEvent('click', this.commentsGo.pass(1, this)).inject( this.nav.prevnext );
		
		this.navclone = new Element('div', {'class': 'comments-nav'} ).inject( this.list, 'before' );
		this.navclone.pages = new Element('span', {'class': 'comments-nav-pages'} ).inject( this.navclone );
		this.navclone.prevnext = new Element('span', {'class': 'comments-nav-prevnext'} ).inject( this.navclone );
		this.navclone.prev = new Element('a', {'class': 'comments-nav-prev', 'href': 'javascript:;', 'html': '<span>&lt;</span>'} ).addEvent('click', this.commentsGo.pass(-1, this)).inject( this.navclone.prevnext );
		this.navclone.next = new Element('a', {'class': 'comments-nav-next', 'href': 'javascript:;', 'html': '<span>&gt;</span>'} ).addEvent('click', this.commentsGo.pass(1, this)).inject( this.navclone.prevnext );
		
		this.commentsGo(0);
	},
	
	commentsGo: function( offset )
	{
		this.page += offset;
		this.page = this.page.limit(0, this.nPages-1);
		var y = this.page * this.hScreen;
		this.list.scrollTop = y;
		
		// output
		var txt = 'page ' + (this.page+1) + ' di ' + this.nPages;
		this.nav.pages.set('html', txt);
		this.navclone.pages.set('html', txt);
	},
	
	relabels: function( )
	{
		var items = $$('.relabel');
		items.each( this.relabel.bind(this) );
	},
	
	relabel: function( obj )
	{
		var frm = obj.getParent('form');
		var labels = frm.getElements('label');
		for (var i=0; i<labels.length; i++)
		{
			if (labels[i].get('for') == obj.get('id'))
			{
				obj.defaultValue = labels[i].get('html');
				labels[i].dispose();
			}
		}
		
		if ( (obj.get('value').length == 0) || (obj.get('value') == obj.defaultValue) )
		{
			obj.set('value', obj.defaultValue);
			if (obj.hasClass('relabel-password'))
				obj.set('type', 'text');
		}
		obj.addEvent('focus', function(obj) {
			if (obj.get('value') == obj.defaultValue)
			{
				obj.set('value', '');
				if (obj.hasClass('relabel-password'))
					obj.set('type', 'password');
			}
		}.pass(obj, this) );
		obj.addEvent('blur', function(obj) {
			if (obj.get('value') == '')
			{
				obj.set('value', obj.defaultValue);
				if (obj.hasClass('relabel-password'))
					obj.set('type', 'text');
			}
		}.pass(obj, this) );
	}
});
