
	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);
		},
		
		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;
		}
		
	}