/* Mouse-over photos to fade the logo and contact information */
/*
var titleHiddenOpacity = 0.4;
var titleVisibleOpacity = 1.0;
var contactHiddenOpacity = 0.4;
var contactVisibleOpacity = 1.0;

function shouldHideTitle() {
	return !$("#headerTitle").hasClass('is-hovered-over') && 
		(
			$("#headerMidground").hasClass('is-hovered-over') ||
			$("#headerForeground").hasClass('is-hovered-over')
		);
}

function shouldHideContact() {
	return !$("#headerContact").hasClass('is-hovered-over') && 
		(
			$("#headerMidground").hasClass('is-hovered-over') ||
			$("#headerForeground").hasClass('is-hovered-over')
		);
}

$("#headerMidground").hover(
	function(){
		$("#headerMidground").addClass('is-hovered-over');
		if(shouldHideTitle()) {
			$("#headerTitle").css('opacity', titleHiddenOpacity);
		}
		if(shouldHideContact()) {
			$("#headerContact").css('opacity', titleHiddenOpacity);
		}
	},
	function(){
		$("#headerMidground").removeClass('is-hovered-over');
		if(!shouldHideTitle()) {
			$("#headerTitle").css('opacity', titleVisibleOpacity);
		}
		if(!shouldHideContact()) {
			$("#headerContact").css('opacity', contactVisibleOpacity);
		}
	}
);
$("#headerForeground").hover(
	function(){
		$("#headerForeground").addClass('is-hovered-over');
		if(shouldHideTitle()) {
			$("#headerTitle").css('opacity', titleHiddenOpacity);
		}
		if(shouldHideContact()) {
			$("#headerContact").css('opacity', titleHiddenOpacity);
		}
	},
	function(){
		$("#headerForeground").removeClass('is-hovered-over');
		if(!shouldHideTitle()) {
			$("#headerTitle").css('opacity', titleVisibleOpacity);
		}
		if(!shouldHideContact()) {
			$("#headerContact").css('opacity', contactVisibleOpacity);
		}
	}
);
$("#headerContact").hover(
	function(){
		$("#headerContact").addClass('is-hovered-over');
		$("#headerTitle").css('opacity', titleHiddenOpacity);
		$("#headerContact").css('opacity', contactVisibleOpacity);
	},
	function(){
		$("#headerContact").removeClass('is-hovered-over');
		$("#headerTitle").css('opacity', titleHiddenOpacity);
		$("#headerContact").css('opacity', contactHiddenOpacity);
	}
);
$("#headerTitle").hover(
	function(){
		$("#headerTitle").addClass('is-hovered-over');
		$("#headerTitle").css('opacity', titleVisibleOpacity);
		$("#headerContact").css('opacity', contactHiddenOpacity);
	},
	function(){
		$("#headerTitle").removeClass('is-hovered-over');
		$("#headerTitle").css('opacity', titleHiddenOpacity);
		$("#headerContact").css('opacity', contactHiddenOpacity);
	}
);
*/
/* Photo animation */
var photos = new Array();
photos[0] = new Array();
photos[0]['imgSrc'] = root+'images/header/headerphoto0.png';
photos[0]['leftStart'] = '-50px';
photos[0]['leftEnd'] = '0px';
photos[0]['topStart'] = '-100px';
photos[0]['topEnd'] = '-50px';
photos[0]['duration'] = 4000;

photos[1] = new Array();
photos[1]['imgSrc'] = root+'images/header/headerphoto5.png';
photos[1]['leftStart'] = '360px';
photos[1]['leftEnd'] = '250px';
photos[1]['topStart'] = '-50px';
photos[1]['topEnd'] = '0px';
photos[1]['duration'] = 4000;

photos[4] = new Array();
photos[4]['imgSrc'] = root+'images/header/headerphoto4.png';
photos[4]['leftStart'] = '390px';
photos[4]['leftEnd'] = '430px';
photos[4]['topStart'] = '-70px';
photos[4]['topEnd'] = '-5px';
photos[4]['duration'] = 4000;

