/***********************************************************************

Author:     Arno Hoogma
URL:        http://www.4rn0.nl

***********************************************************************/


if (typeof window.jQuery != 'undefined') {

	$(function() {
		
		displayCategories();
		navigationHelper();
		categoryHelper();
		externalAnchors();
		
	});

	/**
	 *
	 */
	function displayCategories()
	{
		
		$('div#mainContent dt a').hover(
			function() {
				
				if ($(this).attr('class')) {
				
					// var categories = $(this).attr('class').split(' ');
					// 					
					// $.each(categories, function(i, n) {
					// 	$('ul#projectNavigation a.' + $.trim(n)).addClass('active');
					// });
					
					var category = $(this).attr('class');
					$('ul#projectNavigation a.' + category).addClass('active');
				
				}
			
			},
			function() {
				$('ul#projectNavigation a').removeClass('active');
			}
		);

	}
	
	/**
	 *
	 */
	function navigationHelper()
	{
		
		$('body.home div#mainContent dt a').click(function(e) {
			
			$('div#mainContent dt a').removeClass('active');
			$('div#projectInformation').remove();
			$(this).addClass('active');
			
			var offset = $('div#mainContent').offset();
		
			var x = e.pageX - offset.left;
			var y = e.pageY - offset.top;

				x = (x - 210);
				y = (y - 162);
				x = (x + 420) > $('div#mainContent dl').width() ? 424 : x;
				y = (y + 325) > $('div#mainContent dl').height() ? ($('div#mainContent dl').height() - 281) : y;
				x = (x < 0) ? -16 : x;
				y = (y < 0) ? 0 : y;
			
			var string  = '<div id="projectInformation" style="left: ' + x + 'px; top: ' + y + 'px;">';
				string += '<h3>' + $(this).html() + '</h3>';
				string += '<a href="' + $(this).attr('href') + '"><img src="' + $(this).parent().next().children('a').attr('href') + '"></a>';
				
				if ($(this).parent().prevAll().slice(0,1).size()) {
					string += '<a id="previous" onclick="helperPrev();">&lt; vorige</a>';
				}

				if ($(this).parent().nextAll().slice(1,2).size()) {
					string += '<a id="next" onclick="helperNext();">volgende &gt;</a>';
				}
				
				string += '<a id="close" onclick="removeHelper();">sluiten</a>';
				string += '</div>';
			
			$('div#mainContent').append(string);
			navigationHelperHover();

			e.preventDefault();
			
		});
				
	}

	/**
	 *
	 */
	function helperNext()
	{
		
		var target = $('div#mainContent dt a.active').parent().nextAll().slice(1,2).children('a');
		
		$('div#mainContent dt a').removeClass('active');
		target.addClass('active');
		
		var string  = '<h3>' + target.html() + '</h3>';
			string += '<a href="' + target.attr('href') + '"><img src="' + target.parent().next().children('a').attr('href') + '"></a>';
			
			if (target.parent().prevAll().slice(0,1).size()) {
				string += '<a id="previous" onclick="helperPrev();">&lt; vorige</a>';
			}

			if (target.parent().nextAll().slice(1,2).size()) {
				string += '<a id="next" onclick="helperNext();">volgende &gt;</a>';
			}
			
			string += '<a id="close" onclick="removeHelper();">sluiten</a>';
		
		$('div#projectInformation').html(string);
		navigationHelperHover();
		
		return false;
		
	}
	
	/**
	 *
	 */
	function helperPrev()
	{
		
		var target = $('div#mainContent dt a.active').parent().prevAll().slice(1,2).children('a');

		$('div#mainContent dt a').removeClass('active');
		target.addClass('active');
		
		var string  = '<h3>' + target.html() + '</h3>';
			string += '<a href="' + target.attr('href') + '"><img src="' + target.parent().next().children('a').attr('href') + '"></a>';
			
			if (target.parent().prevAll().slice(0,1).size()) {
				string += '<a id="previous" onclick="helperPrev();">&lt; vorige</a>';
			}

			if (target.parent().nextAll().slice(1,2).size()) {
				string += '<a id="next" onclick="helperNext();">volgende &gt;</a>';
			}
			
			string += '<a id="close" onclick="removeHelper();">sluiten</a>';
		
		$('div#projectInformation').html(string);
		navigationHelperHover();
		
		return false;
		
	}
	
	/**
	 *
	 */
	function removeHelper()
	{
		
		$('div#mainContent dt a').removeClass('active');
		$('div#projectInformation').remove();
		
	}
	
	/**
	 *
	 */
	function navigationHelperHover()
	{
		
		$('#projectInformation img').load(function() {
			$(this).before('<span id="imageHover" style="height: ' + $(this).height() + 'px; width: ' + $(this).width() + 'px;"></span>');	
		});		
		
	}
	
	/**
	 *
	 */
	function categoryHelper()
	{
		
		$('ul.destinations li').click(function() {
			document.location.href = $('a', $(this)).attr('href');
		});
		
	}
	
	function externalAnchors()
	{
	
		$("a[rel='external']").attr('target', '_blank');
		
	}
	
}