var mBrowser, tekstArray, dm, tl;
$(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: 'POST' } );
	
	$.urlParam = function( url, name ) {
		var results = new RegExp('[\\?&]'+name+'=([^&#]*)').exec(url);
		if ( !results ) { return 0; }
		return results[1] || 0;
	}
	
	// Dialogen
	$('#pagina').append( '<div id="dialoog" title="Dialoog"></div><div id="bevestigdialoog" title="Dialoog"></div>' );
	$('#dialoog, #bevestigdialoog').dialog( {
		autoOpen: false,
		width: 600,
		minHeight: 20,
		modal: true,
		resizable: false,
		draggable: false,
		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' } );
		}
	} );
	$('#dialoog').dialog( {
		minHeight: 300,
		buttons: { "Sluit scherm": function() {	$(this).dialog('close'); } }
	} );
	
	// Checkbox lijst
	$('.lijst, .galerij').each( function() {
		if ( !$('input:checkbox', $(this) ).length ) {
			return true;
		}
		
		if ( $(this).hasClass( 'lijst' ) ) {
			$('thead th.check', $(this) ).html( '<input type="checkbox" class="check-alles" />' );
			$('.check-alles', $(this) ).click( function() {
				$('.item input:checkbox', $(this).parents('.lijst') ).attr( 'checked', $(this).attr('checked') ).trigger('change');
			} );
		} else {
			var kop = $(this).prev();
			if ( kop.length && kop.hasClass('kop') ) {
				$(kop).html( '<input type=\"checkbox\" class=\"check-alles\" />'+$(kop).html() );
				$('.check-alles', $(kop) ).click( function() {
					$('.item input:checkbox', $(this).parent().parent().find('.galerij:eq(0)') ).attr( 'checked', $(this).attr('checked') ).trigger('change');
				} );
			}			
		}
	} );
		
	if ( !runLite ) {
		// Advertentielijst
		initLijst();
		
		// Hover over knoppen
		initKnopHover();
		
		// Knoppen centreren
		$('.gecentreerd .knop:last').css( 'marginRight', 0 );
		setTimeout( function() {
			$('.gecentreerd').each( function() {
				if ( $('input.knop', $(this) ).length == 0 ) {
					return false;
				}
				
				var breedte = 0;
				$('.knop', $(this) ).each( function() {
					breedte += $(this).outerWidth(true);
				} );
				
				$(this).wrapInner( '<div style="width: '+breedte+'px;" />' );
			} );
		}, 20 );
		
		// Kolomrijen
		initKolomRijen();
		
		// Autocomplete
		var acCache = {}, acLast;
		var acEmpty = '';
		$("input[name=q]").autocomplete( {
			source: function( request, response ) {
				var term = request.term;
				if ( term in acCache ) {
					response( acCache[ term ] );
					return;
				}
				
				if ( acEmpty.length > 0 && term.length > acEmpty.length && term.indexOf( acEmpty ) == 0 ) {
					acCache[ term ] = {};
					return;
				}
				
				$("input[name=q]").autocomplete( "option", "delay", 50 );
				acLast = $.getJSON( "/jx.php?lijst=suggestie", request, function( data, status, xhr ) {
					for ( var i = 0; i < data.length; i++ ) {
						data[i].value = boldTekst( term, data[i].value );
					}
					if ( data.length == 0 ) {
						acEmpty = term;
					}
					
					acCache[ term ] = data;
					if ( xhr === acLast ) {
						response( data );
					}
				} );
			},
			delay: 400,
			minLength: 2,
			focus: function(event, ui) {
				ui.item.value = ui.item.value.replace('<b>','').replace('</b>','')
			},
			select: function(event, ui) {
				ui.item.value = ui.item.value.replace('<b>','').replace('</b>','')
				//$.ajax( { data: { trck: 'klk', id: 'zoeksuggestie' } } );
				$(this).parents('form').find('input[name=q-l]').val( ui.item.value );
				$(this).val(ui.item.value).parents('form').submit();
			},
			change: function(event, ui) {
				if ( typeof ui.item !== "undefined" && ui.item !== "null" ) {
					ui.item.value = ui.item.value.replace('<b>','').replace('</b>','')
				}
			}
		} );
	}
	
	$('tr.lbl-hor td:first-child').addClass( 'lbl' );
	
	// Tracking
	$('a[class*="]"]').filter( function() {
		return ( $(this).attr('class').match( klkExp ) );
	} ).click( function() {
		return trackKlik( this );
	} );
	
	// Pagina
	if ( typeof( initPagina ) != 'undefined' ) { 
		initPagina();
	}
	
	// Valuta
	$('input.valuta').keydown( function(e) { return valutaInvoer( e ); } ).keyup( function(e) {
		$(this).val( $(this).val().replace('.', ',') );
	} );
} );

