$(document).ready(function() {

	if (segment(2) == "flat")
	{
		DaGallery.init();
	}
	
	$("div.sub_menu_global_box").hover(function() {
		var id = $(this).attr("id");
		
		$(this).children("a").css("backgroundPosition", "0px -40px");
		$(this).children("a").children("p.text_main_menu_left").css("backgroundPosition", "0px -40px");
		$(this).children("a").children("p.text_main_menu_right").css("backgroundPosition", "right -40px");
		$(this).children("a").children("p").children("span").children("span.text_main_menu_bottom").css("color", "#73a7f8");

		if($("*").is("#" + id + " div.sub_menu_box"))
		{
			if($("#" + id + " div.sub_menu_box").attr("id") == "last_submenu")
			{
				var top = $(this).offset().top + $(this).outerHeight();
				var right = $(window).width() - $(this).offset().left - $(this).width();
				var min_width = 150;
				
				$("#" + id + " div.sub_menu_box").css("top", top);
				$("#" + id + " div.sub_menu_box").css("right", right);
				$("#" + id + " div.sub_menu_box").css("min-width", min_width);
				
				$("#" + id + " div.sub_menu_box").show();
			}
			else
			{
				var top = $(this).offset().top + $(this).outerHeight();
				var left = $(this).offset().left;
				var min_width = $(this).outerWidth();
				
				$("#" + id + " div.sub_menu_box").css("top", top);
				$("#" + id + " div.sub_menu_box").css("left", left);
				$("#" + id + " div.sub_menu_box").css("min-width", min_width);
				
				var right_menu = parseInt(left) + parseInt($("#" + id + " div.sub_menu_box").width());
				var right_limit = parseInt($("div.site").offset().left) + parseInt($("div.site").width()) - 4;
				var delta = right_menu - right_limit;
				if(delta > 0)
				{
					var max_width = parseInt($("#" + id + " div.sub_menu_box").width()) - delta;
					$("#" + id + " div.sub_menu_box").css("max-width", max_width);
				}
				
				$("#" + id + " div.sub_menu_box").show();
			}
		}
	}, function() {
		var id = $(this).attr("id");
		
		$(this).children("a").css("backgroundPosition", "0px 0px");
		$(this).children("a").children("p.text_main_menu_left").css("backgroundPosition", "0px 0px");
		$(this).children("a").children("p.text_main_menu_right").css("backgroundPosition", "right 0px");
		$(this).children("a").children("p").children("span").children("span.text_main_menu_bottom").css("color", "#ffbacf");
		
		if($("*").is("#" + id))
		{
			$("#" + id + " div.sub_menu_box").hide();
		}
	});
	
	$("a.link_pages_arrow").click(function() {
		var object = $(this).parent().children().children("div.add_pages_box");
		$(object).children().children("input.pages_number").val("");
		$(object).animate({"height": "show", "width": "show"}, "fast");
	});

	$(document).click(function(event) {
		if($("*").is("div.add_pages_box"))
		{
			$("div.add_pages_box").each(function() {
				if($(this).css("display") != "none")
				{
					if($(event.target).parents().filter('div.add_pages_box').length != 1 && $(this).width() > 10)
					{
						$(this).animate({"height": "hide", "width": "hide"}, "fast");
					}
				}
			});
		}
	});

	$("input.pages_number").keypress(function(key) {
		if(key.keyCode == 13)
		{
			$("input.change_page").click();
		}
	});
	
	$("input.change_page").click(function() {
		var page = $.trim($(this).parent().children("input.pages_number").val());
		if(page != "")
		{
			if(page * 1 >= 1 && page * 1 < 5000)
			{
				if(page * 1 > $.trim($(this).parent().children("input.max_page").val()) * 1)
				{
					page = $.trim($(this).parent().children("input.max_page").val()) * 1;
				}
				
				window.location.href = $.trim($(this).parent().children("input.begin_page").val()) + page + $.trim($(this).parent().children("input.end_page").val());
			}
		}
	});
	
	$("p.text_bookmark").each(function() {
		$(this).parent().css("backgroundPosition", "right -42px");
		$(this).parent().parent().css("backgroundPosition", "left -42px");
		$(this).parent().parent().parent().css("backgroundPosition", "0px -42px");
	});
	
	$("a.link_bookmark").hover(function() {
		$(this).parent().css("backgroundPosition", "right -21px");
		$(this).parent().parent().css("backgroundPosition", "left -21px");
		$(this).parent().parent().parent().css("backgroundPosition", "0px -21px");
	}, function() {
		$(this).parent().css("backgroundPosition", "right 0px");
		$(this).parent().parent().css("backgroundPosition", "left 0px");
		$(this).parent().parent().parent().css("backgroundPosition", "0px 0px");
	});
	
	if(segment(2) == "myarticles")
	{
		$("a.link_delete").click(function() {
			if(confirm("Вы действительно хотите удалить эту статью?"))
			{
				var id = $(this).attr("id").split("_");
				var post_mas = {"news_id": id[1]};
				$.post(base_url() + "pub/deleteNews/", post_mas, delete_news_result);
			}
		});
		
		function delete_news_result(data)
		{
			window.location.reload();
		}
	}
	
	if(segment(2) == "myalbums")
	{
		$("a.link_delete").click(function() {
			if(confirm("Вы действительно хотите удалить этот альбом?"))
			{
				var id = $(this).attr("id").split("_");
				var post_mas = {"albums_id": id[1]};
				$.post(base_url() + "pub/deleteAlbums/", post_mas, delete_albums_result);
			}
		});
		
		function delete_albums_result(data)
		{
			window.location.reload();
		}
	}
	
	if(segment(2) == "myphotos")
	{
		$("a.link_delete").click(function() {
			if(confirm("Вы действительно хотите удалить эту фотографию?"))
			{
				var id = $(this).attr("id").split("_");
				var post_mas = {"photos_id": id[1]};
				$.post(base_url() + "pub/deletePhotos/", post_mas, delete_photos_result);
			}
		});
		
		function delete_photos_result(data)
		{
			window.location.reload();
		}
		
		$("input.radio").click(function() {
			var id = $(this).attr("id").split("_");
			var post_mas = {"albums_id": segment(7),
							"photos_id": id[1]};
			$.post(base_url() + "pub/mainPhotos/", post_mas, main_photos_result);
		});
		
		function main_photos_result(data)
		{
			//window.location.reload();
		}
	}
	
	$("input[name=vote]:first").attr("checked", "checked");
	
	$("#votes_show_results").click(function() {
		$("div.votes_box").addClass("hidden_votes");
		$("div.result_box").removeClass("hidden_votes");
	});
	
	$("#votes_show_votes").click(function() {
		$("div.votes_box").removeClass("hidden_votes");
		$("div.result_box").addClass("hidden_votes");
	});
	
	$("#votes_enter").click(function() {
		var id = "";
		$("input[name=vote]").each(function() {
			if($(this).attr("checked") == "checked")
			{
				id = $(this).attr("id").split("_");
			}
		});
		var post_mas = {"variants_id": id[1]};
		$.post(base_url() + "pub/saveVariants/", post_mas, votes_result);
	});
	
	function votes_result(data)
	{
		window.location.reload();
	}
	
	$("a.notauth_button").click(function() {
		var id = $(this).attr("id").split("_");
		$("div.notauth_box span.text_comments_info").css("display", "none");
		$("#" + id[0] + "_hid_notauth").css("display", "inline");
		
		$("div.black_back").animate({"opacity": "show"}, "slow");
		$("div.notauth_box").animate({"opacity": "show"}, "slow");
	});
	
	$("#top_add_topics").click(function() {
		$("div.notauth_box span.text_comments_info").css("display", "none");
		$("#topics_hid_notauth").css("display", "inline");
		
		$("div.black_back").animate({"opacity": "show"}, "slow");
		$("div.notauth_box").animate({"opacity": "show"}, "slow");
	});
	
	$("#bottom_add_topics").click(function() {
		$("div.notauth_box span.text_comments_info").css("display", "none");
		$("#topics_hid_notauth").css("display", "inline");
		
		$("div.black_back").animate({"opacity": "show"}, "slow");
		$("div.notauth_box").animate({"opacity": "show"}, "slow");
	});
	
	$("#notauth_close").click(function() {
		$("div.black_back").animate({"opacity": "hide"}, "slow");
		$("div.notauth_box").animate({"opacity": "hide"}, "slow");
	});
	
	$("#open_auth").click(function() {
		$("div.notauth_box").animate({"opacity": "hide"}, "slow", function() {
			$("div.auth_box").animate({"opacity": "show"}, "slow");
		});
	});
	
	$("#open_comments_auth").click(function() {
		if($("*").is("div.photos_show_box") && $("div.photos_show_box").css("display") != "none")
		{
			$("#is_open").val("1");
			$("div.photos_show_box").animate({"opacity": "hide"}, "slow", function() {
				$("div.auth_box").animate({"opacity": "show"}, "slow");
			});
		}
		else
		{
			$("#is_open").val("0");
			$("div.black_back").animate({"opacity": "show"}, "slow");
			$("div.auth_box").animate({"opacity": "show"}, "slow");
		}
	});
	
	if(!$.browser.msie)
	{
		$("div.social_panel").hover(function() {
			$(this).animate({"opacity": "1"}, "fast");
		}, function() {
			$(this).animate({"opacity": "0.5"}, "fast");
		});
	}
});
 
function bookmark(object)
{
	var url = window.document.location;
	var title = window.document.title;
	if($.browser.mozilla)
	{
		window.sidebar.addPanel(title, url, base_url());
	}
	else
	{
		if($.browser.opera)
		{
			$(object).attr("href", url);
			$(object).attr("title", title);
			$(object).attr("rel", "sidebar");
		}
		else
		{
			if($.browser.webkit)
			{
				alert("Нажмите Ctrl+D, чтобы добавить страницу в закладки.");
			}
			else
			{
				if($.browser.msie)
				{
					window.external.AddFavorite(url, title);
				}
				else
				{
					alert("Нажмите Ctrl+D, чтобы добавить страницу в закладки.");
				}
			}
		}
	}
}
