/**
 * site.js
 * @author jjarolim, <office@jarolim.com>
 */

$(document).ready(
	function() {
		
		// Menulinks anpassen: 
		// Von Ankerlinks zu ScrollTo-Calls
		
		$('.menu a').each(
			function() {
				
				var $this = $(this);
				$(this).data('href', $this.attr('href'));
				$this.attr('href', 'javascript:;');
				
				$this.click(
					function() {
						var $this = $(this);
						$this.blur();
						$(window).scrollTo($this.data('href'), 1000);
					}
				)
					
			}
		);
			
		// Projektlinks anpassen (Sektion craftmansship)
		
		$('.craftmansship .navigation a').each(
			function() {
			
				var $this = $(this);
				var href = $this.attr('href');
				
				$this
				.data('href', href)
				.attr('href', 'javascript:;')
				.click(function() {
					showProject(href);
				});
			
			}
		);
			
		// Navigation onScroll verschieben
		
		$(window).scroll(moveMenu);
		setMenu();
			
	}
);


function showProject(id) {
	
	// Entsprechenden Navigationspunkt highlighten
	
	$('.craftmansship .navigation a').each(
		function() {
			var $this = $(this);
			$this.blur();
			if ($this.data('href') == id) {
				$this.closest('li').addClass('active');
			} else {
				$this.closest('li').removeClass('active');
			}
		}
	);
	
	// Ein- und Ausblenden
	
	$('.craftmansship .items .item:visible').fadeOut(1000);
	$('.craftmansship '+id).fadeIn(1000);
	
}


/**
 * Mappings von Scrollbereich auf Position des Menus 
 * (y1, y2) Der Bereich des Scrollbars
 * top: Die Position des Menus, falls man sich zwischen y1 und y2 befindet
 */

var currentIndex = 0;
var mappings = new Array();

mappings[0] = { y1:0,    y2:110,    top:448 };
mappings[1] = { y1:111,  y2:560,   top:448 };
mappings[2] = { y1:561,  y2:1180,  top:1230 };
mappings[3] = { y1:1181, y2:1930,  top:1680 };
mappings[4] = { y1:1931, y2:10000, top:2900 };

/**
 * Das Menu ohne Animation an die derzeit
 * aktuelle Position setzen
 */
function setMenu() {
	var $menu = $('.menu');
	var scrollTop = $(window).scrollTop();
	for (i=0,ilen=mappings.length; i<ilen; i++) {
		
		var mapping = mappings[i];
		
		if (scrollTop >= mapping.y1 && scrollTop <= mapping.y2) {
			
			// Den Index setzen
			currentIndex = i;

			if (mapping.top != $menu.offset().top) {
				$menu.css('top', mapping.top + 'px');
				$('.menu li').removeClass('active');
				$('.menu li:eq('+currentIndex+')').addClass('active');
				break;
			}

		}
		
	}
}


/**
 * Funktion schiebt Navigation immer in Sichtbereich
 */
function moveMenu() {

	var $menu = $('.menu');
	var scrollTop = $(window).scrollTop();

	for (i=0,ilen=mappings.length; i<ilen; i++) {
		
		var mapping = mappings[i];
		
		if (scrollTop >= mapping.y1 && scrollTop <= mapping.y2) {

			// Den Index setzen
			currentIndex = i;

			if (mapping.top != $menu.offset().top) {

				// Animation starten
				
				$menu
				.stop()
				.animate(
					{ 'top': mapping.top + 'px' }, 
					'slow',
					'swing',
					function() {
						$('.menu li').removeClass('active');
						$('.menu li:eq('+currentIndex+')').addClass('active');
					}
				);
					
			} else {
				
				// Der Div ist an der richtigen Position
				// Wir setzen nur das Highlight neu
				
				$('.menu li').removeClass('active');
				$('.menu li:eq('+currentIndex+')').addClass('active');
				
			}
				
			break;
				
		}
	}

}


