/*
jQuery.fn.replaceWith = function(replacement) {
	return this.each(function(){
	element = $(this);
	$(this)
		.after(replacement).next()
		.attr('class', element.attr('class')).attr('id',element.attr('id'))
		.html(element.html())
		.prev().remove();
	});
};
*/

$(document).ready(function() {
	//select first menu item if none is selected
	if (!$('#top li.active_menu_item').length)
		$('#top ul li:first').addClass('active_menu_item');
	
	$('#top li a, #top li ul').addClass('round');
	
	$('#top ul:first').addClass('round-bottom')
	
	$('#buttons li, #buttons ul, #buttons ul *').css('background-color','#304269');
	$('#top ul:first > li, #buttons > li').mouseenter( function() {
		$( this ).find( 'ul' ).show();
	} ).mouseleave( function() {
		$( this ).find( 'ul' ).hide();
	} );
	
	/*
	$('#flash dl').replaceWith('<ul></ul>');
	$('#flash span').replaceWith('<li></li>');
	$('#flash li dt').addClass('date').replaceWith('<span></span>');
	$('#flash h2').replaceWith('<h3></h3>');
	*/
	$('#flash > h3').hide();
	if( $( '#news' ).length )
	{
	var ul = $( '<ul></ul>' );
	$( '#flash dl dt' ).each( function() {
		var item = $( this ).add( $( this ).next() ).add( $( this ).next().next() ).add( $( this ).next().next().next().filter( '.more' ) );
		var li = $( '<li></li>' ).append( '<span class="date">' + $(item).filter('dt').html() + '</span>' );
		if( $( item ).filter( '.more' ).length )
			li.append( '<h3><a href="' + $( item ).filter('.more').find('a').attr('href') + '">' + $(item).find('h2').html() + '</a></h3>' );
		else
			li.append( '<h3><span>' + $(item).find('h2').html() + '</span></h3>' );
		li.append( '<p class="description">' + $(item).filter( '.description' ).html() + '</p>' );
		/*
		if( $( item ).filter('.more').length )
			li.append( '<div class="more">' + $(item).filter( '.more' ).html() + '</div>' );
		*/
		ul.append( li );
	} );
	$( '#flash dl' ).after( ul ).remove();
	
	$('#flash ul').attr('id','items')
	$('#flash ul li').append('<span class="fade"></span>');
	
	/*
	$('#flash ul h3').each(function(){
		$(this).html('<a href="#">'+$(this).html()+'</a>').addClass('h3');
	});
	*/
	$('#flash li').each(function(){
		var uhh= $('.more a',this).attr('href');		
		$('.h3 a',this).attr('href',uhh);
	});
	$( '#flash li:first' ).addClass( 'first' );
	}
	$( '#vn_20_maintext table a' )
		.each( function() {
			$( '<div class="popup"><img src="' + $( this ).attr('href') + '"/></div>' )
				.insertAfter( this )
				.css( 'position', 'absolute' )
				.hide()
			;
		} )
		.mouseover( function( e ) {
			$('.popup').hide();
			$( this ).next().fadeIn();
			$( this ).mousemove();
		} )
		.mousemove( function( e ) {
			$window = $(window);
			$popup = $( this ).next();
			$popup.css( 'margin-left', 660 - $popup.find('img').width() );
			$popup.css("top", e.pageY + 10);
		} )
		.mouseout( function() {
			$( this ).next().fadeOut();
		} )
	;
	
	spinner.container = $( '#spinner' );
	spinner.items = $( '#spinner li' );
	spinner.width = spinner.container.width();
	spinner.update_items();
	
	spinner.static = $( '#spinner' ).hasClass( 'static' );
	
	spinner.cycle();
	
	spinner.container
		.hover(
			function() {
				spinner.hover = true;
			},
			function() {
				spinner.hover = false;
				if( !spinner.static ) spinner.cycle();
				else spinner.update_items();
			}
		)
		.mousemove( function( e ) {
			spinner.mousex = e.pageX - spinner.container.offset().left;
			spinner.update_items();
		} )
	;
	if( $( '.item' ).length > 1 )
		$( '.item' ).css( 'height', Math.max( $( '.item:first' ).height(), $( '.item:last' ).height() ) )
	$('#flash li:eq(1)').addClass('first')
	$('#flash li:not(.first) h3 a').each( function() { $( this ).html( $( this ).html().substr( 0, 34 ) ); } )
	
	$( '#segmentLogin_username #uPrefix' ).html( 'Gebruikersnaam:' );
});

function update_popup( $popup, event )
{
}

spinner = {
	container: null,
	static: false,
	items: null,
	width: 0,
	offset: 0,
	hover: false,
	mousex: 0,
	cycle: function()
	{
		if( spinner.hover ) return;
		spinner.update_items();
		spinner.offset += 0.002;
		setTimeout( spinner.cycle, 50 );
	},
	update_items: function( offset )
	{
		this.items.each( function( i ) {
			if( !spinner.static )
			{
				var r = ( i/spinner.items.length + spinner.offset );
				r = ( ( r - Math.floor( r ) ) * 0.6 + 0.2 ) * Math.PI;
				var abssin = Math.abs(Math.sin(r));
				var left = spinner.width/2 - Math.sin(r*2)*spinner.width/2.4;
				var width = ( abssin - 0.5 ) * 400;
				var zindex = Math.round( abssin * 200 ) + 200;
			}
			else
			{
				var x = ( i - spinner.items.length/2 );
				var left = spinner.width/2 + ( i - spinner.items.length/2 +0.5 ) * ( ( spinner.width - spinner.items.length * 10 ) / spinner.items.length );
				var width = 185;
				var zindex = 10;
			}
			
			if( spinner.hover && ( spinner.static || Math.cos(r*2) < 0 ) )
			{
				width = Math.max( width, 220 - Math.abs( left - spinner.mousex ) / 4 );
				zindex = Math.round( 900 - Math.abs( left - spinner.mousex ) );
			}
			
			var height = Math.round( width / 17 * 9 );
			
			$( this )
				.css( {
					marginLeft: left - 0.5 * width,
					zIndex: zindex,
					width: width
				} )
			;
			var a = $( this )
				.find( 'a' ).find( 'img' )
					.css( {
						width: width,
						height: height
					} )
				.end()
			;
			a.css( 'margin-top', 125 - height );
		} );
	}
};

 var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-23596966-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