/**
 * Knop met hovereffect
 */
function initKnopHover( EleStr ) {
	var eleLijst = {};
	if ( typeof EleStr == 'object' ) {
		eleLijst = EleStr;
	} else {
		EleStr = ( typeof EleStr != 'string' ? '' : EleStr+' ' );
		eleLijst = $(EleStr+'input,'+EleStr+'img,'+EleStr+'a');
		
		$(EleStr+'img').parent().css( 'outline', 'none' );
	}
	
	eleLijst.filter('.knop').unbind('hover').hover(
		function() { knopHover( $(this), true ); },
		function() { knopHover( $(this), false ); }
	);
}

function knopHover( $$, Hover ) {
	var imgSrc = ( $$.attr('href') ? $( 'img', $$ ).attr('src') : $$.attr('src') ? $$.attr('src') : $$.css('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 );
	( $$.attr('href') ? $( 'img', $$ ).attr('src', imgSrc) : $$.attr('src') ? $$.attr('src', imgSrc) : $$.css('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 innoTrack( Cat1, Cat2, Cat3 ) {
	var pageTracker = _gat._getTracker("UA-5097174-7");
	pageTracker._initData();
	pageTracker._trackEvent( Cat1, Cat2, Cat3 );
}

function trackCam( Acc ) {
	$.ajax( { data: { trck: 'webcam', acc: Acc } } );
}

function toonMededeling( Tekst, Ele, Functie ) {
	if ( $('#mededelingen-dynamic').length ) {
		$('#mededelingen-dynamic li').html( Tekst );
	} else {
		var html = '<div class="mededelingen" id="mededelingen-dynamic" style="width: auto; margin-bottom: 10px; display: none;"><div>';
		html += '<ul><li>'+Tekst+'</li></ul></div></div>';
		if ( Functie == 'before' ) {
			$(Ele).before( html );
		} else {
			$(Ele).html( html );
		}
		$('#mededelingen-dynamic').slideDown();
	}
}

function verbergMededeling( Ele ) {
	$('#mededelingen-dynamic').slideUp( 'normal', function() { $(this).remove() } );
}

function initKolomRijen() {
	$('.kolomrij .kolomlinks:first').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() - 2 );
				if ( hoogte > maxHoogte ) {
					maxHoogte = hoogte;
				}
			} ).css( 'min-height', maxHoogte );
		}
	} );
}

function valutaInvoer( e ) {
	var code = e.which;
	if ( ( code >= 48 && code <= 57 ) || ( code >= 96 && code <= 105 ) || code == 35 || code == 36 || code == 8 || code == 9 || code == 46 || code == 188 || code == 110 || code == 190 || code == 16 || code == 13 || code == 37 || code == 39 ) {
		return true;
	}
	return false;
}

function confirmDialoog( Ele, Titel, Tekst ) {
	var dialoogKnoppen = {};
	dialoogKnoppen[ $_t( 'Knop.Alt.JA', 'Ja' ) ] = function() {              
		$(this).dialog("close");
		if ( Ele.nodeName == 'INPUT' ) {
			$(Ele).unbind( 'click.verwijder' ).trigger( 'click' );
		} else {
			document.location.replace( $(Ele).attr('href') );
		}
	}
	dialoogKnoppen[ $_t( 'Knop.Alt.NEE', 'Nee' ) ] = function() {
		$(this).dialog("close");
	}
	
	$('#bevestigdialoog').dialog( {
		buttons: dialoogKnoppen,
		title: Titel
	} ).html( Tekst ).dialog('open');
	return false;
}

function initLijst() {
	$('.lijst:not(.archief) tr.item td.omschr p').css( { cursor: 'pointer' } ).click( function() {
		window.location = $('a.titel', $(this).parent() ).attr('href');
	} );
}

function $_t( Id, Tekst ) {
	return ( typeof tekstArray !== 'undefined' && tekstArray[ Id ] ? tekstArray[ Id ] : Tekst );
}

function boldTekst( Term, Tekst ) {
	var prefix = Tekst.substr( 0, ( Term.length ) );
	var suffix = Tekst.substr( Term.length );
	/*var expl = Term.split( ' ' );
	Tekst = Tekst.replace( Term, '<b>'+Term+'</b>' );
	for ( var n = 0; n < expl.length; n++ ) {
		Tekst = Tekst.replace( expl[n], '<b>'+expl[n]+'</b>' );
	}*/
	return prefix+'<b>'+suffix+'</b>';
}
