$(document).ready(function() {
	
	/* Background Images */
	bgImage = $('#bgImage');			
	bgImageFade = $('#bgImageFade');
	

	/* General - Main Logo Fade */
	$('#mbLogo').hover(function() {
      	$(this).stop().animate({opacity: '0'}, '100');
    },
    function() {
      	$(this).stop().animate({opacity: '1'}, '100');
    });
	
	
	/* Animation - Navigation Boxes */
	$('.navigation > li').hover(function() {
		if(!($(this).children().hasClass('activeLink'))){
			moveRight($(this).children());
		}
   	},
    function() {
		if(!($(this).children().hasClass('activeLink'))){
			moveLeft($(this).children());
		}
    });
	
	function moveLeft(element) {
		element.stop().animate({'margin-left': '0'}, '500', function() {
			element.css({'color': '#000'});																	 
		});	

	}
	
	/* 10px*/
	function moveRight(element) {
		element.stop().animate({'margin-left': '7px'}, '500', function() {
			element.css({'color': '#0473c4'});																	 
		});		
	}	
	
	
	/* Animation - Info Box (Project Pages)*/
	var showInfoBox = false;
	
	$('.showInfoBox').click(function(){	// Link in Navigation Box
		if (!showInfoBox) {
			$('#infoBox').removeClass('hidden').addClass('active');
			$('#closeInfoBox').removeClass('hidden').addClass('active');
			$(this).children().addClass('activeLink');
		}
		else {
			$('#infoBox').removeClass('active').addClass('hidden');
			$('#closeInfoBox').removeClass('active').addClass('hidden');
			$(this).children().removeClass('activeLink');	
			moveLeft($(this).children());
		}
		showInfoBox = !showInfoBox;
	});
	
	$('#closeInfoBox').click(function(){	// Button - top right of the infoBox
		if (showInfoBox) {
			$('#infoBox').removeClass('active').addClass('hidden');
			
			if($('.showInfoBox')) {
				$('.showInfoBox').children().removeClass('activeLink');
				$('#closeInfoBox').removeClass('active').addClass('hidden');
				moveLeft($('.showInfoBox').children());
			}
			showInfoBox = !showInfoBox;
		}
	});
	
	/* Animation - Box (Biographies)*/
	
	var contentBoxToogle = true;
	$('#minimizeBox').click(function(){
		if(contentBoxToogle) {
			$('#minimizeBox').removeClass('minBox').addClass('maxBox');
			$('#contentBox .content').hide();
			$('#contentBox').animate({'height' : '60px', 'margin-top' : '460px'} , '300');
			contentBoxToogle = false;
		}
		else {
			$('#minimizeBox').removeClass('maxBox').addClass('minBox');
		  	$('#contentBox').animate({'height' : '500px', 'margin-top' : '20px'} , '300', function(){
		   	$('#contentBox .content').show();
		   
	   });
			contentBoxToogle = true;			
		}
	});	
	
	
	
	/* Gallery - Projects Page - Extracting Projectname From Image Alt Tag*/
	var carousel = $('#projects');				// Carousel Div
	var carouselImages = $('#projects img');	// Images
	var textOutput = $('#projectTitle'); 		// Output
	var activeImage = null;
	var animationRunning = false;
	var animationSpeed = '500';

	function setTitleText() {
		textOutput.hide();
		if (activeImage) {
			textOutput.text(activeImage.attr('alt'));
		}
		else textOutput.text(' ');
		textOutput.fadeIn(animationSpeed);
	}	

	carouselImages.hover(function() {
		activeImage = $(this);
		if (!animationRunning) setTitleText();
    },
    function() {
		activeImage = null;
		if(!animationRunning) {
			animationRunning = true;
			textOutput.fadeOut(animationSpeed,function() {
				animationRunning = false;
				setTitleText();
			});
		}
    });
	
	carousel.mouseout(function(){
		activeImage = null;
		textOutput.fadeOut(animationSpeed);
	 });


	/* Gallery - Project Detail Page - Changing Background Image*/
	var projectImages = $('#projectImages img');
	var thumbnailFolder = "/thumbs";
	var activeProjectImage = 1;
	
	projectImages.click(function(){
		var imagePath = $(this).attr('src');
		var imageNr;
		var indexThumbs = imagePath.indexOf(thumbnailFolder);
		imagePath = imagePath.substring(0,indexThumbs) + imagePath.substring(indexThumbs+thumbnailFolder.length);
		imageNr = imagePath.charAt(imagePath.length-5);
		
		if(imageNr != activeProjectImage) {
			fadeToBgImage(imagePath);
			activeProjectImage = imageNr;
		}
		
		projectImages.removeClass('activeImage');
		$(this).addClass('activeImage');
	});



});


/* Random Background */
var imageCount = 127; // Bilderanzahl im Ordner images/random/

function randomImage() {
	currentImage = Math.floor(Math.random() * (imageCount - 1)) + 1;
	setBackground('images/random/'+currentImage+'.jpg');
}

/* Background Functions */
var bgImage;
var bgImageFade;
var imageToogle = false;

function setBackground(imagePath) {
	bgImage.attr('src',imagePath);
}

var imageToggle = false;
function fadeToBgImage(imagePath) {
	(imageToogle ? bgImage : bgImageFade).attr('src',imagePath).animate( { opacity: 1.0 }, 1500);
	(imageToogle ? bgImageFade : bgImage).animate( { opacity: 0.0 }, 1500);
	imageToogle = !imageToogle;
}

