(function($) {
	$(document).ready( function() {
		var clickedMenu = null;

		var menuClick = function (obj) {
			var menuItem = obj;

			// Set language
			setLang($("meta[name='language']").attr('content'));

			if ($(menuItem).is('a')) {
				// clicking twice at the same button will not result in more calls.
				if (clickedMenu == menuItem) {
					return false;
				}
				else {
					clickedMenu = menuItem;
				}
				// discard links with a # as href.
				if ($(menuItem).attr('href').indexOf('#') != -1) {
					return false;
				}

				// a file download has a different extenion so allow that.
				/*var extension = getExtension($(menuItem).attr('href'));
				if (!(extension == 'php' || extension == '')) {
					return true;
				}*/

				if($(menuItem).attr('href').match(new RegExp('pdf', 'i'))) {
					return true;
				}

				// if link of class 'lnk-follow' follow the link
				if(document.location.href.match(new RegExp('vacature/[0-9]+', 'i'))) {
					return true;
				}

				if ($(menuItem).attr('href').match(new RegExp('(vacatures\.php|vacancies\.php)', 'i')) && document.location.href.match(new RegExp('(vacture)', 'i'))) {
					return false;
				}

				// if link of class 'lnk-follow' follow the link
				if($(menuItem).attr('href').match(new RegExp('vacature/[0-9]+', 'i'))) {
					return true;
				}

				$('body, a').css('cursor', 'wait');

				$.get($(menuItem).attr('href'), function(data) {
					var startAt = '<body>';
					var endAt = '</body>';
					var data = data.substring(data.indexOf(startAt), data.indexOf(endAt) + endAt.length);

					var contentArea = $('#content-area', data);

					// for IE's sake we have to check bg-pos manually.
					var style = contentArea.attr('style');
					if (style) {
						style = style.toLowerCase();
						style = style.substring(style.indexOf(':') + 1, style.length);
						style = style.replace('top', '0px');
						style = style.replace(';', '');
						style = $.trim(style);
					}
					else {
						style = '0px 0px';
					}

					$("div#content-area").animate({ backgroundPosition: style }, "slow");

					var content = contentArea.find('div.text');
					var navbar = contentArea.find('div#nav');

					// if content can't be found, redirect to the page
					if (content.length == 0) {
						$(menuItem).unbind();
						return true;
					}

					content.hide();

					$('div.contentInner').children().fadeOut(300, function() {
						$('div.contentInner')
							.empty()
							.append(content);

						content.fadeIn(1000, function() {

						//scrollbar maken
						$('.scroll').jScrollPane({scrollbarOnLeft:true, maintainPosition:true});


						//navbar ophalen
						navbar.insertBefore($('.jScrollPaneContainer'))

						//Flash header
						makeFlash();
});

					});

					$('body').css('cursor', 'auto');
					$('a').css('cursor', 'pointer');
				});
			}

			return false;
		}

		// controls menu-switching to display or to hide.
		// other functions take care of loading the appropriate pages.
		$('div.bottomMenuInner1 a').click(function() {

			// Alle huidige menu's inklappen
			$('ul.menu li').removeClass('hover');
			$('ul.menu ul').removeClass('dropDown');

			// Menu uitklappen
			$(this).parents("li").addClass("hover");
			$(this).parents('ul').addClass("dropDown");
			$(this).siblings('ul').addClass("dropDown");
		});

		// all links within the contentarea will be loaded via ajax.
		$('div.jScrollPaneContainer div.text a').livequery('click', function() {
			if (/^http/i.test($(this).attr('href')) && $(this).attr('target') != '_blank') {
				return menuClick(this);
			}
		});

		// put scrolling on all but the first and the last link.
		$('div.bottomNavigation a:not(:first):not(:last)').bind('click', {index: 0}, function() {
			return menuClick(this);
		});

		// put scrolling on all menu-items.
		$('div.bottomMenuInner1 ul.menu > li').each(function(i) {
			if($("meta[name='language']").attr('content') == 'nl') {
				$(this).addClass('menu1' + (parseInt(i) + 1));
			} else {
				$(this).addClass('menu' + (parseInt(i) + 1));
			}
		});

		// put scrolling on all menu-items.
		$('div.bottomMenuInner1 ul.menu > li').each(function(i) {

			// for menu items that don't need content swap.
			var no_bind = [ 'menuitem_187' ];
			if(jQuery.inArray($('a', this).attr('id'), no_bind) !== -1) {
				$('a', this).unbind();
			} else {
				$('a', this).bind('click', {index: i + 1}, function() {
					return menuClick(this);
				});
			}
		});

		// use scrollTo function for scrolling to anchors
		$(".content a[href*='#']").livequery('click', function() {
			var naam = $(this).attr('href').split('#')[1];
			var locatie = parseInt($("a[name='" + naam + "']").offset().top);
			$('.scroll')[0].scrollTo(locatie - 185);
			return false;
		});


	});

	var getExtension = function (s) {
		if(!s) {
			return '';
		}

		var dot = s.lastIndexOf(".");
		if( dot == -1 ) {
			return '';
		}

		return s.substr(dot + 1, s.length);
	}
	try {
		makeFlash()
	}
	catch (e) {}
})(jQuery);
