jQuery(document).ready(
	function($) {
		$('.my-skinnable-select').each(function(i) {
			applySkinnableSelect($,$(this));
		});
		$('select#category').change(function(){
			var cat = $('select#category Option:selected').val();
			$(this).prev().text(cat);
			reloadKeywords($,$(this).parent().parent(), cat);
		});
	}
);

function applySkinnableSelect($,selectContainer) {
	// Remove the class for non JS browsers
	selectContainer.removeClass('my-skinnable-select');
	// Add the class for JS Browers
	selectContainer.addClass('skinned-select');
	// Find the select box
	selectContainer.children().before('<div class="select-text">Geen trefwoorden</div>').each(
		function() {
			$(this).prev().text(this.options[this.selectedIndex].innerHTML);
		}
	);
	// Store the parent object
	var parentTextObj = selectContainer.children().prev();
	// As we click on the options
	selectContainer.children().each(function(){
		$(this).click(function() {
			// Set the value of the html
			var selected = this.options[this.selectedIndex].innerHTML;
			if (parentTextObj.text() != selected) {
				parentTextObj.text(this.options[this.selectedIndex].innerHTML);
				if ($(selectContainer).attr('id') == 'search-category') {
					var cat = $('select#category option:selected').val();
					reloadKeywords($,parentTextObj.parent().parent(), cat);
				}
			}
		});
	});
}

function reloadKeywords($, searchForm, cat) {
	try {
		var results_page = $('.results-page').val();
		searchForm.parent().load(
			'index.php',
			'eID=tx_nkztrdg_eID&rubriek='+cat+'&results_page='+results_page,
			function(){
				$('.my-skinnable-select').each(function(i) {
					applySkinnableSelect($,$(this));
				});
			}
		);
	} catch( ex ) {
		//alert(ex);
	}
}

