var Svidgen_ac_timeout = 100;
// var bs_lat = 1000;
var Svidgen_ac = new Array();

var Svidgen_ac_special_keys = {
	// backspace
	'8': function(id) { return true; },

	// tab
	'9': function(id) {
		if (Svidgen_ac[id].textbox.value.length > 0) {
			Svidgen_ac[id].textbox.value = Svidgen_ac[id].textbox.value + ',';
		}
		Svidgen_ac[id].textbox.focus();
		return false;
	},

	// comma
	'188': function(id) {
		if (Svidgen_ac[id].textbox.value.length > 0) {
			Svidgen_ac[id].textbox.value = Svidgen_ac[id].textbox.value + ',';
		}
		Svidgen_ac[id].textbox.focus();
		return false;
	},

	// left arrow
	'37': function(id) { return true; },

	// right arrow
	'39': function(id) { return true; },

	// down arrow
	'40': function(id) { return true; },

	// up arrow
	'38': function(id) { return true; },

	// home key
	'36': function(id) { return true; },

	// end key
	'35': function(id) { return true; },

	// delete key
	'46': function(id) { return true; },

	// spacebar
	'32': function(id) { return true; },

	// shift
	'16': function(id) { return true; }

};

/*
var Svidgen_ac_template = {
	'url': false,
	'textbox': false,
	'div': false,
	'timeout': false,
	'tags': false,
	'old_text': '',
	'new_text': '',
	'waiting': false,
	'directory': '',
	'separator': '',
	'a_class': '',
	'span_class': '',
	'user': '',
	'h_of': '',
};
*/


function Svidgen_init_autocompleter(def) {
	var id = Svidgen_ac.length;

	if (!def['timeout']) {
		def['timeout'] = Svidgen_ac_timeout;
	}

	def['query_cache'] = new Array();

	if (def['textbox']) {
		def['textbox'] = document.getElementById(def['textbox']);
	} else {
		return false;
	}

	if (def['div']) {
		def['div'] = document.getElementById(def['div']);
	} else {
		def['div'] = false;
	}

	if (!def['separator']) {
		def['separator'] = '';
	}

	if (!def['directory']) {
		def['directory'] = '';
	}

	if (!def['a_class']) {
		def['a_class'] = '';
	}

	if (!def['span_class']) {
		def['span_class'] = '';
	}

	if (!def['user']) {
		def['user'] = '';
	}

	if (!def['h_of']) {
		def['h_of'] = '';
	}

	if (!def['url']) {
	// 	def['url'] = '/ajax/subtags.js?id=' + id + '&tags=' + encodeURI(Svidgen_querytags) + '&user=' + Svidgen_ac[id].user + '&h_of=' + Svidgen_ac[id].h_of;
	}

	def['old_text'] = '';
	def['new_text'] = '';
	def['waiting'] = false;
	def['tags'] = false;
	def['interval'] = Svidgen_ac_timeout;
	def['cache'] = new Array();


	def['textbox'].onkeydown = function(e) { return refreshTags(e || window.event, id); };
	def['textbox'].onclick = function() { this.select(); return false; };
	def['textbox'].autocomplete = 'off';

	Svidgen_ac[id] = def;

	return true;
} // Svidgen_init_autocompleter()


function refreshTags(e, id) {
	if (!Svidgen_ac[id]) {
		return false;
	}
	
	if (Svidgen_ac[id].timeout) {
		clearTimeout(Svidgen_ac[id].timeout);
	}

	var code = e.charCode || e.keyCode;

	var rv;
	if (Svidgen_ac_special_keys[code] != null) {
		if (Svidgen_ac_special_keys[code](id)) {
			// let the key pass through to the textbox, but do not trigger
			// the autocomplete event
			rv = true;
		} else {
			rv = false;
			Svidgen_ac[id].timeout = setTimeout(function() {fetchTags(id);}, Svidgen_ac[id].interval);
		}
	} else {
		rv = true;
		Svidgen_ac[id].timeout = setTimeout(function() {fetchTags(id);}, Svidgen_ac[id].interval);
	}

	return rv;
	
} // refreshTags()


