function initSlideshows() {

	var slideshowDivs = $$('div.slideshow');

	if (slideshowDivs.length > 0) {

		slideshowDivs.each(function(slideshowDiv, index) {
			slideshowDiv.id = 'slideshow_' + index;

			var slideshowData = new Array();
			var slideshowPhotos = slideshowDiv.getElements('img');
			slideshowPhotos.each(function(slideshowPhoto) {
				slideshowData.push(slideshowPhoto.src);
			});

			// empty slideshow div
			slideshowDiv.empty();

			// create slideshow
			var slideshow = new Slideshow('slideshow_' + index, slideshowData, {
				loader:     false,
				delay:      3500,
				controller: true,
				captions:   true,
				hu:         ''
			});
		});
	}
}

function initAccordions() {

	var accordionDivs = $$('div.accordion');

	if (accordionDivs.length > 0) {

		accordionDivs.each(function(accordionDiv) {

			currentItem = 0;

			var items = accordionDiv.getElements('div.item');

			items.each(function(item, index) {
				var h3 = item.getElement('h3');

				var slideContainer = new Element('div', {
					'id': 'slide_container_' + index,
					'class': 'slide_container'
				});
				var nextElements = h3.getAllNext('*');

				slideContainer.adopt(nextElements);
				slideContainer.inject(h3, 'after');

				h3.addEvent('mouseenter', function() {
					this.fireEvent('click');
				});

				if (h3.getElement('a').href == window.location.href) {
					currentItem = index;
				};
			});

			var accordion = new Accordion(accordionDiv.getElements('div.item h3'), accordionDiv.getElements('div.item div.slide_container'), {
				duration: 'short',
				onActive: function(toggler, element) {
					toggler.addClass('current');
				},
				onBackground: function(toggler, element) {
					toggler.removeClass('current');
				},
				wait: true,
				display: currentItem
			});
		});
	}

	// content accordion - TODO: implement multiple content accordions
	var contentAccordion = $$('div#cnr_ctt_main div.content.accordion')[0];
	if (contentAccordion) {

		var szNormal = 75, szSmall = 50, szFull = 200;

		var contentAccordionThumbnails = $$('div#cnr_ctt_main div.content.accordion li');
		var fx = new Fx.Elements(contentAccordionThumbnails, { wait: false, duration: 300, transition: Fx.Transitions.Back.easeOut });
		contentAccordionThumbnails.each(function(contentAccordionThumbnail, i) {
			contentAccordionThumbnail.addEvent('mouseenter', function(event) {
				var o = {};
				o[i] = { width: [ contentAccordionThumbnail.getStyle('width').toInt(), szFull ] };
				contentAccordionThumbnails.each(function(contentAccordionOtherThumbnail, j) {
					if(i != j) {
						var w = contentAccordionOtherThumbnail.getStyle('width').toInt();
						if(w != szSmall) o[j] = { width: [ w, szSmall ] };
					}
				});
				fx.start(o);
			});
		});

		contentAccordion.addEvent('mouseleave', function(event) {
			var o = {};
			contentAccordionThumbnails.each(function(contentAccordionThumbnail, i) {
				o[i] = { width: [ contentAccordionThumbnail.getStyle("width").toInt(), szNormal ] };
			});
			fx.start(o);
		});
	}
}

function initQuotes() {

	var quotes = $$('div.quote');

	if (quotes.length > 0) {

		quotes.each(function(quote, index) {
			var h3AndFirstP = quote.getElement('h3').getNext();

			var quoteContainer = new Element('div', {
				'id': 'quote_container_' + index,
				'class': 'quote_container'
			});
			var nextElements = h3AndFirstP.getAllNext('*');
			quoteContainer.adopt(nextElements);
			quoteContainer.inject(h3AndFirstP, 'after');

			var quoteAccordion = new Accordion($$(quote), $$(quoteContainer), {
				duration:   'short',
				alwaysHide: true,
				display:    -1,
				onActive: function(toggler, element) {
					toggler.addClass('current');
				},
				onBackground: function(toggler, element) {
					toggler.removeClass('current');
				}
			});
		});
	}
}

