window.addEvent('domready', function(){

	HistoryManager.initialize();
	
	var a = new Element('a', {
		'href': window.location.hash.substring(1)
	});
	a.inject($('container'), 'top');
	
	var hrefs = $('container').getElements('a[href^=/]');
	hrefs.extend($('container').getElements('a[href^='+window.location.protocol+'//'+window.location.hostname+']'));
	
 	var currentIndex = -1;
 
 	var req = new Request.HTML({
		method: 'get',
		//url: el.get('href'),
		data: { 'action1' : 'ajax' },
		evalScripts: false, // don't ask mootools to process js
		evalResponse: false, // we will process it manually
		//onRequest: function() { alert('Request made. Please wait...'+req.url); },
		onSuccess: function(rTree, rElements, rHTML, rJS) {
			$('content').set('html', rHTML);
			//$('content').adopt(html);
			//hrefs.extend = rElements.getElements('a[href^=page_show]')

			var last = hrefs.length;
			hrefs.extend($('content').getElements('a[href^=/]'));
			hrefs.extend($('content').getElements('a[href^='+window.location.protocol+'//'+window.location.hostname+']'));
			hrefs.each(function(el, i) {
					el.addEvent('click', function(e) {
						if (e) new Event(e).stop();
						ajaxUpdate(i);
					});
			});
			$exec(rJS);
			//setLinks($('content'));
		}
		
		//update: $('content'),
		//onComplete: function(response) { alert('Request completed successfully.');
		//}
	});

	var reqHistory = HistoryManager.register(
		'/',
		[0], // default, page 0
		function(values) {
			ajaxUpdate(values[0]);
		},
		function(values) {
			//return '/' + values[0][0] + values[0][1] + '';
			return values[0][1] + '';
		},
		false///\/(\d+)/ // the regexp to match "page-index(0)"
	);
 
	/**
	 * Loads the url (by index) if the index is valid and not the current page
	 */
	function ajaxUpdate(index) {
		
		var url = new Object;

		url.href = hrefs[index].getProperty('href') || null;
		url.href = url.href.replace(window.location.protocol+'//'+window.location.hostname, '');
		url.splitted = url.href.split('/').erase('');
		
		if(url.splitted.length > 0){
			url.language = url.splitted[0];
		}
		if(url.splitted.length > 1){
			url.menu = url.splitted[1];
		}
		if(url.splitted.length > 2){
			url.submenu = url.splitted[2];
		}

		if (!url.href || (currentIndex == index)) return;

		// fancy state change with classes
		if(hrefs[index].getParent().hasClass('menuitem')){
			hrefs.removeClass('active');
			hrefs[index].addClass('active');
		}else{
			hrefs.each(function(el){
				if(el.getProperty('href') == hrefs[index].getProperty('href') && el.getParent() && el.getParent().hasClass('menuitem')){
					hrefs.removeClass('active');
					el.addClass('active');
				}
			});
		}

		currentIndex = index;

		// updating the history
		reqHistory.setValue(0, [index, url.href]);

		// request
		req.options.url = '/ajax/'+url.href;
		req.send();
	};

	hrefs.each(function(el, i) {
		el.addEvent('click', function(e) {
			if (e) new Event(e).stop();
			ajaxUpdate(i);
		});
	});

	//hrefs[0].fireEvent('click');

	HistoryManager.start();
 
});