$(document).ready(
    function() {
        if (typeof InitAuthComponent == 'function') {
            InitAuthComponent();
            register_with_email();
        }

        $("#top input").attr("beg_value", $("#top input").val());
        $("#top input").focus(
            function() {
                if ($(this).val() == $("#top input").attr("beg_value")) {
                    $(this).val("");
                }
            }
        );

        $("table.list_messages tr").mouseover(
            function() {
                if ($(this).find("td").length == 4) {
                    $(this).addClass("hover");
                }
            }
        );
        $("table.list_messages tr").mouseout(
            function() {
                $(this).removeClass("hover");
            }
        );

        $("table.list_partners tr").mouseover(
            function() {
                if ($(this).find("td").length == 3 || $(this).hasClass('topic') || $(this).hasClass('post')) {
                    $(this).addClass("hover");
                    $(this).find("div.actions").show();
                }
            }
        );
        $("table.list_partners tr").mouseout(
            function() {
                $(this).removeClass("hover");
                $(this).find("div.actions").hide();
            }
        );

        $("div.pager ul").each(
            function() {
                $(this).css("width", $(this).width());
                $(this).css("margin-left", ($('.pager').width() - $(this).width()) / 2);
                $(this).css("float", "none");
            }
        );

        $("div.actions").each(
            function() {
                var srcHeight = $(this).height();
                var dstHeight = $(this).parent().height() + 10;
                var dstWidth = $(this).parent().width();
                $(this).css("margin-left", (dstWidth - 34) + "px");
                $(this).css("margin-top", ($(this).parent().offset().top - $(this).offset().top) + "px");
                if (dstHeight >= srcHeight) {
                    $(this).height(dstHeight);
                    $(this).css("padding-top", ((dstHeight - srcHeight) / 2) + "px");
                } else {
                    $(this).parent().height(srcHeight)
                }
                $(this).find("span").wrapInner("<span><span><span><span><span><span><span><span></span></span></span></span></span></span></span></span>");
            }
        );
        $("div.actions").hide();

        $("input[name='birthday_null']").change(
            function()
            {
                if ($(this).attr('checked'))
                {
                    hide('#birthday_text');
                    hide('#birthday_text + span');
                }
                else
                {
                    show('#birthday_text');
                    show('#birthday_text + span');
                }
            }
        );
        $("input[name='birthday_null']").each(
            function()
            {
                if ($(this).attr('checked'))
                {
                    hide('#birthday_text');
                    hide('#birthday_text + span');
                }
            }
        );
        $("input[name='joined_null']").change(
            function()
            {
                if ($(this).attr('checked'))
                {
                    hide('#joined_text');
                    hide('#joined_text + span');
                }
                else
                {
                    show('#joined_text');
                    show('#joined_text + span');
                }
            }
        );
        $("input[name='joined_null']").each(
            function()
            {
                if ($(this).attr('checked'))
                {
                    hide('#joined_text');
                    hide('#joined_text + span');
                }
            }
        );
        $("input[name='cert_cert']").bind('change, keyup',
            function()
            {
                if ($(this).val())
                {
                    show('#cert_initial_text, #cert_initial_text + span');
                    show('#cert_final_text, #cert_final_text + span');
                }
                else
                {
                    hide('#cert_initial_text, #cert_initial_text + span');
                    hide('#cert_final_text, #cert_final_text + span');
                }
            }
        );
        $("input[name='cert_cert']").each(
            function()
            {
                if (!$(this).val())
                {
                    hide('#cert_initial_text, #cert_initial_text + span');
                    hide('#cert_final_text, #cert_final_text + span');
                }
            }
        );
        $("input[name='foundation_year_null']").change(
            function()
            {
                if ($(this).attr('checked'))
                {
                    hide('#foundation_year_select');
                }
                else
                {
                    show('#foundation_year_select');
                }
            }
        );
        $("input[name='foundation_year_null']").each(
            function()
            {
                if ($(this).attr('checked'))
                {
                    hide('#foundation_year_select');
                }
            }
        );
        $(".link_wconfirm").live('click',
            function()
            {
                var id = $(this).attr('id');
                hide_timeout('#' + id, 3800);
                setTimeout("show_timeout('#" + id + "_yn', 3000);", 300);
            }
        );
        $(".link_cancel").live('click',
            function()
            {
                var id = str_replace('_n', '', $(this).attr('id'));
                hide('#' + id + '_yn');
                setTimeout("show('#" + id + "');", 200);
            }
        );
        $('#right .register tr:even').css('background', '#F5F5F5');
        $('.region_select').change(
            function ()
            {
                redirect(register_base_url + $('.region_select').val());
            }
        );
        SmartImages.replace_all();

        $('input.input-hint').focus(
            function()
            {
                var id = $(this).attr('id');
                var def_el = def_id(id);
                if ($(this).val() == $(def_el).val())
                {
                    $(this).val('');
                }
            }
        );
        $('input.input-hint').blur(
            function()
            {
                var id = $(this).attr('id');
                var def_el = def_id(id);
                if ($(this).val() == '')
                {
                    $(this).val($(def_el).val());
                }
            }
        );
   }
);

