var runLite = 'set';

/* Knop met hovereffect
-----------------------------------------------------------------------------------------*/
function initKnopHover( EleStr ) {
	EleStr = ( typeof EleStr != 'string' ? '' : EleStr+' ' );
	$(EleStr+'input,'+EleStr+'img,'+EleStr+'a').filter('.knop').unbind('hover').hover(
		function() { knopHover( this, true ); },
		function() { knopHover( this, false ); }
	);
}
function knopHover( Ele, Hover ) {
	var imgSrc = ( Ele.src ? Ele.src : Ele.style.backgroundImage );
	var imgExt = imgSrc.match( RegExp('\\.([a-z]{3})("\\)|\\)|)$') );
	if ( !imgExt ) {
		return false;
	}
	imgSrc = imgSrc.replace( '-hover.'+imgExt[1], '.'+imgExt[1] );
	( Hover ? imgSrc = imgSrc.replace( '.'+imgExt[1], '-hover.'+imgExt[1] ) : false );
	( Ele.src ? Ele.src = imgSrc : Ele.style.backgroundImage = imgSrc );
}

var klkExp = new RegExp('\\[([a-z-0-9]+)\\]');
function trackKlik( Ele ) {
	if ( !Ele || ( Ele.nodeName != 'A' && Ele.nodeName != 'IMG' ) || !$(Ele).attr('class') ) {
		return true;
	}
	
	var expMatch = $(Ele).attr('class').match( klkExp );
	$.ajax( { data: { trck: 'klk', id: expMatch[1] } } );
	
	return trackReturn( Ele );
}

function trackReturn( Ele ) {
	if ( Ele.nodeName == 'A' && Ele.onclick && $(Ele).attr('target') != '_blank' ) {
		if ( Ele.onclick != 'undefined' && Ele.onclick.toString().indexOf( 'return ' ) > 0 ) {
			return false;
		}
	}
	
	if ( Ele.nodeName == 'IMG' || ( Ele.nodeName == 'A' && Ele.target == '_blank' ) ) {
		return true;
	}
	
	setTimeout( function() { document.location.href = Ele.href; }, 200 );
	return false;
}

function initKolomRijen() {
	$('.kolomrij .kolomlinks').css( { marginLeft: '4px', marginRight: '4px' } );
	$('.kolomrij .kolomlinks:first-child').css( { marginLeft: '0px' } );
	
	$('.kolomrij').each( function() {
		var maxHoogte = 0;
		for ( var n = 0; n <= 1; n++ ) {
			if ( n == 1 && maxHoogte > 0 ) {
				break;
			}
			$('.box'+( n == 0 ? ' .i3' : '' ), $(this) ).each( function() {
				var hoogte = $(this).outerHeight();
				if ( hoogte > maxHoogte ) {
					maxHoogte = hoogte;
				}
			} ).css( 'min-height', maxHoogte );
		}
	} );
}

var qLeegTekst = 'Stel uw vraag...';
var qEersteKlik = false;
function initZoekbalk() {
	$("#qt").blur( function() {
		if ( $(this).val() == '' ) {
			$(this).val( qLeegTekst ).addClass('leeg');
		}
	} ).bind( 'focus click', function() {
		if ( $(this).val() == qLeegTekst || !qEersteKlik ) {
			$(this).val('');
		}
		qEersteKlik = true;
	} ).autocomplete({
		source: "/jx.php?suggestie=vraag",
		minLength: 2,
		select: function(event, ui) {
			document.location.replace( ui.item.id );
		}
	} );
	
	$('#form-zoeken').submit( function() {
		if ( $('#qt').val() == qLeegTekst ) {
			$('#qt').val('');
		}
	} );
}

var mBrowser;
$(document).ready( function() {
	mBrowser = ( typeof mBrowser == 'undefined' ? false : mBrowser );
	runLite = ( Math.round( jQuery.browser.version ) <= 6 && jQuery.browser.msie );
	$.ajaxSetup( { url: '/jx.php', dataType: 'json', type: 'GET' } );
	
	// Dialoog
	$('#pagina').append( '<div id="dialoog" title="Dialoog"></div>' );
	$('#dialoog').dialog( {
		autoOpen: false,
		width: 755,
		minHeight: 240,
		modal: true,
		resizable: false,
		draggable: false,
		buttons: { "Sluit scherm": function() {	$(this).dialog('close'); } },
		open: function() {
			$('iframe, object').filter( function() {
				if ( $(this).parents('#dialoog').length ) {
					return false;
				}
				
				return true;
			} ).css( { visibility: 'hidden' } );
		},
		close: function() {
			$('iframe, object').css( { visibility: 'visible' } );
		}
	} );
	
	// Zoekbalk
	initZoekbalk();
	
	// Mededelingen
	if ( $(".mededelingen") ) {
		$(".mededelingen").fadeTo( 0, 0.95 );
		setTimeout( function() { $(".mededelingen").fadeOut("slow"); }, 5000 );
	}
	
	// Hover over knoppen
	initKnopHover();
	
	// Tracking
	$('a,img').filter( function() {
		return ( $(this).attr('class') && $(this).attr('class').match( klkExp ) );
	} ).click( function() {
		return trackKlik( this );
	} );
	
	if ( runLite ) {
		// Formulier
		$('.form tr.lbl-hor td:first-child').addClass( 'lbl' );
	}
	
	// Pagina
	if ( typeof( initPagina ) != "undefined" ) { 
		initPagina();
	}
	
	// BBcode opmaak
	if ( $.fn.markItUp ) {
		$('.markitup').each( function() {
			var bbCodeSetup = $.extend( true, {}, bbCodeSettings );
			bbCodeSetup.parseElements = $(this);
			if ( $('#bbcode-parser').length ) {
				bbCodeSetup.previewTarget = $('#bbcode-parser').get(0).contentWindow;
				$.merge( bbCodeSetup.parseElements, $('#vraag') );
			} else {
				bbCodeSetup.markupSet.push( { name: 'Voorbeeld', className: 'preview', call: 'preview' } );
			}
			
			$(this).markItUp( bbCodeSetup );
		} );
	}
	
	// Filter formulier
	$('#form-filters select').change( function() {
		$('#form-filters')[0].submit();
	} );
	
	// Box
	( box ? box.init() : false );
	
	// Kolomrijen
	initKolomRijen();
} );
