if(typeof(tmt) == "undefined"){
	tmt = {};
}

if(typeof(tmt.spry) == "undefined"){
	tmt.spry = {};
}

if(typeof(tmt.spry.widget) == "undefined"){
	tmt.spry.widget = {};
}

tmt.spry.widget.AutoSuggestTags = {};

// Constructor
tmt.spry.widget.AutoSuggestTags = function(region, sRegion, dataset, field, options){
	this.separator = "; ";
	this.escapeChar = '"';
	Spry.Widget.AutoSuggest.call(this, region, sRegion, dataset, field, options);
}

// Import all methods
for(var x in Spry.Widget.AutoSuggest.prototype){
	tmt.spry.widget.AutoSuggestTags.prototype[x] = Spry.Widget.AutoSuggest.prototype[x];
}
tmt.spry.widget.AutoSuggestTags.prototype.constructor = tmt.spry.widget.AutoSuggestTags;

/* New and overwritten methods */

tmt.spry.widget.AutoSuggestTags.prototype.getValue = function(){
	if(!this.textElement){
		return "";
	}
	var tags = this.textElement.value.split(this.separator);
	var text = tags[tags.length -1];
	var pattern = new RegExp(this.escapeChar, "g");
	return text.replace(pattern, "");
}

tmt.spry.widget.AutoSuggestTags.prototype.setValue = function(str){
	if(!this.textElement){
		return;
	}
	if(str.indexOf(this.separator) > 0){
		str = this.escapeChar + str + this.escapeChar;
	}
	var currentTags = this.getTags();
	if(currentTags.length > 0){
		currentTags += this.separator;
	}
	this.textElement.value = currentTags + str + this.separator;
	this.showSuggestions(false);
}

tmt.spry.widget.AutoSuggestTags.prototype.getTags = function(){
	if(!this.textElement){
		return "";
	}
	var tags = this.textElement.value.split(this.separator);
	if(tags.length > 0){
		tags.pop();
		return tags.join(this.separator)
	}
	return "";
}