function fetchTags(id) {
	var Svidgen_querytags = Svidgen_ac[id].textbox.value;

	// if (Svidgen_ac[
	var stags_query = '/ajax/subtags.js?id=' + id + '&tags=' + encodeURI(Svidgen_querytags) + '&user=' + Svidgen_ac[id].user + '&h_of=' + Svidgen_ac[id].h_of;

	// perform som really basic query caching ... 
	if (Svidgen_ac[id].cache[Svidgen_querytags]) {
		var cached = Svidgen_ac[id].cache[Svidgen_querytags];
		Svidgen_ac_callback(id, Svidgen_querytags, cached.tags, cached.e_tags, cached.tag_text, cached.start, cached.end); 
	} else {
		var stags_temp = document.createElement('script');
		stags_temp.type = 'text/javascript';
		stags_temp.src = stags_query;
		document.body.appendChild(stags_temp);
	}
} // fetchTags()


function tagsHTMLCallback(tags_html) {
	if (stags_directory) {
		if (!stags_query_cache[stags_awaiting_reply]) {
			stags_query_cache[stags_awaiting_reply] = Object();
		}
		stags_query_cache[stags_awaiting_reply].tags_html = tags_html;
		stags_directory.innerHTML = tags_html;
	}
	stags_awaiting_reply = false;
} // tagsHTMLCallback()


function Svidgen_ac_callback(id, query, tags, e_tags, tag_text, start, end) {
	if (!Svidgen_ac[id].cache[query]) {
		Svidgen_ac[id].cache[query] = {
			'tags': tags,
			'e_tags': e_tags,
			'tag_text': tag_text,
			'start': start,
			'end': end
		};
	}

	Svidgen_ac[id].cache[query].tags = tags;
	Svidgen_ac[id].cache[query].e_tags = e_tags;

	var stags_temp_a;
	var stags_temp_span;
	var stags_temp_text;
	var stags_top_tag = {'tag_text':'','popularity':0};

	// clear the tags DIV
	if (Svidgen_ac[id].div) {
		while(Svidgen_ac[id].div.hasChildNodes()) {
			Svidgen_ac[id].div.removeChild(Svidgen_ac[id].div.lastChild);
		}
		
		if (tags.length == 0) {
			Svidgen_ac[id].div.appendChild(document.createTextNode("No matching subtags."));
		}
	}
		
	// iterate through all of the tags, createing the list for the DIV
	// and determining which is the most popular
	for (var i in tags) {
		stags_temp_tag_text = document.createTextNode(tags[i].tag_text);
		stags_temp_tag_popularity = document.createTextNode(" (" + tags[i].popularity + ")");

		// set the top (most popular) tag, as necessary.
		if (tags[i].popularity > stags_top_tag.popularity) {
			stags_top_tag.tag_text = tags[i];
		}

		// autocomplete the textbox
				
		stags_temp_a = document.createElement('a');
		if (e_tags.length > 0) {
			stags_temp_a.title = e_tags + ',' + tags[i].tag_text;
		} else {
			stags_temp_a.title = tags[i].tag_text;
		}

		stags_temp_a.href = '/browse_bookmarks?tags=' + stags_temp_a.title;

		stags_temp_a.className = Svidgen_ac[id].a_class;
		stags_temp_a.appendChild(stags_temp_tag_text);
		
		stags_temp_span = document.createElement('span');
		stags_temp_span.className = Svidgen_ac[id].span_class;
		stags_temp_span.appendChild(stags_temp_a);
		stags_temp_span.appendChild(stags_temp_tag_popularity);
		
		if (tags.length - 1 > i) {
			stags_temp_span.appendChild(document.createTextNode(Svidgen_ac[id].separator));
		}
		
		Svidgen_ac[id].div.appendChild(stags_temp_span);
	}

	Svidgen_autocomplete(id, tag_text, query, start, end);
} // tagsCallback()


function Svidgen_autocomplete(id, tags, query, start, end) {
	if (Svidgen_ac[id].textbox.value == query && tags.length > 0) {
		// stags_otext = tag_text;
		Svidgen_ac[id].textbox.value = tags;
		if (Svidgen_ac[id].textbox.selectionStart) {
			Svidgen_ac[id].textbox.selectionStart = start;
			// Svidgen_ac[id].textbox.selectionEnd = ;
		} else if (Svidgen_ac[id].textbox.createTextRange) {
			var r = Svidgen_ac[id].textbox.createTextRange();
			r.moveStart('character', start);
			r.select();
		}
	} else {
		// refreshTags;
	}
} // tagsAutocomplete()

