var openLightbox = function() {
	document.appendChild( '<div id="light" class="lightbox_white_content"></div><div id="fade" class="lightbox_black_overlay"></div>' );
	var a = document.getElementByTagName( 'body' );
	a.style.overflow = 'hidden';
}

var selectAll = function ( check, form ) {
	for( n = 0; n < form.elements.length; n++ ) {
		if( form.elements[ n ].type == 'checkbox' ) {
			form.elements[ n ].checked = check;
		}
	}
}

$( function() {
	if( $( '#nieuwsTicker .back_button' ).length ) {
		$( '#nieuwsTicker .back_button' ).remove();
	}
} );

function cycleInit() {
	$( '#highlightmenu li' ).hoverIntent( {
		sensitivity: 1,
		interval: 0,
		over: makeTall,
		timeout: 0,
		out: makeShort
	} );

	$( '#highlightmenu li' ).each( function() { $( this ).find( 'img:first' ).toggle(); } );
	$( '#highlightmenu li#actief img' ).toggle();
	$( '#highlightimages ul.es' ).toggle();
	$( '#highlighttekst div.es' ).css( 'z-index', 2 );
	$( '#highlightkop div.es' ).css( 'z-index', 2 );

	function makeTall() { $( this ).animate( { 'height' : 45 }, 250 ); if( $( this ).attr( 'id' ) != 'actief' ){ $( this ).find( 'img' ).toggle(); } }
	function makeShort() { var Obj = $( this ); if( Obj.attr( 'id' ) != 'actief' ){ Obj.animate( { 'height' : 35 }, 250, function(){ Obj.find( 'img' ).toggle(); } ); } }
}

var timeout;
function homeInit() {
	cycleInit();

	$( '#highlightimages ul.gr, #highlightimages ul.tr, #highlightimages ul.es, #highlightimages ul.ai' ).cycle( { fx: 'fade' } ).cycle( 'pause' );
	/* DEZE ZIJN ERUIT GEHAALD, MAAR 1 PLAATJE: MOCHTEN ER MEER TOEGEVOEGD WORDEN ZET DEZE ERIN!
		, #highlightimages ul.eg, #highlightimages ul.vw
	*/
	$( '#highlightimages ul.es' ).cycle( 'resume' );

	$( '#highlightmenu li' ).click( function() {
		clearTimeout( timeout );
		var $diaClass = $( this ).attr( 'class' );

		$( '#highlightmenu li#actief' ).animate( { 'height' : 35 }, 250, function(){
			$( this ).find( 'img:first' ).show().next().hide();
		} ).removeAttr( 'id' );

		$( this ).find( 'img:first' ).hide().next().show();
		$( this ).attr( 'id', 'actief' ).animate( { 'height' : 45 }, 250 );

		$( '#highlightimages ul.gr, #highlightimages ul.tr, #highlightimages ul.es, #highlightimages ul.eg, #highlightimages ul.ai, #highlightimages ul.vw' ).cycle( 'pause' ).hide();
		$( '#highlightimages ul.' + $diaClass ).cycle( 'resume' ).fadeIn();

		$( '#highlighttekst div, #highlightkop div' ).css( 'z-index', 1 );
		$( '#highlighttekst div.' + $diaClass + ', #highlightkop div.' + $diaClass ).css( 'z-index', 2 );
	} );

	setTimeout( "triggerNextImage();", 3000 );
}

function triggerNextImage() {
	var $Obj		= $( "#highlightmenu" ),
		$nextObj	= $Obj.find( "#actief" ).next();

	if( $nextObj.length ) {
		$nextObj.trigger( "click" );
	}
	else {
		$Obj.find( ":first" ).trigger( "click" );
	}

	timeout = setTimeout( "triggerNextImage();", 5000 );
}

function menuInit() {
	if( $( '#subMenuContainer ul.mainMenu' ).length == 1 ) {
		$( '#subMenuContainer ul.mainMenu' ).addClass( 'subNavigatie' ).removeClass( 'mainMenu' );
	}

	$( 'ul.mainMenu > li:last' ).css( 'border-right', '2px solid #e7e5e6' );
	$( 'ul.subMenu' ).each( function() {
		$( this ).find( 'li:first' ).css( 'padding-top', '20px' );
	} );

	$( 'ul.mainMenu li, ul.mainMenu li a, ul.mainMenu li h5 a' ).each( function() {
	    if( $( this ).find( '.subMenu' ).length ) {
	       zIndexWorkaround( $( this ).find( '.subMenu' ), false );
	    }
	} );

	if( !( navigator.appVersion.indexOf( 'MSIE 7.' ) == -1 ) && navigator.userAgent.match( /MSIE \d\.\d+/ ) ) {
	   // $( 'ul.mainMenu > ul.subMenu:last' ).css( { 'margin-left' : '-90px' });
	}

	$( 'ul.mainMenu li, ul.mainMenu li a, ul.mainMenu li h5 a' ).hover( function() {
		if( $( this ).find( '.subMenu' ).length == 1 && $( this ).find( '.subMenu:animated' ).length == 0 ) {
			var nieuweBreedte = $( this ).width() + 38;
			nieuweBreedte = ( nieuweBreedte > 200 ) ? nieuweBreedte : 200;
			$( this ).find( '.subMenu, .subMenu .li' ).width( nieuweBreedte + 'px' );

			var aantalLi = $( this ).find( 'li' ).length;
			var nieuweHoogte = ( ( aantalLi * 40 ) - ( ( aantalLi > 4 ? ( ( aantalLi / 4 ) * 10 ) : 0 ) ) ) + 'px';
			$( this ).find( '.subMenu' ).css( { height: nieuweHoogte, display: 'block', position: 'absolute' } );

			if( $( this ).find( '.subMenu:last' ).length == 1 ) {
				$( '.subMenu:last' ).css( { 'margin-left' : '-85px' } );
			}

			if( !( navigator.appVersion.indexOf( 'MSIE 7.' ) == -1 ) ) {
				zIndexWorkaround( $( this ).find( '.subMenu' ), true );

                if( $( this ).find( '.subMenu:last' ).length == 1 ) {
				    $( '.subMenu:last' ).css( { 'margin-left' : '-95px' } );
			    }
			}
		}
	}, function() {
		$( this ).find( '.subMenu' ).css( { height: 0, display: 'none' } );
	} );
}

function zIndexWorkaround( $this, init ) {
    if( init ) {
        $this.find( 'li' ).width( $this.width() );
    }

	$( 'ul.subMenu' ).parents().each( function() {
		var pos = $( this ).css( 'position' );

		// If it's positioned,
		if( pos == 'relative' ||
			pos == 'absolute' ||
			pos == 'fixed')
		{
			/*
			** Add the 'on-top' class name when the
			** mouse is hovering over it, and remove
			** it when the mouse leaves.
			*/
			$( this ).hover( function() {
					$( this ).addClass( 'on-top' );
				},
				function() {
					$( this ).removeClass( 'on-top' );
				} );
		}
	} );
}
