$(document).ready(function() {
	$("div.category").hover(function() {
		if($("#sub_" + $(this).attr("id")).css("display") == "none")
		{
			$(this).css("background-color", "#77ac17");
			$(this).css("background-position", "0px -20px");
		}
	}, function() {
		if($("#sub_" + $(this).attr("id")).css("display") == "none")
		{
			$(this).css("background-color", "#83bc1c");
			$(this).css("background-position", "0px 0px");
		}
	});
	
	if(segment(1) == "contacts")
	{
		email("info", "adress_here", "komisionka.com");
	}
	
	if(segment(1) == "help" && segment(2) == "14")
	{
		email("info", "help_adress", "komisionka.com");
	}
	
	if($("*").index($("td.stat_help_light")[0]) != -1)
	{
		tooltip("stat_help_light", {});
	}
	
	$("div.category").click(function() {
		var this_id = $(this).attr("id");
		
		$("div.category").each(function() {
			if($("#sub_" + $(this).attr("id")).css("display") != "none")
			{
				if($(this).attr("id") != this_id)
				{
					$("#sub_" + $(this).attr("id")).animate({height: "hide"}, "slow");
					$(this).css("background-color", "#83bc1c");
					$(this).css("background-position", "0px 0px");
				}
			}
		});

		if($("#sub_" + $(this).attr("id")).css("display") != "none")
		{
			$(this).css("background-color", "#83bc1c");
			$(this).css("background-position", "0px 0px");
			$("#sub_" + $(this).attr("id")).animate({height: "hide"}, "slow");
		}
		else
		{
			$(this).css("background-color", "#77ac17");
			$(this).css("background-position", "0px -20px");
			$("#sub_" + $(this).attr("id")).animate({height: "show"}, "slow");
		}
	});
	
	$("div.ad").hover(function() {
		if($("#sub_" + $(this).attr("id")).css("display") == "none")
		{
			$(this).css("background-color", "#77ac17");
			$(this).css("background-position", "0px -20px");
		}
	}, function() {
		if($("#sub_" + $(this).attr("id")).css("display") == "none")
		{
			$(this).css("background-color", "#83bc1c");
			$(this).css("background-position", "0px 0px");
		}
	});
	
	$("div.ad").click(function() {
		var this_id = $(this).attr("id");
		
		$("div.ad").each(function() {
			if($("#sub_" + $(this).attr("id")).css("display") != "none")
			{
				if($(this).attr("id") != this_id)
				{
					$("#sub_" + $(this).attr("id")).animate({height: "hide"}, "slow");
					$(this).css("background-color", "#83bc1c");
					$(this).css("background-position", "0px 0px");
				}
			}
		});

		if($("#sub_" + $(this).attr("id")).css("display") != "none")
		{
			$(this).css("background-color", "#83bc1c");
			$(this).css("background-position", "0px 0px");
			$("#sub_" + $(this).attr("id")).animate({height: "hide"}, "slow");
		}
		else
		{
			$(this).css("background-color", "#77ac17");
			$(this).css("background-position", "0px -20px");
			$("#sub_" + $(this).attr("id")).animate({height: "show"}, "slow");
		}
	});
	
	$("div.adrek").hover(function() {
		var this_id = $(this).attr("id").split("_");
		if($("#adreksub_" + this_id[1]).css("display") == "none")
		{
			$(this).css("background-color", "#77ac17");
			$(this).css("background-image", "url('/images/adrek_hover.jpg')");
		}
	}, function() {
		var this_id = $(this).attr("id").split("_");
		if($("#adreksub_" + this_id[1]).css("display") == "none")
		{
			$(this).css("background-color", "#83bc1c");
			$(this).css("background-image", "url('/images/adrek.jpg')");
		}
	});
	
	$("div.adrek").click(function() {
		var this_id = $(this).attr("id").split("_");
		
		$("div.adrek").each(function() {
			var help_id = $(this).attr("id").split("_");
			if($("#adreksub_" + help_id[1]).css("display") != "none")
			{
				if(help_id[1] != this_id[1])
				{
					$("#adreksub_" + help_id[1]).animate({height: "hide"}, "slow");
					$(this).css("background-color", "#83bc1c");
					$(this).css("background-image", "url('/images/adrek.jpg')");
				}
			}
		});

		if($("#adreksub_" + this_id[1]).css("display") != "none")
		{
			$(this).css("background-color", "#83bc1c");
			$(this).css("background-image", "url('/images/adrek.jpg')");
			$("#adreksub_" + this_id[1]).animate({height: "hide"}, "slow");
		}
		else
		{
			$(this).css("background-color", "#77ac17");
			$(this).css("background-image", "url('/images/adrek_hover.jpg')");
			$("#adreksub_" + this_id[1]).animate({height: "show"}, "slow");
		}
	});
	
	$("#auth_login").val("");
	$("#auth_pass").val("");
	
	$("#auth_login").focus(function() {
		$("#auth_mess > p").css("display", "none");
	});
	
	$("#auth_login").keypress(function(key) {
		if(key.keyCode == 13)
		{
			$("#auth_enter").click();
		}
		else
		{
			$("#auth_mess > p").css("display", "none");
		}
	});
	
	$("#auth_pass").focus(function() {
		$("#auth_mess > p").css("display", "none");
	});
	
	$("#auth_pass").keypress(function(key) {
		if(key.keyCode == 13)
		{
			$("#auth_enter").click();
		}
		else
		{
			$("#auth_mess > p").css("display", "none");
		}
	});
	
	$("#auth_enter").click(function() {
		$("#auth_mess > p").css("display", "none");
		var error = validate("auth_login", "0..9<>A..Z<>a..z<> <>_<>-<>@<>.", false, "none");
		if(error == "ok")
		{
			error = validate("auth_pass", "0..9<>A..Z<>a..z<> <>_", false, "none");
			if(error == "ok")
			{
				$("#auth_wait").css("display", "block");
				$.post(base_url() + "pub/login/", {"login": $.trim($("#auth_login").val()), "pass": $.trim($("#auth_pass").val())}, loggedin_result)
			}
			else
			{
				$("#auth_" + error + "_pass").css("display", "block");
			}
		}
		else
		{
			$("#auth_" + error + "_login").css("display", "block");
		}
	});
	
	function loggedin_result(data)
	{
		$("#auth_mess > p").css("display", "none");
		
		if(data == "database")
		{
			$("#auth_database").css("display", "block");
		}
		else
		{
			if(data == "wrong_login")
			{
				$("#auth_wrong_login").css("display", "block");
			}
			else
			{
				if(data == "wrong_pass")
				{
					$("#auth_wrong_pass").css("display", "block");
				}
				else
				{
					if(data == "notactive")
					{
						$("#auth_not_active").css("display", "block");
					}
					else
					{
						if(data == "ban")
						{
							$("#auth_ban").css("display", "block");
						}
						else
						{
							if(data == "freeze")
							{
								$("#auth_freeze").css("display", "block");
							}
							else
							{
								if(data == "empty")
								{
									$("#auth_empty").css("display", "block");
								}
								else
								{
									if(data == "ok")
									{
										$("#auth_ok").css("display", "block");
										$("#auth_login").val("");
										$("#auth_pass").val("");
	
										window.location.reload();
									}
									else
									{
										$("#auth_unknown").css("display", "block");
									}
								}
							}
						}
					}
				}
			}
		}
	}
	
	$("#auth_exit").click(function() {
		$.post(base_url() + "pub/logout/", {}, logout_result)
	});
	
	function logout_result(data)
	{
		if(data == "ok")
		{
			window.location.reload();
		}
	}
	
	$("span.span_link").hover(function() {
		$(this).css("color", "#000000");
		$(this).css("text-decoration", "none");
	}, function() {
		$(this).css("color", "#4f8618");
		$(this).css("text-decoration", "underline");
	});
	
	$("span.span_link").click(function() {
		$("#sign_mess > p").css("display", "none");
		$("#sign_wait").css("display", "block");
		$.post(base_url() + "pub/changeCaptcha/", {}, print_captcha);
	});
	
	function print_captcha(data)
	{
		$("#sign_mess > p").css("display", "none");
		$("#captcha").html(data);
	}
	
	$("#search_text").val("");
	$("#hard_search").attr("checked", false);
	
	$("#search_text").focus(function() {
		$("#search_mess > p").css("display", "none");
	});
	
	$("#search_text").keypress(function(key) {
		if(key.keyCode == 13)
		{
			$("#search").click();
		}
		else
		{
			$("#search_mess > p").css("display", "none");
		}
	});
	
	$("#hard_search").focus(function() {
		$("#search_mess > p").css("display", "none");
	});
	
	$("#hard_serach").keypress(function(key) {
		if(key.keyCode == 13)
		{
			$("#search").click();
		}
		else
		{
			$("#search_mess > p").css("display", "none");
		}
	});
	
	$("#search").click(function() {
		$("#search_mess > p").css("display", "none");
		
		if($.trim($("#search_text").val()) != "")
		{
			if($("#hard_search").attr("checked"))
			{
				$("#hard").val("yes");
			}
			
			$("#search_wait").css("display", "block");
			$("#search_form").submit();
		}
		else
		{
			$("#search_empty_text").css("display", "block");
		}
	});
	
	$("div.show_text_content").click(function() {
		var but_id = $(this).attr("id").split("_");
		var cur_id = but_id[0] + "_" + but_id[1] + "_title";
		
		if($(this).height() == "29")
		{
			$(this).animate({"height": $("#inner_" + but_id[1]).height()}, "slow", function() {
				$("#" + cur_id).css("background-position", "3px -20px");
			});
		}
		else
		{
			$(this).animate({"height": 29}, "slow", function() {
				$("#" + cur_id).css("background-position", "3px 0px");
			});
		}
	});
	
	$("div.show_contacts_title").click(function() {
		var cur_id = $(this).attr("id");
		var but_id = $(this).attr("id").split("_");
		var id = but_id[0] + "_" + but_id[1] + "_box";
		if(but_id[0] == "showtext")
		{
			if($("#" + id).height() == "29")
			{
				$("#" + id).animate({"height": $("#inner_" + but_id[1]).height()}, "slow", function() {
					$("#" + cur_id).css("background-position", "3px -20px");
				});
			}
			else
			{
				$("#" + id).animate({"height": 29}, "slow", function() {
					$("#" + cur_id).css("background-position", "3px 0px");
				});
			}
		}
		else
		{
			$("#" + id).animate({"height": "toggle"}, "slow", function() {
				if($("#" + id).css("display") != "none")
				{
					$("#" + cur_id).css("background-position", "3px -20px");
				}
				else
				{
					$("#" + cur_id).css("background-position", "3px 0px");
				}
			});
		}
	});
	
	$("div.show_contacts_title_light").click(function() {
		var cur_id = $(this).attr("id");
		var but_id = $(this).attr("id").split("_");
		var id = but_id[0] + "_" + but_id[1] + "_box";
		if(but_id[0] == "showtext")
		{
			if($("#" + id).height() == "29")
			{
				$("#" + id).animate({"height": $("#inner_" + but_id[1]).height()}, "slow", function() {
					$("#" + cur_id).css("background-position", "3px -20px");
				});
			}
			else
			{
				$("#" + id).animate({"height": 29}, "slow", function() {
					$("#" + cur_id).css("background-position", "3px 0px");
				});
			}
		}
		else
		{
			$("#" + id).animate({"height": "toggle"}, "slow", function() {
				if($("#" + id).css("display") != "none")
				{
					$("#" + cur_id).css("background-position", "3px -20px");
				}
				else
				{
					$("#" + cur_id).css("background-position", "3px 0px");
				}
			});
		}
	});
	
	$("span.numpage_span").hover(function() {
		$(this).css("color", "#ffffff");
		$(this).css("text-decoration", "none");
	}, function() {
		$(this).css("color", "#fcff00");
		$(this).css("text-decoration", "underline");
	});
	
	$("span.numpage_span").click(function() {
		$("#simple_form").attr("action", $(this).attr("id"));
		$("#simple_form").submit();
	});
	
	$("#go_to").val("");
	
	$("#go_to").keypress(function(key) {
		if(key.keyCode == 13)
		{
			$("#go_to_button").click();
		}
	});

	$("#go_to_button").click(function() {
		if($.trim($("#go_to").val()) != "")
		{
			var check = "no";
			var str = $.trim($("#go_to").val());
			for(i = 0; i < str.length; i++)
			{
				var code = str.charCodeAt(i)
				if(code < 48 || code > 57)
				{
					check = "yes";
				}
			}
			
			if(check == "no")
			{
				if(parseInt($.trim($("#go_to").val())) <= $("#max_pages").val())
				{
					if($.trim($("#go_to").val()) != segment(3))
					{
						$("#simple_form").attr("action", base_url() + "pub/simpleSearch/" + $.trim($("#go_to").val()) + "/");
						$("#simple_form").submit();
					}
				}
			}
		}
	});
	
	$("#go_toprod").val("");
	
	$("#go_toprod").keypress(function(key) {
		if(key.keyCode == 13)
		{
			$("#go_toprod_button").click();
		}
	});

	$("#go_toprod_button").click(function() {
		if($.trim($("#go_toprod").val()) != "")
		{
			var check = "no";
			var str = $.trim($("#go_toprod").val());
			for(i = 0; i < str.length; i++)
			{
				var code = str.charCodeAt(i)
				if(code < 48 || code > 57)
				{
					check = "yes";
				}
			}
			
			if(check == "no")
			{
				if(parseInt($.trim($("#go_toprod").val())) <= $("#max_pages").val())
				{
					if($.trim($("#go_toprod").val()) != segment(3))
					{
						window.location.href = base_url() + "prods/" + segment(2) + "/" + $.trim($("#go_toprod").val()) + "/";
					}
				}
			}
		}
	});
	
	$("#go_toad").val("");
	
	$("#go_toad").keypress(function(key) {
		if(key.keyCode == 13)
		{
			$("#go_toad_button").click();
		}
	});

	$("#go_toad_button").click(function() {
		if($.trim($("#go_toad").val()) != "")
		{
			var check = "no";
			var str = $.trim($("#go_toad").val());
			for(i = 0; i < str.length; i++)
			{
				var code = str.charCodeAt(i)
				if(code < 48 || code > 57)
				{
					check = "yes";
				}
			}
			
			if(check == "no")
			{
				if(parseInt($.trim($("#go_toad").val())) <= $("#max_pages").val())
				{
					if($.trim($("#go_toad").val()) != segment(3))
					{
						window.location.href = base_url() + "ads/" + segment(2) + "/" + $.trim($("#go_toad").val()) + "/";
					}
				}
			}
		}
	});
	
	$("input.input_button_prodsale").click(function() {
		if(confirm("Дійсно даний товар вже проданий?"))
		{
			var id = $(this).attr("id").split("_");
			$.post(base_url() + "pub/isSale/", {"id": id[1]}, sale_result);
		}
	});
	
	function sale_result(data)
	{
		if(data == "ok")
		{
			window.location.reload();
		}
	}
	
	$("input.input_button_proddel").click(function() {
		if(confirm("Ви дійсно бажаєте видалити даний товар?"))
		{
			var id = $(this).attr("id").split("_");
			$.post(base_url() + "pub/delProd/", {"id": id[1]}, sale_result);
		}
	});
	
	$("input.input_button_addel").click(function() {
		if(confirm("Ви дійсно бажаєте видалити дане оголошення?"))
		{
			var id = $(this).attr("id").split("_");
			$.post(base_url() + "pub/delAd/", {"id": id[1]}, sale_result);
		}
	});
	
	$("div.add_ad_button").hover(function() {
		$(this).css("background-position", "0px -75px");
	}, function() {
		$(this).css("background-position", "0px 0px");
	});
	
	$("div.add_prod_button").hover(function() {
		$(this).css("background-position", "0px -75px");
	}, function() {
		$(this).css("background-position", "0px 0px");
	});
	
	$("div.signup_button").hover(function() {
		$(this).css("background-position", "0px -100px");
	}, function() {
		$(this).css("background-position", "0px 0px");
	});
	
	$("div.signup_button").click(function() {
		window.location.href = base_url() + "signup/";
	});
});

function delfav(id)
{
	if(confirm("Ви дійсно бажаєте видалити даний товар з обраного?"))
	{
		$.post(base_url() + "pub/delFav/", {"prod_id": id}, fav_result);
	}
}

function fav_result(data)
{
	if(data == "database")
	{
		alert("Помилка бази даних. Спробуйте ще раз");
	}
	else
	{
		window.location.reload();
	}
}

function save_vyp(but_id)
{
	var id = but_id.split("_");
	if($.trim($("#vyp_" + id[1]).val()) != "")
	{
		$.post(base_url() + "pub/saveVyp/", {"num": id[1], "text": encodeURI($.trim($("#vyp_" + id[1]).val()))}, vyp_result);
	}
	else
	{
		alert("Ви не ввели текст виправдання");
	}
}

function vyp_result(data)
{
	if(data == "database")
	{
		alert("Помилка бази даних. Спробуйте ще раз");
	}
	else
	{
		if(data == "ok")
		{
			alert("Виправдання успішно збережене");
		}
		else
		{
			alert("Невідома помилка");
		}
	}
}
