(function($)
{
    $.extend(true, 
    {
        bone : {
            base_url : false,
            embed_video : {
                module : false,
                comment_text_area_sel : false
            }
        }
    });
    
    $(function()
    {
        function show_embed_video()
        {
            var embed_tarea = 
                '<textarea name="embed_video_code" style="width:400px;height:200px;"></textarea>' +
                '<div style="font-size:11px;color:#6a6a6a;padding:5px 0 5px 0;">'+$.bone.lang.paste_embed_code_here+'</div>' +
                '<div class="fl"><img src="'+$.bone.image_path+'/btnml.gif" alt=""></div>' +
                '<div class="fl"><input class="btn4" style="width: 60px;" value="'+$.bone.lang.insert+'" type="button"></div>' +
                '<div class="fl"><img src="'+$.bone.image_path+'/btnmr.gif" alt=""></div>' +
                '<div class="clear"><!-- --></div>';
            
            if ($("tr#embed_videos_row").length > 0) {
                $("tr#embed_videos_hr_row").show();
                $("tr#embed_videos_row").show();
                $("tr#embed_videos_hr_row2").show();
                $("tr#photos_hr_row").show();
            }
            else
            {
                if ($("tr#emoticons_row").length > 0)
                {
                    $("tr#emoticons_row").after(
                        '<tr id="embed_videos_hr_row">' +
                        '    <td colspan="2"><div class="hr5"><!-- --></div></td>' +
                        '</tr>' +
                        '<tr id=\"embed_videos_row\">' +
                        '    <td>&nbsp;</td>' + 
                        '    <td colspan>' + embed_tarea + '</td>' +
                        '</tr>'
                    );
                    if ($("tr#photos_hr_row").length > 0)
                        $("tr#photos_hr_row").show();
                    else
                        $("tr#embed_videos_row").after(
                             '<tr id="embed_videos_hr_row2">' +
                            '    <td colspan="2"><div class="hr5"></div></td>' +
                            '</tr>'
                        );
                    
                    $("tr#embed_videos_row input[type='button']").click(function()
                    {
                        $.post($.bone.base_url + 'src/pages/ajax_embed_video.php',
                        {
                            embed_videos_module : $.bone.embed_video.module,
                            code : $("textarea[name='embed_video_code']").val()
                        }, function(json)
                        {
                            if (json.success) {
                                $.insertAtCaret($($.bone.embed_video.comment_text_area_sel), json.tag);
                                $("textarea[name='embed_video_code']").val('');
                                hide_embed_video();
                            } else {
                                $.alerts.okButton = '&nbsp;'+$.bone.lang.ok+'&nbsp;';
                                jAlert($.bone.lang.embed_video_saving_error, $.bone.lang.information);
                            }
                        }, 'json');
                        
                        return false;
                    });
                }
            }
            
            update_submits();
        }
        
        function hide_embed_video()
        {
            // Hide
            $("tr#embed_videos_hr_row").hide();
            $("tr#embed_videos_row").hide();
            $("tr#embed_videos_hr_row2").hide();
            $("tr#photos_hr_row").hide();
            
            update_submits();
        }
        
        function row_is_visible(row)
        {
            if (row.length == 0)
                return false;
            else if (row.is(":visible") == false)
                return false;
            else if (row.is(":visible") == true && row.height() == 0) // For IE6
                return false;
            else
                return true;
        }
        
        function update_submits()
        {
            if (row_is_visible($("#emoticons_row")))
            {
                if (row_is_visible($("tr#embed_videos_row")))
                    $("#embed_videos_hr_row").show();
                else {
                    $("#photos_hr_row").show();
                }
                
            }
            
            if (row_is_visible($("#embed_videos_hr_row")))
                $("#photos_hr_row").show();
            
            if (row_is_visible($("tr#emoticons_row")) == false &&
                row_is_visible($("tr#embed_videos_row")) == false &&
                row_is_visible($("tr#photos_row")) == false)
            {
                $("#photos_hr_row").hide();
            }
            
            if ($("#submit_up").length > 0 && $("#submit_down").length > 0) {
                if (row_is_visible($("tr#emoticons_row")) || row_is_visible($("tr#embed_videos_row")) ||
                    row_is_visible($("tr#photos_row")))
                {
                    $("#submit_up").hide();
                    $("#submit_down").show();
                }
                else
                {
                    $("#submit_up").show();
                    $("#submit_down").hide();
                }
            }
        }
        
        if ($.bone.base_url && $.bone.embed_video.module && $.bone.embed_video.comment_text_area_sel)
        {
            $(".embed_video_btn").click(function()
            {
                if (row_is_visible($("tr#embed_videos_row")))
                {
                    hide_embed_video();
                }
                else
                {
                    show_embed_video();
                }
                
                return false;
            });
        }
    });
})(jQuery);