//$(document).ready(function() {
$(window).load(function() {	
	
	// -----------------------------------------------------------
	// Projektnav
	// -----------------------------------------------------------	
	
	// Sticky nav
	// -----------------------------------------------------------	
	adminbar = 0; // Height of admin bar when logged out
	if($("body").hasClass("admin-bar")) {
		adminbar = 28; // Height of admin bar when logged in
	}
	navbar = $( "#project-navigation .header-stick" );
	navPos = 171;
	
	// Scroll event
	$(window).scroll(function() {
		var scrollTop = parseInt($(window).scrollTop());
		var newNavPos = 0;
		newNavPos = navPos-adminbar;
		if( scrollTop > navPos ) {
			navbar.css({
				position: "fixed",
				top: "0px"
			});
		} else if( navPos > scrollTop ) {
			navbar.css({
				position: "absolute"
			});
		}
	});	
	

	// Flexible width
	// ----------------------------------------------------------
	/*
	// Settings
	minWidth = 980;
	maxWidth = 1180;
	padding = 15*2;
	
	// Flex on page-load
	flex();
	
	// Flex on resize-event
	$(window).resize(function() {
		flex();
	});
	
	function flex() {
		var windowWidth = parseInt($("body").width())-padding;
		if( windowWidth < maxWidth && windowWidth > minWidth) {
			var newWidth = windowWidth;
			navbar.width(newWidth);
		} else if ( windowWidth > maxWidth ) {
			navbar.width(maxWidth);
		} else if ( windowWidth < maxWidth ) {
			navbar.width(minWidth);
		}
	}*/
	
	// Jump-to buttons
	// ----------------------------------------------------------
	function scroll(direction) {		
		var scroll, i, positions = [], here = $(window).scrollTop(), collection = $('.project.box'), duration = 750, easing = "easeOutExpo";
		
		collection.each(function() {
			positions.push(parseInt($(this).offset()['top'], 10));
		});
		
		for( i = 0; i < positions.length; i++ ) {
			if( direction == 'next' && positions[i] > here ) { 
				scroll = collection.get(i);
				break;
			}
			if( direction == 'prev' && i > 0 && positions[i] >= here ) { 
				scroll = collection.get(i-1); 
				break;
			}
		}
		
		if(scroll) {
			$.scrollTo( scroll, { duration: duration, easing: easing });
        }
		
		return false;
	}

	$("a#prev, a#next").click(function() {
		return scroll($(this).attr( 'id' ));
	});
	
	// Ajax load categories
	// -----------------------------------------------------------
	$(".project-categories a#choosen").parent("li").addClass("current-cat");
	
	// Click handler
	$(".project-categories a").click(function() {	
		
		// Settings
		var speed = 500;
		var easing = 'easeOutExpo';
		var cat = $(this).attr("id");
		//var uri = $(this).attr("href");
		
		$('.project.box').fadeOut(speed, easing, function() {
		
			// Show loading animation
			$('.projects').addClass("loading");
			
			$(this).remove();
		});
		$('.project-categories li.current-cat').removeClass('current-cat');
		$(this).parent('li').addClass('current-cat');
		
		// Ajax call
		$.get('/category-ajax/?category='+cat, function(data) {
		
			// Remove loading animation
			$('.projects').removeClass("loading");
			
			// Append content
			$('.projects').html(data);
			
			// Fade in
			var i = $('.projects .project.box').length;
			$('.projects .project.box').fadeIn(speed, "easeInQuad", function() {
				// Slider
				if ( i-- < 2 ) {
					slider();
				}
			});
			
		});
		return false;
	});	
	
	// --------------------------------------------------------------
	function slider() {
		$('div.slider').orbit({
			timer: false,
			animationSpeed: 800, 
			captions: true,
		});
		
		$("ul.involvement").each(function(){
			$(this).children().each(function(i) {
				$(this).addClass("item-" + (i+1));
			});
		});
	
		$(".read-more").hide();
		$('.more-link').click(function(){
			$(this).closest(".right-column").find(".read-more").slideToggle("slow");  		
			$(this).toggleClass("active"); 
		});
			
		$('.slider').each(function(){
			$(this).css("width","100%");
			$(this).children("img").css("width","100%");
			$(this).css("height","820px");
			$(this).css("width","100%");
		});
	}

	return slider();
});
