// Navigation menu highlighting
var highlightedMenu, highlightBlocked;
function openNav0() {
	if (currentPage!='_') return;
	$('nav0Div').addClass('highlightedmenu');
	$(window).addEvent('afterPartialPageLoad', closeNav0);
}

function closeNav0(state) {
	$('nav0Div').removeClass('highlightedmenu');
}


// Initial crossfade animation
var tweenOptions={ duration: 1000}; // transitions last 200ms
var tweenPause=1000; // ms

/** Start the initial crossfade animation.
 * For landing on the homepage, do the full animation.
 * Otherwise (and also when partial-page-load URLs complicate the matter)
 * do a simple fade-in (i.e. the last step of the animation). */
function startAnimation() {
   if (window.hidePageContents) {
		if ($('animation1'))	$('animation1').setStyle('display', 'none');
		$('animation3').setStyle('visibility', 'visible');
		// wait until PPL happens before starting animation
		window.addEvent('partialPageLoad', animationStep3);
	} else if (!$('animation1')) {
		$('animation3').setStyle('visibility', 'visible');
		// wait until images are loaded before starting animation
		window.addEvent('load', animationStep3);
	} else {
		// wait until images are loaded before starting animation
		window.addEvent('load', animationStep1);
	}
}
/** Animation step 1 */
function animationStep1() {
	window.removeEvent('load', animationStep1);
  (function() {
    $('animation2').setStyle('visibility', 'visible');
    $('animation1').get('tween', tweenOptions).start('opacity', 1, 0)
		.addEvent('complete', animationStep2);
  }).delay(tweenPause);
}
/** Animation step 2 */
function animationStep2() {
	if ($('animation1')) $('animation1').setStyle('display', 'none');
  (function() {
    $('animation3').setStyle('visibility', 'visible');
    $('animation2').get('tween', tweenOptions).start('opacity', 1, 0)
	    .addEvent('complete', animationStep3);
  }).delay(tweenPause);
}
/** Animation step 3 */
function animationStep3() {
	window.removeEvent('partialPageLoad', animationStep3);
	window.removeEvent('load', animationStep3);
	if ($('animation2')) $('animation2').setStyle('display', 'none');
  (function() {
    $('animation4').setStyle('visibility', 'visible');
	 updateVerticalLineSize();
    $('animation3').get('tween', tweenOptions).start('opacity', 1, 0)
	    .addEvent('complete', animationStep4);
  }).delay(tweenPause);
}
/** Animation step 4 (cleanup) */
function animationStep4() {
    $('animation3').setStyle('display', 'none');
	 updateVerticalLineSize();
}
/** Set background image for current pageLevel */
var currentBackground='largelogo';
function setBackgroundImage(newBackground) {
	if (newBackground==currentBackground) return;
	currentBackground=newBackground;
	doWhenDomReady(function() {
		$('animation3').set('class', currentBackground);
		$('animation4').set('class', currentBackground);
	});
}

/** Open or close an image by resizing it and hiding/showing parts
 * Invoked from project_show */
function resizeMyImage(id, state, margin, startpoint, thumbheight, largeheight) {
	if(state=="open"){
		$(id+'_div1').style.display = "none";
		$(id+'_div2').style.display = "block";
		$(id+'_maindiv').style.zIndex = 96;
		var myEffect = new Fx.Morph(id+'_image', {
			duration: '500',
			transition: Fx.Transitions.Sine.easeIn,
			onComplete: function() {
				$(id+'_caption').style.display = "";
			}
		});
		myEffect.start({
			'width': [91, 766],   //Morphs the 'width' style from 91px to 750px.
			'height': [thumbheight, largeheight], //Morphs the 'height' style from thumbheight to largeheight.
			'margin-top': [margin,startpoint]  //Morphs the 'margin-top' from 0px to 56px.
		});
	} else {
		$(id+'_caption').style.display = "none";
		var myEffect = new Fx.Morph(id+'_image', {
			duration: '500',
			transition: Fx.Transitions.Sine.easeIn,
			onComplete: function() {
				$(id+'_div1').style.display = "block";
				$(id+'_div2').style.display = "none";
				$(id+'_maindiv').style.zIndex = 95;
			}
		});
		myEffect.start({
			'width': [766, 91],   //Morphs the 'width' style from 750px to 91px.
			'height': [largeheight, thumbheight], //Morphs the 'height' style from largeheight to thumbheight.
			'margin-top': [startpoint, margin] //Morphs the 'margin-top' from 56px to 0px.
		});
	}
}

