(function(window,undefined){

	// Prepare our Variables
	var
		History = window.History,
		$ = window.jQuery,
		document = window.document;

	// Check to see if History.js is enabled for our Browser
	if ( !History.enabled ) {return false;}

	// Wait for Document
	$(function(){
		// Prepare Variables
		var
			/* Application Specific Variables */
			contentSelector = '#content_layer',
			$content = $(contentSelector).filter(':first'),
			contentNode = $content.get(0),
			/* Application Generic Variables */
			$body = $(document.body),
			rootUrl = History.getRootUrl(),
			absoluteUrl = HTTPLANG;

		// Ensure Content
		if ( $content.length === 0 ) {$content = $body;}

		// Ajaxify Helper
		$.fn.ajaxify = function(){
			// Prepare
			var $this = $(this);

			// Ajaxify all proper links
			$this.find('a.ajaxy').click(function(event){
				// Prepare
				var
					$this = $(this),
					url = $this.attr('href'),
					title = $this.attr('title')||null;

				// Continue as normal for cmd clicks etc
				if ( event.which == 2 || event.metaKey ) { return true; }

				// Ajaxify this link
				History.pushState(null,title,url);
				event.preventDefault();
				return false;
			});

			// Chain
			return $this;
		};

		// Ajaxify our Internal Links
		$body.ajaxify();

		// Hook into State Changes
		$(window).bind('statechange',function(){
			// Prepare Variables
			var State = History.getState();
			var url = State.url;
			
			// Clear Stage, Block UI and Show Loader
            clearStage();
	
			// Ajax Request the Traditional Page
			$.ajax({
				type: "GET",
				url: url,
				dataType: "json",
				success: function(data, textStatus, jqXHR){

					// Call Load Stage Function
					loadStage(data, textStatus, jqXHR);

				},
				error: function(jqXHR, textStatus, errorThrown){
					document.location.href = url;
					return false;
				}
			}); // end ajax


		}); // end onStateChange

	}); // end onDomLoad

})(window); // end closure
