var runLite = 'set';
$.fn.delay = function(time, callback){
	jQuery.fx.step.delay = function(){};
	return this.animate({delay:1}, time, callback);
}

function initAutocomplete() {
	if ( !$('#autosuggestie').length ) {
		return;
	}
	
	if ( $('#autosuggestie').val() == 'plaats' ) {
		$('#form-zoeken').append( '<input type="hidden" id="extra-provincie" name="provincie" /><input type="hidden" id="extra-plaats" name="plaats" />' );
	} else if ( $('#autosuggestie').val() == 'merk' ) {
		$('#form-zoeken').append( '<input type="hidden" id="extra-merk" name="merk" /><input type="hidden" id="extra-model" name="model" />' );
	}
	
	$("#q").autocomplete( '/jx.js', {
		autoFill: false,
		delay: 50,
		closeButton: "Sluiten",
		extraParams: {
			autosuggestie: $('#autosuggestie').val()
		},
		onItemSelect: function(li) {
			if ( $('#autosuggestie').val() == 'plaats' ) {
				var provincieNaam = ( li.extra && li.extra[0] ? li.extra[0] : '' );
				var plaatsNaam = $(li).html();
				$('#form-zoeken #extra-provincie').val( provincieNaam );
				$('#form-zoeken #extra-plaats').val( plaatsNaam );
			} else if ( $('#autosuggestie').val() == 'merk' ) {
				var merkNaam = ( li.extra && li.extra[0] ? li.extra[0] : '' );
				var modelNaam = ( li.extra && li.extra[1] ? li.extra[1] : '' );
				$('#form-zoeken #extra-merk').val( merkNaam );
				$('#form-zoeken #extra-model').val( modelNaam );
			}
			$('#q').focus();
		},
		onChange: function() {
			$('#form-zoeken input[id^=extra-]').val('');
		}
	} );
}

var klkExp = new RegExp('\\[([a-z-0-9]+)\\]');
function initTrackKlik() {
	$('a,img').filter(
		function() { return ( $(this).attr('class').match( klkExp ) ); }
	).click(
		function() { return trackKlik( this ); }
	);
}

function trackKlik( Ele ) {
	var eleNode = ( Ele ? Ele.nodeName.toUpperCase() : false );
	if ( eleNode != 'A' && eleNode != 'IMG' ) {
		return true;
	}
	
	var expMatch = $(Ele).attr('class').match( klkExp );
	$.ajax( { data: { trck: 'klk', id: expMatch[1] } } );
	
	if ( eleNode == 'A' && Ele.onclick ) {
		if ( Ele.onclick != undefined && Ele.onclick.toString().indexOf( 'return ' ) > 0 ) {
			return false;
		}
	}

	if ( eleNode == 'IMG' || ( eleNode == 'A' && Ele.target == '_blank' ) ) {
		return true;
	}
	
	setTimeout( function() { document.location.href = Ele.href; }, 300 );
	return false;
}

function initFilters() {
	$('.filterlijst select, #form-srtr select').bind( 'change keyup', function() {
		$( '#form-'+this.id.replace( 'filter-', '' ).replace( 'min', '' ).replace( 'max', '' ) ).submit();
	} );
	$('select[id$=\'min\'], select[id$=\'max\']', $('#form-zoeken') ).bind( 'change keyup', function() {
		var filterNaam = $(this).attr('id').replace( 'min', '' ).replace( 'max', '' );
		var filterObj = $(this).attr('id').replace( filterNaam, '' );
		var filterMin = $('#'+filterNaam+'min').get(0);
		var filterMinI = filterMin.selectedIndex;
		var filterMax = $('#'+filterNaam+'max').get(0);
		var filterMaxI = filterMax.selectedIndex;
		
		if ( filterMinI && filterMaxI ) {
			if ( parseInt( $('#'+filterNaam+'min option:eq(1)').val(), 0 ) > parseInt( $('#'+filterNaam+'min option:last').val(), 0 ) ) {
				if ( filterObj == 'min' && filterMinI <= filterMaxI ) {
					filterMax.selectedIndex = ( filterMinI - 1 );
				} else if ( filterObj == 'max' && filterMaxI >= filterMinI ) {
					if ( filterMin.length > ( filterMaxI + 1 ) ) {
						filterMin.selectedIndex = ( filterMaxI + 1 );
					}
				}
			} else {
				if ( filterObj == 'min' && filterMinI >= filterMaxI ) {
					if ( filterMax.length > ( filterMinI + 1 ) ) {
						filterMax.selectedIndex = ( filterMinI + 1 );
					}
				} else if ( filterObj == 'max' && filterMaxI <= filterMinI ) {
					filterMin.selectedIndex = ( filterMaxI - 1 );
				}
			}
		}
	} );
}

var aexToon = 'index';
function initActueel() {
	setInterval( function() {
		if ( aexToon == 'index' ) {
			$('#actueelbalk .aex .waarde').fadeOut( "normal", function() {
				$('#actueelbalk .aex .verschil').fadeIn("normal");
			} );
		} else {
			$('#actueelbalk .aex .verschil').fadeOut( "normal", function() {
				$('#actueelbalk .aex .waarde').fadeIn("normal");
			} );
		}
		
		aexToon = ( aexToon == 'index' ? 'verschil' : 'index' );
	}, 6000 );
}

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();
				if ( hoogte > maxHoogte ) {
					maxHoogte = hoogte;
				}
			} ).height( maxHoogte );
		}
	} );
}

function initBox() {
	var borderRadiusAvailable = false;
	try {
		if ( document.body.style.borderRadius !== undefined || document.body.style.MozBorderRadius !== undefined || document.body.style.WebkitBorderRadius !== undefined ) {
			borderRadiusAvailable = true;
		}
	} catch(err) {}

	if ( !borderRadiusAvailable ) {
		( box ? box.init() : false );
	}
}

$(document).ready( function() {
	runLite = ( Math.round( jQuery.browser.version ) <= 6 && jQuery.browser.msie );
	
	$.ajaxSetup( {
		url: "/jx.js",
		global: false,
		type: "GET"
	} );
	
	// Mededelingen
	if ( $(".mededelingen") ) {
		$(".mededelingen").fadeTo( 0, 0 ).fadeTo( 500, 0.95 );
		setTimeout( function() { $(".mededelingen").fadeOut( 500 ); }, 4000 );
	}
	
	// Tracking
	initTrackKlik();
	
	// Filters
	initFilters();
	
	// Actueel
	initActueel();
	
	// Kolomrijen
	initKolomRijen();
	
	if ( !runLite ) {
		// Box
		initBox();
		
		// Autocomplete
		initAutocomplete();
	} else {
		if ( DD_belatedPNG ) {
			DD_belatedPNG.fix( '#logo,#footer,#actueelbalk,#actueelbalk img,#weer-actueel,.weer-icoon,#hoofdtabs a,#hoofdtabs span,.box .bt,.box .bt div,.box .bb,.box .bb div,.box .i1,.box .i2' );
		}
	}
} );
