var newContent = null;
var fadingOut = false;
var loading = false;

function fadeContent(path) {

	if (path == '/') {
		$('imprint').removeClass('first');
		$('english').setStyles({'display':'inline'});
	} else {
		$('imprint').addClass('first');
		$('english').setStyles({'display':'none'});
	}
	var contentNode = $('content');
	if (contentNode) {
		fadingOut = true;
		newContent = null;
		var xmlRequest = new Request.HTML({
			url:path,
			onSuccess: function (responseTree, responseElements, responseHTML, responseJavaScript) {
				newContent = responseHTML;
				contentFadeIn();
			}
		}).get({'isAjax':1}); 
			
		var node = $('grainNavi');
		if (node) {
			node.innerHTML = '';
		}

		var fadeFx = new Fx.Tween(contentNode, {
			duration: 500, 
			transition: new Fx.Transition(Fx.Transitions.Sine).easeOut,
			onComplete: function(){
				$('content').innerHTML = '';
				fadingOut = false;
				contentFadeIn();
			}
		});
		fadeFx.start('opacity', 0);				
	}
}

function contentFadeIn() {
	var contentNode = $('content');
	if (contentNode) {
		if (newContent && !fadingOut) {
			contentNode.innerHTML = newContent;
			var fadeFx = new Fx.Tween(contentNode, {
				duration: 500, 
				transition: new Fx.Transition(Fx.Transitions.Sine).easeOut,
				onComplete: function(){
					$('content').innerHTML = newContent;
					newContent = null;
					initAfterLoad();
					loading=false;
				}
			});
			fadeFx.start('opacity', 1);				
		}
	}
}










function fadeWindowContent(path) {
	
	if (path == '/') {
		$('imprint').removeClass('first');
		$('english').setStyles({'display':'inline'});
	} else {
		$('imprint').addClass('first');
		$('english').setStyles({'display':'none'});
	}
	var contentNode = $$('#content div.windowContent');
	if (contentNode.length > 0) {
		contentNode = contentNode[0];
	} else {
		contentNode = null;
	}
	if (contentNode) {
		fadingOut = true;

		var xmlRequest = new Request.HTML({
			url:path,
			onSuccess: function (responseTree, responseElements, responseHTML, responseJavaScript) {
				newContent = responseHTML;
				windowContentFadeIn();
			}
		}).get({'isAjax':1, 'isWindow': 1}); 
			


		var fadeFx = new Fx.Tween(contentNode, {
			duration: 500, 
			transition: new Fx.Transition(Fx.Transitions.Sine).easeOut,
			onComplete: function(){
				var contentNode = $$('#content div.windowContent');
				if (contentNode.length > 0) {
					contentNode = contentNode[0];
				} else {
					contentNode = null;
				}
				if (contentNode) {
					contentNode.innerHTML = '';
				}
				fadingOut = false;
				windowContentFadeIn();
			}
		});
		fadeFx.start('opacity', 0);				
	}
}

function windowContentFadeIn() {
	var contentNode = $$('#content div.windowContent');
	if (contentNode.length > 0) {
		contentNode = contentNode[0];
	} else {
		contentNode = null;
	}
	if (contentNode) {
		if (newContent && !fadingOut) {
			contentNode.innerHTML = newContent;
			var fadeFx = new Fx.Tween(contentNode, {
				duration: 500, 
				transition: new Fx.Transition(Fx.Transitions.Sine).easeOut,
				onComplete: function(){
					$$('#content div.windowContent').innerHTML = newContent;
					newContent = null;
					initAfterLoad();
					loading=false;
				}
			});
			fadeFx.start('opacity', 1);				
		}
	}
}




function initAfterLoad() {
	initGrainNavi();
	initWindowSlider();
	initLinks();
}