var klkExp	 = new RegExp('\\[([a-z-0-9]+)\\]');
var bronExp  = new RegExp('site-([0-9]+)');
var runHeavy = true;
var pSetup 	 = {};

jQuery.noConflict();
jQuery(document).ready( function() {
	runHeavy = !( Math.round( jQuery.browser.version ) <= 6 && jQuery.browser.msie );
	jQuery.ajaxSetup( {
		url: "/jx.php",
		global: false,
		type: "POST",
		dataType: "json"
	} );
	
	if ( runHeavy ) {
		initItemlijst();
	}
	initMededeling();
	
	jQuery('a[class*="]"]').filter( function() { return jQuery(this).attr('class').match( klkExp ); } ).click( function() { return trackKlik( this ); } );
	
	// Pagina
	if ( typeof( initPagina ) != 'undefined' ) { 
		initPagina();
	}
	
	if ( !runHeavy ) {
		jQuery('.knop.klein').addClass('knopklein');
		jQuery('.knop.wit').addClass('knopwit');
	}
	
	initInfobox();
	initFormCommentaar();
	initZoekformulier();
	
	// Valuta
	jQuery('input.valuta').keypress( function(e) { return valutaInvoer( e ); } );
	
	// Pagina eind
	if ( typeof( initPaginaEind ) != 'undefined' ) { 
		initPaginaEind();
	}
} );

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

/**
 * Valuta
 */
function valutaInvoer( e ) {
	var code = e.which;
	if ( ( code >= 48 && code <= 57 ) || code == 0 || code == 8 || code == 9 || code == 13 ) {
		return true;
	}
	return false;
}

function trackKlik( ele, returnWaarde ) {
	if ( !ele || ( ele.nodeName != 'A' && ele.nodeName != 'IMG' ) || !jQuery(ele).attr('class') ) {
		return true;
	}
	var expMatch = jQuery(ele).attr('class').match( klkExp );
	
	jQuery.ajax( { data: { trck: 'klk', id: expMatch[1] } } );
	
	if ( typeof returnWaarde != "undefined" ) {	
		return returnWaarde;
	}
	return trackReturn( ele );
}

