var hoverIndex = -1;

function str2subject(field) {
	var str = field.value;
	
	//Exclamation marks shouldn't be in titles
	str = str.replace(/[!]/g, '');
	
	//Multiple question marks are obnoxious
	str = str.replace(/([\?]+)/g, '?');
	
	//Multiple spaces just waste space
	str = str.replace(/([ ]+)/g, ' ');
	
	field.value = str;
}

function str2path(str) {
	//Switch to lowercase
	str = str.toLowerCase();
	
	//Replace punctuation with spaces
	str = str.replace(/([-_\/\\ ]+)/g, ' ');
	
	//Replace spaces with hyphens
	str = str.replace(/([ ]+)/g, '-');
	
	//Remove all remaining invalid characters
	str = str.replace(/[^a-z0-9-]/g, '');
	
	return str;
}

function tag2path(field) {
	field.value = str2path(field.value);
}

function suggestTag(e, id) {
	var field = document.getElementById(id);
	var newValue = str2path(field.value);
	
	if(field.value.length > 0) {
		new Ajax.Request(
			'/suggest/' + field.value,
			{
				method: 'get',
				onSuccess: function(transport, json) {
					if(json.length > 1 || (json.length == 1 && json[0] != field.value)) {
						var suggestions = document.getElementById(id + '_suggest');
						var list = document.createElement('ul');
						
						json.reverse();
						
						for(var i = 0; i < json.length; i++) {
							var suggestion = document.createElement('li');
							var match = new RegExp('(' + field.value + ')', 'g');
							suggestion.innerHTML = json[i].replace(match, '<strong>' + field.value +  '</strong>');
							
							suggestion.setAttribute('onClick', 'popSuggest("' + id + '", "' + json[i] + '");');
							suggestion.onClick = function() { popSuggest(id, json[i]); };
							
							suggestion.setAttribute('onMouseOver', 'hoverSuggestion("' + id + '", "' + i + '");');
							suggestion.onMouseOver = function() { hoverSuggestion(id, i); };
							
							list.appendChild(suggestion);
						}
						
						suggestions.innerHTML = '';
						suggestions.appendChild(list);
					} else {
						hideSuggestions(id);
					}
				}
			}
		);
	} else {
		 hideSuggestions(id);
	}
	
	e = (e) ? e : ((window.event) ? event : null);
	
	if (e.keyCode) {
		if(e.keyCode == 38) {
			//Up
			if(hoverIndex > 0) {
				hoverSuggestion(id, hoverIndex - 1);
			}
		} else if(e.keyCode == 40) {
			//Down
			if(hoverIndex < document.getElementById(id + '_suggest').getElementsByTagName('li').length) {
				hoverSuggestion(id, hoverIndex + 1);
			}
		}
	}
}

function hideSuggestions(id) {
	setTimeout(
		function() {
			document.getElementById(id + '_suggest').innerHTML = '';
			hoverIndex = -1;
		},
		125
	);
}

function popSuggest(id, value) {
	document.getElementById(id).value = value;
	hideSuggestions(id);
	var nextFocus = document.getElementById('tag' + (parseInt(id.replace('tag', '')) + 1));
	if(nextFocus != null) {
		nextFocus.focus();
	} else {
		document.getElementById('submit').focus();
	}
}

function hoverSuggestion(id, suggestionIndex) {
	var suggestions = document.getElementById(id + '_suggest').getElementsByTagName('li');
	
	suggestions[hoverIndex].setAttribute('class', '');
	suggestions[hoverIndex].setAttribute('className', '');
	
	hoverIndex = suggestionIndex;
	
	suggestions[hoverIndex].setAttribute('class', 'hover');
	suggestions[hoverIndex].setAttribute('className', 'hover');
}
