$(document).ready( 
	function()
	{
		
		$("#producenciTab").click(function() {
			
			$("#producenciTab").removeClass("producenciInactive").addClass("producenciActive");
			$("#asortymentTab").removeClass("asortymentActive").addClass("asortymentInactive");
			
			
			$("#asortymentContainer").fadeOut('slow', function() {
				$("#producenciContainer").fadeIn('slow');
			});
			
			
		});
		
		$("#asortymentTab").click(function() {
			
			$("#producenciTab").removeClass("producenciActive").addClass("producenciInactive");
			$("#asortymentTab").removeClass("asortymentInactive").addClass("asortymentActive");
			
			$("#producenciContainer").fadeOut('slow', function() {
				$("#asortymentContainer").fadeIn('slow');
			});
			
			
		});
		
		$("#categorySelectBox-1").change(function() {
			
			myChange(1, this.value);
			
			return false;
			
		}
		);
		
		function myChange(level, categoryId) {
			var currentLevel = level + 1;
			$.getJSON("/category/getsubcategoriesajax", { categoryId: categoryId, shopId: $("#categoryId").val(), languageId: $("#lanugageId").val()},
					
					function(data) {
						
						if (data.length > 0) {
							
							var selectBox = '';
							selectBox += '<select class="advancedSearchSelect" id="categorySelectBox-' + currentLevel + '" name="categories[]">\n';
							selectBox += '<option value="0">--wszystkie--</option>\n';
							
							for (i in data) {
								
								option = '<option value="' + data[i].id + '">' + data[i].extcategories[0].name + '</option>\n';
								selectBox += option + "\n";
								
							}
							selectBox += '</select>\n';
							
							$("#category-" + currentLevel).html(selectBox);
							for (i=currentLevel + 1;i<6;i++) {
								$("#category-" + i).html('');
							}
							$("#traits").html('');
							$("#manufacturers").html('');
							$("#series").html('');
							
							
						} else {
							
							for (i=currentLevel;i<6;i++) {
								$("#category-" + i).html('');
							}
							
							
							if (categoryId != 0) {
								
								selectOthers(categoryId);
								
							} else {
								$("#traits").html('');
								$("#manufacturers").html('');
								$("#series").html('');
							}
						
							
						
						}
						
						$("#categorySelectBox-" + currentLevel).change(function() {
							myChange(currentLevel, this.value);
						});
						
						
					});
				
				
				return false;
		}
		
		
		function selectOthers(categoryId) {
			//wybierz cechy
			$.getJSON("/search/gettraitsajax", { categoryId: categoryId, shopId: $("#categoryId").val(), languageId: $("#lanugageId").val()},function(data) {
				if (data.length > 0) {
					var selectBoxes = '';
					for (i in data) {
						
						var selectBox = '<div style="clear: both;">';
						selectBox += '<div class="advancedSearchLabel">';
						selectBox += data[i].name;
						selectBox += '</div>';
						selectBox += '<div style="float: left;">';
						selectBox += '<select class="advancedSearchSelect" id="trait-' + data[i].id + '" name="traits[' + data[i].id + ']">\n';
						selectBox += '<option value="0">--wszystkie--</option>\n';
						for (j in data[i].values) {
							option = '<option value="' + data[i].values[j].id + '">' + data[i].values[j].name + '</option>\n';
							selectBox += option + "\n";
						}
						
						
						selectBox += '</select>\n';
						selectBox += '</div></div>';
						selectBoxes += selectBox;
					}
					
					
					$("#traits").html(selectBoxes);
					
				} else {
					$("#traits").html("");
				}
					
			});
			
			//wybierz producenta
			
			$.getJSON("/search/getmanufacturersajax", { categoryId: categoryId, shopId: $("#categoryId").val(), languageId: $("#lanugageId").val()},function(data) {
				
				if (data.length > 0) {
					
					
					var selectBox = '<div style="clear: both;">';
					selectBox += '<div class="advancedSearchLabel">producent</div>';
					selectBox += '<div style="float: left;">';
					
					selectBox += '<select class="advancedSearchSelect" id="manufacturer" name="manufacturer">\n';
					selectBox += '<option value="0">--wszyscy--</option>\n';
					
					for (i in data) {
						option = '<option value="' + data[i].id + '">' + data[i].extmanufacturers[0].name + '</option>\n';
						selectBox += option + "\n";
					}
					
					selectBox += '</select></div></div>\n';
					
					
					$("#manufacturers").html(selectBox);
					$("#series").html("");
					
					selectBox = '';
					
					$('#manufacturer').change(function() {
							
						manufacturerId = this.value;
							$.getJSON("/search/getseriesajax", { categoryId: categoryId, shopId: $("#categoryId").val(), languageId: $("#lanugageId").val(), manufacturerId: manufacturerId},function(data) {
								
								if (data.length > 0) {
									
									var selectBox = '<div style="clear: both;">';
									selectBox += '<div class="advancedSearchLabel">seria</div>';
									selectBox += '<div style="float: left;">';
									
									selectBox += '<select class="advancedSearchSelect" id="serie" name="collection">\n';
									selectBox += '<option value="0">--wszystkie--</option>\n';
									
									for (i in data) {
										option = '<option value="' + data[i].id + '">' + data[i].name + '</option>\n';
										selectBox += option + "\n";
									}
									
									selectBox += '</select></div></div>\n';
									
									$("#series").html(selectBox);
									
								} else {
									$("#series").html("");
								}
									
							});
					});
					
					
								
					
					
				} else {
					$("#manufacturers").html("");
					$("#series").html("");
				}
				
				
				
					
			});
			
			//wybierz serie
			
		
			
		}
		
     }
);  