var oid_res = {
	'vk':{ r:'http://vkontakteid.ru/', d:true },
	'lj':{ r:'ваше-имя.livejournal.com', b:8 },
	'yandex':{ r:'ваше.имя@yandex.ru', b:8 },
	'google':{ r:'https://www.google.com/accounts/o8/id', d:true },
	'default':{ r:'' }
	};
var oid_need_fix = { f:false };
var lacl_filtered = false;

jQuery(document).ready(function() {
	$("input:radio").attr("checked", false);

	$("#login").click(function() {
		$("#login_wnd").toggle("slow");
		$("#hide_bg").toggle("fast");
	});

	$("#logout").click(function() {
		$.cookie("zip_k", null, { path: '/' });
		window.location.replace("/");
	});

	function oid_proceed_answer(oid_answer)
	{
		alert(oid_answer);
		if ( oid_answer == "verified" )
		{
			$("#hide_bg").remove();
			$("#oid_wnd").remove();
			$("#login_wnd").remove();
			$("#login").attr("id", "logout");
			$("#logout").html("Выйти.");
		}
		else
		{
			$("#hide_bg").show();
			$("#msg_popup").show();
		}
	}

	$("#oid_auth_form").submit(function() {
		$("#login_wnd").hide("fast");
		$("#hide_bg").hide("fast");
	});

	$("table.msg_login_oid_list td").click(function() {
		try {
			if ( $(this).attr("id").match(/^oid_.+$/) )
			{
				var op = $(this).attr("id").replace(/^oid_(.+)/, "$1");
				$("#oid").removeAttr('readonly');
				if ( oid_res[op]["r"] != undefined )
				{
					$("#oid").val(oid_res[op]["r"]);
					if ( oid_res[op]["d"] == true )
						$("#oid").attr('readonly', 'readonly');
					if ( oid_res[op]["b"] != undefined )
					{
						oid_need_fix["f"] = true;
						oid_need_fix["b"] = oid_res[op]["b"];
						if ( oid_res[op]["e"] != undefined )
							oid_need_fix["e"] = oid_res[op]["e"];
					}
				}
			}
		} catch(e) {
			// ignore error
		}
	});

	$("#oid").focus(function() {
		if ( oid_need_fix["f"] )
		{
			oid_need_fix["f"] = false;
			if ( oid_need_fix["e"] != undefined )
				$("#oid").val($("#oid").val().slice(oid_need_fix["b"], oid_need_fix["e"]));
			else
				$("#oid").val($("#oid").val().slice(oid_need_fix["b"]));
			oid_need_fix["b"] = undefined;
			oid_need_fix["e"] = undefined;
		}
	});

	$("div.law_cont div.law_article_title:first").addClass("law_article_title_active");
	$("div.law_cont div.law_article_cont:not(:first)").hide();
	$("div.law_cont div.law_article_title").click(function(){
		$(this).next("div.law_article_cont").slideToggle("fast")
			.siblings("div.law_article_cont:visible").slideUp("fast");
		$(this).toggleClass("law_article_title_active");
		$(this).siblings("div.law_article_title").removeClass("law_article_title_active");
	});

	$("div.law_cont div.law_article_add_cmt_title").click(function(){
		$(this).next("div.law_article_add_cmt").slideToggle("fast");
	});

	$("div.law_article_add_cmt form input:button").click(function(){
		$(this).parent().find("input[name='lac_vote']").val($(this).attr("name"));
		if ( $(this).closest("form").find("textarea").val().length < 5 )
		{
			alert("Комментарий слишком краток");
			return false;
		}
		$(this).closest("form").append($("#common_captcha"));
		$("#hide_bg").show();
		$("#common_captcha").show();
		return true;
	});

	$("img.lacl_comment_rate_pos").click(function(){
		var cid = $(this).closest("div").attr("id").replace(/^lacl_id_(.+)/, "$1");
		$.ajax({
			type: "POST",
			contentType: "application/x-www-form-urlencoded; charset=utf-8",
			//contentType: "application/json; charset=utf-8",
			url: "/cmt_rate.cgi",
			dataType: "json",
			data: { "area":0, "cid":cid, "rate":1 },
			success: function(msg) {
				if ( msg.status && msg.cid.match(/^\d+$/) && msg.rating.match(/^[\+-]?\d+$/) )
				{
					$("#lacl_id_" + msg.cid).find("div.lacl_comment_rating").html(msg.rating);
					if ( msg.rating == -5 )
						$("#lacl_id_" + msg.cid).closest("div.lacl_comment_block").find("div.lacl_comment").removeClass("lacl_comment_negative");
				}
			}
		});
	});

	$("img.lacl_comment_rate_neg").click(function(){
		var cid = $(this).closest("div").attr("id").replace(/^lacl_id_(.+)/, "$1");
		$.ajax({
			type: "POST",
			contentType: "application/x-www-form-urlencoded; charset=utf-8",
			//contentType: "application/json; charset=utf-8",
			url: "/cmt_rate.cgi",
			dataType: "json",
			data: { "area":0, "cid":cid, "rate":0 },
			success: function(msg) {
				if ( msg.status && msg.cid.match(/^\d+$/) && msg.rating.match(/^[\+-]?\d+$/) )
				{
					$("#lacl_id_" + msg.cid).find("div.lacl_comment_rating").html(msg.rating);
					if ( msg.rating == -6 )
						$("#lacl_id_" + msg.cid).closest("div.lacl_comment_block").find("div.lacl_comment").addClass("lacl_comment_negative");
				}
			}
		});
	});

	$("div.article_add_cmt_cont div.article_add_cmt_title").click(function(){
		$(this).next("div.article_add_cmt").slideToggle("fast");
	});

	$("div.article_add_cmt_cont form input:button").click(function(){
		$(this).parent().find("input[name='ac_vote']").val($(this).attr("name"));
		if ( $(this).closest("form").find("textarea").val().length < 5 )
		{
			alert("Комментарий слишком краток");
			return false;
		}
		$(this).closest("form").append($("#common_captcha"));
		$("#hide_bg").show();
		$("#common_captcha").show();
		return true;
	});

	$("img.acl_comment_rate_pos").click(function(){
		var cid = $(this).closest("div").attr("id").replace(/^acl_id_(.+)/, "$1");
		$.ajax({
			type: "POST",
			contentType: "application/x-www-form-urlencoded; charset=utf-8",
			//contentType: "application/json; charset=utf-8",
			url: "/cmt_rate.cgi",
			dataType: "json",
			data: { "area":1, "cid":cid, "rate":1 },
			success: function(msg) {
				if ( msg.status && msg.cid.match(/^\d+$/) && msg.rating.match(/^[\+-]?\d+$/) )
				{
					$("#acl_id_" + msg.cid).find("div.acl_comment_rating").html(msg.rating);
					if ( msg.rating == -5 )
						$("#acl_id_" + msg.cid).closest("div.acl_comment_block").find("div.acl_comment").removeClass("acl_comment_negative");
				}
			}
		});
	});

	$("img.acl_comment_rate_neg").click(function(){
		var cid = $(this).closest("div").attr("id").replace(/^acl_id_(.+)/, "$1");
		$.ajax({
			type: "POST",
			contentType: "application/x-www-form-urlencoded; charset=utf-8",
			//contentType: "application/json; charset=utf-8",
			url: "/cmt_rate.cgi",
			dataType: "json",
			data: { "area":1, "cid":cid, "rate":0 },
			success: function(msg) {
				if ( msg.status && msg.cid.match(/^\d+$/) && msg.rating.match(/^[\+-]?\d+$/) )
				{
					$("#acl_id_" + msg.cid).find("div.acl_comment_rating").html(msg.rating);
					if ( msg.rating == -6 )
						$("#acl_id_" + msg.cid).closest("div.acl_comment_block").find("div.acl_comment").addClass("acl_comment_negative");
				}
			}
		});
	});

	$("div.lacl_sort_proceed input:button").click(function(){
		var f = $(this).closest("div.law_user_comments_cont");
		lacl_filtered = !lacl_filtered;
		if ( lacl_filtered )
		{
			if ( f.find("input:radio[name=lacl_sort_vote]:checked").val() )
				f.find("div.lacl_fake_item").not("div.lacl_fake_" + f.find("input:radio[name=lacl_sort_vote]:checked").val()).hide();
			if ( f.find("input:radio[name=lacl_sort_rating]:checked").val() )
				f.find("div.lacl_fake_item").not("div.lacl_fake_" + f.find("input:radio[name=lacl_sort_rating]:checked").val()).hide();
			if ( f.find("input:radio[name=lacl_sort_type]:checked").val() )
				f.find("div.lacl_fake_item").not("div.lacl_fake_type" + f.find("input:radio[name=lacl_sort_type]:checked").val()).hide();
		}
		else
		{
			f.find("div.lacl_fake_item").show();
			f.find("input:radio").attr("checked", false);
		}
	});

	$("#data_add").submit(function(){
		if ( $(this).find("#data_add_name").val().length < 5 )
		{
			alert("Название документа слишком кратко");
			return false;
		}
		if ( $(this).find("textarea").val().length < 50 )
		{
			alert("Текст документа слишком краток");
			return false;
		}
		return true;
	});

	$("#survey_submit").click(function(){
		var res = [0, 0, 0, 0];
		$("#survey_cont input:radio:checked").each(function(){
			switch ( $(this).val() )
			{
				case "a1":
					res[0] += 1;
					break;
				case "a4":
					res[1] += 1;
					break;
				case "a2":
					res[2] += 1;
					break;
				case "a3":
					res[3] += 1;
					break;
				default:
					break;
			}
		});
		$.ajax({
			type: "POST",
			contentType: "application/x-www-form-urlencoded; charset=utf-8",
			url: "/survey/diagram/",
			data: { "res[]":res },
			success: function(msg) {
				$("#survey_diagram").attr("src", "data:image/png;base64," + msg);
			}
		});
	});

	$("#common_captcha_cancel").click(function(){
		$("#common_captcha").hide();
		$("#hide_bg").hide();
		$(this).closest("form").find("input[name='lac_vote']").val('');
		$(this).closest("form").find("textarea[name='lac_content']").val('');
		$(this).closest("form").closest("div.law_article_add_cmt").slideToggle("fast");
		return false;
	});

	$("div.tab_title_cont span.tab_title").click(function(){
		$("div.tab_title_cont span.tab_title").removeClass("tab_title_active");
		$(this).addClass("tab_title_active");
		$("div.tab_article_cont").hide();
		$("#tab_" + $(this).attr("id")).show();
	});

	$("#msg_popup_cancel").click(function(){
		$("#msg_popup").hide();
	});
});

