$(document).ready(function() {
	$(".tabs li.active").live("click", function() {
		return(false);
	});
	$("#index-new").live("click", function() {
		$(this).parent(".tabs").children("li.active").removeClass("active");
		$(this).addClass("active");
		$(this).closest(".tabctn").children(".tabc").load("api.php?content=index-new");
	});
	$("#index-new-game").live("click", function() {
		$(this).parent(".tabs").children("li.active").removeClass("active");
		$(this).addClass("active");
		$(this).closest(".tabctn").children(".tabc").load("api.php?content=index-new-game");
	});
	$("#index-new-video").live("click", function() {
		$(this).parent(".tabs").children("li.active").removeClass("active");
		$(this).addClass("active");
		$(this).closest(".tabctn").children(".tabc").load("api.php?content=index-new-video");
	});
	$("#index-pop").live("click", function() {
		$(this).parent(".tabs").children("li.active").removeClass("active");
		$(this).addClass("active");
		$(this).closest(".tabctn").children(".tabc").load("api.php?content=index-pop&period="+hperiod);
	});

	$("#i-period-day:not(.active)").live("click", function() {
		hperiod = "period-day";
		$(this).closest(".tabctn").children(".tabc").load("api.php?content=index-pop&period="+hperiod);
	});
	$("#i-period-week:not(.active)").live("click", function() {
		hperiod = "period-week";
		$(this).closest(".tabctn").children(".tabc").load("api.php?content=index-pop&period="+hperiod);
	});
	$("#i-period-month:not(.active)").live("click", function() {
		hperiod = "period-month";
		$(this).closest(".tabctn").children(".tabc").load("api.php?content=index-pop&period="+hperiod);
	});
	$("#i-period-all:not(.active)").live("click", function() {
		hperiod = "period-all";
		$(this).closest(".tabctn").children(".tabc").load("api.php?content=index-pop&period="+hperiod);
	});

	$("#ctop").live("click", function() {
		hsec = "categories-top";
		$(this).parent(".tabs").children("li.active").removeClass("active");
		$(this).addClass("active");
		$(this).closest(".tabctn").children(".tabc").load("api.php?content=categories-top&cat_id="+hcid);
	});
	$("#cnew").live("click", function() {
		hsec = "categories-new";
		$(this).parent(".tabs").children("li.active").removeClass("active");
		$(this).addClass("active");
		$(this).closest(".tabctn").children(".tabc").load("api.php?content=categories-new&cat_id="+hcid+"&page="+hpage);
	});
	$("#cpop").live("click", function() {
		hsec = "categories-pop";
		$(this).parent(".tabs").children("li.active").removeClass("active");
		$(this).addClass("active");
		$(this).closest(".tabctn").children(".tabc").load("api.php?content=categories-pop&cat_id="+hcid+"&period="+hperiod+"&page="+hpage);
	});

	$("#period-day:not(.active)").live("click", function() {
		hperiod = "period-day";
		$(this).closest(".tabctn").children(".tabc").load("api.php?content=categories-pop&cat_id="+hcid+"&period="+hperiod+"&page="+hpage);
	});
	$("#period-week:not(.active)").live("click", function() {
		hperiod = "period-week";
		$(this).closest(".tabctn").children(".tabc").load("api.php?content=categories-pop&cat_id="+hcid+"&period="+hperiod+"&page="+hpage);
	});
	$("#period-month:not(.active)").live("click", function() {
		hperiod = "period-month";
		$(this).closest(".tabctn").children(".tabc").load("api.php?content=categories-pop&cat_id="+hcid+"&period="+hperiod+"&page="+hpage);
	});
	$("#period-all:not(.active)").live("click", function() {
		hperiod = "period-all";
		$(this).closest(".tabctn").children(".tabc").load("api.php?content=categories-pop&cat_id="+hcid+"&period="+hperiod+"&page="+hpage);
	});

	$("#p-period-day:not(.active)").live("click", function() {
		hperiod = "period-day";
		$(this).closest(".tabctn").children(".tabc").load("api.php?content=pop&period="+hperiod+"&page="+hpage);
	});
	$("#p-period-week:not(.active)").live("click", function() {
		hperiod = "period-week";
		$(this).closest(".tabctn").children(".tabc").load("api.php?content=pop&period="+hperiod+"&page="+hpage);
	});
	$("#p-period-month:not(.active)").live("click", function() {
		hperiod = "period-month";
		$(this).closest(".tabctn").children(".tabc").load("api.php?content=pop&period="+hperiod+"&page="+hpage);
	});
	$("#p-period-all:not(.active)").live("click", function() {
		hperiod = "period-all";
		$(this).closest(".tabctn").children(".tabc").load("api.php?content=pop&period="+hperiod+"&page="+hpage);
	});

	$("#message-inbox").live("click", function() {
		$(this).parent(".tabs").children("li.active").removeClass("active");
		$(this).addClass("active");
		$(this).closest(".tabctn").children(".tabcd").load("api.php?content=message-list&folder=inbox&page="+hpage);
	});
	$("#message-outbox").live("click", function() {
		$(this).parent(".tabs").children("li.active").removeClass("active");
		$(this).addClass("active");
		$(this).closest(".tabctn").children(".tabcd").load("api.php?content=message-list&folder=outbox&page="+hpage);
	});
	$("#message-send").live("click", function() {
		$(this).parent(".tabs").children("li.active").removeClass("active");
		$(this).addClass("active");
		$(this).closest(".tabctn").children(".tabcd").load("api.php?content=message-send");
	});
	$("#fmsendb").live("click", function() {
		$(this).after('<img src="i/indicator.gif" id="indicator" style="vertical-align:middle" />');
		jQuery.ajax({
			type: $("#fmsend").attr("method"),
			url: $("#fmsend").attr("action"),
			data: $("#fmsend").serialize()+"&"+dscauth,
			success: function(msg) {
				$("#indicator").remove();
				if (msg.length) {
					$("#fmserror").html(msg+"<br /><br />");
				} else {
					$("#fmsend").children("#addcmt").fadeOut(500, function() {
						$(this).html("Съобщението е изпратено успешно.").fadeIn(500);
					});
				}
			}
		});
		return(false);
	});
	$("#message-report").live("click", function() {
		var sthis = $(this).closest("div");
		sthis.html('Записвам<img src="i/indicator.gif" style="vertical-align:middle" />');
		jQuery.ajax({
			type: "post",
			url: "api.php?content=message-report&item_id="+sthis.attr("id").slice(9),
			data: dscauth,
			success: function(msg) {
				if (msg.length) {
					sthis.html(msg);
				}
			}
		});
	});
	$("#message-del").live("click", function() {
		var sthis = $(this);
		jQuery.ajax({
			type: "post",
			url: "api.php?content=message-del",
			data: $("#fmlist").serialize()+"&"+dscauth,
			success: function(msg) {
				if (msg.length) {
					sthis.html(msg);
					setTimeout(function() {
						sthis.fadeOut(500, function() {
							$(this).html("Изтрий избраните").fadeIn();
						})
					}, 3000);
				} else {
					sthis.closest(".tabcd").load("api.php?content=message-list&folder="+hfolder+"&page="+hpage);
					$("html").animate({scrollTop:0}, "slow");
				}
			}
		});
	});
	$("#message-unread").live("click", function() {
		var sthis = $(this);
		jQuery.ajax({
			type: "post",
			url: "api.php?content=message-unread",
			data: $("#fmlist").serialize()+"&"+dscauth,
			success: function(msg) {
				if (msg.length) {
					sthis.html(msg);
					setTimeout(function() {
						sthis.fadeOut(500, function() {
							$(this).html("Маркирай като непрочетени").fadeIn();
						})
					}, 3000);
				} else {
					sthis.closest(".tabcd").load("api.php?content=message-list&folder="+hfolder+"&page="+hpage);
					$("html").animate({scrollTop:0}, "slow");
				}
			}
		});
	});
	$("#message-choose-all").live("click", function() {
		$("#fmlist input:checkbox").attr("checked", true);
	});
	$("#message-choose-read").live("click", function() {
		$("#fmlist input:checkbox:not(.unread)").attr("checked", true);
		$("#fmlist input:checkbox.unread").attr("checked", false);
	});
	$("#message-choose-none").live("click", function() {
		$("#fmlist input:checkbox").attr("checked", false);
	});

	$(".nav li span[id]").live("click", function() {
		hpage = $(this).attr("id").slice(5);
		//document.location.href = document.location.href.split("#")[0]+"#p"+hpage;
		//document.location.hash = "#p"+hpage;
		if (hsec.slice(0, 10) == "categories") {
			$(this).closest(".tabctn").children(".tabc").load("api.php?content="+hsec+"&cat_id="+hcid+"&period="+hperiod+"&page="+hpage);
		} else if (hsec == "flash") {
			$(this).closest("#cmts").load("api.php?content=comments&image_id="+himid+"&page="+hpage+"&sdir="+hsdir);
			var target = $("#cmts").offset().top;
			$("html").animate({scrollTop:target -40}, "slow");
		} else if (hsec.slice(0, 6) == "member") {
			$(this).closest(".tabctn").children(".tabc").load("api.php?content="+hsec+"&place="+hplace+"&user_id="+huid+"&page="+hpage);
			var target = $(".tabctn").offset().top;
			$("html").animate({scrollTop:target -40}, "slow");
		} else if (hsec.slice(0, 6) == "stream") {
			$(this).closest(".tabctn").children(".tabc").load("api.php?content="+hsec+"&place="+hplace+"&user_id="+huid+"&page="+hpage);
			var target = $(".tabctn").offset().top;
			$("html").animate({scrollTop:target -40}, "slow");
		} else if (hsec.slice(0, 5) == "users") {
			$(this).closest(".tabc").load("api.php?content="+hsec+"&page="+hpage+"&"+$("#fusers").serialize());
			var target = $("#main").offset().top;
			$("html").animate({scrollTop:target -40}, "slow");
		} else if ($(this).closest("div").attr("class").slice(0, 6) == "search") {
			var tsec = $(this).closest("div").attr("class").slice(7);
			if (tsec == "users") {
				$(this).closest("div").load("api.php?content=users-new&page="+hpage+"&user_name="+encodeURIComponent($("#fs").val())+"&search-users&hppmult="+hppmult);
			} else { // content
				$(this).closest("div").load("api.php?content=search&page="+hpage+"&s="+encodeURIComponent($("#fs").val())+"&type="+tsec+"&hppmult="+hppmult);
			}
			var target = $(this).closest("div").offset().top;
			$("html").animate({scrollTop:target -40}, "slow");
		} else if (hsec == "message-list") {
			$(this).closest(".tabcd").load("api.php?content=message-list&folder="+hfolder+"&page="+hpage);
			$("html").animate({scrollTop:0}, "slow");
		} else if (hsec == "new") {
			$(this).closest(".tabc").load("api.php?content=new&cat_id="+hcid+"&page="+hpage);
			$("html").animate({scrollTop:0}, "slow");
		} else if (hsec == "pop") {
			$(this).closest(".tabc").load("api.php?content=pop&period="+hperiod+"&page="+hpage);
			$("html").animate({scrollTop:0}, "slow");
		}
	});

	$("#member-wall").live("click", function() {
		hsec = "member-wall";
		$(this).parent(".tabs").children("li.active").removeClass("active");
		$(this).addClass("active");
		$(this).closest(".tabctn").children(".tabc").load("api.php?content=member-wall&user_id="+huid+"&page="+hpage);
	});
	$("#member-games").live("click", function() {
		hsec = "member-games";
		$(this).parent(".tabs").children("li.active").removeClass("active");
		$(this).addClass("active");
		$(this).closest(".tabctn").children(".tabc").load("api.php?content=member-games&place="+hplace+"&user_id="+huid+"&page="+hpage);
	});
	$("#member-videos").live("click", function() {
		hsec = "member-videos";
		$(this).parent(".tabs").children("li.active").removeClass("active");
		$(this).addClass("active");
		$(this).closest(".tabctn").children(".tabc").load("api.php?content=member-videos&place="+hplace+"&user_id="+huid+"&page="+hpage);
	});
	$("#member-links").live("click", function() {
		hsec = "member-links";
		$(this).parent(".tabs").children("li.active").removeClass("active");
		$(this).addClass("active");
		$(this).closest(".tabctn").children(".tabc").load("api.php?content=member-links&user_id="+huid+"&page="+hpage);
	});
	$("#member-notes").live("click", function() {
		hsec = "member-notes";
		$(this).parent(".tabs").children("li.active").removeClass("active");
		$(this).addClass("active");
		$(this).closest(".tabctn").children(".tabc").load("api.php?content=member-notes&user_id="+huid+"&page="+hpage);
	});

	$("#member-all:not(.active)").live("click", function() {
		hplace = "member-all";
		$(this).closest(".tabctn").children(".tabc").load("api.php?content="+hsec+"&place=member-all&user_id="+huid+"&page="+hpage);
	});
	$("#member-upped:not(.active)").live("click", function() {
		hplace = "member-upped";
		$(this).closest(".tabctn").children(".tabc").load("api.php?content="+hsec+"&place=member-upped&user_id="+huid+"&page="+hpage);
	});
	$("#member-fav:not(.active)").live("click", function() {
		hplace = "member-fav";
		$(this).closest(".tabctn").children(".tabc").load("api.php?content="+hsec+"&place=member-fav&user_id="+huid+"&page="+hpage);
	});

	$("#comments-first:not(.active)").live("click", function() {
		hsdir = "asc";
		$(this).closest("#cmts").load("api.php?content=comments&image_id="+himid+"&page="+hpage+"&sdir="+hsdir);
	});
	$("#comments-last:not(.active)").live("click", function() {
		hsdir = "desc";
		$(this).closest("#cmts").load("api.php?content=comments&image_id="+himid+"&page="+hpage+"&sdir="+hsdir);
	});

	$("#stream-wall").live("click", function() {
		hsec = "stream-wall";
		$(this).parent(".tabs").children("li.active").removeClass("active");
		$(this).addClass("active");
		$(this).closest(".tabctn").children(".tabc").load("api.php?content=stream-wall&page="+hpage);
	});
	$("#stream-games").live("click", function() {
		hsec = "stream-games";
		$(this).parent(".tabs").children("li.active").removeClass("active");
		$(this).addClass("active");
		$(this).closest(".tabctn").children(".tabc").load("api.php?content=stream-games&place="+hplace+"&page="+hpage);
	});
	$("#stream-videos").live("click", function() {
		hsec = "stream-videos";
		$(this).parent(".tabs").children("li.active").removeClass("active");
		$(this).addClass("active");
		$(this).closest(".tabctn").children(".tabc").load("api.php?content=stream-videos&place="+hplace+"&page="+hpage);
	});
	$("#stream-links").live("click", function() {
		hsec = "stream-links";
		$(this).parent(".tabs").children("li.active").removeClass("active");
		$(this).addClass("active");
		$(this).closest(".tabctn").children(".tabc").load("api.php?content=stream-links&page="+hpage);
	});
	$("#stream-notes").live("click", function() {
		hsec = "stream-notes";
		$(this).parent(".tabs").children("li.active").removeClass("active");
		$(this).addClass("active");
		$(this).closest(".tabctn").children(".tabc").load("api.php?content=stream-notes&page="+hpage);
	});

	$("#fav-all").live("click", function() {
		$(this).parent(".tabs").children("li.active").removeClass("active");
		$(this).addClass("active");
		$(this).closest(".tabctn").children(".tabc").load("api.php?content=fav-all");
	});
	$("#fav-games").live("click", function() {
		$(this).parent(".tabs").children("li.active").removeClass("active");
		$(this).addClass("active");
		$(this).closest(".tabctn").children(".tabc").load("api.php?content=fav-games");
	});
	$("#fav-videos").live("click", function() {
		$(this).parent(".tabs").children("li.active").removeClass("active");
		$(this).addClass("active");
		$(this).closest(".tabctn").children(".tabc").load("api.php?content=fav-videos");
	});
	$("#fav-cards").live("click", function() {
		$(this).parent(".tabs").children("li.active").removeClass("active");
		$(this).addClass("active");
		$(this).closest(".tabctn").children(".tabc").load("api.php?content=fav-cards");
	});
	$("#fav-kids").live("click", function() {
		$(this).parent(".tabs").children("li.active").removeClass("active");
		$(this).addClass("active");
		$(this).closest(".tabctn").children(".tabc").load("api.php?content=fav-kids");
	});

	$("#dec.active").live("click", function() {
		if (ff) {
			if ($("#box").innerWidth() > 640) {
				$("#main").width($("#main").width() - 100);
				$("#content").width($("#content").width() - 100);
			}
			$("#box").width($("#box").width() - 100).height($("#box").height() - dy);
			if ($("#box").innerWidth() <= 440) {
				$("#dec").removeClass("active");
			}
			$("#inc").addClass("active");
		} else {
			if ($("#box").innerWidth() > 640) {
				$("#main").animate({width:"-=100px"});
				$("#content").animate({width:"-=100px"});
			}
			$("#box").animate({width:"-=100px",height:"-="+dy+"px"}).each(function() {
				if ($("#box").innerWidth() <= 440) {
					$("#dec").removeClass("active");
				}
				$("#inc").addClass("active");
			});
		}
	});
	$("#inc.active").live("click", function() {
		if (ff) {
			if ($("#box").innerWidth() >= 640) {
				$("#main").width($("#main").width() + 100);
				$("#content").width($("#content").width() + 100);
			}
			$("#box").width($("#box").width() + 100).height($("#box").height() + dy);
			if ($("#box").innerWidth() >= 1040) {
				$("#inc").removeClass("active");
			}
			$("#dec").addClass("active");
		} else {
			if ($("#box").innerWidth() >= 640) {
				$("#main").animate({width:"+=100px"});
				$("#content").animate({width:"+=100px"});
			}
			$("#box").animate({width:"+=100px",height:"+="+dy+"px"}).each(function() {
				if ($("#box").innerWidth() >= 1040) {
					$("#inc").removeClass("active");
				}
				$("#dec").addClass("active");
			});
		}
	});

	$("#nvl li a").hover(function() {
		$(this).animate({paddingLeft:"8px"}, 300);
	}, function() {
		$(this).animate({paddingLeft:"5px"});
	});

	$("#in:not(.pop)").live("click", function() {
		$("#layer").remove();
		$(".pop").removeClass("pop");
		var offset = $(this).offset();
		$(this).addClass("pop");
		$("select,embed,object").css("visibility", "hidden");
		$("body").append('<div id="layer"><span class="close" title="Затвори"></span><div class="ctr"><div class="ltcr"><div class="rtcr"><div class="tln"></div></div></div><div class="lln"><div class="rln"><div id="con"><div class="popup"></div></div></div></div><div class="lbcr"><div class="rbcr"><div class="bln"></div></div></div></div></div>');
		$(".popup").load("api.php?content=login-form", function() {
			$("#layer").css({"visibility":"visible", "top": + offset.top + 33 + "px", "left": + offset.left -22 + "px"});
			$("#layer").fadeIn(500);
			$(".popup input:first:visible").focus();
			$("#layer .close").click(function() {
				$("#layer").fadeOut(500, function() {
					$("#layer").remove();
					$("#in").removeClass("pop");
					$("select,embed,object").css("visibility", "visible");
				});
			});
		});
	});
	$("#fg").live("click", function() {
		$(".popup").slideToggle(function() {
			$(".popup").load("api.php?content=forgotten-form").slideToggle(function() {
				$(".popup input:first:visible").focus();
				$("#layer .close").click(function() {
					$("#layer").fadeOut(500, function() {
						$("#layer").remove();
						$("#in").removeClass("pop");
						$("select,embed,object").css("visibility", "visible");
					});
				});
			});
		});
	});
	$("#lg").live("click", function() {
		$(".popup").slideToggle(function() {
			$(".popup").load("api.php?content=login-form").slideToggle(function() {
				$(".popup input:first:visible").focus();
				$("#layer .close").click(function() {
					$("#layer").fadeOut(500, function() {
						$("#layer").remove();
						$("#in").removeClass("pop");
						$("select,embed,object").css("visibility", "visible");
					});
				});
			});
		});
	});
	$("#floginb").live("click", function() {
		jQuery.ajax({
			type: $("#flogin").attr("method"),
			url: $("#flogin").attr("action"),
			data: $("#flogin").serialize(),
			success: function(msg) {
				if (msg.length) {
					$("#flerror").html(msg);
				} else {
					window.location = window.location.href;
				}
			}
		});
		return(false);
	});
	$("#fforgotb").live("click", function() {
		jQuery.ajax({
			type: $("#fforgot").attr("method"),
			url: $("#fforgot").attr("action"),
			data: $("#fforgot").serialize(),
			success: function(msg) {
				if (msg.slice(0, 1) == "1") {
					$("#fferror").html(msg.slice(1));
				} else {
					$("#fferror").attr("class", "green").html(msg.slice(1)).nextAll().remove();
				}
			}
		});
		return(false);
	});

	$("#game:not(.pop)").live("click", function() {
		$("#layer").remove();
		$(".pop").removeClass("pop + active");
		var offset = $(this).offset();
		$(this).addClass("pop + active");
		$("body").append('<div id="layer"><span class="close" title="Затвори"></span><div class="ctr"><div class="ltcr"><div class="rtcr"><div class="tln"></div></div></div><div class="lln"><div class="rln"><div id="con"><div class="popup"></div></div></div></div><div class="lbcr"><div class="rbcr"><div class="bln"></div></div></div></div></div>');
		$(".popup").load("api.php?content=member-share-game", function() {
			$("textarea").TextAreaExpander();
			$("#layer").fadeIn(500);
			$(".popup input:first:visible").focus();
			$("#layer").css({"visibility":"visible", "top": + offset.top + "px", "left": + offset.left + "px"});
			$("#layer .close").click(function() {
				$(this).closest("#layer").fadeOut(500, function() {
					$(this).remove();
					$("#game").removeClass("pop + active");
				});
			});
			$("#fgameb").live("click", function() {
				$("#layer label").removeClass("red");
				if ($("#layer input[id=fimage]").val().replace(/^\s+|\s+$/g, "").length == 0) {
					$("#fgimage").addClass("red");
					$("#layer input[id=fimage]").focus();
				} else if ($("#layer input[id=fthumb]").val().replace(/^\s+|\s+$/g, "").length == 0) {
					$("#fgthumb").addClass("red");
					$("#layer input[id=fthumb]").focus();
				} else if ($("#layer input[id=ftitle]").val().replace(/^\s+|\s+$/g, "").length == 0) {
					$("#fgtitle").addClass("red");
					$("#layer input[id=ftitle]").focus();
				} else if ($("#layer select[id=fcat]").val().replace(/^\s+|\s+$/g, "").length == 0) {
					$("#fgcat").addClass("red");
					$("#layer select[id=fcat]").focus();
				} else if ($("#layer textarea[id=ffnote]").val().replace(/^\s+|\s+$/g, "").length == 0) {
					$("#fgnote").addClass("red");
					$("#layer textarea[id=ffnote]").focus();
				} else {
					$(this).after('<img src="i/indicator.gif" id="indicator" style="vertical-align:middle" />');
					$("#dscauth").val($.cookie("bgflashauth"));
					$("#fgame").submit();
				}
				return(false);
			});
		});
	});
	$("#video:not(.pop)").live("click", function() {
		$("#layer").remove();
		$(".pop").removeClass("pop + active");
		var offset = $(this).offset();
		$(this).addClass("pop + active");
		$("body").append('<div id="layer"><span class="close" title="Затвори"></span><div class="ctr"><div class="ltcr"><div class="rtcr"><div class="tln"></div></div></div><div class="lln"><div class="rln"><div id="con"><div class="popup"></div></div></div></div><div class="lbcr"><div class="rbcr"><div class="bln"></div></div></div></div></div>');
		$(".popup").load("api.php?content=member-share-video", function() {
			$("textarea").TextAreaExpander();
			$("#layer").fadeIn(500);
			$(".popup input:first:visible").focus();
			$("#layer").css({"visibility":"visible", "top": + offset.top + "px", "left": + offset.left + "px"});
			$("#layer .close").click(function() {
				$(this).closest("#layer").fadeOut(500, function() {
					$(this).remove();
					$("#video").removeClass("pop + active");
				});
			});
			$("#fvideob").live("click", function() {
				$("#layer label").removeClass("red");
				if ($("#layer input[id=furl]").val().replace(/^\s+|\s+$/g, "").length == 0) {
					$("#fvurl").addClass("red");
					$("#layer input[id=furl]").focus();
				} else {
					$(this).after('<img src="i/indicator.gif" id="indicator" style="vertical-align:middle" />');
					jQuery.ajax({
						type: $("#fvideo").attr("method"),
						url: $("#fvideo").attr("action"),
						data: $("#fvideo").serialize()+"&"+dscauth,
						success: function(msg) {
							$("#indicator").remove();
							if (msg.length) {
								$("#fverror").html(msg+"<br /><br />");
							} else {
								window.location = window.location.href;
							}
						}
					});
				}
				return(false);
			});
		});
	});
	$("#link:not(.pop)").live("click", function() {
		$("#layer").remove();
		$(".pop").removeClass("pop + active");
		var offset = $(this).offset();
		$(this).addClass("pop + active");
		$("body").append('<div id="layer"><span class="close" title="Затвори"></span><div class="ctr"><div class="ltcr"><div class="rtcr"><div class="tln"></div></div></div><div class="lln"><div class="rln"><div id="con"><div class="popup"></div></div></div></div><div class="lbcr"><div class="rbcr"><div class="bln"></div></div></div></div></div>');
		$(".popup").load("api.php?content=member-share-link", function() {
			$("textarea").TextAreaExpander();
			$("#layer").fadeIn(500);
			$("#layer").css({"visibility":"visible", "top": + offset.top + "px", "left": + offset.left + "px"});
			$(".popup input:first:visible").focus();
			$("#layer .close").click(function() {
				$(this).closest("#layer").fadeOut(500, function() {
					$(this).remove();
					$("#link").removeClass("pop + active");
				});
			});
			$("#flinkb").live("click", function() {
				$("#layer label").removeClass("red");
				if ($("#layer input[id=furl]").val().replace(/^\s+|\s+$/g, "").length == 0) {
					$("#flurl").addClass("red");
					$("#layer input[id=furl]").focus();
				} else {
					$(this).after('<img src="i/indicator.gif" id="indicator" style="vertical-align:middle" />');
					jQuery.ajax({
						type: $("#flink").attr("method"),
						url: $("#flink").attr("action"),
						data: $("#flink").serialize()+"&"+dscauth,
						success: function(msg) {
							$("#indicator").remove();
							if (msg.length) {
								$("#flnerror").html(msg+"<br /><br />");
							} else {
								window.location = window.location.href;
							}
						}
					});
				}
				return(false);
			});
		});
	});
	$("#note:not(.pop)").live("click", function() {
		$("#layer").remove();
		$(".pop").removeClass("pop + active");
		var offset = $(this).offset();
		$(this).addClass("pop + active");
		$("body").append('<div id="layer"><span class="close" title="Затвори"></span><div class="ctr"><div class="ltcr"><div class="rtcr"><div class="tln"></div></div></div><div class="lln"><div class="rln"><div id="con"><div class="popup"></div></div></div></div><div class="lbcr"><div class="rbcr"><div class="bln"></div></div></div></div></div>');
		$(".popup").load("api.php?content=member-share-note", function() {
			$("textarea").TextAreaExpander();
			$("#layer").fadeIn(500);
			$("#layer").css({"visibility":"visible", "top": + offset.top + "px", "left": + offset.left + "px"});
			$(".popup input:first:visible").focus();
			$("#layer .close").click(function() {
				$(this).closest("#layer").fadeOut(500, function() {
					$(this).remove();
					$("#note").removeClass("pop + active");
				});
			});
			$("#fnoteb").live("click", function() {
				$("#layer label").removeClass("red");
				if ($("#layer input[id=ftitle]").val().replace(/^\s+|\s+$/g, "").length == 0) {
					$("#fntitle").addClass("red");
					$("#layer input[id=ftitle]").focus();
				} else if ($("#layer textarea[id=ffnote]").val().replace(/^\s+|\s+$/g, "").length == 0) {
					$("#fnnote").addClass("red");
					$("#layer textarea[id=ffnote]").focus();
				} else {
					$(this).after('<img src="i/indicator.gif" id="indicator" style="vertical-align:middle" />');
					jQuery.ajax({
						type: $("#fnote").attr("method"),
						url: $("#fnote").attr("action"),
						data: $("#fnote").serialize()+"&"+dscauth,
						success: function(msg) {
							$("#indicator").remove();
							if (msg.length) {
								$("#fnerror").html(msg+"<br /><br />");
							} else {
								window.location = window.location.href;
							}
						}
					});
				}
				return(false);
			});
		});
	});
	$("#mood:not(.pop)").live("click", function() {
		$("#layer").remove();
		$(".pop").removeClass("pop + active");
		var offset = $(this).offset();
		$(this).addClass("pop + active");
		$("body").append('<div id="layer"><span class="close" title="Затвори"></span><div class="ctr"><div class="ltcr"><div class="rtcr"><div class="tln"></div></div></div><div class="lln"><div class="rln"><div id="con"><div class="popup"></div></div></div></div><div class="lbcr"><div class="rbcr"><div class="bln"></div></div></div></div></div>');
		$(".popup").load("api.php?content=mood", function() {
			$("textarea").TextAreaExpander();
			$("#layer").fadeIn(500);
			$("#layer").css({"visibility":"visible", "top": + offset.top + "px", "left": + offset.left + "px"});
			$(".popup textarea:first:visible").focus();
			$("#layer .close").click(function() {
				$(this).closest("#layer").fadeOut(500, function() {
					$(this).remove();
					$("#mood").removeClass("pop + active");
				});
			});
			$("#fmoodb").live("click", function() {
				if ($("#layer textarea").val().replace(/^\s+|\s+$/g, "").length > 0) {
					$(this).after('<img src="i/indicator.gif" id="indicator" style="vertical-align:middle" />');
					jQuery.ajax({
						type: $("#fmood").attr("method"),
						url: $("#fmood").attr("action"),
						data: $("#fmood").serialize()+"&"+dscauth,
						success: function(msg) {
							$("#indicator").remove();
							$("#prf .status").fadeOut(500).remove();
							$("#prf h1").after(' <span class="status" style="display:none">'+msg+' <span class="date">току-що</span> <span class="del" id="delmood">изчисти</span></span>');
							$("#prf .status").fadeIn(500);
							$("#layer").fadeOut(500, function() {
								$("#layer").remove();
								$("#mood").removeClass("pop + active");
							});
							// re-initialize del handler
							$("#prf .del[id=delmood]").live("click", function() {
								var sthis = $(this);
								jQuery.ajax({
									type: "post",
									url: "api.php?content=mood",
									data: "mood="+"&"+dscauth,
									success: function(msg) {
										sthis.closest(".status").fadeOut(500, function() {
											sthis.remove();
										});
									}
								});
							});
						}
					});
				} else {
					$("#fmerror").html("текст").fadeIn(1000);
					$(".popup textarea:first:visible").focus();
				}
				return(false);
			});
		});
	});
	$("#prf .del[id=delmood]").live("click", function() {
		var sthis = $(this);
		jQuery.ajax({
			type: "post",
			url: "api.php?content=mood",
			data: "mood="+"&"+dscauth,
			success: function(msg) {
				sthis.closest(".status").fadeOut(500, function() {
					sthis.remove();
				});
			}
		});
	});

	$("#comments").live("click", function() {
		if (!$(this).is(".active")) {
			$(this).addClass("active").closest(".inv").next().after('<div id="cmts"></div>');
			$("#cmts").load("api.php?content=comments&image_id="+himid);
			$("#comment").addClass("ready");
		}
		var target = $("#cmts").offset().top;
		$("html").animate({scrollTop:target -40}, "slow");
	});
	$("#comment").live("click", function() {
		if ($(this).is(".ready")) {
			var target = $("#addcmt").offset().top;
			if (target) {
				$("html").animate({scrollTop:target -100}, "slow");
				$("#addcmt textarea:first:visible").focus();
			} else {
				target = $("#cmts").offset().top;
				$("html").animate({scrollTop:target -40}, "slow");
			}
		} else {
			$(this).addClass("ready").closest(".inv").next().after('<div id="cmts"></div>');
			$("#cmts").load("api.php?content=comments&image_id="+himid, function() {
				var target = $("#addcmt").offset().top;
				if (target) {
					$("html").animate({scrollTop:target -100},"slow");
					$("#addcmt textarea:first:visible").focus();
				} else {
					target = $("#cmts").offset().top;
					$("html").animate({scrollTop:target -40}, "slow");
				}
				$("#comments").addClass("active");
			});
		}
	});
	$("#fcommentb").live("click", function() {
		$(this).after('<img src="i/indicator.gif" id="indicator" style="vertical-align:middle" />');
		jQuery.ajax({
			type: $("#fcomment").attr("method"),
			url: $("#fcomment").attr("action"),
			data: $("#fcomment").serialize()+"&"+dscauth,
			success: function(msg) {
				$("#indicator").remove();
				if (msg.length) {
					$("#fcerror").html(msg+"<br />");
				} else {
					hpage = 1;
					hsdir = "desc";
					$("#cmts").load("api.php?content=comments&image_id="+himid+"&page="+hpage+"&sdir="+hsdir);
					var target = $("#cmts").offset().top;
					$("html").animate({scrollTop:target -40}, "slow");
				}
			}
		});
		return(false);
	});

	$(".fav:not(.faved)").live("click", function() {
		var sthis = $(this);
		sthis.html('Записвам<img src="i/indicator.gif" style="vertical-align:middle" />');
		jQuery.ajax({
			type: "post",
			url: "api.php?content=add2fav&image_id="+himid,
			data: dscauth,
			success: function(msg) {
				sthis.addClass("faved favedh").html("Премахни от колекция");
				if (msg.length) {
					sthis.html(msg);
				}
			}
		});
	});
	$(".fav.faved").live("click", function() {
		var sthis = $(this);
		sthis.html('Записвам<img src="i/indicator.gif" style="vertical-align:middle" />');
		jQuery.ajax({
			type: "post",
			url: "api.php?content=favdel&image_id="+himid,
			data: dscauth,
			success: function(msg) {
				sthis.removeClass("faved favedh").html("Добави в колекция");
				if (msg.length) {
					sthis.html(msg);
				}
			}
		});
	});
	$(".favdel").live("click", function() {
		var sthis = $(this);
		jQuery.ajax({
			type: "post",
			url: "api.php?content=favdel&image_id="+sthis.attr("id").slice(5),
			data: dscauth,
			success: function(msg) {
				if (msg.length) {
					sthis.html(msg);
				} else {
					sthis.closest("li").fadeOut(500, function() {
						sthis.closest("li").remove();
						if (!$(".lst li").size()) {
							$(".lst").html('<p align="center">Нямате флашове в колекцията.</p>');
						}
					});
				}
			}
		});
	});
	$(".fav").live("mouseover", function() {
		$(this).addClass("favh");
	}).live("mouseout", function() {
		$(this).removeClass("favh");
	});
	$(".faved").live("mouseover", function() {
		$(this).addClass("favedh");
	}).live("mouseout", function() {
		$(this).removeClass("favedh");
	});

	$("#friend-add").live("click", function() {
		var sthis = $(this);
		if (!sthis.is(".active")) {
			sthis.addClass("active").html("Записвам");
			jQuery.ajax({
				type: "post",
				url: "api.php?content=friend-add&friend_id="+huid,
				data: dscauth,
				success: function(msg) {
					sthis.removeClass("active").addClass("del").attr("id", "friend-del");
					if (msg.length) {
						sthis.html(msg);
					} else {
						sthis.html("Спри да следваш");
					}
				}
			});
		}
	});
	$("#friend-del").live("click", function() {
		var sthis = $(this);
		if (!sthis.is(".active")) {
			sthis.addClass("active").html("Записвам");
			jQuery.ajax({
				type: "post",
				url: "api.php?content=friend-del&friend_id="+huid,
				data: dscauth,
				success: function(msg) {
					sthis.removeClass("active").removeClass("del").attr("id", "friend-add");
					if (msg.length) {
						sthis.html(msg);
					} else {
						sthis.html("Последвай");
					}
				}
			});
		}
	});

	$(".rating.custom.unrated .star").live("mouseover", function() {
		$(this).children("span").addClass("hstar");
		$(this).prevAll(".star").find("span").addClass("hstar");
		$(this).nextAll(".star").children("span").addClass("istar");
	}).live("mouseout", function() {
		$(this).closest(".rating").find(".star span").removeClass("hstar istar");
	});
	$(".rating.custom.voted .star").live("mouseover", function() {
		if (!$(this).closest(".rating").find(".temp").size()) {
			$(this).closest("div").find(".rating").append('<li class="temp-voted">Вече сте гласували.</li>');
		}
	}).live("mouseout", function() {
		if ($(this).closest(".rating").find(".temp-voted").size()) {
			$(this).closest(".rating").find(".temp-voted").remove();
		}
	});
	$(".rating.custom.own .star").live("mouseover", function() {
		$(this).closest("div").find(".rating").append('<li class="temp-own">Не може да гласувате за себе си.</li>');
	}).live("mouseout", function() {
		$(this).closest(".rating").find(".temp-own").remove();
	});
	$(".rating.custom.unrated .star").live("click", function() {
		var rate = $(this).prevAll().size();
		//$(this).children("span").addClass("starred").removeClass("hstarred");
		//$(this).prevAll(".star").find("span").addClass("starred").removeClass("hstarred");
		//$(this).nextAll(".star").children("span").removeAttr("class");
		//$(this).closest(".rating").removeClass("unrated");
		var sthis = $(this);
		sthis.closest("div").find(".rating").append('<img src="i/indicator.gif" style="vertical-align:middle" />');
		jQuery.ajax({
			type: "post",
			url: "api.php?content=rate&image_id="+himid,
			data: "rate="+rate+"&"+dscauth,
			success: function(msg) {
				if (msg.slice(0, 1) == "1") {
					sthis.closest(".rating").find(".temp").remove();
					sthis.closest(".rating").append('<li class="temp red">'+msg.slice(1)+'</li>');
				} else {
					//var num = parseInt(sthis.closest(".rating").find(".votes span").text());
					//sthis.closest(".rating").find(".votes span").text(num + 1);
					sthis.closest(".rating").before(msg);
					sthis.closest("div").find(".rating").append('<li class="temp">Гласуването е успешно.</li>');
					sthis.closest(".rating").remove();
				}
				setTimeout(function() {
					$(".temp").fadeOut(500, function() {
						$(this).remove();
					})
				}, 3000);
			}
		});
	});

	$("#profile-info").live("click", function() {
		$(this).parent(".tabs").children("li.active").removeClass("active");
		$(this).addClass("active");
		$(this).closest(".tabctn").children(".tabc").load("api.php?content=profile-info");
	});
	$("#profile-pass").live("click", function() {
		$(this).parent(".tabs").children("li.active").removeClass("active");
		$(this).addClass("active");
		$(this).closest(".tabctn").children(".tabc").load("api.php?content=profile-pass", function() {
			$("#fppass input:first:visible").focus();
		});
	});
	$("#profile-prefs").live("click", function() {
		$(this).parent(".tabs").children("li.active").removeClass("active");
		$(this).addClass("active");
		$(this).closest(".tabctn").children(".tabc").load("api.php?content=profile-prefs");
	});
	$("#avatar:not(.pop)").live("click", function() {
		$("#layer").remove();
		$(".pop").removeClass("pop");
		var offset = $(this).offset();
		$(this).addClass("pop + active");
		$("body").append('<div id="layer"><span class="close" title="Затвори"></span><div class="ctr"><div class="ltcr"><div class="rtcr"><div class="tln"></div></div></div><div class="lln"><div class="rln"><div id="con"><div class="popup"></div></div></div></div><div class="lbcr"><div class="rbcr"><div class="bln"></div></div></div></div></div>');
		$(".popup").load("api.php?content=profile-avatar", function() {
			$("#layer").fadeIn(500);
			$(".popup input:first:visible").focus();
			$("#layer").css({"visibility":"visible", "top": + offset.top + "px", "left": + offset.left + "px"});
			$("#layer .close").click(function() {
				$(this).closest("#layer").fadeOut(500, function() {
					$(this).remove();
					$("#avatar").removeClass("pop + active");
				});
			});
			$("#favatarb").live("click", function() {
				if ($("#layer input").val().replace(/^\s+|\s+$/g, "").length > 0) {
					$("#indicator").remove();
					$(this).after('<img src="i/indicator.gif" id="indicator" style="vertical-align:middle" />');
					$("#dscauth").val($.cookie("bgflashauth"));
					$("#favatar").submit();
				} else {
					$("#faerror").html("сега").fadeIn(1000);
					return(false);
				}
			});
		});
	});
	$(".pb .del[id=delavatar]").live("click", function() {
		var sthis = $(this);
		jQuery.ajax({
			type: "post",
			url: "api.php?content=profile-avatar",
			data: "del="+"&"+dscauth,
			success: function(msg) {
				sthis.closest(".ult").children("li").children("a").children("img").fadeOut(500, function() {
					sthis.closest(".ult").children("li").children("a").children("img").attr("src", msg).fadeIn(500);
				});
				sthis.fadeOut(500, function() {
					sthis.remove();
				});
			}
		});
	});
	$("#fpinfob").live("click", function() {
		$(this).after('<img src="i/indicator.gif" id="indicator" style="vertical-align:middle" />');
		jQuery.ajax({
			type: $("#fpinfo").attr("method"),
			url: $("#fpinfo").attr("action"),
			data: $("#fpinfo").serialize()+"&"+dscauth,
			success: function(msg) {
				$("#indicator").remove();
				$("#fpierrort").css({"display":$("#dummy").css("display")});
				if (msg.length) {
					$("#fpierror").removeClass("green").addClass("red").html(msg);
				} else {
					$("#fpierror").removeClass("red").addClass("green").html("Данните са променени успешно.");
				}
			}
		});
		return(false);
	});
	$("#fppassb").live("click", function() {
		$(this).after('<img src="i/indicator.gif" id="indicator" style="vertical-align:middle" />');
		jQuery.ajax({
			type: $("#fppass").attr("method"),
			url: $("#fppass").attr("action"),
			data: $("#fppass").serialize()+"&"+dscauth,
			success: function(msg) {
				$("#indicator").remove();
				$("#fpperrort").css({"display":$("#dummy").css("display")});
				if (msg.length) {
					$("#fpperror").removeClass("green").addClass("red").html(msg);
				} else {
					$("#fpperror").removeClass("red").addClass("green").html("Паролата е променена успешно.");
				}
			}
		});
		return(false);
	});
	$("#fpprefsb").live("click", function() {
		$(this).after('<img src="i/indicator.gif" id="indicator" style="vertical-align:middle" />');
		jQuery.ajax({
			type: $("#fpprefs").attr("method"),
			url: $("#fpprefs").attr("action"),
			data: $("#fpprefs").serialize()+"&"+dscauth,
			success: function(msg) {
				$("#indicator").remove();
				$("#fpprerrort").css({"display":$("#dummy").css("display")});
				if (msg.length) {
					$("#fpprerror").removeClass("green").addClass("red").html(msg);
				} else {
					$("#fpprerror").removeClass("red").addClass("green").html("Настройките са променени успешно.");
				}
			}
		});
		return(false);
	});

	$("#users-top").live("click", function() {
		$(this).parent(".tabs").children("li.active").removeClass("active");
		$(this).addClass("active");
		$("#section").val(hsec = "users-top");
		$(this).closest(".tabctn").children(".tabc").load("api.php?content="+hsec+"&page="+hpage+"&"+$("#fusers").serialize());
	});
	$("#users-new").live("click", function() {
		$(this).parent(".tabs").children("li.active").removeClass("active");
		$(this).addClass("active");
		$("#section").val(hsec = "users-new");
		$(this).closest(".tabctn").children(".tabc").load("api.php?content="+hsec+"&page="+hpage+"&"+$("#fusers").serialize());
	});
	$("#users-online").live("click", function() {
		$(this).parent(".tabs").children("li.active").removeClass("active");
		$(this).addClass("active");
		$("#section").val(hsec = "users-online");
		$(this).closest(".tabctn").children(".tabc").load("api.php?content="+hsec+"&page="+hpage+"&"+$("#fusers").serialize());
	});
	$("#users-follow").live("click", function() {
		$(this).parent(".tabs").children("li.active").removeClass("active");
		$(this).addClass("active");
		$("#section").val(hsec = "users-follow");
		$(this).closest(".tabctn").children(".tabc").load("api.php?content="+hsec+"&page="+hpage+"&"+$("#fusers").serialize());
	});
	$("#users-followers").live("click", function() {
		$(this).parent(".tabs").children("li.active").removeClass("active");
		$(this).addClass("active");
		$("#section").val(hsec = "users-followers");
		$(this).closest(".tabctn").children(".tabc").load("api.php?content="+hsec+"&page="+hpage+"&"+$("#fusers").serialize());
	});
	$("#users-mutual").live("click", function() {
		$(this).parent(".tabs").children("li.active").removeClass("active");
		$(this).addClass("active");
		$("#section").val(hsec = "users-mutual");
		$(this).closest(".tabctn").children(".tabc").load("api.php?content="+hsec+"&page="+hpage+"&"+$("#fusers").serialize());
	});

	$("#ut-period-week:not(.active)").live("click", function() {
		$("#period").val("ut-period-week");
		$(this).closest(".tabctn").children(".tabc").load("api.php?content="+hsec+"&page="+hpage+"&"+$("#fusers").serialize());
	});
	$("#ut-period-month:not(.active)").live("click", function() {
		$("#period").val("ut-period-month");
		$(this).closest(".tabctn").children(".tabc").load("api.php?content="+hsec+"&page="+hpage+"&"+$("#fusers").serialize());
	});
	$("#ut-period-year:not(.active)").live("click", function() {
		$("#period").val("ut-period-year");
		$(this).closest(".tabctn").children(".tabc").load("api.php?content="+hsec+"&page="+hpage+"&"+$("#fusers").serialize());
	});

	$(".sh").live("mouseover", function() {
		$(this).addClass("sho");
	}).live("mouseout", function() {
		$(this).removeClass("sho");
	});
	$(".sh").live("click", function() {
		$(this).closest("p").children(".shid").removeClass("shid").next().remove();
	});

	// del handler
	$(document).click(function(e) {
		if (!$(e.target).closest(".mywall").length) {
			$(".wmu.active").remove();
		}
	});
	$(".mywall").live("mouseover", function() {
		$(this).children("div:not(.nv,.prot)").hover(function() {
			if (!$(this).find(".wmu").length) {
				$(this).not(".active").prepend('<ul class="wmu"><li><span class="delc">Изтрий</span><ul><li>Потвърждавам</li></ul></li></ul>');
			}
		}, function() {
			$(this).find(".wmu:not(.active)").remove();
		});
	});
	$(".mywall > div:not(.nv)").live("click", function(e) {
		if (!e.isPropagationStopped()) {
			$(this).find(".wmu").removeClass("active").find("span.active").removeClass("active").removeClass("hsf").next("ul").toggle();
		}
	});
	$(".wmu > li > span").live("click", function(e) {
		e.stopPropagation();
		$(".mywall").find(".wmu > li > span.active").not(this).closest(".wmu").remove();
		$(this).closest(".wmu").toggleClass("active");
		$(this).toggleClass("active").next("ul").toggle();
		$(".wmu ul li").hover(function() {
			$(this).addClass("hl");
		}, function() {
			$(this).removeClass("hl");
		});
	});
	$(".wmu ul li").live("click", function() {
		var sthis = $(this);
		jQuery.ajax({
			type: "post",
			url: "api.php?content="+hdelact+"&tab="+hsec+"&place="+hplace+"&item_id="+sthis.closest("div").attr("id").slice(5),
			data: dscauth,
			success: function(msg) {
				if (msg.length) {
					sthis.closest("div").children(".red").remove();
					sthis.closest("div").prepend('<div class="red">'+msg+'</div>');
				} else {
					if (hdelact == "message-del") {
						$(".tabc.mywall").fadeOut(500, function() {
							$(this).html('<div>Съобщението е изтрито успешно. Продължете от <a href="message">тук</a>.</div>').removeClass("mywall").fadeIn();
						});
					} else {
						sthis.closest("div").fadeOut(500, function() {
							sthis.remove();
						});
					}
				}
			}
		});
	});

	// code reload
	$("#chgimgsec").live("click",function(){
		jQuery.ajax({
			type: "post",
			url: "api?content=genimgsec",
			success: function(msg) {
				var data = msg.split("\n");
				$("#code").attr("src","regimg?sec="+data[0]);
				$("#comimgsec").val(data[1]);
			}
		});
	});

	// misc
	var dscauth = "dscauth="+$.cookie("bgflashauth");
	$.ajaxSetup({ cache: false });

	var ff = false;
	if (/firefox/i.test(navigator.userAgent)) { ff = true; }
});
