$(document).ready(function () {
	
	var bgMode = "flash";
	var nextScene;
	
	var hash = window.location.hash.toString();
	var szene; 
	setNavigationActives();
	switch(hash) {
		case '#blog':
			szene = 'blog';
			break;
		
		case '#news':
			szene = 'news';
			break;
			
		case '#gallery':
			szene = 'gallery';
			break;
			
		case '#contact':
			szene = 'contact';
			break;
			
		case '#sponsors':
			szene = 'sponsors';
			break;
			
		case '#bio':
			szene = 'bio'
			break;
			
		default:
			chooseStarter();
			szene = 'news';
			break;		
	}
	
	$('#btn_'+szene).addClass('mark');
	
	/*if ($.browser.mozilla) {
    	$('.content').css('display', 'block');
 	}*/
	
	if(!directVisit) {
		$('#vorhang').css('display', 'block');
	}
	
	$('.bg_container').css('height', $(document).height());
	
	$(window).resize(function() {
 	 $('.bg_container').css('height', $(document).height());
	});
	
	$('.bg_container').flashembed({
			src: 'WernerHeel.swf',
			allowfullscreen: false,
			version: [10, 0],
			wmode: 'opaque',
			id: 'backgroundFilm',
			onFail: function() {
				bg_mode = "nonFlash";
				$('#crazy_animtion').fadeOut();
				$('#hard_facts_title').html('');
			}
	}, {
			startSzene: szene,
			directStart: directVisit
	});
	
	pageInit();
	if(szene == 'gallery') {
		loadGallery();
	} else if(szene == 'sponsors') {
		loadSponsors();
	} else {
		loadContent(szene);
	}
	
	
	$('#btn_news').click(function() {
		sceneChangeAS('news');
		loadContent('news');
		setNavigationActives();
		$(this).removeClass('inactive');
		$(this).addClass('mark');
		return false;
	});
	
	$('#btn_blog').click(function() {
		sceneChangeAS('blog');	
		loadContent('blog');
		setNavigationActives();
		$(this).removeClass('inactive');
		$(this).addClass('mark');
		return false;
	});
	
	$('#btn_bio').click(function() {
		sceneChangeAS('bio');
		loadContent('bio');
		setNavigationActives();
		$(this).removeClass('inactive');
		$(this).addClass('mark');
		return false;
	});
	
	$('#btn_contact').click(function() {
		if(openSceneName !== 'makingof') {
			sceneChangeAS('contact');
			loadContent('contact');
			setNavigationActives();
			$(this).removeClass('inactive');
			$(this).addClass('mark');
		}
		return false;
	});
	
	$('#btn_sponsors').click(function() {
		sceneChangeAS('sponsors');
		loadSponsors();
		setNavigationActives();
		$(this).removeClass('inactive');
		$(this).addClass('mark');
		return false;
	});
	
	$('#btn_gallery').click(function() {
		sceneChangeAS('gallery');
		loadGallery();
		setNavigationActives();
		$(this).removeClass('inactive');
		$(this).addClass('mark');
		return false;
	});
	
	$('.lang_btn').live('click', function() { 
		gLang = $(this).attr('rel');
		setLanguage();
		return false;
	});
	
	$('.nav_posts').live('click', function() {
		openScene.fadeOut();
		var api = openScene.children('.scroll_panel').data('jsp');
		api.getContentPane().load($(this).attr('rel'), {lang:gLang}, function() {
			setLanguage();	
			openScene.fadeIn();	
			api.reinitialise();																  
		});	
		return false;
	});
	
	
	
});
	 var jsReady = false;
	 
	 var openScene;
	 var nextScene;
	 var openSceneName = '';
	 var animationRunning = true;
	 var gX;
	 var gY;
	 var directVisit = true;
	 var subNavigation = false;
	 
	 var gLang = 'de';
	 
	 function chooseStarter() {
		$('#bg_image').css({
			'left': (($(document).width() / 2) - (1506 / 2)),
			'top': (($(document).height() / 2) - (980 / 2) - 10)
		});
		$('#intro').fadeIn(1000);
		directVisit = false;
		$('.crazy_btn').click(function() {
			gLang = $(this).attr('rel');
			setLanguage();
			$('#intro').fadeOut(300);
			$('#vorhang').fadeOut(300);
			startFilm();
		});	
	 }
	 
     function isReady() {
         return jsReady;
     }
     function pageInit() {
         jsReady = true;
     }
     function thisMovie(movieName) {
         if (navigator.appName.indexOf("Microsoft") != -1) {
             return window[movieName];
         } else {
             return document[movieName];
         }
     }
	 
	 function sceneChangeAS(scene) {
		 $('.overlay[rel="'+openSceneName+'"]').css('display', 'none');
		 var makingOfException = true;
		 if(scene == 'contact' && openSceneName == 'makingof') {
			makingOfException = true;
		 } else {
			 makingOfException = false;
		 }
		if(openScene.attr('id') !== scene+'_container' && !makingOfException) {
			if($('#sub_menu').attr('visible') == "true") {
				$('#sub_menu').attr('visible', 'false')
			}
			var oldSceneName = openSceneName;
			openScene.fadeOut(100, function() {
				if(oldSceneName == 'makingof') {
					$('#makingof_container').children().remove();
					$('#makingof_container').attr('loaded', 'false');
				}
			});
			$('#navigation').fadeOut(100, function() {
				$('#sub_menu').css('display', 'none');									   
			});
			thisMovie("backgroundFilm").sceneChangeAS(scene);
			animationRunning = true;
		}
	 }
	 
	 function startFilm() {
		 thisMovie("backgroundFilm").startFilm();
	 }
	 
	 // Wird von Flash aufgerufen, wenn Filme geladen wurden. Übergibt 0-Punkt der linken oberen Filmecke.
	 // Zur Synchronisation des HTML-Contents
	 function setZero(coStr) {
		 var coordinates = coStr.split('/');
		 gX = parseFloat(coordinates[0]);
		 gY = parseFloat(coordinates[1]);
		 $("div[marker='container']").each(function(index) {
		 	$(this).css('left', (parseFloat($(this).attr('xPos')) - ( -gX )));
			$(this).css('top', (parseFloat($(this).attr('yPos')) - ( -gY )));
		 });
		 var navTop =  (782 - ( -gY ));
		 $('#navigation').css('top', navTop);
	 }
	 
	 // Wird von Flash aufgerufen, zeigt Ladefortschritt an
	 function setProgress(percent) {
		 if($('#loader').css('display') == 'none') {
			 $('#loader').html('0');
			 $('#loader').fadeIn();
		 }
	 	$('#loader').html(percent);
	 }
	 
	 function hideProgress() {
		 $('#loader').fadeOut(200);
	 }
	 
	 // Wird von Flash aufgerufen, wenn Intro der aktuellen Szene abgespielt wurde.
	 // Dann kann der Inhalt eingefadet werden.
	 function sceneChanged(value) {
		 animationRunning = false;
		 window.location.hash = '#'+openSceneName;
		 $('.overlay[rel="'+openSceneName+'"]').fadeIn();
		 if(openScene.attr('loaded') == 'true'){
			 $('#sub_menu').children().each(function(index) {
				$(this).css('display', 'none');
			 });
			 if(subNavigation) {
				 $('#sub_menu').css('display', 'block');
				 $('#sub_menu').attr('visible', 'true');
				 $('#'+openSceneName+'_navigation').css('display', 'block');
			 }
			 openScene.fadeIn();
			 $('#navigation').fadeIn();
			 if(openScene.attr('paneled') == 'false') {
				 openScene.attr('paneled', 'true');
				 openScene.children('.scroll_panel').jScrollPane({
									verticalDragMinHeight: 80,
									verticalDragMaxHeight: 80,
									maintainPosition: false
				 });
			 }
		 }
		
	 }
	 
	 // Wird von FLash aufgerufen bei Making Of Events
	 
	 function specialEvent(value) {
		if(value == 'start_making_of') {
			$('#loader').css({left: parseInt($('#loader').css('left'))+140, top: parseInt($('#loader').css('top'))+57});
			 openScene.fadeOut(100);
			$('#navigation').fadeOut(100, function() {
				$('#sub_menu').css('display', 'none');									   
			});
			animationRunning = true;
			loadContent('makingof');
		 } else if(value == 'making_of_end') {
			 animationRunning = false;
			  $('#loader').css({left: parseInt($('#loader').css('left'))-140, top: parseInt($('#loader').css('top'))-57});
			  openScene.fadeIn(200);
			$('#navigation').fadeIn(200)
		 } else if(value == 'making_of_loaded') {
			 $('#loader').fadeOut(200);
		 } else if(value == 'licht_an') {
			 $('#gallery_overlay').css('display', 'none');
			 $('#gallery_container').css('display', 'none');
		 } else if(value == 'licht_aus') {
			 $('#gallery_overlay').css('display', 'block');
			 $('#gallery_container').css('display', 'block');
		 }
	 }
	 
	 
	 function loadSponsors() {
			openSceneName = 'sponsors';
			subNavigation = true;
			var obj = $('#sponsors_container');
			 if (obj.attr('loaded') == 'true') {
				 openScene = $('#sponsors_container');
			 } else {
				openScene = $('#sponsors_container');
				openScene.load(openScene.attr('url'), {lang: gLang}, function() {																			
						openScene.attr('loaded', 'true');
						$('#sponsor_navigation').css('display', 'block');
						setLanguage();
						$('.sponsor_click').click(function() {
							$('.sponsor_click').removeClass('mark');
							$(this).addClass('mark');
							$('.sponsors').css('display', 'none');
							$('.sponsors[rel="'+$(this).attr('rel')+'"]').css('display', 'block');
							return false;
						});
						if(!animationRunning) {
							openScene.fadeIn();
						}
				 });
			 }
			
	 } 
	 
	 function loadGallery() {
			openSceneName = 'gallery';
			subNavigation = true;
			var obj = $('#gallery_container');
			 if (obj.attr('loaded') == 'true') {
				 openScene = $('#gallery_container');
			 } else {
				openScene = $('#gallery_container');
				openScene.load(openScene.attr('url'), {lang: gLang}, function() {																		
						openScene.attr('loaded', 'true');
						setGalleryNavigation();
						setLanguage();
						if(!animationRunning) {
							openScene.fadeIn();
						}
				 });
			 }
			
	 }
	 
	 function loadContent(scene) {
		 openSceneName = scene;
		 nextScene = scene;
		 subNavigation = false;
		 var obj = $('#'+scene+'_container');
		 if (obj.attr('loaded') == 'true') {
			 openScene = $('#'+scene+'_container');
		 } else {
			 openScene = $('#'+scene+'_container');
			 $.ajax({
				  url: openScene.attr('url'),
				  data: {lang: gLang},
				  type: 'POST',
				  error: function() {
					  	loadContent(scene);
				  },
				  success: function(data) {
					  	openScene.html(data);
						if(openSceneName == 'makingof') {
								openScene.children('.scroll_panel').prepend('<iframe src="http://player.vimeo.com/video/16773148?color=ffffff" width="370" height="278" frameborder="0" id="makingof_player"></iframe>');	
						}
						setLanguage();
						openScene.attr('loaded', 'true');
						
						if(!animationRunning) {
							openScene.fadeIn();
							openScene.children('.scroll_panel').jScrollPane({
								verticalDragMinHeight: 80,
								verticalDragMaxHeight: 80,
								maintainPosition: false												
							});
						}
				  }
				});
			 /*
			 openScene.load(openScene.attr('url'), {lang: gLang}, function() {
					setLanguage();
					console.log('Content from '+scene+' loaded');																				
					openScene.attr('loaded', 'true');
					if(!animationRunning) {
						openScene.fadeIn();
			 			openScene.children('.scroll_panel').jScrollPane({
							verticalDragMinHeight: 50,
							verticalDragMaxHeight: 50												
						});
						console.log('Content faded in (from loadContent-Handler)');
					}
			 });	*/ 
		 }
	 }
	 function setNavigationActives() {
		$('.navigation_buttons').children().each(function(index) {
			$(this).removeClass('mark');
			$(this).addClass('inactive');
		});
	 }
	 
	 function setLanguage() {
		$('span[lang]').each(function(index) {						  
			if($(this).attr('lang') !== gLang) {
				$(this).css('display', 'none');	
			} else {
				if($(this).attr('displayType') =="inline") {
					$(this).css('display', 'inline');
				} else {
					$(this).css('display', 'block');
				}
			}
		});
		$('.lang_btn').removeClass('mark');
		$('.lang_btn[rel="'+gLang+'"]').addClass('mark');
		var api = openScene.children('.scroll_panel').data('jsp');
		if(api != null && openSceneName !== 'news') {
			api.reinitialise();
		}
	 }

