function clearOptions(name)
{
	if(name == null) return false;
	var element = $j('<option />').attr('value', '-1');
	$j('#' + name + ' option').each(function(option) {
		if($j(this).attr('value') == '-1')
		{
			element.text($j(this).text());
		}
	});
	$j('#' + name).html(element);
}

function getCategories(idAlbum)
{
	$j('#rodzaj').attr("disabled", true);
	var active_option = $j('#rodzaj [selected]').val();
	if(idAlbum == null) {
		idAlbum = $j('#gatunek').val();	
	}
	if(idAlbum < 0) {
		clearOptions('rodzaj');
		return false;
	} else {
		$j.ajax({
			  url: '/index/ajax/id/' + idAlbum,
			  dataType: 'json',
			  success: function(options) {
				clearOptions('rodzaj');
			  	for(var option in options){
				  	element = $j('<option />');
				  	element.attr('value', option);
				  	element.text(options[option]);
				  	if(option == active_option) element.attr('selected', true);
				  	$j('#rodzaj').append(element);
				}			
			  	$j('#rodzaj').removeAttr("disabled"); 		
			  }
		});
	}				
}
$j(document).ready(function()
{
	$j('#gatunek').change(function() {
		getCategories($j(this).val());
	});
	getCategories();
});