function setWidthIE6() {
    var minWidth = 1000;
    var maxWidth = 1420;

    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").width(newWidth);
    $("#bottom").width(newWidth);
}

function photo_delete()
{
    $.get(
        "/ajax/user_photo_delete/",
        { },
        function(data)
        {
            if (data.length > 0 && data == 'OK')
            {
                hide('#usdel_photo_li');
                hide('#user_photo');
                hide('#cu_photo_small');
            }
        }
    );
}

function company_logo_delete(company_id)
{
    $.get(
        "/ajax/company_logo_delete/" + company_id + "/",
        { },
        function(data)
        {
            if (data.length > 0 && data == 'OK')
            {
                hide('#del_logo_li');
                hide('#company_logo');
            }
        }
    );
}

function talk_logo_delete(talk_id)
{
    $.get(
        "/ajax/talk_logo_delete/" + talk_id + "/",
        { },
        function(data)
        {
            if (data.length > 0 && data == 'OK')
            {
                hide('#del_logo_li');
                hide('#talk_logo');
            }
        }
    );
}

//not allowed query symbols to be stripped off
var not_allowed = new RegExp("[!\"#&'%()*+/:;<>?\\[\\]\\\\_`{|}]", "g")

var soundEmbed = null;
//======================================================================
function soundPlay(which)
{
    if (!soundEmbed)
    {
        soundEmbed = document.createElement("embed");
        soundEmbed.setAttribute("src", "/application/views_files/wav/"+which+".wav");
        soundEmbed.setAttribute("hidden", true);
        soundEmbed.setAttribute("autostart", true);
    }
    else
    {
        document.body.removeChild(soundEmbed);
        soundEmbed.removed = true;
        soundEmbed = null;
        soundEmbed = document.createElement("embed");
        soundEmbed.setAttribute("src", "/application/views_files/wav/"+which+".wav");
        soundEmbed.setAttribute("hidden", true);
        soundEmbed.setAttribute("autostart", true);
    }
    soundEmbed.removed = false;
    document.body.appendChild(soundEmbed);
}

function title_asterisk()
{
    var title = $('title').html();
    if (substr(title, 0, 1) != '*')
    {
        $('title').html('* ' + title);
    }
    $.titleAlert("Входящее сообщение!", {
        requireBlur:true,
        stopOnFocus:true,
        duration:20000,
        interval:1000
    });
}

function remove_asterisk()
{
    
}

function def_id(name)
{
    if (name == 'pr_to' || name == 'sq_to' || name == 'fl_to' || name == 'fc_to')
    {
        return '#to_def';
    }
    else if (name == 'pr_f' || name == 'sq_f' || name == 'fl_f' || name == 'fc_f')
    {
        return '#f_def';
    }
    else
    {
        return '#' + name + '_def';
    }
}
