/*******************************************************************************
 * 
 * Last update: 2010-01-28 Created by: Tessa Bakker Website:
 * http://www.tessabakker.nl
 * 
 ******************************************************************************/

// globals
var timelineWidth = 0;
var timelineWindowWidth = 0;
var tempWidth = 600;
var project = null;

/**
 * Show project
 * 
 * @param projectObject
 *            (htmlObject) Current selected project anchor
 * @param boolPopup
 *            (boolean) Popup the  box with the project
 * @return void
 */
function LoadProject(projectObject, boolPopup) {

	// document.getElementById('main').style.display = 'none';

	// setup default settings voor popup
	if (boolPopup == null)
		boolPopup = true;

	window.project = $(projectObject);

	// enable or disable arrows
	if (window.project.next() == null)
		$('next_project').hide();
	else
		$('next_project').show();

	if (window.project.previous() == null)
		$('prev_project').hide();
	else
		$('prev_project').show();

	// get the length of the timeline
	window.timelineWindowWidth = $('timeline').offsetWidth;

	var scroll = 0;

	if (boolPopup == true) {
		// calculate position on the side of the information box
		var scroll = (window.timelineWindowWidth / 4 - projectObject.childNodes[0].offsetWidth / 2) + 50;
	}

	// calculate where to scroll to
	var scroll = projectObject.childNodes[0].offsetLeft - scroll;

	if (scroll > window.timelineWidth - window.timelineWindowWidth
			&& boolPopup == true) {
		$('timeline_content').setStyle( {
			'width' : (window.timelineWidth + window.tempWidth) + 'px'
		});
	}

	$('timeline').scrollLeft = scroll;

	if (boolPopup == true) {
		new Ajax.Updater('info_content', projectObject.getAttribute('href'), {
			onComplete : function() {
				$('info_content').scrollTop = 0;
			}
		});
		$('information').style.display = 'block';
	}
}

window.RestoreScroll = function() {
	if ($('timeline_content').offsetWidth > window.timelineWidth) {
		$('timeline_content').setStyle( {
			'width' : window.timelineWidth + 'px'
		});
	}
};

window.onload = function() {

	// disable page scroll
	document.documentElement.style.overflow = 'hidden';
	document.body.scroll = 'no';

	// get all project links
	var links = $$('#timeline a');

	// goto latest project
	window.timelineWidth = $('timeline_content').offsetWidth;

	// change project links
	for ( var i = 0; i < links.length; i++) {

		links[i].onclick = function(e) {

			LoadProject(this);

			return false;
		};

		if (i == links.length - 1) {
			LoadProject(links[i], false);
		}

	}

	// get all non project links
	links = $$('#header a');
	
	// change non project links
	for ( var i = 0; i < links.length; i++) {
		links[i].onclick = function() {
			new Ajax.Updater('info_content', this.getAttribute('href'));
			$('information').style.display = 'block';
			return false;
		};
	}

	// close button for the  box
	var close = $$('a.close');
	close[0].onclick = function() {
		window.RestoreScroll();
		$('information').style.display = 'none';
		return false;
	};

	// open previous project
	$('prev_project').onclick = function() {
		var prev = window.project.previous();
		if (prev != null) {
			if ($('information').getStyle('display') == 'none') {
				LoadProject($(prev), false);
			} else {
				LoadProject($(prev), true);
			}
		}
		return false;
	};

	// open next project
	$('next_project').onclick = function() {
		var next = window.project.next();
		if (next != null) {
			if ($('information').getStyle('display') == 'none') {
				LoadProject($(next), false);
			} else {
				LoadProject($(next), true);
			}
		}
		return false;
	};
};