$(document).ready(function(){
	
	// set background
	//$(".hoofdcontainer").css("background-image","url('/images/schaduw.png')");

	//Util methods
	
	function findValueCallback(event, data, formatted) {
		$("<li>").html( !data ? "No match!" : "Selected: " + formatted).appendTo("#result");
	};
	
	function formatItem(item) {
		return item[0] + " (<strong>id: " + item[1] + "</strong>)";
	};
	
	function formatResult(item) {
		return item[0].replace(/(<.+?>)/gi, '');
	};
	
	
	//Local Data
	/*
	$('#_TextSingleCity').autocomplete({data: cities, scroll: true});
	$('#_TextSingleCity2').autocomplete({data: cities, matchContains: true, minChars: 0});
	
	$('#_TextMonth').autocomplete({
		data: months,
		minChars: 0,
		max: 12,
		autoFill: true,
		mustMatch: true,
		matchContains: false,
		scrollHeight: 220,
		formatItem: function(item, i, total) {
			// don't show the current month in the list of values (for whatever reason)
			if ( item[0] == months[new Date().getMonth()] ) 
				return false;
			return item[0];
		}
	});	
	
	$('#_TextEmail').autocomplete({
		data: emails,
		minChars: 0,
		width: 310,
		matchContains: true,
		autoFill: false,
		formatItem: function(item, i, total) {
			return i + "/" + total + ": \"" + item.name + "\" [" + item.to + "]";
		},
		formatMatch: function(item, i, total) {
			return item.name + " " + item.to;
		},
		formatResult: function(item) {
			return item.to;
		}
	});	
	
	$('#_TextMultipleCities').autocomplete({
		data: cities,
		multiple: true,
		mustMatch: true,
		autoFill: true
	});	
	
	$('#_TextTags').autocomplete({
		data: ['c++', 'java', 'php', 'coldfusion', 'javascript', 'asp'],
		width: 320,
		max: 4,
		highlight: false,
		multiple: true,
		multipleSeparator: " ",
		scroll: true,
		scrollHeight: 300
	});	

	//Remote Data
	*/
	
	function changeRadius(event, ui)
	{
		var loc = document.location.href;
		loc = loc.replace(/\?page=[0-9]{0,3}&/,'?');
		var radius = '&straal=' + ui.value;
		
		if (loc.indexOf('straal=') != -1){
			loc = loc.replace(/(&straal=)([0-9]){1,3}/,radius);
		}else{
			loc = loc + radius;
		}
		
		document.location.href = loc;
	}
	
	function updateSlideValue(event, ui)
	{
		$("#slider_value").text(''+ ui.value);
	}

	$(document).ready(function()
	 {
	    var loc = document.location.href;
	    var defaultValue = null;
	    
	    if(loc.indexOf('straal=') != -1){
	    	var radius = loc.match(/&straal=[0-9]{1,3}/);
	    	
	    	if(null != radius && radius.length == 1){
	    		values = radius[0].split('=');
	    		defaultValue = values[1];
	    	}
	    }
	    
	    if(null != defaultValue){
	    	$("#slider").slider({ max: 150 ,min:0, step:1, stop:changeRadius,slide:updateSlideValue, value:defaultValue});
	    	$("#slider_value").text(''+ defaultValue);
	    	
	    }else{
	    	$("#slider").slider({ max: 150 ,min:0, step:1, stop:changeRadius,slide:updateSlideValue, value:50});
	    	$("#slider_value").text(''+ 50);
	    }
	    
	    $("#slider_min").text('0');
	    $("#slider_max").text('150');
	  });
	
	$("#aantal").change(function() {
        // selected value
		var src = $("option:selected", this).val();
        
		// accessed url
		var loc = document.location.href;
        
		// & or ?
		var p = '&';
        if (loc.indexOf('?') == -1){
        	p = '?';
        }
        
        // take page off to go back to page 0
        loc = loc.replace(/\?page=[0-9]{0,3}&/,'?');
		
        // init count
        var count =  p + 'aantal=' + src;
		
		// change count or set count
		if (loc.indexOf('aantal=') != -1){
			loc = loc.replace(/(aantal=)([0-9]){1,3}/,count);
		}else{
			loc = loc + count;
		}
		
		document.location.href = loc;
    });

	
	$('#q').autocomplete({
		url: '/autocompleet/functienamen.php',
		width: 456,
		selectFirst: false
	});
	
	$('#locatie').autocomplete({
		url: '/autocompleet/plaatsnamen.php',
		width: 275,
		selectFirst: false
	});
});

function changeOptions(){
	var max = parseInt(window.prompt('Please type number of items to display:', jQuery.Autocompleter.defaults.max));
	if (max > 0) $("#_TextSingleCity").autocomplete('options', {max: max});
};

function changeScrollHeight() {
    var h = parseInt(window.prompt('Please type new scroll height (number in pixels):', jQuery.Autocompleter.defaults.scrollHeight));
    if(h > 0) $("#_TextSingleCity").autocomplete('options', {scrollHeight: h});
};

//function changeToMonths(){
//	$("#_TextSingleCity")
//		// clear existing data
//		.val("")
//		// change the local data to months
//		.('options', {data: months})
//		// get the label tag
//		.prev();
//}