/**
 * Language Switch
 */
$(function() {  
	var switcher = true;
	
	if ($.browser.msie) {
		switcher = false;
		$('a.switch').hide();
	}
	
		var pageHeight = $(document).height();
		var pageWidth = $(document).width();
		var switchWidth = (pageWidth - 1050) / 2;
	
		//$('a.switch').show();
		
						  
		
		function setLanguageSwitch() {	
			$('#black.page').height(pageHeight-1);
			// Show switch only if viewport has enough width	
			if (pageWidth > 1200) {			
				$('.switch').css({
					width: switchWidth, height: pageHeight
				}).show();
			} else {
				$('.switch').hide();	
			}	
		}	
	
	if (switcher == true) {
	
		setLanguageSwitch();
		
		$(window).resize(function() {
			setLanguageSwitch();
		});	
	
		/**
		 * LANGUAGE SLIDE: to black page
		 */
		$('#toblack, #white #Sprachwahl a, .switch-to-black').click(function(e) {		
			e.preventDefault();
			
			$('body').css('overflow-x', 'hidden');
			var loadURL = $('#white #Sprachwahl .first a').attr('href');
			
			/* If on Address/Adresse => direct jump (because slide with Google Maps not working) */
			var request = document.URL.toLowerCase();
			if (request.search("adresse") > 0 || request.search("impressum") > 0) {
				window.location.href=loadURL;
				return false;
			}
			
			// 1. Extend body/page width to double size
			$('html, body').css('width', 2*pageWidth);
			$('.page').width(pageWidth);
			$('.page').css('position', 'absolute');
			$('.page').css('left', pageWidth);
			
			// 2. Set the viewport to the current page
			$('html, body').scrollLeft(pageWidth);
			
			// 3. Load the new page (outside the viewport)		
			// 4. Scroll viewport to left		
			// 5. Reload new page (as reqular page)		
			$.ajax({
				type: "GET",
				url: loadURL,
				success: function(data) {				
					$('<div class="page">').attr("id", "black").css("left", 0).prependTo("body");
					$("#black").width(pageWidth).height(pageHeight);
					$('#black').css('position', 'absolute');
					$("#black").html(data);
		
					$("#towhite").width(switchWidth);
					$("#towhite").show();
								
					$('html, body').animate({			
						scrollLeft: $("#black").offset().left
					}, 1500, function() {	
						window.location.href=loadURL;					
					});				
				} // END success
			}); // END $.ajax
		
		}); // END click
		
		
		// Click on #towhite
		$('#towhite, #black #Sprachwahl a, .switch-to-black').click(function(e) {
			e.preventDefault();
			$('body').css('overflow-x', 'hidden');
			var loadURL2 = $('#black #Sprachwahl .first a').attr('href');
			
			/* If on Address/Adresse => direct jump (because slide with Google Maps not working) */
			var request = document.URL.toLowerCase();
			if (request.search("address") > 0 || request.search("imprint") > 0) {
				window.location.href=loadURL2;
				return false;
			}		
			
			// 1. Extend body/page width to double size
			$('html, body').css('width', 2*pageWidth);
			$('.page').width(pageWidth);
			$('.page').css('position', 'absolute');
			//$('.page').css('left', pageWidth);		
			
			// 2. Set the viewport to the current page
			//$('html, body').scrollLeft(pageWidth);
			
			// 3. Load the new page (outside the viewport)		
			// 4. Scroll viewport to left		
			// 5. Reload new page (as reqular page)		
			$.ajax({
				type: "GET",
				url: loadURL2,
				success: function(data2) {				
					$('<div class="page">').attr("id", "white").css("left", pageWidth).prependTo("body");				
					$("#white").width(pageWidth).height(pageHeight);
					$('#white').css('position', 'absolute');				
					$("#white").html(data2);
					
					//$("#black .switch").attr("id", "towhite");
					$("#toblack").width(switchWidth);				
										
					//$('head').clone().appendTo('html').remove();					
					
					//$('<div class="switch" id="towhite">').prependTo('#black');
					//setDimensions();				
				
					$('html, body').animate({			
						scrollLeft: $("#white").offset().left
					}, 1500, function() {
						//$('#white').remove();					
						window.location.href=loadURL2;					
					});				
				} // END success
			}); // END $.ajax
		
		}); // END click
		
	}
});  
