$(document).ready(function() {
	comments_prepare();
});

function comments_prepare() {
	$(".photo .comments .newentry .from").focus(function() {
		if($(this).val()=="Имя") $(this).val("")
	});
	
	$(".photo .comments .newentry .text").focus(function() {
		if($(this).val()=="Комментарий") $(this).val("")
	});	
}

function uploadphoto() {
	$("#progresser")
	.ajaxStart(function(){
		$(this).css("visibility", "visible");
		$("#adminbar #uploader :button").attr("disabled", "disabled");
	})
	.ajaxComplete(function(){
		$(this).css("visibility", "hidden");
		$("#adminbar #uploader :button").removeAttr("disabled");
	});
	
	if($("#fileToUpload").val()=="") return;
	
	var catid=$("#catid").text();
	$.ajaxFileUpload
	(
		{
			url: "handler.php?type=ajaxuploadphoto&catid="+catid, 
			secureuri: false,
			fileElementId: 'fileToUpload',
			dataType: 'json',
			success: function(data, status)
			{
				//alert(data.filesize);
				if(!data.isokey) {
					alert("Произошла ошибка: "+data.error);
				}
				else {
					//alert("Фото успешно загружено, айди и категория: "+data.id+", "+data.catid)
										
					$.ajax({
						url: "handler.php?type=ajaxloadphotos&photo_id="+data.id,
						cache: false,
						success: function(data) {
							if($("li").length>0) {
								$("li:last").after(data);
							} else {
								$("h1").html($("#catname"));	
								$("#dragness").html(data);
							}
							scrollTo($("li:last"));
						}
					});
					
				}
				
			},
			error: function(data, status, e)
			{
				alert("Ошибка "+e);
			}
		}
	)
	
	return false;
}

function photo_delete(id) {
	$.ajax({
		url: "handler.php?type=photodelete&photoid="+id,
		cache: false,
		dataType: "json",
		success: function(data) {
			if(data.isokey) {
				$("#photo"+id).parents("li").remove();
				//сдвинуть позиции
				for(var i=1; i<=$("#dragness li").length; i++) {
					$("#dragness li:nth-child("+i+")").attr("id", "pos"+i);
				}
			} else {
				alert("Ошибка: "+data.error);
			}			
		}
	});
}

function comment_addblock_toggle(idphoto) {
	if ($("#photo"+idphoto+" .comments .newentry").is(":hidden")) {
		$("#photo"+idphoto+" .comments .newentry").slideDown("slow");
		}
	else {
		$("#photo"+idphoto+" .comments .newentry").hide("slow");
	}

}

function comment_add(photo_id) {
	$("#progresser_bar")
	.ajaxStart(function(){
		$(this).css("visibility", "visible");
		$("photo"+photo_id+" comments newentry input:button").attr("disabled", "disabled");
	})
	.ajaxComplete(function(){
		$(this).css("visibility", "hidden");
		$("photo"+photo_id+" comments newentry input:button").removeAttr("disabled");
	});
	
	
	var author=$("#photo"+photo_id+" .comments .newentry .from").val();
	var text=$("#photo"+photo_id+" .comments .newentry .text").val();
	//alert(author+text);
	if(author=="" || author=="Имя") {alert("Укажите свое имя."); return false;}
	if(text=="" || text=="Комментарий") {alert("Не введен комментарий."); return false;}
	
	$.ajax({
		url: "handler.php?type=comment_add&photo_id="+photo_id,
		cache: false,
		dataType: "html",
		type: "POST",
		data: "author="+author+"&text="+text,
		success: function(data) {
			//alert(data);
			$("#photo"+photo_id+" .comments").replaceWith(data);
			comments_prepare()
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			alert(textStatus);
		}
	});
}

function totop() {
		scrollTo($(".pusher"));
}

function morephotos(count) {
	$("#progresser1")
	.ajaxStart(function(){
		$(this).show();
	})
	.ajaxComplete(function(){
		$(this).hide();
	});
	
	
	if($("#countphotoblocks").val()=="all" && count!=1) return false;
	
	if($(".photobox").length>0) {
		var catid=$("#catid").text();
		var countphotoblocks=Number($("#countphotoblocks").text());
		//alert(countphotoblocks);
		$.ajax({
			url: "handler.php?type=ajaxloadphotos&catid="+catid+"&start="+countphotoblocks+"&count="+count,
			cache: false,
			success: function(data) {
				$(".photobox:first").replaceWith(data);
				
				if(count=="all") {
					//alert("Было контейнеров "+$(".photobox").length);
					$(".photobox").remove();
					//alert("Стало контейнеров "+$(".photobox").length);
				}				
			
				if($(".photobox").length>0) {//если остался хотя бы один контейнер
					$("li:last").after($(".morehref"));
				}
				else {
					$(".morehref").remove();
				}
				

				
				if($(".photobox").length==1) {
					$(".morehref .next").remove();
				}
				
				
				//присвоить количество оставшихся фоток ссылке "Показать остальные #nextallcount"		
				if(count=="all") {
					$("#countphotoblocks").text("all");
				}
				else {
					$("#nextallcount").text(Number($("#nextallcount").text())-25);
					$("#countphotoblocks").text(countphotoblocks+1);
				}
			}
		});
		
		
	}
	
}

function toggleuploader() {	
	if ($("#uploader").css("visibility")=="hidden") {
		 $("#uploader").css("visibility", "visible");
		}
	else {
		 $("#uploader").css("visibility", "hidden");
	}
}

function scrollTo(selector) {
	var targetOffset = $(selector).offset().top;
	$('html,body').animate({scrollTop: targetOffset}, 500);
}


function changeimg(imgid) {
	//alert($("#img"+imgid).html());
	$("#img"+imgid).hide();
	$("#upl_"+imgid).show();

}
