	
	var Photobar = {
		
		actionsPath: "/actions/nlife_actions.php",
		photosList : null,
		currentPhoto : 0,
		currentIndex : 0,
		reportId : 0,
		range : {
			start : 0,
			finish : 0
		},
		
		Refresh : function() 
		{
			this.currentIndex = this.GetCurrentPhotoIndex();
			//omg. но пока сойдёт)
			var limit = 2;
			var j = 0;
			for (var i=this.currentIndex-1;i>=0 && j<limit;i--) {
				j++;
				this.range.start = i;
			}
			limit = limit+(limit - j);
			j = 0;
			for (var i=this.currentIndex;i<this.photosList.length && j<=limit;i++) {
				j++;
				this.range.finish = i;
			}
			limit = limit+(limit - j);
			if (limit > 0) {
				j = 0;
				for (var i=this.range.start;i>=0 && j<limit;i--) {
					j++;
					this.range.start = i;
				}
			}
			this.MakePhotobar();
		},
		
		MakePhotobar : function() 
		{
			document.getElementById("photobar").innerHTML = "";
			var html = "";
			for (var i=this.range.start;i<=this.range.finish;i++) {
				html += this.getHtml(this.photosList[i]);
			}
			document.getElementById("photobar").innerHTML = html;
		},
		
		getHtml : function(photo_id) 
		{
			var html = "";
			html += '<div><a href="/nightlife/fotoreports/?reportid='+this.reportId+'&photoid='+photo_id+'#focusonfoto">';
			html += '<img';
			if (this.currentPhoto == photo_id) html += ' class="current_photo"';//!!!
			//onclick="return Photobar.LoadPhoto('+ photo_id +');"
			html += ' alt="img" src="/files/fotoreports/'+this.reportId+'/middle/'+photo_id+'.jpg" border="0"></a></div>';
			return html;
		},
		
		LoadPhoto : function(photo_id) 
		{
			document.getElementById("nlife_photo").src = "/files/fotoreports/" + this.reportId + "/insite/" + photo_id + ".jpg";			
			Photobar.currentPhoto = photo_id;
			Photobar.Refresh();
			
			commentsPlaceNum = photo_id;
			getPosts("", "no", false);
			
			NlifeTagging.photoId = photo_id;
			NlifeTagging.GetTaggedUsers();
			
			document.location.hash = "#photoid=" + photo_id;
			
			return false;
		},
		
		GetCurrentPhotoIndex : function()
		{
			for (var i=0;i<this.photosList.length;i++) {
				if (this.photosList[i] == this.currentPhoto) return i;
			}
			return 0;
		},
		
		ScrollDown : function()
		{
			if (this.photosList[this.range.finish+1]) {
				this.range.start++;
				this.range.finish++;
				this.MakePhotobar();
			}
		},
		
		ScrollUp : function()
		{
			if (this.photosList[this.range.start-1]) {
				this.range.start--;
				this.range.finish--;
				this.MakePhotobar();
			}
		},
		
		Scroll : function(event)
		{
			var delta = 0;
			if (event.wheelDelta) { 
	                delta = event.wheelDelta/120;
	               /* if (OP)
						delta = -delta;*/
	        } else if (event.detail) {
				delta = -event.detail/3;
	        }
			if (delta < 0) this.ScrollDown();
			if (delta > 0) this.ScrollUp();
			if (event.preventDefault) event.preventDefault();
			event.returnValue = false;
		}
		
	}

	var NlifeTagging = 
	{		
		actionsPath : "/actions/nlife_actions.php",
		reportId : 0,
		photoId : 0,
		
		coords : {
			image_x : 0,
			image_y : 0,
			doc_x : 0,
			doc_y : 0
		},
		is_objects_init: false,
		nlife_photo : null,
		utag_photocover : null,
		utag_layer : null,
		utag_layer_title : null,
		
		InitObjects : function()
		{
			if (!this.is_objects_init) {
				this.nlife_photo = document.getElementById("nlife_photo");
				this.utag_photocover = document.getElementById("utag_photocover");
				this.utag_layer = document.getElementById("utag_layer");
				this.utag_layer_title = document.getElementById("utag_layer_title");
				this.is_objects_init = true;
			}
		},
		
		StartTagging : function()
		{
			this.InitObjects();
			if (!this.nlife_photo.complete) {	
				alert("Дождитесь полной загрузки фотографии.");
				return false;
			}
			document.getElementById("tagging_info_block").style.display = "block";
			this.utag_photocover.style.display = "block";			
			var pos = getXY(this.nlife_photo);
			this.utag_photocover.style.left = pos.x + "px";
			this.utag_photocover.style.top = pos.y + "px";
			this.utag_photocover.style.width = this.nlife_photo.clientWidth + "px";
			this.utag_photocover.style.height = this.nlife_photo.clientHeight + "px";
		},
		FinishTagging : function()
		{
			document.getElementById("tagging_info_block").style.display = "none";
			this.utag_photocover.style.display = "none";
			this.utag_layer.style.display = "none";
			this.SwitchPhotoUser("add");
		},
		CancelTagging : function()
		{
			document.getElementById("tagging_info_block").style.display = "none";
			this.utag_photocover.style.display = "none";
			this.utag_layer.style.display = "none";
			this.coords.image_x = 0;
			this.coords.image_y = 0;
		},
		
		OnImageClick : function(object, event)
		{
			this.utag_layer.style.display = "block";//???
			this.utag_layer_title.innerHTML = "Я";
			this.coords.doc_x = (IE) ? event.clientX + document.documentElement.scrollLeft :  event.pageX;
			this.coords.doc_y = (IE) ? event.clientY + document.documentElement.scrollTop : event.pageY;
			this.ShowTitle(this.coords.doc_x, this.coords.doc_y);
			
			this.DocToImgCoords();
			//document.getElementById("utag_layer").innerHTML = "x: " + this.coords.image_x + "; y: " +  this.coords.image_y;
		},
		ShowTitle : function(x, y)
		{
			this.utag_layer.style.left = (x - 90) + "px";
			this.utag_layer.style.top = (y + 5) + "px";
		},
		DocToImgCoords : function()
		{
			var pos = getXY(this.nlife_photo);
			this.coords.image_x = this.coords.doc_x - pos.x;
			this.coords.image_y = this.coords.doc_y - pos.y;
		},
		ImgToDocCoords : function()
		{
			var pos = getXY(this.nlife_photo);
			this.coords.doc_x = this.coords.img_x + pos.x;
			this.coords.doc_y = this.coords.img_y + pos.y;
		},
		
		ShowTag : function(x, y, title)
		{
			if (x<=0 || y<=0 || x>640 || y>480) return false;  
			this.InitObjects();
			var pos = getXY(this.nlife_photo);
			this.ShowTitle(x + pos.x, y + pos.y);
			this.utag_layer_title.innerHTML = title;
			this.utag_layer.style.display = "block";
		},
		HideTag : function()
		{
			this.utag_layer.style.display = "none";
		},
		
		//userOnPhoto : false,		
		SwitchPhotoUser : function(tag_action)
		{
			var vars = {
				action: "switchphotouser",
				reportid: this.reportId,
				photoid: this.photoId,
				subaction: tag_action,
				posx: this.coords.image_x,
				posy: this.coords.image_y
			}
			try {
				sendXmlHttpRequest("post", this.actionsPath, xhParseParams(vars), function(jsHttp) 
				{
					eval("var json = " + jsHttp.responseText);
					if (json.Message.Type == "info") 
					{
						if (json.Result == true) 
						{
							//NlifeTagging.userOnPhoto = !NlifeTagging.userOnPhoto;
							var tagging_link = document.getElementById("tagging_link");
							if (tag_action == "add") {
								document.getElementById("tagging_link_delete").style.display = "inline";
							} else {
								document.getElementById("tagging_link_delete").style.display = "none";
							}
							document.getElementById("photousers_list").innerHTML = json.PhotoUsers;
						}
					} else {
						alert(json.Message.Type + ":\n" + json.Message.Text);
					}
				});
			} catch (e) {
				alert(e);
			}
		},
		
		GetTaggedUsers : function()
		{
			var vars = {
				action: "gettaggedusers",
				reportid: this.reportId,
				photoid: this.photoId
			}
			try {
				sendXmlHttpRequest("post", this.actionsPath, xhParseParams(vars), function(jsHttp) 
				{
					eval("var json = " + jsHttp.responseText);
					if (json.Message.Type == "info") 
					{
						//NlifeTagging.userOnPhoto = !NlifeTagging.userOnPhoto;
						var tagging_link = document.getElementById("tagging_link");
						if (json.UserOnPhoto) {
							document.getElementById("tagging_link_delete").style.display = "inline";
						} else {
							document.getElementById("tagging_link_delete").style.display = "none";
						}
						document.getElementById("photousers_list").innerHTML = json.PhotoUsers;
					} else {
						alert(json.Message.Type + ":\n" + json.Message.Text);
					}
				});
			} catch (e) {
				alert(e);
			}
		},
		
		DeleteAllUserTags : function()
		{
			if (confirm("Удалить все отметки с фотографий?") == false) return false;
			var vars = {
				action: "deleteallusertags"
			}
			try {
				sendXmlHttpRequest("post", this.actionsPath, xhParseParams(vars), function(jsHttp) 
				{
					eval("var json = " + jsHttp.responseText);
					if (json.Message.Type == "info") 
					{
						if (json.Result == true) 
						{
							location.reload();
						}
					} else {
						alert(json.Message.Type + ":\n" + json.Message.Text);
					}
				});
			} catch (e) {
				alert(e);
			}
		}
		
	}
	
	function GetCommentsRange(photo_id, comment_id)
	{
		var comments_range = document.getElementById("comments_range_"+photo_id);
		if (comments_range.innerHTML.length != 0) {
			comments_range.style.display = "block";
			return;
		}
		var vars = "action=getcommentsrange&photoid=" + photo_id +"&commentid=" + comment_id;
		try {
			sendXmlHttpRequest("post", "/actions/nlife_actions.php", vars, function(jsHttp) {
				eval("var json = " + jsHttp.responseText);
				if (json.Message.Type == "info") {
					comments_range.style.display = "block";
					document.getElementById("comments_range_"+photo_id).innerHTML = json.Html;
				} else {
					alert(json.Message.Type + ":\n" + json.Message.Text);
				}
			} 
			);
		} catch (e) {
			alert(e);
		}
	}