var reg_login = /^[A-Za-z0-9_]+$/;
var reg_e_mail = /^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[a-z]{2,3})$/;

$(document).ready(
    function() {
        if (($.browser.msie) && ($.browser.version == "6.0")) {
            $(window).load(setWidthIE6);
            $(window).resize(setWidthIE6);
        }
        $(window).load(setStar);
        $(window).resize(setStar);
        $("span.site_counter").wrapInner(document.createElement("span")).wrapInner(document.createElement("span"));
        $("span.user_counter").wrapInner(document.createElement("span")).wrapInner(document.createElement("span"));

        $("#main_comment_form input:text").each(
            function() {
                $(this).attr("beg_val", $(this).val());
            }
        );
        $("#main_comment_form textarea").each(
            function() {
                $(this).attr("beg_val", $(this).html());
            }
        );
        $("#main_comment_form input:text").focus(
            function() {
                if ($(this).val() == $(this).attr("beg_val")) {
                    $(this).val("");
                    $(this).addClass("focus");
                }
            }
        );
        switch ($("#search_type").val()) {
	       case "site":
	           $("#search_site").addClass("active");
	           break;
	       case "author":
	           $("#search_author").addClass("active");
	           break;
	       case "motive":
	           $("#search_motive").addClass("active");
	           break;
	   }
        $("#main_comment_form textarea").each(
            function() {
                $(this).attr("beg_val", $(this).html());
            }
        );
        $("#main_comment_form textarea").focus(
            function() {
                if ($(this).html() == $(this).attr("beg_val")) {
                    $(this).html("");
                    $(this).addClass("focus");
                }
            }
        );

        $("#complaints_text a").each(
            function() {
                $(this).attr("oldclass", $(this).attr("class"));
            }
        );
        $("#complaints_type li").mouseover(
            function () {
                $(this).addClass("hover");
            }
        );
        $("#complaints_type li").mouseout(
            function () {
                $(this).removeClass("hover");
            }
        );
        $("#complaints_type li").click(changeViewComplaints);

        $("#link_dialog_login").click(
            function() {
                $("#dialog").dialog({
                    modal: true,
                    title: "Я свой, пустите меня!",
                    width: 500,
                    height: 200,
                    overlay: {opacity: 0.5, background: "black"},
                    open: function() {
                        $("#dialog").html($("#dialog_login").html());
                        $("#dialog #form_login").submit(
                            function() {
                                var errors = "";
                                if ($("#dialog #in_login").val() == "") {
                                    errors += "Введите логин!\n";
                                }
                                if ($("#dialog #in_pass").val() == "") {
                                    errors += "Введите секретный пароль!\n";
                                }
                                if (errors != "") {
                                    alert(errors);
                                    return false;
                                }
                            }
                        );
                        $("#dialog").show();
                        $("#dialog #in_login").focus();
                    },
                    close:function(){$("#dialog").dialog("destroy");}
                });
            }
        );

		$("#main_comment_form").submit(
			function() {
				var errors = "";
				if ($("#name").length > 0 && ($("#name").val() == $("#name").attr("beg_val") || $("#name").val() == "")) {
					errors += "Введите имя!\n";
				}
				if ($("#site").val() == $("#site").attr("beg_val") || $("#site").val() == "") {
					errors += "Введите адрес сайта!\n";
				}
				if ($("#comment_textarea").val() == $("#comment_textarea").attr("beg_val") || $("#comment_textarea").val() == "") {
					errors += "Оставьте свой комментарий!\n";	
				}
				if (errors != "") {
                    alert(errors);
                    return false;
                }
				$("#cfh").val(MD5($('#site').val()));
			}
		);

		$("#do_comment").submit(
			function() {
				var errors = "";
				if ($("#name").length > 0 && ($("#name").val() == $("#name").attr("beg_val") || $("#name").val() == "")) {
					errors += "Введите имя!\n";
				}
				if ($("#site").val() == $("#site").attr("beg_val") || $("#site").val() == "") {
					errors += "Введите адрес сайта!\n";
				}
				if ($("#comment_textarea").val() == $("#comment_textarea").attr("beg_val") || $("#comment_textarea").val() == "") {
					errors += "Оставьте свой комментарий!\n";	
				}
				if (errors != "") {
                    alert(errors);
                    return false;
                }
                $("#cfh").val(MD5($('#site').val()));
			}
		);

		$("#serch_comment").submit(
			function() {
				var errors = "";
				if ($("#name").length > 0 && ($("#name").val() == $("#name").attr("beg_val") || $("#name").val() == "")) {
					errors += "Введите имя!\n";
				}
				if ($("#comment_textarea").val() == $("#comment_textarea").attr("beg_val") || $("#comment_textarea").val() == "") {
					errors += "Оставьте свой комментарий!\n";	
				}
				if (errors != "") {
                    alert(errors);
                    return false;
                }
                $("#cfh").val(MD5($('#sitename').val()));
			}
		);

        $("#search_form_left span").mouseover(
            function() {
                $(this).addClass("hover");
            }
        );
        $("#search_form_left span").mouseout(
            function() {
                $(this).removeClass("hover");
            }
        );
        $("#search_form_left span").click(
            function() {
                if (!$(this).hasClass("active")) {
                    $("#search_form_left span").removeClass("active");
                    switch ($(this).attr("id")) {
                        case "search_site":
                            $("#search_site").addClass("active");
                            $("#search_type").val("site");
                            break;
                        case "search_author":
                            $("#search_author").addClass("active");
                            $("#search_type").val("author");
                            break;
                        case "search_motive":
                            $("#search_motive").addClass("active");
                            $("#search_type").val("motive");
                            break;
                    }
                }
            }
        );

        $("#link_dialog_register").click(
            function() {
                $("#dialog").dialog({
                    modal: true,
                    title: "Регистрация в члены партии",
                    width: 500,
                    height: 280,
                    overlay: {opacity: 0.5, background: "black"},
                    open: function() {
                        if ($("#name").val() != $("#name").attr("beg_val")) {
                            $("#reg_name").val($("#name").val());
                        } else {
                            $("#reg_name").val("");
                        }

                        $("#dialog").html($("#dialog_register").html());

                        $("#dialog #form_register").submit(
                            function() {
                                var errors = "";
                                if ($("#dialog #reg_name").val() == "") {
                                    errors += "Введите имя добровольца!\n";
                                }
                                if ($("#dialog #reg_login").val() == "") {
                                    errors += "Введите логин!\n";
                                } else if (!reg_login.test($("#dialog #reg_login").val())) { 
                                    errors += "Логин должен содержать только 'A-Za-z0-9_'!\n";
                                }
                                if ($("#dialog #reg_e_mail").val() == "") {
                                    errors += "Введите e-mail!\n";
                                } else if (!reg_e_mail.test($("#dialog #reg_e_mail").val())) { 
                                    errors += "Скорее всего такого e-mail'а не существует!\n";
                                }
                                if ($("#dialog #reg_password").val() == "") {
                                    errors += "Введите секретный пароль!\n";
                                } else if ($("#dialog #reg_password").val().length < 6) {
                                	errors += "Пароль слишком короткий (менее 6-ти символов)!\n";
                                } else if ($("#dialog #reg_password").val() != $("#dialog #reg_repassword").val()) {
                                    errors += "Секретный пароль и опять пароль не совпадают!\n";
                                }
                                if (errors != "") {
                                    alert(errors);
                                    return false;
                                }
                                $("#dialog #cfhr").val(MD5($('#dialog #reg_login').val()));
                            }
                        );

                        $("#dialog").show();
                        $("#dialog #reg_name").focus();
                    },
                    close:function(){$("#dialog").dialog("destroy");}
                });
            }
        );

		$("#main_form #form_remind").submit(
			function() {
				var errors = "";
				if ($("#form_remind #remind_e_mail").val() == "") {
					errors += "Введите e-mail!\n";
				} else if (!reg_e_mail.test($("#form_remind #remind_e_mail").val())) { 
					errors += "Скорее всего такого e-mail'а не существует!\n";
				}
				if (errors != "") {
					alert(errors);
					return false;
				}
			}
		);

        $("a.search_comment_link").click(toggleCommentForm);
    }
);

