var load	= 0;


function filter(data) {
	
	$('#keyword option').each(function() {
		
		
		if ( -1 == jQuery.inArray($(this).val(), data.keyword) ) {
			$(this).attr('disabled', 'disabled');
			$(this).css('color', '#ddd');
		}

	});
	 
	$('#source option').each(function() {
		
		if ( -1 == jQuery.inArray($(this).val(), data.source) ) {
			$(this).attr('disabled', 'disabled');
			$(this).css('color', '#ddd');
		}
	});
	
	$('#time option').each(function() {
		
		if ( -1 == jQuery.inArray($(this).val(), data.time) ) {
			$(this).attr('disabled', 'disabled');
			$(this).css('color', '#ddd');
		}
	});
	
}



$(document).ready(function() {

	// singleton (for some reason js is included twice)
	// don't want to look into it right now
	if ( load == 1 )
	return false;
	
	load = 1;
	
	
	// reset all
	$('.resetall').click(function() {
		
		$('#keyword option').each(function() {
			$(this).attr('disabled', false);
			$(this).attr('selected', false);
			$(this).css('color', '#000');
		});
		
		$('#source option').each(function() {
			$(this).attr('disabled', false);
			$(this).attr('selected', false);
			$(this).css('color', '#000');
		});
		
		$('#time option').each(function() {
			$(this).attr('disabled', false);
			$(this).attr('selected', false);
			$(this).css('color', '#000');
		});
		
	});
	
	
	
	
	// reset anchors
	$('#resetkeyword').click(function() {
		
		$('#keyword option').each(function() {
			$(this).attr('disabled', false);
			$(this).attr('selected', false);
			$(this).css('color', '#000');
		});
		
	});
	
	$('#resetsource').click(function() {
		
		$('#source option').each(function() {
			$(this).attr('disabled', false);
			$(this).attr('selected', false);
			$(this).css('color', '#000');
		});
		
	});
	
	$('#resettime').click(function() {
		
		$('#time option').each(function() {
			$(this).attr('disabled', false);
			$(this).attr('selected', false);
			$(this).css('color', '#000');
		});
		
	});
	
	
	
	
	// filter keyword
	$('#keyword').click(function() {
		
		var keyword = '';

		$('#keyword option:selected').each(function() {
			keyword += '&keyword[]=' + $(this).val();
		});
		
		var url = '/kildesearch/_small2/ajax.php?'+keyword;

		$.getJSON(url, function(data){
			
			filter(data);
			
		});
		
	});	
	
	// filter source
	$('#source').click(function() {
		
		var source = '';
		
		$('#source option:selected').each(function() {
			source += '&source[]=' + $(this).val() ;
		});
		
		var url = '/kildesearch/_small2/ajax.php?'+source;
		
		$.getJSON(url, function(data){
			
			filter(data);
			
		});
		
	});	

	// filter time
	$('#time').click(function() {
		
		var time = '';
		
		$('#time option:selected').each(function() {
			time += '&time[]=' + $(this).val() ;
		});
		
		var url = '/kildesearch/_small2/ajax.php?'+time;
		
		$.getJSON(url, function(data){
			
			filter(data);
			
		});
		
	});	

	
	
	
	
	
	$('#search').click(function() {
		
			var rows = $('#display tr').length;
			for ( i = 0; i < rows; i++ ) {
				document.getElementById('display').deleteRow(0);
			}

			var term = $('#term').val();
			url = '/kildesearch/_small2/ajax.php?&term=' + term;
				   
			if ( $('#case:checked').length )
			url += '&case=on';

			$('#keyword option:selected').each(function() {
				url += '&keyword[]='+$(this).val();
			});

			$('#source option:selected').each(function() {
				url += '&source[]='+$(this).val();
			});

			$('#time option:selected').each(function() {
				url += '&time[]='+$(this).val();
			});
			
			
			
			$.getJSON(url, function(data){
				
				var tr 	= document.getElementById('display').insertRow(0);
				var one	= tr.insertCell(0);
				var two	= tr.insertCell(1);
				one.innerHTML = 'Antal resultater:';
				two.innerHTML = data.search.length;

				var tr 	= document.getElementById('display').insertRow(1);
				var one	= tr.insertCell(0);
				var two	= tr.insertCell(1);
				one.innerHTML = 'Navn:';
				two.innerHTML = 'Titel';
				
				$.each(data.search, function(k,v) {

					var tr 	= document.getElementById('display').insertRow(k+2);
		    		var one	= tr.insertCell(0);
		    		var two	= tr.insertCell(1);

		    		one.innerHTML = '<a rel="shadowbox;width=760" href="http://www.kvinfo.dk/kilde.php?kilde='+v.id+'" title="'+v.name+'">'+v.name+'</a> ';
		    		two.innerHTML = '<a rel="shadowbox;width=760" href="http://www.kvinfo.dk/kilde.php?kilde='+v.id+'" title="'+v.name+'">'+v.title+'</a> ';;

		    	});

				Shadowbox.init();

			});

			
		});


		rgx = /\/1056\/$/;
		if ( !rgx.test(location.pathname) )
		Shadowbox.init();

	
	
	
	

});

