function showFields(id)
{
	$('#product_list').empty();
	if (id != 'special_needs') {
		$('#nutritional_form div').hide();
		$('#field_size').show();
		switch (id) {
			case 'size':
				$('#field_age_'+$('#size').val()).show();
				break;
			case 'age_mini':
				$('#field_age_1').show();
				if($('#age_mini').val() > 2){
					$('#field_breed_1').show();
				}
				break;
			case 'age_medium':
				$('#field_age_2').show();
				if($('#age_medium').val() > 2){
					$('#field_breed_2').show();
				}
				break;
			case 'age_maxi':
				$('#field_age_3').show();
				if($('#age_maxi').val() > 2){
					$('#field_breed_3').show();
				}
				break;
			case 'age_giant':
				$('#field_age_4').show();
				if($('#age_giant').val() > 3){
					$('#field_breed_4').show();
				}
				break;
			case 'breed_1':
				$('#field_age_1').show();
				$('#field_breed_1').show();
				if ($('#breed_1').val() == 99 && $('#age_mini').val() > 3){
					$('#field_special_needs').show();
				}
				break;
			case 'breed_2':
				$('#field_age_2').show();
				$('#field_breed_2').show();
				if ($('#breed_2').val() == 99 && $('#age_medium').val() > 3){
					$('#field_special_needs').show();
				}
				break;
			case 'breed_3':
				$('#field_age_3').show();
				$('#field_breed_3').show();
				if ($('#breed_3').val() == 99 && $('#age_maxi').val() > 3){
					$('#field_special_needs').show();
				}
				break;
			case 'breed_4':
				$('#field_age_4').show();
				$('#field_breed_4').show();
				break;
		}
		resetFields();
	}
	$('#field_buttons').show();
}

function resetFields()
{
	$('#nutritional_search_form div:hidden input:radio:checked').each(function(){
		$(this).attr('checked', false);
	});
	$('#nutritional_search_form fieldset:hidden input:radio:checked').each(function(){
		$(this).attr('checked', false);
	});
	$('#nutritional_search_form div:hidden select').each(function(){
		$(this).val(0);
	});
	$('#nutritional_search_form fieldset:hidden select').each(function(){
		$(this).val(0);
	});
}

function getProducts()
{
	if ($('#background_clon').length > 0) {
		showBackground();
	}
	$('#product_list').empty();
	var string = '{';
	$('#nutritional_search_form div:visible input:radio:checked,#nutritional_search_form div:visible select').each(function() {
		string += $(this).attr('name') + ':\'' + $(this).attr('value') + '\',';
	});
	string += 'pet_type:\'1\'}';
	if ($('#ns_loading').length == 0) {
		createLoading();
	}
	$('#ns_loading').show();
	eval("$.get('"+BASE_PATH+"modules/_nutritional_search.php', " + string + ", function(data){createProducts(data)});");

}

function createProducts(data)
{
	$('#ns_loading').hide();
	var html = '';
	var products = eval(data);
	if (products) {
		if ($('#background_clon').length == 0) {
			cloneBackground();
		}
		$('#contents .box').css('background', 'none');
		$('#product_list').append('<h2>Resultados</h2><p class="result">Se han encontrado <strong>'+products.length+'</strong> productos</p>');
		for (var i = 0; i<products.length; i++) {
			html = '<div class="product">';
			html += '<div class="product_picture" onclick="window.open(\''+products[i]['url']+'\');"><img src="'+products[i]['picture']+'" /></div>';
			html += '<div class="product_introduction"><h3><a href="'+products[i]['url']+'" onclick="window.open(this.href);return false;">'+products[i]['product']+'</a></h3><p>'+products[i]['introduction']+'</p>';
			html += '<a href="'+products[i]['url']+'">Ficha del producto</a></div></div>';
			$('#product_list').append(html);
		}
	} else {
		$('#product_list').append('<h2>Resultados</h2><p class="result">No hay productos que listar</p>');
	}
}

function createLoading()
{
	$('#contents').append($('#loading_template').clone().attr('id', 'ns_loading'));
}

function cloneBackground()
{
//	var cloneDiv = $('#contents div.box').clone();
// 	cloneDiv.attr('id', 'background_clon');
//	cloneDiv.css('display', 'none');
//	cloneDiv.css('background-image', $('#contents .box').css('background-image'));
//	cloneDiv.css('background-attachment', $('#contents .box').css('background-attachment'));
//	cloneDiv.css('background-position', $('#contents .box').css('background-position'));
//	cloneDiv.css('background-repeat', $('#contents .box').css('background-repeat'));
//	$('body').append(cloneDiv);
}

function showBackground()
{
	$('#contents .box').css('background-image', $('#background_clon').css('background-image'))
			.css('background-attachment', $('#background_clon').css('background-attachment'))
			.css('background-position', $('#background_clon').css('background-position'))
			.css('background-repeat', $('#background_clon').css('background-repeat'));
}