/** Open or close a video by resizing it and hiding/showing parts
 * Invoked from project_show */
function resizeMyVideo(id, state, margin, startpoint) {
	if(state=="open"){
		$(id+'_maindiv').style.zIndex = 96;
		$(id+'_div1').style.display = "none";
		$(id+'_div2').style.display = "block";
		//$(id+'_div3').style.display = "none";
		var myEffect = new Fx.Morph('thumbnail'+id, {
			duration: '500',
			transition: Fx.Transitions.Sine.easeIn,
			onComplete: function(){
				$(id+'_div1').style.display = "none";
				$(id+'_div2').style.display = "none";
				$(id+'_div3').style.display = "block";
			}
		});
		myEffect.start({
			'width': [91, 766],   //Morphs the 'width' style from 91px to 750px.
			'height': [68, 450], //Morphs the 'height' style from thumbheight to largeheight.
			'margin-top': [margin,startpoint]  //Morphs the 'margin-top' from 0px to 56px.
		});
	} else {
		//$(id+'_div1').style.display = "none";
		$(id+'_div2').style.display = "block";
		$(id+'_div3').style.display = "none";
		$(id+'_maindiv').style.zIndex = 95;
		var myEffect = new Fx.Morph('thumbnail'+id, {
			duration: '500',
			transition: Fx.Transitions.Sine.easeIn,
			onComplete: function(){
				$(id+'_div1').style.display = "block";
				$(id+'_div2').style.display = "none";
				$(id+'_div3').style.display = "none";
			}
		});
		myEffect.start({
			'width': [766, 91],   //Morphs the 'width' style from 750px to 91px.
			'height': [450, 68], //Morphs the 'height' style from largeheight to thumbheight.
			'margin-top': [startpoint, margin] //Morphs the 'margin-top' from 56px to 0px.
		});
	}
}

/** Open or close a map by resizing it and hiding/showing parts
 * Invoked from project_show_map */
function resizeMyMap(id, state) {
	if(state=="open"){
		$(id+'_div1').style.display = "none";
		$(id+'_div2').style.display = "block";
		var myEffect = new Fx.Morph(id+'_image', {
			duration: '500',
			transition: Fx.Transitions.Sine.easeIn,
			onComplete: function(){
				$(id+'_dots').style.display = "";
				$(id+'_caption').style.display = "";
			}
		});
		myEffect.start({
			'width': [53, 438],  //Morphs the 'width' style from 53px to 438px.
			'height': [70, 579] //Morphs the 'height' style from 70px to 579px.
		});
		// on click-outside-map close map again
		$(document.body).addEvent('click', function(e) {
			var srcElement=e.target || e.srcElement;
			if (!srcElement.getParent('[id^='+id+'_div]')) {
				resizeMyMap(id, 'close');
				$(document.body).removeEvent('click', arguments.callee);
			} else if (!($(id+'_div1'))
				|| $(id+'_div1').getStyle('display')=='block') {
				// close superfluous on-click handler
				$(document.body).removeEvent('click', arguments.callee);
			}
		});
	} else {
		$(id+'_dots').style.display = "none";
		$(id+'_caption').style.display = "none";
		var myEffect = new Fx.Morph(id+'_image', {
			duration: '500',
			transition: Fx.Transitions.Sine.easeIn,
			onComplete: function(){
				$(id+'_div1').style.display = "block";
				$(id+'_div2').style.display = "none";
			}
		});
		myEffect.start({
			'width': [438, 53],  //Morphs the 'width' style from 53px to 438px.
			'height': [579, 70] //Morphs the 'height' style from 70px to 579px.
		});
	}
}

