function showFields(id)
{
	$('#product_list').empty();
	$('#nutritional_form div').hide();
	$('#nutritional_form fieldset').hide();
	$('#field_age').show();
	switch (id) {
		case 'age':
			if ($('#age').val() == "3") {
				$('#field_overweight').show();
			} else if($('#age').val() == "2") {
				$('#field_breed').show();
			} else if($('#age').val() == "4") {
				$('#field_outdoor').show();
			}
			break;
		case 'overweight_label1':
		case 'overweight_label2':
			$('#field_overweight').show();
			if ($('#overweight1').attr('checked') == true) {
				$('#field_breed').show();
			}
			break;
		case 'breed':
			$('#field_breed').show();
			if ($('#overweight1').attr('checked') == true) {
				$('#field_overweight').show();
				if ($('#breed').val() == 99) {
					$('#field_outdoor').show();
				}
			} 
			break;
		case 'outdoor_label1':
		case 'outdoor_label2':
			$('#field_outdoor').show();
			if ($('#overweight1').attr('checked') == true) {
				$('#field_overweight').show();
				if ($('#breed').val() == 99) {
					$('#field_breed').show();
					if ($('#outdoor2').attr('checked') == true) {
						$('#field_outdoor').show();
						$('#field_specials').show();
					} else if ($('#outdoor1').attr('checked') == true) {
						$('#field_outdoor').show();
						$('#field_hair').show();
					}
				}
			} 
			break;
		case 'hair_label1':
		case 'hair_label2':
			$('#field_hair').show();
			if ($('#overweight1').attr('checked') == true) {
				$('#field_overweight').show();
				if ($('#breed').val() == 99) {
					$('#field_breed').show();
					if ($('#outdoor1').attr('checked') == true) {
						$('#field_outdoor').show();
						$('#field_specials').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 fieldset:visible input:radio:checked,#nutritional_search_form div:visible select').each(function() {
		string += $(this).attr('name') + ':\'' + $(this).attr('value') + '\',';
	});
	string += 'pet_type:\'2\'}';
	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'));
}