function trackReturn( ele ) {
	if ( ele.nodeName == 'A' && ele.onclick && jQuery(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;
}

/**
 * Items in de lijst.
 */
function initItemlijst() {
	jQuery('.item').each( function() {
		jQuery('.ft', jQuery(this)).wrapInner('<a href="'+jQuery('a', jQuery(this)).attr('href')+'" target="_blank" class="'+jQuery('a', jQuery(this)).attr('class')+'"><\/a>');	
	} );
	
	jQuery('.bloklijst tbody td.item, .lijst tbody tr.item[id]').hover(
		function() {
			var itemNode = this.nodeName.toLowerCase();
			
			var itemRow = pSetup.itemArray[ jQuery(this).attr('id').replace('item-', '') ];
			if ( !itemRow ) {
				return;
			}
			
			// Provincie
			if ( itemNode == 'tr' && itemRow.prv ) {
				jQuery('td.loc', this).css( { backgroundImage: 'url(/images/provincie/'+itemRow.prv.lnd+'-'+itemRow.prv.lnk+'.png)' } );
			}			
			
			// Infobox
			jQuery('#infobox').stop().hide().height('auto').css('opacity', 1).width( ( itemNode == 'tr' ? 400 : jQuery(this).outerWidth() + 1 ) );
			jQuery('#infobox .content').html( getItemHTML( itemRow, this ) );
			jQuery('#infobox').attr('rel', '{"top": '+jQuery(this).offset().top+', "left": '+jQuery(this).offset().left+', "width": '+jQuery(this).outerWidth()+', "height": '+jQuery(this).outerHeight()+'}');
			
			// Foto eerst laden
			if ( itemRow.ft && typeof itemRow.ftload == "undefined" ) {
				itemRow.ftload = true;
				jQuery('#infobox .content .foto img').aeImageResize( { height: 140, width: jQuery(this).outerWidth() - 40 } );
				jQuery('<img rel="'+itemNode+'" />').load( function() {
					toonInfobox( ( jQuery(this).attr('rel') == 'tr' ? 'verticaal' : 'cirkel' ) );
				} ).attr('src', itemRow.ft);
			} else {
				toonInfobox( ( itemNode == 'tr' ? 'verticaal' : 'cirkel' ) );
			}
		},
		function() {
			jQuery('#infobox').attr('rel','').stop().hide();
			jQuery('td.loc', this).css( { backgroundImage: 'none' } );
		}
	).find('a').click( function() {
		jQuery('#infobox').attr('rel','').stop().hide();
	} );
	
	// Foto verkleinen
	jQuery('.item .ft img').aeImageResize( { height: 70, width: 70 } );
}

/**
 * HTML genereren voor de infobox.
 */
function getItemHTML( itemRow, ele ) {
	var html = '';
	
	// Foto
	if ( itemRow.ft ) {
		html += '<div class="foto" style="height: 140px;"><img src="'+itemRow.ft+'" height="140" style="height: 140px;" /></div>';
	}
	
	// Omschrijving
	if ( itemRow.omschr ) {
		html += '<p>'+itemRow.omschr+'</p>';
	}
	
	html += '<table><tr>';
	html += '<td><ul>';
	
	// Prijs
	if ( jQuery('.pr', ele).length ) {
		html += '<li><div>'+$_t( 'Tekst.PRIJS', 'Prijs' )+'</div>'+( jQuery('.pr', ele).text().length ? jQuery('.pr', ele).text() : '&nbsp;' )+'</li>';
	}
	
	// Plaats
	if ( jQuery('.loc', ele).length ) {
		var afstand = jQuery('.loc span', ele).text();
		html += '<li><div>'+$_t( 'Tekst.PLAATS', 'Plaats' )+'</div>'+jQuery('.loc', ele).html().replace('<span>'+afstand+'</span>', '')+'</li>';
		if ( afstand.length ) {
			html += '<li><div>'+$_t( 'Tekst.AFSTAND', 'Afstand' )+'</div>'+afstand+'</li>';	
		}
	}
	
	// Provincie
	if ( itemRow.prv ) {
		html += '<li><div>'+$_t( 'Tekst.PROVINCIE', 'Provincie' )+'</div>'+itemRow.prv.nm+'</li>';
	}
	
	// Datum
	if ( itemRow.dt ) {
		html += '<li><div>'+$_t( 'Tekst.GEPLAATST', 'Geplaatst' )+'</div>'+itemRow.dt+'</li>';
	}
	
	// Extra informatie
	if ( itemRow.xtra ) {
		for ( veld in itemRow.xtra ) {
			html += '<li><div>'+veld+'</div>'+itemRow.xtra[ veld ]+'</li>';
		}
	}
	
	// Bron
	if ( bronMatch = jQuery(ele).find('h3 a').attr('class').match( bronExp ) ) {
		html += '<li><div>'+$_t( 'Tekst.BRON', 'Bron' )+'</div>'+pSetup.siteArray[ bronMatch[1] ]+'</li>';
	}
	
	html += '</ul>';
	html += '</td>';
	
	// Provincie
	if ( itemRow.prv ) {
		html += '<td class="provincie"><img src="/images/provincie/'+itemRow.prv.lnd+'-'+itemRow.prv.lnk+'.png" alt="'+itemRow.prv.naam+'" /></td>';
	}
	
	html += '</tr></table>';
	
	return html;
}

/**
 * Informatiebox tonen
 */
function toonInfobox( kompas ) {
	if ( !jQuery('#infobox').length || !jQuery('#infobox').attr('rel').length ) {
		return;
	}
	
	var itemSet = jQuery.parseJSON( jQuery('#infobox').attr('rel') );
	var docSet  = { top: jQuery(document).scrollTop(), left: jQuery(document).scrollLeft(), width: jQuery(window).width(), height: jQuery(window).height() };
	var boxSet  = { width: jQuery('#infobox').width(), height: jQuery('#infobox').height() };
	var docOverflow = { noord: false, oost: false, west: false, zuid: false };
	var cssClassV = cssClassH = '';
	
	// Overflow: verticaal
	if ( kompas == 'verticaal' ) {
		// Noord
		if ( itemSet.top - boxSet.height < docSet.top ) {
			docOverflow.noord = true;
		}
		
		// Zuid
		if ( itemSet.top + itemSet.height + boxSet.height > docSet.top + docSet.height ) {
			docOverflow.zuid = true;
		}
	}
	
	// Overflow: cirkel
	else {
		// Noord
		if ( itemSet.top + ( itemSet.height / 2 ) - ( boxSet.height / 2 ) < docSet.top ) {
			docOverflow.noord = true;
		}
		
		// Oost
		if ( itemSet.left + itemSet.width + boxSet.width > docSet.left + docSet.width ) {
			docOverflow.oost = true;
		}
		
		// Zuid
		if ( itemSet.top + ( itemSet.height / 2 ) + ( boxSet.height / 2 ) > docSet.top + docSet.height ) {
			docOverflow.zuid = true;
		}
		
		// West
		if ( itemSet.left - boxSet.width < docSet.left ) {
			docOverflow.west = true;
		}
	}
	
	// CSS: verticaal
	if ( kompas == 'verticaal' ) {
		if ( docOverflow.noord ) {
			cssClassV = 'n';
			jQuery('#infobox').css( { top: ( itemSet.top + itemSet.height - 6 )+'px' } );
		} else {
			cssClassV = 'z';
			jQuery('#infobox').css( { top: ( itemSet.top - boxSet.height + 5 )+'px' } );
		}
		jQuery('#infobox').css( { left: ( itemSet.left + ( itemSet.width / 2 ) - ( boxSet.width / 2 ) )+'px' } );
	}
	
	// CSS: cirkel
	else {
		if ( docOverflow.noord ) {
			cssClassV = 'n';
			jQuery('#infobox').css( { top: ( itemSet.top + ( itemSet.height / 2 ) + 4 )+'px' } );
		} else if ( docOverflow.zuid ) {
			cssClassV = 'z';
			jQuery('#infobox').css( { top: ( itemSet.top + ( itemSet.height / 2 ) - boxSet.height )+'px' } );
		} else {
			jQuery('#infobox').css( { top: ( itemSet.top + ( itemSet.height / 2 ) - ( boxSet.height / 2 ) + 4 )+'px' } );
		}
	
		if ( !docOverflow.oost ) {
			cssClassH = 'w';
			jQuery('#infobox').css( { left: ( itemSet.left + itemSet.width - 11 )+'px' } );
		} else {
			cssClassH = 'o';
			jQuery('#infobox').css( { left: ( itemSet.left - boxSet.width )+'px' } );
		}
	}
	
	jQuery('#infobox').attr('class', cssClassV+cssClassH ).delay( 400 ).fadeIn( 'fast' );
}

/**
 * Infobox
 */
function initInfobox() {
	jQuery('body').append( '<div id="infobox"><div class="content"></div></div>' );
	jQuery('#infobox').hide();
}

/**
 * Formulier invoervelden met commentaar
 */
function initFormCommentaar() {
	jQuery('form input:text[title], form textarea[title]').each( function() {
		jQuery(this).attr('alt', jQuery(this).attr('title')).attr('title', '');
	} ).blur( function() {
		if ( jQuery(this).val() == '' ) {
			jQuery(this).val( jQuery(this).attr('alt') ).addClass('nonactief');
		}
	} ).bind('click focus clear', function() {
		if ( jQuery(this).val() == jQuery(this).attr('alt') ) {
			jQuery(this).removeClass('nonactief').val('');
		}
	} ).trigger('blur');
	
	jQuery('form input:text[alt], form textarea[alt]').parents('form').submit( function() {
		jQuery('input.nonactief, textarea.nonactief', this).val('');
	} );
}

/**
 * Zoekformulier
 */
function initZoekformulier() {
	jQuery('#az-zoek .eenvoudig a').click( function() {
		if ( jQuery('#az-zoek .uitgebreid').is(':visible') ) {
			jQuery('#q').focus();
			jQuery('#az-zoek #uz').val(0);
			jQuery('#az-zoek .uitgebreid').slideUp('normal', function() { jQuery('#az-zoek .eenvoudig').removeClass('uitgeklapt'); } );
		} else {
			jQuery('#az-zoek .eenvoudig').addClass('uitgeklapt');
			jQuery('#az-zoek .uitgebreid').slideDown('normal', function() { jQuery('#az-zoek #uz').val(1); jQuery('#rbr').focus(); });
		}
		return false;
	} );
	
	// Provincie of postcode
	jQuery('#prv').focus( function() {
		jQuery('#pc').attr( 'rel', jQuery('#pc').val() ).val('');
	} );
	jQuery('#pc').focus( function() {
		if ( !jQuery(this).val().length ) {
			jQuery(this).val( jQuery(this).attr('rel') );
		}
		jQuery('#prv option:eq(0)').attr('selected', 'selected');
	} ).blur( function() {
		jQuery('#pc').attr( 'rel', jQuery('#pc').val() );
	} );
}

/**
 * CSS 'eerste'-klasse
 */
function setLijstEerste( eleLijst ) {
	jQuery('tr', eleLijst).removeClass('eerste');
	jQuery('tr:visible:eq(0)', eleLijst).addClass('eerste');
}

/**
 * Mededeling
 */
function initMededeling() {
	jQuery('#mededeling').hide().slideDown('normal', function() {
		jQuery(this).delay( 3500 ).fadeOut();
	} );
}
function toonMededeling( tekst, vast ) {
	jQuery('#mededeling').remove();
	
	jQuery('#pagina').append('<div id="mededeling" style="display: none;"><div class="container">'+tekst+'<\/div><\/div>');
	if ( typeof vast != "undefined" && vast === true ) {
		jQuery('#mededeling').slideDown('normal');
	} else {
		initMededeling();
	}
}
function setMededeling( tekst ) {
	if ( jQuery('#mededeling').length == 0 ) {
		toonMededeling( tekst, true );
	} else {
		jQuery('#mededeling .container').html( tekst );
	}
}
