(function($){
	$.fn.scrolllogo = function() {
	
		return this.each(function() {
		var element = $(this);
		var toScroll = 0;
			
			$(window).scroll(function(){ 
				var scroll = $(window).scrollTop();
				toScroll = scroll*(-1);

				if(toScroll < 40){
					element.css('margin-top', '40' + "px" );
					element.css('position', 'fixed' );	
				} else {
					element.css("margin-top", "40px");
					element.css('position', 'absolute' );
				}

			});
			
		});
	}
})(jQuery)

$(document).ready(function() {
	
	var	direction = 0;
		
	// Backwards navigation
	$("#back").click(function() {
		stopAnimation();
		direction = 2;
		navigate("back");
	});
	
	// Forward navigation
	$("#next").click(function() {
		stopAnimation();
		direction = 1;
		navigate("next");
	});
	
	var interval;
	$("#control").toggle(function(){
		stopAnimation();
	}, function() {
		
		// Show the next image
		navigate("next");
		
		// Start playing the animation
		interval = setInterval(function() {
			navigate("next");
		}, slideshowSpeed);
	});
	
	
	var activeContainer = 1;	
	var currentImg = 0;
	var currentTitle = 0;
	var currentCapt = 0;
	var currentLink = 0;
	var animating = false;
	var navigate = function(direction) {
		// Check if no animation is running. If it is, prevent the action
		if(animating) {
			return;
		}
		
		// Check which current image we need to show
		if(direction == "next") {
			currentImg++;
			if(currentImg == photos.length + 1) {
				currentImg = 1;
				currentTitle = 1;
				currentCapt = 1;
				currentLink = 1;
			}
		} else {
			currentImg--;
			if(currentImg == 0) {
				currentImg = photos.length;
			}
		}
		
		// Check which container we need to use
		var currentContainer = activeContainer;
		if(activeContainer == 1) {
			activeContainer = 2;
		} else {
			activeContainer = 1;
		}
		
		showImage(photos[currentImg - 1], currentContainer, activeContainer);
		
	};
	
	var currentZindex = -1;
	var showImage = function(photoObject, currentContainer, activeContainer) {
		animating = true;
		
		// Make sure the new container is always on the background
		currentZindex--;
		
		// Set the background image of the new active container
		$("#headerimg" + activeContainer).css({
			"background-image" : "url(" + photoObject.image + ")",
			"display" : "block",
			"z-index" : currentZindex
		});
		$("#slideshowtxt" + activeContainer).css({
			"z-index" : currentZindex +1
		});
			
			// Set the new header text
			if((direction == 0) || (currentImg == 1)){
				$("#firstline" + activeContainer).html(introcontent);
				$("#slide-title" + activeContainer).html('');
			} else {
				$("#firstline" + activeContainer).html(photoObject.firstline);
				$("#slide-title" + activeContainer)
					.html(photoObject.title);
			}
			
			
			
			// Fade out the current container
			// and display the header text when animation is complete
			if(direction == 0){
				animating = false;
				
			} else if(direction == 1){
				$("#slideshowtxt2").css({"display" : "block"});
				$("#slideshowtxt" + activeContainer).animate({left: '100%'}, 0, "linear");
				$("#headerimg" + activeContainer).animate({left: '100%'}, 0, "linear", function() {
				$("#headerimg" + activeContainer).animate({left: '0%'}, 450, "easeInOutCubic");
				$("#headerimg" + currentContainer).animate({left: '-=100%'}, 500, "easeInOutCubic");
				$("#slideshowtxt" + activeContainer).animate({left: '0%'}, 550, "easeInOutCubic");
				$("#slideshowtxt" + currentContainer).animate({left: '-=100%'}, 600, "easeInOutCubic");
				animating = false;
			});
			
			} else {
				$("#slideshowtxt2").css({"display" : "block"});
				$("#slideshowtxt" + activeContainer).animate({left: '-100%'}, 0, "linear");
				$("#headerimg" + activeContainer).animate({left: '-100%'}, 0, "linear", function() {
				$("#headerimg" + activeContainer).animate({left: '0%'}, 450, "easeInOutCubic");
				$("#headerimg" + currentContainer).animate({left: '100%'}, 500, "easeInOutCubic");
				$("#slideshowtxt" + activeContainer).animate({left: '0%'}, 550, "easeInOutCubic");
				$("#slideshowtxt" + currentContainer).animate({left: '100%'}, 600, "easeInOutCubic");
				animating = false;
				
			});
			
			}
		};
	
	var stopAnimation = function() {
		clearInterval(interval);
	};
	
	// We should statically set the first image
	navigate("next");
	
	// Start playing the animation
	interval = setInterval(function() {
		navigate("next");
	}, slideshowSpeed);
  $('#big_logo').scrolllogo();
	
});
