$.selector = {
	firstClick : true,
	previousSelect: false,
	triggered:false,
	init:function() {
		$("#resetsearchfilter").bind('click',function(){
			$('#loadingpic').fadeIn(200);
			$.selector.disableSelectBoxes();
			$.post('/fileadmin/scripts/getcategories.php',{reset:1},function(data){
			$('#loadingpic').fadeOut(200,function(){
					$('#customfields').html(data);
					$.selector.enableSelectBoxes();
				});
			})
			
		return false;
		})
		$('#customfields select').live('change',function(e){
			//first fetch the id's of the selected categories
			selectedCategories = $.selector.fetchCategories();
			categoryNames = $.selector.fetchCategoryNames();
			selectboxName = $(this).attr('id');
			
			if($.selector.previousSelect==false) {
				$.selector.previousSelect = selectboxName;
			}
			
			if($.selector.previousSelect == selectboxName && !$.selector.triggered)  {
				$.selector.firstClick = true;
			} else {
				$.selector.firstClick = false;
				$.selector.triggered = true;
			}
			
			//we have to store the current values of the last used selectbox because we don;'t want to change it's content
			currentSelectboxContent = new Array();
			
			$.each(this,function(i,selected){
				currentSelectboxContent[i] = $(this).val();
				}) ;
			
			
			//perform an ajax request
			//data contains the returned value from the ajax request
			$('#loadingpic').fadeIn(200);
			$.selector.disableSelectBoxes();
			$.post('/fileadmin/scripts/getcategories.php',{currentValues:currentSelectboxContent,firstClick:$.selector.firstClick,categories:selectedCategories,name:selectboxName,activeSelectbox:$(this).attr('id')},function(data){
				$.selector.firstClick = false;
				$('#loadingpic').fadeOut(200,function(){
					$('#customfields').html(data);
					$.selector.enableSelectBoxes();
				});
			})
		})
	},
	
	fetchCategories: function ()
	{
		var selectedCategories= new Array();
		//get the values from all three selectboxes
		$('.category-select').each(function(i,selected){
			selectedCategories.push($(selected).val());
		})
		
		return selectedCategories;
	},
	
	fetchCategoryNames: function()
	{
		var categoryNames = new Array();
		//get the values from all three selectboxes
		$('.category-select').each(function(e){
			categoryNames.push($(this).attr('id'));
		})
		return categoryNames;
	},
	
	disableSelectBoxes: function()
	{
		$('.category-select').each(function(e){
			$(this).attr('disabled','disabled');
		})
	},
	enableSelectBoxes: function()
	{
		$('.category-select').each(function(e){
			$(this).attr('disabled',false);
		})
	}
}

$(document).ready(function() {
	
    $.selector.init();
    //first fetch the id's of the selected categories
	selectedCategories = $.selector.fetchCategories();
	categoryNames = $.selector.fetchCategoryNames();
	selectboxName = $(this).attr('id');
	
	//perform an ajax request
	//data contains the returned value from the ajax request
	$('#loadingpic').fadeIn(200);
	$.selector.disableSelectBoxes();
	$.post('/fileadmin/scripts/getcategories.php',{categories:selectedCategories,name:selectboxName,activeSelectbox:$(this).attr('id')},function(data){
	
		$('#loadingpic').fadeOut(200,function(){
			$('#customfields').html(data);
			$.selector.enableSelectBoxes();
		});
	})
	
});