photos[2] = new Array();
photos[2]['imgSrc'] = root+'images/header/headerphoto2.png';
photos[2]['leftStart'] = '560px';
photos[2]['leftEnd'] = '610px';
photos[2]['topStart'] = '-110px';
photos[2]['topEnd'] = '-10px';
photos[2]['duration'] = 4000;

photos[3] = new Array();
photos[3]['imgSrc'] = root+'images/header/headerphoto3.png';
photos[3]['leftStart'] = '728px';
photos[3]['leftEnd'] = '720px';
photos[3]['topStart'] = '-70px';
photos[3]['topEnd'] = '-5px';
photos[3]['duration'] = 4000;

var photoCount = photos.length;
var photoPause = 0;

function startPhotoAnimation(photo) {
	//Brings photo to front before animating
	addPhotoToHeader(photo);
	
	$("#headerFloatingPhoto"+photo).css('opacity', 0);
	$("#headerFloatingPhoto"+photo).css('left', photos[photo]['leftStart']);
	$("#headerFloatingPhoto"+photo).css('top', photos[photo]['topStart']);
	$("#headerFloatingPhoto"+photo).animate({
		opacity: 1.0,
		left: photos[photo]['leftEnd'],
		top: photos[photo]['topEnd']
	}, photos[photo]['duration'], function () {
		//Set full opacity and final position after animation period
		$("#headerFloatingPhoto"+photo).css('opacity', 1.0);
		$("#headerFloatingPhoto"+photo).css('left', photos[photo]['leftEnd']);
		$("#headerFloatingPhoto"+photo).css('top', photos[photo]['topEnd']);
	});
}

function schedulePhotoAnimation(photo, start, period, repetitions) {
	if(repetitions > 0) {
		if(start == 0) {
			setTimeout("startPhotoAnimation("+photo+")", 0);
		}
		else {
			setTimeout("startPhotoAnimation("+photo+")", start);
		}
		setTimeout("schedulePhotoAnimation("+photo+",0,"+period+","+(repetitions-1)+")", start+period);
	}
}

function addPhotoToHeader(photo) {
	//Remove existing photo if it exists
	$("#headerFloatingPhoto"+photo).remove();
	
	//Add photo as top layer to header
	$("#headerMidground").html($("#headerMidground").html() +
		'<div id="headerFloatingPhoto'+photo+'" class="floatingPhoto" style="left:'+photos[photo]['leftEnd']+'; top:'+photos[photo]['topEnd']+';">\
			<img src="'+photos[photo]['imgSrc']+'" />\
		</div>');
}

/* Start up */
$(document).ready(function() {
	//Randomise photo order
	var photoList = new Array();
	var i=0;
	for (i=0; i<photoCount; i=i+1) {
		photoList[i] = i;
	}	
	var photoOrder = new Array();
	for (i=0; i<photoCount; i=i+1) {
		rand = Math.floor(Math.random()*(photoList.length));
		photoOrder[i] = photoList.splice(rand, 1);
	}
	
	//Insert initial floating photos and schedule animations
	for (i=0; i<photoCount; i=i+1) {
		addPhotoToHeader(photoOrder[i]);
		schedulePhotoAnimation(photoOrder[i], i*4000 , photoCount*4000, 2);
	}
});

/* Popups */
var maxPopupWidth = 300;
var maxPopupHeight = 400;

function doResize(){
	if($("#popup") && $("#popup").is(":visible")) {
	    showPopup(maxPopupWidth, maxPopupHeight);
	}
}
window.onresize = doResize;

function closePopup() {
	$("#popupContainer").hide();
}

function showPopup(maxWidth, maxHeight) {
	maxPopupWidth = maxWidth;
	maxPopupHeight = maxHeight;
	
	var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    
	var popupWidth = Math.min(maxPopupWidth, (windowWidth - 20));
	var popupHeight = Math.min(maxPopupHeight, (windowHeight - 20));
	
	$("#popup").width(popupWidth);
	$("#popup").height(popupHeight);
	$("#popupBody").height(popupHeight); //temporarily make it slightly higher, until it's drawn
	
	$("#popup").css({
		"top"      : windowHeight/2 - popupHeight/2,
		"left"     : windowWidth/2 - popupWidth/2
    });
	
	$("#popupContainer").show();
	$("#popupBody").height(popupHeight-$("#popupHeader").innerHeight());
}

