/* 
 *	Author: Alexei Matveev
 *
 */

(function($) {
	var App = {};

	App.scrollToPoint = function(point){
		App.isScrolling = true;

		$('html,body').animate({scrollTop: point},'slow', function() { 
			App.isScrolling = false; 
		});
	};

	
	$(function() {
		
		// Hide toggleable blocks only if javascript is active,
		// so we don't hide content irreversibly
		
		$('.toggleBlock').map( function(){ 
			$(this).attr('style','display:none;')
		});
		
		// Expand links
		$('.body').delegate('.expandlink', 'click', function(e){
			var $container = $(this).parents('.expandable');
			//var currentPosition = $(this).offset().top;
			
			$container.toggleClass('expanded');
			if ($(this).attr('data-toggle-text') != undefined) {
				var oldText = $(this).text();
				$(this).text($(this).attr('data-toggle-text'));
				$(this).attr({'data-toggle-text':oldText});
			}
			
			// $('html,body').animate({scrollTop: currentPosition},'slow', function() { 
			// 	App.isScrolling = false; 
			// });
				
			$container.find('.toggleBlock').slideToggle('fast');
			
			return false;
		});


	
		
		// Scroll on Impressum
		// $('#impressumlink').bind('click', function() {
		// 	App.gotoByScroll('Impressum');
		// })
	
		
		// Image Gallery transitions
		$('.thumbnails').delegate('.image','click', function(e) {
			
			//alert($(this).width() + "x" + $(this).height());
			
			$thumbnails = $(this).parent('.thumbnails');
			$gallery = $thumbnails.siblings('.gallery');
			$currentImage = $gallery.find('.current');
			
			var imageNumber = $(this).prevAll().length;
			var thumbNumber = $currentImage.prevAll().length;
			
			$pendingImage = $gallery.children()[imageNumber];
			$pendingThumb = $thumbnails.children()[thumbNumber];
			
			$currentImage.hide('slide', {direction: 'down'}, 500, function(){
				$(this).removeClass('current');
				$( $pendingImage ).addClass('current').show('slide', {direction: 'down'}, 500);
				$( $pendingThumb ).removeClass('current').css({display:"block", visibility:"visible"}).show('slide', {direction: 'up'}, 500);
			});
			$( this ).hide('slide', {direction: 'up'}, 500, function() {
				$(this).addClass('current').css({display:"block", visibility:"hidden"});
			});
		});
		

	});
	
// Navigation & History

	$(function(){ 
		// Get elements
		var $menu = $('#navigation')
					
		// Get original document title
		var document_title = document.title;
		
		var onNavigationClick = function(e) {
			e.preventDefault();

			var state = $(this).attr('data-state');
			
			if (state == $.History.state.replace("/","")) { return; }
			
			$.History.go("/" + state);
			
			
		}
		
		// Define menu update function
		// Showis our position in navigation structure and hides other branches
		var updateMenu = function(state){
		        // Update menu
				state = state.replace('/', '');
				
				
				// Close all expanded containers besides the current one
				$menu.find('li.current:not(:has(a[data-state="'+state+'"]))').find('ul').slideUp('fast', function(){$(this).parent().removeClass('expanded');});
		        $menu.find('li.current:not(:has(a[data-state="'+state+'"]))').removeClass('current');
				// Open current selection
				$menu.find('li:has(a[data-state="'+state+'"])').addClass('current').find('ul').slideDown('fast', function(){$(this).parent().addClass('expanded');});
		    };		
		
		// Bind a handler for hash/state changes
		$.History.bind(function(state){
			
				if ($.History.state == "/Projekte") { $.History.go("/Portikus"); }
				// Update the page's title with our current state on the end
				document.title = document_title + ' | ' + decodeURI(state.replace('_', ' ').replace('/', ''));
			
				// Update menu
				updateMenu(state);
				
				// Animated Transition
				$('.section.current').fadeOut('fast', function(){
					$(this).removeClass('current');
					$('#'+state.replace('/', '')).fadeIn('fast', function(){
						$(this).addClass('current');
					});
				});
				
				// Normal Transition
				// $('.section.current').removeClass('current');
				// $('#'+state.replace('/', '')).addClass('current');

		});	
		
		// Watch clicks in navigation menu
		$('#sidebar').delegate('a', 'click', onNavigationClick);		
		$('#impressumlink').bind('click', onNavigationClick);		
	});

	
})(jQuery);

















