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);
				});
			});
			
			$('content').getElements('a[href^=Fotoboek]').each(function(el){
				el.addEvent('click', function(e){
					if (e) new Event(e).stop();
					$('megazine').setStyle('display','block');
					$('megazine').fade('in');
					$('container').setStyles({
						'height':window.getSize().y,
						'overflow': 'hidden'
					});
					openMegazine(el.get('href'));
				});
			});
			$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();

	function openMegazine(name){
		var flashvars = {
			/* This is the ABSOLUTE base that to use for all path resolving. This has an effect on ALL paths (including GUI, sounds etc). */
			basePath: "http://www.vanhalenevents.nl/athena/site/megazine/",
			
			/* Used to pass the name of the xml file to use. Path is RELATIVE to basePath, or, if not set, to the megazine.swf file. */
			xmlFile: "../php/content_gallery.php?name="+escape(name)
			
			/* When set to true, log messages are printed to the JavaScript console (using the console.log() function) */
			//logToJsConsole: "true"
		};
		var params = {
			/* Determines whether to enable transparency (show HTML background). Not recommended (slow). Use book/background instead. */
			wmode: "transparent",
			menu: "false",
			/* Necessary for proper scaling of the content. */
			scale: "noScale",
			/* Necessary for fullscreen mode. */
			allowFullscreen: "true",
			/* Necessary for SWFAddress and other JavaScript interaction. */
			allowScriptAccess: "always",
			/* This is the background color used for the Flash element. */
			bgcolor: "#000000"
		};
		var attributes = {
			/* This must be the same as the ID of the HTML element that will contain the Flash element. */
			id: "megazine_inner"
		};
		/* Actually load the Flash. */
		swfobject.embedSWF("../megazine/preloader.swf", "megazine_inner", "100%", "100%", "9.0.115", "../js/expressInstall.swf", flashvars, params, attributes);
	}
 
});
