window.addEvent("domready", function() {
	// Content holder is:
	var contentHolder = document.id("contentHolder");

	// Create the request
	var request = new Request.HTML({
		onSuccess: function(nodeTree, elements, html) {
			// Set the content into the content holder
			contentHolder.set("html", html);
			// Execute directions that should be executed whenever a page changes

			onPageUpdate();
		}
	});
	request.send({
		url: 'home.html'
	});

	// Create a function that loads the page content
	var loadPage = function(url) {
		//request.send({
		//			url: url
		//		
		//		});

		//
		if (url != '') {
			// Make a HTML request to get the content for this page
			if (url != '/') {
				request.send({
					url: url
				});
			}
		}

	};

	// The listener that manages all clicks
	var listener = function(evt) {
		evt.preventDefault(); // Prevent following the URL
		var href = this.get('href'); // Get the URL
		History.push(href); // Push the new URL
	};

	// Listener for the "Back" link
	var back = function(evt) {
		evt.preventDefault();
		History.back(); // Go back
	};



	// Add event delegation to add clicks 
	//document.body.addEvent("click:relay(a:not([href=#]):not([href^=http://]):not([data-noxhr]))",listener);

	document.addEvent("click:relay(a:not([href=#],[href^=pdf/],[href^=http://],[data-noxhr]))", listener);


	// Function that will execute whenever a page gets changed
	var onPageUpdate = function() {

		if (document.getElementById('image_gallery') != null) {
			var thumns = $$(".thumbs li a");
			var gallery6 = new fadeGallery($$(".gallery6"), {
				speed: 500,
				autoplay: true,
				duration: 2000,
				onStart: function() {
					thumns.removeClass("active");
					thumns[this.current].addClass("active");
				},
				onPlay: function() {
					this.fireEvent("start");
				}
			});

			thumns.each(function(el, i) {
				el.addEvent("click", function() {
					thumns.removeClass("active");
					this.addClass("active");
					gallery6.current = i;
					gallery6.play(true);
					return false;
				});
			});
		}

		if (document.getElementById('news_wrapper') != null) {

			window.addEvent("domready", function() {
				var T = new SimpleMooTicker({
					container: document.id("news_wrapper"),
					step: 3,
					periodical: 50
				});
			});
		}

	};

	// When the history changes, update the content 
	History.addEvent('change', loadPage);

	// Handle the initial load of the page if the browser does not support pushState, check if the hash is set
	if (!History.hasPushState()) {
		// Check if there is a hash
		var hash = document.location.hash.substr(1);
		if (!hash) return;

		// If the hash equals the current page, don't do anything
		var path = document.location.pathname.split('/');
		path = path[path.length - 1];
		if (hash == path) return;

		// Load the page specified in the hash
		loadPage(hash);
	}

	// Update the page
	onPageUpdate();

});
