function toggleHerb(locationProductId) {
	var raw = document.getElementById('raw'+locationProductId);
	var select = document.getElementById('select'+locationProductId);
	var hidden = document.getElementById('products'+locationProductId);
	
	if (hidden.value != '') {
		select.style.display = 'none';
		raw.style.display = 'block';
		hidden.value = '';
	} else {
		raw.style.display = 'none';
		select.style.display = 'block';
		hidden.value = locationProductId;
	}
	return false;
}

function doSearchSubmit() {
	document.getElementById('searchForm').submit();
	return false;
}

function rolloverHerb(locationProductId) {
	// only roll over for non selected herb
	var raw = document.getElementById('raw'+locationProductId);
	var select = document.getElementById('select'+locationProductId);
	var hidden = document.getElementById('products'+locationProductId);
	
	if (hidden.value == '') {
		raw.style.display = 'none';
		select.style.display = 'block';
	}
}

function rolloffHerb(locationProductId) {
	// only roll off for non selected herb
	var raw = document.getElementById('raw'+locationProductId);
	var select = document.getElementById('select'+locationProductId);
	var hidden = document.getElementById('products'+locationProductId);

	if (hidden.value == '') {
		select.style.display = 'none';
		raw.style.display = 'block';
	}	
}

rules = ''; //make it global
$(document).ready(function(){

	$('.searchButton').click(function() {
		doSearchSubmit();
	});
		
	$('.clearButton').click(function() {
		resetForm();
	});
	
	

	$('.tipsButton').click(function() {
		$('.tip').slideDown();
	});
	
	$('.tipClose').click(function() {
		$('.tip').slideUp();
	});
	
	$('.refine, #refine').click(function() {
		$('.resultBelow').css('height','2px');
		$('.refineindex').slideDown();
	});
	
	$('.refineClose').click(function() {
		$('.resultBelow').css('height','1px');
		$('.refineindex').slideUp();
	});
	
		
	$.ajax({
		type: "POST",
		url: '/recipe_exclusions/exclude',
		dataType: 'json',
		success: function(returned){	
			rules = returned;
	  	}
	});
	
	
	function resetForm() {
		$('#searchForm :text').val('');
		$('#searchForm :checkbox').removeAttr('checked');
		
		$('#searchForm select').each(
    		function() {
        		$(this).val($("#"+$(this).attr('id')+" option:first").val());
    		}
		);
		
		$('#searchForm .productSelector input').each(
    		function() {
    			if($(this).val() != "") {
    				$("#"+$(this).attr('id').replace("products", "raw")).show();
    				$("#"+$(this).attr('id').replace("products", "select")).hide();
        		}
    		}
		);
		

		resetExceptions();
	}
	
	
	function excludeInput(field,option,rule) {	
		$('#Search'+field+'Id'+option).removeAttr('checked');
		$('#Search'+field+'Id'+option).attr('disabled','disabled');
		$('#Search'+field+'Id option[value="'+option+'"]').remove();
	}
	
	function resetExceptions() {
		$('#searchForm input').removeAttr('disabled');
		
		
		SearchLocaleCuisineId = $('#SearchLocaleCuisineId option:selected').val();
		SearchLocaleMealTypeId = $('#SearchLocaleMealTypeId option:selected').val();  
		
		$('#SearchLocaleCuisineId option').remove();
		$('#SearchLocaleMealTypeId option').remove();
		
		$.each($('#SearchLocaleCuisineIdOrg option'), function() {
			$('#SearchLocaleCuisineId').append('<option value=\''+$(this).val()+'\'>'+$(this).html()+'</option>');
		});
		
		$.each($('#SearchLocaleMealTypeIdOrg option'), function() {
			$('#SearchLocaleMealTypeId').append('<option value=\''+$(this).val()+'\'>'+$(this).html()+'</option>');
		});
		
		 $('#SearchLocaleCuisineId').val(SearchLocaleCuisineId);
		 $('#SearchLocaleMealTypeId').val(SearchLocaleMealTypeId);
	}
	
	$('#searchForm input, #searchForm select')
		.change(function () {
			resetExceptions();
					
			$.each(rules, function(initiatorType,ruleItems) {
				$.each(ruleItems, function(initiatorValue,ruleItem) {
					$.each(ruleItem, function(i,excludedItem) {
						
						if(($('#Search'+initiatorType+'Id option:selected').val() == initiatorValue)) {
							excludeInput(excludedItem.excluded_type,excludedItem.excluded_id,excludedItem.recipe_exclusion_id);							
						}
						
						if($('#Search'+initiatorType+'Id'+initiatorValue).attr('checked') == 'checked') {
							excludeInput(excludedItem.excluded_type,excludedItem.excluded_id,excludedItem.recipe_exclusion_id);
						}
						
					});
				});
	   		});
		}
	);
	
	$('#searchForm input, #searchForm select').keypress(function(e) {
    	if(e.which == 13) {
        	doSearchSubmit();
    	}
	});

});
