Event.observe(window, 'load', function(event) {
	var form = $('advancedSearchForm');

	if (form) {
		refreshCriteria();
	}
});

function showSearchCriteria(criteria) {
	$('search-form-id').addClassName('search-page');
	$('search-form-id').show();
	refreshCriteria(criteria);
}

function refreshCriteria() {
	refreshCriteria('');
}

function refreshCriteria(criteria) {
	var colors = [];
	var habitats = [];
	var regions = [];
	var sizes = [];
	var fruits = [];
	var leafs = [];
	var months = [];
	var wingShapes = [];
	var songOrCallTypes = [];
	var songOrCallPatterns = [];
	var locomotions = [];
	
	var text = $('textCriteriaId');
	var select = $('categoryId');
	var subCategorySelect = $('subCategoryId');
	var criteriaDiv = $('additionalCriteria');
	
	if (criteria) {
		criteria = eval(criteria);
		
		if (text && criteria.textCriteria) {
			text.value = criteria.textCriteria;
		}
		
		if (typeof(criteria.colors) == 'string') {
			colors = new Array(criteria.colors);
		} else {
			colors = criteria.colors;
		}
		
		if (typeof(criteria.habitats) == 'string') {
			habitats = new Array(criteria.habitats);
		} else {
			habitats = criteria.habitats;
		}
		
		if (typeof(criteria.regions) == 'string') {
			regions = new Array(criteria.regions);
		} else {
			regions = criteria.regions;
		}
		
		if (typeof(criteria.sizes) == 'string') {
			sizes = new Array(criteria.sizes);
		} else {
			sizes = criteria.sizes;
		}
		
		if (typeof(criteria.fruits) == 'string') {
			fruits = new Array(criteria.fruits);
		} else {
			fruits = criteria.fruits;
		}
		
		if (typeof(criteria.leafs) == 'string') {
			leafs = new Array(criteria.leafs);
		} else {
			leafs = criteria.leafs;
		}
		
		if (typeof(criteria.months) == 'string') {
			months = new Array(criteria.months);
		} else {
			months = criteria.months;
		}
		
		if (typeof(criteria.wingShapes) == 'string') {
			wingShapes = new Array(criteria.wingShapes);
		} else {
			wingShapes = criteria.wingShapes;
		}
		
		if (typeof(criteria.songOrCallTypes) == 'string') {
			songOrCallTypes = new Array(criteria.songOrCallTypes);
		} else {
			songOrCallTypes = criteria.songOrCallTypes;
		}
		
		if (typeof(criteria.songOrCallPatterns) == 'string') {
			songOrCallPatterns = new Array(criteria.songOrCallPatterns);
		} else {
			songOrCallPatterns = criteria.songOrCallPatterns;
		}
		
		if (typeof(criteria.locomotions) == 'string') {
			locomotions = new Array(criteria.locomotions);
		} else {
			locomotions = criteria.locomotions;
		}
	}
	
	var isFull = false;
	if ($('isFull')) {
		isFull = true;
	}
	
	AdvancedSearchDWRController.refreshSearchFields(select.value, isFull, function(json) {
		subCategorySelect.innerHTML = '';
		criteriaDiv.innerHTML = '';
		json = eval(json);
		$A(json).each(function(sub) {
			var mname = sub.key;
			
			if (mname != 'subcategories') {
				criteriaDiv.appendChild(Builder.node('h3', {className : 'line'}, mname.capitalize()));
			}
			
			if (mname == 'wing shape') {
				mname = 'wingShape';
			} else if (mname == 'song or call type') {
				mname = 'songOrCallType';
			} else if (mname == 'song or call pattern') {
				mname = 'songOrCallPattern';
			}
			
			$A(sub.value).each(function(c) {
				if (mname == 'subcategories') {
					var o = Builder.node('option', {value : c.key}, c.value);
					subCategorySelect.appendChild(o);
				} else {
					var check;
					if ($A(colors).size() > 0 && mname == 'colors') {
						$A(colors).each(function(color) {
							if (color == c.key) {
								check = Builder.node('input', {name : mname, type : 'checkbox', checked : 'checked', className : 'check', value : c.key});
							}
						});
					} else if ($A(habitats).size() > 0 && mname == 'habitats') {
						$A(habitats).each(function(habitat) {
							if (habitat == c.key) {
								check = Builder.node('input', {name : mname, type : 'checkbox', checked : 'checked', className : 'check', value : c.key});
							}
						});
					} else if ($A(regions).size() > 0 && mname == 'regions') {
						$A(regions).each(function(region) {
							if (region == c.key) {
								check = Builder.node('input', {name : mname, type : 'checkbox', checked : 'checked', className : 'check', value : c.key});
							}
						});
					} else if ($A(sizes).size() > 0 && mname == 'sizes') {
						$A(sizes).each(function(size) {
							if (size == c.key) {
								check = Builder.node('input', {name : mname, type : 'checkbox', checked : 'checked', className : 'check', value : c.key});
							}
						});
					} else if ($A(fruits).size() > 0 && mname == 'fruits') {
						$A(fruits).each(function(fruit) {
							if (fruit == c.key) {
								check = Builder.node('input', {name : mname, type : 'checkbox', checked : 'checked', className : 'check', value : c.key});
							}
						});
					} else if ($A(leafs).size() > 0 && mname == 'leafs') {
						$A(leafs).each(function(leaf) {
							if (leaf == c.key) {
								check = Builder.node('input', {name : mname, type : 'checkbox', checked : 'checked', className : 'check', value : c.key});
							}
						});
					} else if ($A(months).size() > 0 && mname == 'months') {
						$A(months).each(function(month) {
							if (month == c.key) {
								check = Builder.node('input', {name : mname, type : 'checkbox', checked : 'checked', className : 'check', value : c.key});
							}
						});
					}
					
					 else if ($A(wingShapes).size() > 0 && mname == 'wingShapes') {
						 alert('1: ' + mname);
							$A(wingShapes).each(function(wingShape) {
								if (wingShape == c.key) {
									alert('name = ' + name + '; value = ' + c.key);
									check = Builder.node('input', {name : mname, type : 'checkbox', checked : 'checked', className : 'check', value : c.key});
								}
							});
					} else if ($A(songOrCallTypes).size() > 0 && mname == 'songOrCallTypes') {
						$A(songOrCallTypes).each(function(songOrCallType) {
							if (songOrCallType == c.key) {
								check = Builder.node('input', {name : mname, type : 'checkbox', checked : 'checked', className : 'check', value : c.key});
							}
						});
					} else if ($A(songOrCallPatterns).size() > 0 && mname == 'songOrCallPatterns') {
						$A(songOrCallPatterns).each(function(songOrCallPattern) {
							if (songOrCallPattern == c.key) {
								check = Builder.node('input', {name : mname, type : 'checkbox', checked : 'checked', className : 'check', value : c.key});
							}
						});
					} else if ($A(locomotions).size() > 0 && mname == 'locomotions') {
						$A(locomotions).each(function(locomotion) {
							if (locomotion == c.key) {
								check = Builder.node('input', {name : mname, type : 'checkbox', checked : 'checked', className : 'check', value : c.key});
							}
						});
					}
					
					if (!check) {
						check = Builder.node('input', {name : mname, type : 'checkbox', className : 'check', value : c.key});
					}
					
					criteriaDiv.appendChild(check);
					criteriaDiv.appendChild(Builder.node('span', {}, ' ' + c.value));
					criteriaDiv.appendChild(Builder.node('br'));
				}
			});
		});
		
		if (criteria && criteria.subCategoryId) {
			subCategorySelect.value = criteria.subCategoryId;
		} else {
			subCategorySelect.value = -1;
		}
	});
	
	return false;
}
