
var Menu = new Class({
	initialize: function(container, options) {
		this.container = $(container);
		this.container.getElements('div').each(function(element) {
			var uri = new URI();
		
			switch(true) {
				case element.hasClass('button'):
					var button = new Gx.Button.Simple(element);
					
					if (button.hasClass('submit')) {
						if (uri.getData('s'))
							button.addClass('selected');
						
						button.addEvent('click', (function() {
							if ((query = this.container.getElement('[name=search]').getValue()) != '')
								new URI('/').setData('s', query).go();
						}).bind(this));
					} else {
						if (uri.getData('s') == null && window.location.pathname.contains(button.get('href'), '/'))
							button.addClass('selected');
						
						button.addEvent('click', function() {
							window.location = this.get('href');
						});
					}
					break;
					
				case element.hasClass('entry'):
					var entry = new Gx.Entry(element);
					break;
			}
		}, this);

		window.addEvent('resize', this.center.bind(this));
		this.center();
		this.container.setStyle('visibility', 'visible');
	},
	center: function() {
		this.container.setStyle('margin-left', (window.getSize().x / 2) - (this.container.getSize().x / 2));
	}
});

Gx.Button.Simple.Post = new Class({
	Extends: Gx.Button.Simple,
	mouseenter: function(event) {
		this.container.addClass('hover');
		this.parent(event);
	},
	mouseleave: function(event) {
		this.container.removeClass('hover');
		this.parent(event);
	}
});

/*
 * The principle is simple. JavaScript detects the browser and 
 * platform, visitors can apply a specific class to the html 
 * element. Consequently, all page elements will be well in this 
 * class hierarchy.
 */

window.addEvent('domready', function() {
	$(document.html).addClass(Browser.Platform.name).addClass(Browser.Engine.name);
});

window.addEvent('load', function() {
	var mb, nav, mosaic, post = $('post');

	// Milkbox
	mb = new Milkbox();

	// Milkbox options
	if (mb.galleries.length != 0)
		mb.changeOptions({maxHeight: 800, autoPlay: false});

	// New top menu
	new Menu('menu');

	// Navigation tabpanel
	nav = new Gx.Tabpanel('navigation', {
		initSelect: {
			// Page
			0: function() { new Tree('tree'); },
			// Category
			1: function() { new Tree('category'); }
		}
	});
	
	// Detect if we are in a page or article
	nav.select((nav.container.getElement('.current_page_item')) ? 0 : 1, true);

	// Mosaic tabpanel
	//mosaic = new Gx.Tabpanel('mosaic').select(0, true);
	
	//
	if (post) {
		post.getElements('a[rel*=tag], a.post-edit-link, a.comments-link').each(function(element) {
			new Gx.Button.Simple.Post(element);
		});
	}
});
