// IN_PRODUCTION, LANGUAGES, LANG, SCRIPTS have already been set globally

// Wait for all required setup steps to check in before application initialization
var bootstrap = (function() {
	// 2 things have to be done: load required scripts (only in production), set up the DOM
	var requiredSteps = IN_PRODUCTION ? 2 : 1;
	return function() {
		requiredSteps --;
		if (requiredSteps == 0) {
			$.fancybox.hideActivity();
			init();
		}
	};
})();



// Redirects to dynamic version, if the URL starts with anything but a '#' after the host name
// e.g. redirects from http://sinelege.com/de/filme/ to http://erwinbauer.com/de/#filme/
(function() {
	var pathWithoutLanguage = window.location.pathname.replace(new RegExp('^/'+LANG+'/'), '/');
	var matches = pathWithoutLanguage.match(/^\/(.+)/);
	if (matches) {
		var request = matches[1];
		window.location.href = window.location.protocol + '//' + window.location.host + '/'+LANG+'/#' + request;
	}
})();



// Load scripts asynchronously in production
if (IN_PRODUCTION) {

	// Load the required scripts; when all are loaded, tell bootstrap
	(function() {
		var requiredScripts = SCRIPTS.length;
		for (var i in SCRIPTS) {
			$.getScript(SCRIPTS[i], function() {
				requiredScripts --;
				if (requiredScripts == 0) {
					// If a specific page is being requested, show spinner while content is being loaded
					if (window.location.hash) {
						$(function () {
							$.fancybox.showActivity();
						});
					}
					bootstrap();
				}
			});
		}
	})();
}



// Wait for the DOM to be ready before adding content
$(function() {
	var urls = null;
	var divs = null;
	var pages = {};

	// Get the URLs of all the pages
	$.getJSON('/'+LANG+'/urls/', function (data) {
		urls = data;
		loadedListener();
	});
	// Get all the pages in one request, disable video autoplay, and remove videos and store them away,
	// so there won't be any loading bottlenecks on initial page load
	$.post('/'+LANG+'/content/', {ajax: true}, function (data) {
		divs = $(data);
		var video = divs.find('video').attr({autoplay: ''});
		var poster = $('<img/>')
			.css({position: 'absolute', top: 0, left: 0, zIndex: 2})
			.attr('src', video.attr('poster'));
		var videoWrapper = divs.find('#video-wrapper');
		videoWrapper.data({video: videoWrapper.find('#video-container').remove(), poster: poster});
		loadedListener();
	});
	// Call the handler only after all the content has been loaded
	function loadedListener() {
		if ( ! (urls && divs)) return;
		loadedHandler();
	}
	
	// Manipulate the DOM to accomodate all the content
	function loadedHandler() {
		var content = $('.content');
		var contentWidth = content.outerWidth();
		var viewportWidth = $(window).width();
		var margin = (viewportWidth - contentWidth) / 2;
		var languageRegexp = new RegExp('('+LANGUAGES.join('|')+')/');
		
		// Prepare the content
		for (var i in urls) {
			var currentUrl = urls[i];
			var data = divs.eq(i);
			var cssClass = currentUrl
				.replace(languageRegexp, '')
				.replace(/^\//, '')
				.replace(/\//g, '_');
			cssClass || (cssClass = '_');
			pages[currentUrl] = $('<div/>', {'class': 'content'}).addClass(cssClass);
			pages[currentUrl].append(data);
		}

		// Make footer positioned, so it will keep its place when the content is being pulled from the flow
		var footer = $('#footer');
		footer.css({
			top: footer.position().top,
			left: footer.position().left,
			width: footer.width(),
			position: 'absolute'
		});
		
		// Bring original container to front, so logo will stay visible in IE <= 7
		$('#container').css({zIndex: 1});
		
		// The element containing the sliding part
		var sliderContainer = $('<div/>', {
			id: 'slider-container',
			css: {
				height: content.height(),
				width: viewportWidth,
				position: 'absolute',
				top: content.offset().top,
				right: 0
			}
		}).appendTo($('body'));
		
		// The sliding content container
		$('<div>', {
			id: 'slider',
			css: {
				position: 'absolute',
				top: 0,
				right: 0
			}
		}).appendTo(sliderContainer);
		
		// Reorder URL array, so the originally requested URL comes first 
		for (var i in urls) {
			if (content.hasClass(urls[i])) break;
			urls.push(urls.shift());
		}
		
		// Add content to sliding content container
		for (var i in urls) {
			$('#slider').append(pages[urls[i]].css({
				position: 'absolute',
				top: 0
			}));
		}
		
		// After everything is in place, the original content can be removed
		content.remove();
		
		// Tell bootstrap that the DOM is ready
		bootstrap();
	}

});


