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.TAGS_SEPARATOR = ", ";
	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.TAGS_SEPARATOR);
	return tags[tags.length -1];
}

tmt.spry.widget.AutoSuggestTags.prototype.setValue = function(str){
	if(!this.textElement){
		return;
	}
	var currentTags = this.getTags();
	if(currentTags.length > 0){
		currentTags += this.TAGS_SEPARATOR;
	}
	this.textElement.value = currentTags + str + this.TAGS_SEPARATOR;
	this.showSuggestions(false);
}

tmt.spry.widget.AutoSuggestTags.prototype.getTags = function(){
	if(!this.textElement){
		return "";
	}
	var tags = this.textElement.value.split(this.TAGS_SEPARATOR);
	if(tags.length > 0){
		tags.pop();
		return tags.join(this.TAGS_SEPARATOR)
	}
	return "";
}