window.addEvent('domready', function() {

	// fix header margins (if css didn't work)
	var marginFixElements = $$(
		'div#cnr_ctt_nav1 div:first-child + div h2:first-child',
		'div#cnr_ctt_nav1 div:first-child + div h3:first-child',
		'div#cnr_ctt_main div:first-child h1:first-child',
		'div#cnr_ctt_nav2 div:first-child h2:first-child',
		'div#cnr_ctt_nav2 div:first-child h3:first-child'
	);

	marginFixElements.each(function(marginFixElement) {
		marginFixElement.setStyle('margin-top', '0');
	});

	// init slideshows
	initSlideshows();

	// init accordions
	initAccordions();

	// init quotes
	initQuotes();

	// animate header
	var documentBody = $(document.body);

	if (!documentBody.hasClass('dontanimate')) {

		var headerDiv             = $('wpr_header');
		var contentDiv            = $('wpr_content');
		var mainmenuItems         = $$('div#cnr_mainmenu ul li');
		var inactiveMainmenuItems = [];
		var headerCollapsed       =	false;

		if (documentBody.hasClass('startcollapsed')) {
			documentBody.addClass('collapsed');
			headerCollapsed = true;
			contentDiv.addClass('arrow');
		}

		// init Tween for collapsing/expanding header
		var headerTween = new Fx.Tween(headerDiv, {
			duration: 'short',
			property: 'height',
			link: 'cancel',
			onComplete: function() {
				if (headerCollapsed) {
					documentBody.removeClass('collapsed');
					headerCollapsed = false;
				} else {
					documentBody.addClass('collapsed');
					headerCollapsed = true;
				}
			}
		});

		// init Tween for collapsing header on page load
		var headerStartTween = new Fx.Tween(headerDiv, {
			duration: 'short',
			property: 'height',
			onComplete: function () {
				documentBody.addClass('collapsed');
				// remove event after single use
				documentBody.removeEvent('mousemove', headerStart);
			}
		});

		// actions for animating header on page load
		var headerStart = function(e) {
			if (e.page.y > 300 && !this.hasClass('collapsed')) {
				// animate header
				headerStartTween.start(headerDiv.getStyle('height').toInt(), 150);

				// animate mainmenu items
				inactiveMainmenuItems.each(function(inactiveMainmenuItem) {
					inactiveMainmenuItem.tween(0);
				});

				// set and animate arrow image
				contentDiv.setStyle('background-position', 'center -25px');
				contentDiv.addClass('arrow');
				contentArrowTween.start('center 4px');
			}
		};

		var contentArrowTween = new Fx.Tween(contentDiv, {
			duration: 'long',
			property: 'background-position'
		});

		// add event on documentBody
		documentBody.addEvent('mousemove', headerStart);

		var mainmenuItemOriginalHeight = mainmenuItems[0].getStyle('height');

		mainmenuItems.each(function(mainmenuItem) {
			if (!mainmenuItem.hasClass('current')) {
				mainmenuItem.set('tween', {
					duration: 'short',
					property: 'height'
				});
				inactiveMainmenuItems.push(mainmenuItem);
			}
		});

		// add events on headerDiv and animate it
		headerDiv.addEvents({
			'mouseenter': function() {
				// expand header
				headerTween.start(headerDiv.getStyle('height').toInt(), 300);

				// animate mainmenu items
				inactiveMainmenuItems.each(function(inactiveMainmenuItem) {
					inactiveMainmenuItem.tween(mainmenuItemOriginalHeight);
				});

				// remove arrow image
				contentDiv.removeClass('arrow');
			},
			'mouseleave': function() {
				// collapse header
				headerTween.start(headerDiv.getStyle('height').toInt(), 150);

				// animate mainmenu items
				inactiveMainmenuItems.each(function(inactiveMainmenuItem) {
					inactiveMainmenuItem.tween(0);
				});

				// set and animate arrow image
				contentDiv.setStyle('background-position', 'center -25px');
				contentDiv.addClass('arrow');
				contentArrowTween.start('center 4px');
			}
		});
	}

	// remove margin-right on content columns and thumbnails
	var contentColumns = $$('div.content.col');
	contentColumns.each(function(contentColumn, index) {
		if (((index + 1) % 2) == 0) {
			contentColumn.addClass('lastcol');
		}
	});

	var thumbnails = $$('div.content.thumbnail');
	thumbnails.each(function(thumbnail, index) {
		if (((index + 1) % 4) == 0) {
			thumbnail.addClass('lastcol');
		}
	});

	var thumbnails_small = $$('div.content.thumbnails_small ul li');
	thumbnails_small.each(function(thumbnail, index) {
		if (((index + 1) % 4) == 0) {
			thumbnail.addClass('lastcol');
		}
	});

	// ons werk thumbnail behavior
	if ($(document.body).hasClass('ons_werk')) {
		$$('div.content.thumbnail').each(function(thumbnail) {
			thumbnail.addEvent('mouseenter', function() {
				// doe iets
			});
		});
	}

	// timeline behaviour
	var timeline = $('timeline');
	if (timeline) {

		timeline.highlights = timeline.getElements('ul.highlights li');
		if (timeline.highlights.length > 0) {

			timeline.highlights.each(function(highlight) {

				highlight.content_box = highlight.getElement('.wpr-content');
				highlight.content_box.fade('hide');

				highlight.hover_bullet = new Element('div', {
					'class': 'hover-bullet'
				}).inject(highlight, 'top');
				highlight.hover_bullet.fade('hide');

				highlight.addEvents({
					'mouseenter': function(e) {
						highlight.content_box.fade('in');
						highlight.hover_bullet.fade('in');
					},
					'mouseleave': function(e) {
						highlight.content_box.fade('out');
						highlight.hover_bullet.fade('out');
					}
				});
			});
		}
	}

	// employees menu behaviour
	var employees_menu = $('employees-menu');
	if (employees_menu) {

		employees_menu.items        = employees_menu.getElements('ul li');
		employees_menu.current_item = employees_menu.getElement('ul li.current');

		employees_menu.items.each(function(item) {
			item.link_el = item.getElement('a');
			item.image_el = item.getElement('.image');

			if (!item.hasClass('current')) {

				item.image_el.fade('hide');
				item.link_el.addEvents({
					'mouseenter': function(e) {

						if (employees_menu.current_item) {
							employees_menu.current_item.image_el.fade('out');
							employees_menu.current_item.removeClass('current');
						}

						item.image_el.fade('in');
					},
					'mouseleave': function(e) {

						if (employees_menu.current_item) {
							employees_menu.current_item.image_el.fade('in');
							employees_menu.current_item.addClass('current');
						}

						item.image_el.fade('out');
					}
				});
			}
		});
	}

	// open links to downloadable files or other sites in a new window
	Array.prototype.f_contains = function(s) {
		for (var i = 0; i < this.length; i++) {
			if (this[i] == s) return true;
		}
		return false;
	};

	// known extensions for downloadable files
	var exts = ['png', 'jpg', 'jpeg', 'gif', 'pdf', 'doc', 'xls', 'ppt', 'zip', 'gz', 'mpg', 'mpeg', 'mov', 'mp4', 'avi', 'wmv', 'mp3', 'wav', 'swf', 'bmp', 'tif', 'tiff', 'svg'];

	var links = document.getElementsByTagName('a');

	for (var i = 0; i < links.length; i++) {
		var link  = links[i];

		// find extension
		var split_slash = link.href.split('/');
		var split_ext   = split_slash[split_slash.length - 1].split('.');
		var ext = split_ext[split_ext.length - 1];

		// check if extension indicates a downloadable file
		if (split_ext.length > 1) {
			if (exts.f_contains(ext)) {
				links[i].target = '_blank';
			}
		}

		// look for links to other sites
		if (link.href.substring(0, 7) == 'http://') {
			if (link.href.substring(7, 7 + window.location.hostname.length) != window.location.hostname) {
				links[i].target = '_blank';
			}
		}
	}

	// known extensions for Shadowbox media files
	var shadowboxMediaFound = false;

	var s_exts = ['jpg', 'jpeg', 'png', 'gif', 'flv'];

	var s_links = document.getElementsByTagName('a');

	for (var j = 0; j < s_links.length; j++) {
		var s_link  = s_links[j];

		// find extension
		var s_split_slash = s_link.href.split('/');
		var s_split_ext   = s_split_slash[s_split_slash.length - 1].split('.');
		var s_ext = s_split_ext[s_split_ext.length - 1];

		// check if extension indicates a Shadowbox media file
		if (s_split_ext.length > 1) {
			if (s_exts.f_contains(s_ext)) {
				s_links[j].rel = 'shadowbox[all]';
				shadowboxMediaFound = true;
			}
		}
	}

	// initialize Shadowbox (if necessary)
	if (shadowboxMediaFound) {
		var options = {
			flvPlayer: '/swf/lib/shadowbox/flvplayer.swf'
		};

		Shadowbox.init(options);
	}
});
