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

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

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


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

	var init = false;
		
	$(function()
	{
		
		initNavigation();
		initFixedNavigation();
		prepareNewsFlash();
		initClientPortfolio();
		initExternalAnchors();
							
	});
	
	/**
	 *
	 **/
	function initNavigation()
	{
		
		$('ul#navigation ul li.active').parent().parent().addClass('current');
		
		$('ul#navigation > li:not(.current)').hover(
			function() {
				$('ul#navigation li.current ul').css('visibility', 'hidden');
			},
			function() {
				$('ul#navigation li.current ul').css('visibility', 'visible');
			}
		);
		
	}
	
	/**
	 *
	 **/
	function initFixedNavigation()
	{
		
		if (!($.browser.msie && parseInt($.browser.version) == 6)) {
			
			$('div#branding').css({
				position: 	'fixed',
				left:  		'50%',
				marginLeft:	'-500px',
				zIndex: 		10
			});
		
			$('h4.news, div#news').css({
				position: 	'fixed',
				zIndex: 		10
			});

			$('div#inhoud').css('top', $('div#branding h1').height());
			$('div#inhoud').css('margin-bottom', $('div#branding h1').height() + 30);
			$('h4.news').css('right', (($(window).width() - 1000) / 2) + 175);
			$('div#news').css('right', ($(window).width() - 1000) / 2);
			
			$(window).resize(initFixedNavigation);
			
		}
		
	}
	
	/**
	 *
	 **/
	function prepareNewsFlash()
	{
		
		$('div#news').css('overflow', 'hidden');
		
		$('div#news p').css({
			whiteSpace: 'nowrap',
			padding: 	0
		});
		
		window.setTimeout(function() {
			initNewsFlash();
			initNewsFlashControls();
		}, 750);
		
	}
	
	/**
	 *
	 **/
	function initNewsFlash()
	{
		
		if (!init) {

			$('div#news p').wrapInner('<span></span>');
			$('div#news p').append('<span>' + $('div#news p span:first').html() + '</span>');
			$('div#news p').append('<span>' + $('div#news p span:first').html() + '</span>');
			$('div#news p').append('<span>' + $('div#news p span:first').html() + '</span>');
			$('div#news p').append('<span>' + $('div#news p span:first').html() + '</span>');
		
		}
				
		$('div#news p').css({
			position: 	'absolute',
			left: 		(init ? '170px' : 0)
		})
		.animate(
			{ left: '-' + ($('div#news p').width() + 10) + 'px' }, 
			($('div#news p').width() * 50), 
			'linear', 
			initNewsFlash
		);
		
		init = true;
		
	}
	
	function initNewsFlashControls()
	{
		
		$('div#news').hover(
			function() {
				$('div#news p').stop();
			},
			function() {				
				$('div#news p').animate(
					{ left: '-' + ($('div#news p').width() + 10) + 'px' }, 
					(($('div#news p').width() + parseInt($('div#news p').css('left'))) * 40), 
					'linear', 
					initNewsFlash
				);
			}
		);
		
	}
	
	/**
	 *
	 **/
	function initClientPortfolio()
	{

		$('body.opdrachtgevers ul.portfolio').hide();
		
		$('body.opdrachtgevers h3.client, body.opdrachtgevers h3.client + p').css('cursor', 'pointer').click(function() {

			if (!$(this).nextAll('ul.portfolio').slice(0, 1).hasClass('active')) {
				$('ul.portfolio.active').removeClass('active').slideUp(300);			
				$(this).nextAll('ul.portfolio').slice(0, 1).toggleClass('active').slideToggle(300);
			}
			
			else {
				$(this).nextAll('ul.portfolio').slice(0, 1).toggleClass('active').slideToggle(300);
			}
			
		});

	}
	

	/**
	 * External links
	 */
	
	function initExternalAnchors()
	{
		
		$('a[rel=external]').attr('target', '_blank');
		
	}
	
}