function setWidthIE6() {
    var minWidth = 1000;
    var maxWidth = 1260;

    var newWidth = "100%";

    if ((document.documentElement.clientWidth || document.body.clientWidth) < minWidth) {
        newWidth = minWidth + "px";
    } else {
        if ((document.documentElement.clientWidth || document.body.clientWidth) > maxWidth) {
            newWidth = maxWidth + "px";
        }
    }

    $("#main_content").width(newWidth);
    $("#bottom_content").width(newWidth);
}

function setStar() {
    var currentWidth = $("#main_content").width();
    var minWidth     = 1000;
    var maxWidth     = 1260;

    var newWidth  = 166 + Math.floor((currentWidth - minWidth) / 5.5);
    var newMargin = 0 + Math.floor((currentWidth - minWidth) / 7.4);

    if (currentWidth == minWidth) {
        newWidth  = 166;
        newMargin = 0;
    } else {
        if (currentWidth == maxWidth) {
            newWidth  = 213;
            newMargin = 35;
        }
    }
    if (($.browser.msie) && ($.browser.version == "6.0")) {
        newMargin = newMargin / 2;
    }

    $("#top_search").width(newWidth + "px");
    $("#top_search > div").css("margin-right", newMargin + "px");
}

function changeViewComplaints() {
    if (!$(this).hasClass("active")) {
        $("#complaints_type li").removeClass("active");
        $(this).addClass("active");

        switch ($(this).attr("id")) {
            case "select_cloud":
                $("#complaints_surprise").hide();
                var newContent = document.createElement("div");
                $("#complaints_text a").each(
                    function() {
                        var newA = document.createElement("a");
                        $(newA).attr("href", $(this).attr("href"));
                        $(newA).attr("oldclass", $(this).attr("oldclass"));
                        $(newA).attr("class", $(this).attr("oldclass"));
                        $(newA).html($(this).html());
                        $(newContent).append(newA).append(", ");
                    }
                );
                $("#complaints_text").html(newContent);
                $("#complaints_text").show();
                $(".comm_count").each(
		            function() { $(this).hide(); }
		        );
                break;
            case "select_list":
                $("#complaints_surprise").hide();
                var newContent = document.createElement("div");
                $("#complaints_text a").each(
                    function() {
                        var newA = document.createElement("a");
                        $(newA).attr("href", $(this).attr("href"));
                        $(newA).attr("oldclass", $(this).attr("oldclass"));
                        $(newA).html($(this).html());
                        $(newContent).append(newA).append("<br />");
                    }
                );
                $("#complaints_text").html(newContent);
                $("#complaints_text").show();
                $(".comm_count").each(
		            function() { $(this).show(); }
		        );
                break;
            case "select_surprise":
                $("#complaints_text").hide();
                $("#complaints_surprise").show();
                break;
        }
    }
}

function toggleCommentForm() {
    if ($("#comment_form").attr("visible") == "1") {
        $(this).html($(this).attr("oldname"));
        $("div.search_result_item").show();
        $("#comment_form").hide();
        $("#comment_form").attr("visible", "0");
    } else {
    	$("#sitename").val($(this).attr("id")); 
        $(this).attr("oldname", $(this).html());
        $(this).html("Нет-нет-нет, передумал");
        $("div.search_result_item").hide();
        $(this).parent().show();
        $("#comment_form").show();
        $("#comment_form").attr("visible", "1");
    }
}