Request.PartialPageLoad.spinOpacity=0.3;

function openIssueViewer(href) {
	window.open(href, 'issuu',
		 'width=420,height=254,dependent=yes,directories=no,location=no,'
       + 'menubar=no,resizable=no,scrollbars=no,status=no,personalbar=no'
       + ',toolbar=0');
	return false;
}

function updateVerticalLineSize() {
	var els=$$('div.contentsframe');
	var els2, el;
	for(var i=1; i<=2; i++) {
		DHTMLSelectables['navselect'+i].each(function(ids, val) {
			ids.each(function(id) {
				if (!deselectedAttributes.has(id)) return;
				el=$(id);
				if (el) el=el.getElement('div.dropovermenu');
				if (el) els.push(el);
			})
		});
	}
	var maxSize=673;
	els.each(function(el) {
		var s=el.getElement('table').getDimensions();
		if (s && s.height && s.height>maxSize) maxSize=s.height;
	});
	els.each(function(el) {
		el.setStyle('height', maxSize+'px');
		var el2=el.getParent().getElement('div.dropovermask');
		if (el2) el2.setStyle('height', maxSize+'px');
	});
}

/** Clicking on a menu mask will return you to a page higher up in the page navigation tree, and move you so that the clicked element is visible */
function processMaskClick(event, pageLevel, page, el) {
	event=new Event(event);
	var baseY=$(el).getParent().getPosition().y;
	var itemY=event.page.y-baseY;
	var res=fakePartialPageLoad(pageLevel, page, el);
	// The item we were hovering above has shifted downwards, as the entire
	// menu has increased in size. We estimate the amount of shift, and scroll
	// downwards by the same amount.
	var itemYShift=itemY*1.46;
	var w=$(window);
	var c=w.getScroll();
	w.scrollTo(c.x, c.y+itemYShift);
	return res;
}

/** For efficiency, the following function fakes the page-load and ensuing side-effects, for a page that is assumed to be an ancestor of the current page, without any content */
function fakePartialPageLoad(pageLevel, page, el) {
	window.historyManager.resetState();
	currentPage=page;
	fullPageRelUrl='index.php?page='+page;
	fullPageRelUrlLongId=window.historyManager.getURIRelativeToRoot(el.href);
	itemTitle=el.title;
	document.title=document.title.replace(/:.*/, ': '+itemTitle);
	doTrackPageView();
//	selectButtonsByValue('navselect'+pageLevel, page);
	for(var i=parseInt(pageLevel)+1; i<=4; i++) {
		selectButtonsByValue('navselect'+i, []);
	}
	selectButtonsByValue('navselectproject', []);
	selectButtonsByValue('highlightedmenu', page);
	$$('div.contentsframe').dispose();
	window.historyManager.update(el.href);
	updateVerticalLineSize();
	return false;
}

/** Level-3 pagemenu's have a mouseover effect to increase display width */
var semihighlightingInhibited=false;
var semihighlightedMenu=null;
function semihighlightMenu(el, state) {
	if (!semihighlightingInhibited && !el.hasClass('highlightedmenu')
		 && el.getElement('div.dropovermenu2')) {
		if (typeof(state)=='undefined') state=true;
		if (state) {
			el.addClass('semihighlightedmenu');
			semihighlightedMenu=el;
		} else {
			el.removeClass('semihighlightedmenu');
			semihighlightedMenu=null;
		}
	}
}
function inhibitSemihighlight() {
	if (!semihighlightingInhibited) {
		semihighlightingInhibited=true;
		if (semihighlightedMenu) semihighlightedMenu.removeClass('semihighlightedmenu');
		semihighlightedMenu=null;
		(function() { semihighlightingInhibited=false; }).delay(1000);
	}
}

