
function TagsControl()
{
	this.actions_path = "/common/actions/tags.php";
	this.cache = {};
	this.min_length = 2;
	
	this.Init = function(container_name)
	{
		var obj = this;
		$(container_name).bind( "keydown", function( event ) {
			if ( event.keyCode === $.ui.keyCode.TAB &&
					$( this ).data( "autocomplete" ).menu.active ) {
				event.preventDefault();
			}
		})
		.autocomplete({
			minLength: obj.min_length,
			source: function( request, response ) {				
				var term = request.term;
				if ( term in obj.cache ) {
					response( obj.cache[ term ] );
					return;
				}

				$.ajax({
					url: obj.actions_path,
					type: "GET",
					dataType: "json",
					data: {
						action: "tags_search",
						hash: global_hash,
						name: obj.extractLast(request.term)
					},
					success: function(data) {
						obj.cache[ term ] = data.Tags;
						response( data.Tags );
					}
				});
			},
			focus: function( event, ui ) {
				return false;
			},
			select: function( event, ui ) {
				var terms = obj.split(this.value);
				terms.pop();
				terms.push(ui.item.value);
				terms.push("");
				this.value = terms.join(", ");
				return false;
			}
		});
	};
	
	this.split = function(val) {
		return val.split(/,\s*/);
	};
	this.extractLast = function(term) {
		return this.split(term).pop();
	};
	
}

//old
	var Tags = {
		
		tagsActions : "/common/actions/tags.php",
		selStart : 0,
		selEnd : 0,
		
		/*
		TagsAll : "",
		newSearchTags : function(textbox) {
			var tagName = this.getTagName(textbox);
			tagName = "при";			
			re = new RegExp("(?:^|,)("+ tagName +"[a-zа-я0-9-]+)", "gi");
			matches = this.TagsAll.match(re);
			if (matches)
				for (var i=0;i<matches.length;i++) {
					if (matches[i].charAt(0) == ",") matches
				}
		},
		*/
		
		test : function() 
		{
			var vars = "";
			try {
				sendXmlHttpRequest("get", this.tagsActions, vars, this.testCallback, "");
			} catch (e) {
				alert(e);
			}
		},
		testCallback : function(xmlHttp) {
			var test = eval(xmlHttp.responseText);
			alert(test.tag);
		},
		
		CheckTagLength : function(textbox, event)
		{
			/*var key = (event.keyCode == 0) ? event.charCode : event.keyCode;
			//document.title = key;
			var char = String.fromCharCode(key);
			if (event.ctrlKey || event.altKey || (key < 41) || key == 46 || key == 188 || key == 44) return true;
			var re = /[a-zа-я0-9\-\.\ ]/i;
			if (re.test(char)) 
			{				
				var tagName = this.getTagName(textbox);
				if (tagName.length >= 20 ) return false;
			} else 
				return false;*/
			/*var tagName = this.getTagName(textbox);
			document.title = tagName;
			var re = /[^a-zа-я0-9\-\.\ ]/i;
			tagName = tagName.replace(re, "");
			if (tagName.length >= 20 ) 
			{
				tagName = tagName.substr(0, 19);
			}
			this.PasteTag(tagName, true);*/
		},
		
		SearchTags : function(textbox) 
		{
			var tagName = this.getTagName(textbox);
			if (tagName.length < 2) return false;
			var vars = "action=tags_search&name=" + tagName;// + "&all=" + textbox.value;
			//this.getTags(textbox);
			try {
				sendXmlHttpRequest("get", this.tagsActions, vars, this.searchTagsCallback, "");
			} catch (e) {
				alert(e);
			}
		},
		getTags : function(textbox) {
			//var text = textbox.value;
			//var re = /\b([a-zа-я0-9\-]*)\b/i;
			//matches = text.match(re);
			//alert(matches);
		},
		getTagName : function(textbox) {
			var range = this.getTagRange(textbox);
			return textbox.value.substring(range.leftPos + 1, range.rightPos);
		},
		getTagRange : function(textbox) {
			var text = textbox.value;
			var caretPos = 0;
			if (document.selection) {
				textbox.focus();
				var selection = document.selection.createRange();
				selection.moveStart("character", -textbox.value.length);
				caretPos = selection.text.length;
			} else if (textbox.selectionStart || textbox.selectionStart == "0") {
				caretPos = textbox.selectionStart;
			}
			var range = {leftPos : 0, rightPos : 0};
			range.leftPos = text.lastIndexOf(",", caretPos);
			range.rightPos = text.indexOf(",", caretPos);
			if (range.rightPos == -1) range.rightPos = text.length;
			return range;
		},
		PasteTag : function(tagName) 
		{
			var photo_tags = document.getElementById("photo_tags");
			var range = this.getTagRange(photo_tags);
			if (range.leftPos > 0) tagName = ", " + tagName;
			var text = photo_tags.value.substring(0, range.leftPos) + tagName;
			text += photo_tags.value.substring(range.rightPos, photo_tags.value.length);
			photo_tags.value = text;
			photo_tags.focus();
			setHtml("tags_div", "<br>");
		},
		
		searchTagsCallback : function(jsHttp) 
		{
			eval("var json = " + jsHttp.responseText);
			var tagsDiv = document.getElementById("tags_div");
			if (!json.Tags) { 
				return false;
			}
			tagsDiv.innerHTML = "";
			var html = "";
	
			for (i=0;i<json.Tags.length;i++) {
				html += '<a href="javascript:///" class="small_link" onClick="Tags.PasteTag(this.innerHTML);">'+json.Tags[i]+'</a> ';
			}
			tagsDiv.innerHTML = html;
		}
		
	}
