function multimedia_list_videos(pagina)
{
    if (!document.sending) {
        document.sending = true;
        $.ajax({
            type: "POST",
            url: WEB_PATH + "app/ajax/multimedia_list.php",
            data: {
                type: 'VID',
                p: pagina
            },
            success: function(response) {
                document.sending = false;
                $('#bd').html(response);
            },
            error: function(obj, what_happen, other_obj) {
            }
        });
    }
}
function multimedia_list_fotos(pagina)
{
    if (!document.sending) {
        document.sending = true;
        $.ajax({
            type: "POST",
            url: WEB_PATH + "app/ajax/multimedia_list.php",
            data: {
                type: 'IMG',
                p: pagina
            },
            success: function(response) {
                document.sending = false;
                $('#bd').html(response);
            },
            error: function(obj, what_happen, other_obj) {
            }
        });
    }
}
function multimedia_show_picture(id_img_tag, archivo, width, height)
{
    if (!document.sending) {
        document.sending = true;
        $.ajax({
            type: "POST",
            url: WEB_PATH + "app/ajax/multimedia_get_foto_default.php",
            data: {
                archivo: archivo,
                width: width,
                height: height
            },
            success: function(response) {
                document.sending = false;
                $('#'+id_img_tag).attr('src', response);
            }
        });
    }
}
function multimedia_show_video(id_div_tag, id_video)
{
    if (!document.sending) {
        document.sending = true;
        $.ajax({
            type: "POST",
            url: WEB_PATH + "app/ajax/multimedia_get_video.php",
            data: {
                id_video: id_video
            },
            success: function(response) {
                document.sending = false;
                $('#'+id_div_tag).html(response);
            }
        });
    }
}

function multimedia_add_video()
{

    if (!document.sending && multimedia_valid_video()) {
        var data = {
            titulo: $('#txtTituloVideo').val().trim(),
            youtube: $('#txtYoutubeVideo').val().trim(),
            descripcion: $('#txtDescripcionVideo').val().trim(),
            id_escuela: $('#hidIdEscuela').val().trim(),//$('#consulta_msg').val().trim()
            id_video: $('#hidIdVideo').val()
        }
        document.sending = true;
        $('#pVideoError').html(MSG_SENDING);
        $.ajax({
            type: 'POST',
            url: WEB_PATH + 'app/ajax/multimedia_add_video.php',
            data: data,
            success: function(response) {
                document.sending = false;

                if (response==1) {

                    $('#txtDescripcionVideo').val('');
                    $('#pVideoError').html('Video agregado');
                    window.location.href = window.location.href;
                } else {
                    $('#pVideoError').html('Error al agregar un video. Vuelve a intentar en unos minutos');
                }
            }
        });
    }

}

function multimedia_valid_video()
{
    var valid = true;
    if ($('#txtTituloVideo').val().trim() == '')
    {
        valid = false;
        $('#pVideoError').html('Debe ingresar un título');
    }
    else if ($('#txtYoutubeVideo').val().trim() == '')
    {
        valid = false;
        $('#pVideoError').html('Debe ingresar un link a Youtube');
    }
    else if ($('#txtDescripcionVideo').val().trim() == '')
    {
        valid = false;
        $('#pVideoError').html('Debe ingresar una descripción');
    }
	
    return valid;
}

function multimedia_add_foto()
{

    if (!document.sending && multimedia_valid()) {
        var data = {
            titulo: $('#txtTituloVideo').val().trim(),
            youtube: $('#txtYoutubeVideo').val().trim(),
            descripcion: $('#txtDescripcionVideo').val().trim(),
            id_escuela: 1//$('#consulta_msg').val().trim()
        }
        document.sending = true;
        $('#pVideoError').html(MSG_SENDING);
        $.ajax({
            type: 'POST',
            url: WEB_PATH + 'app/ajax/multimedia_add_video.php',
            data: data,
            success: function(response) {
                document.sending = false;
                if (response==1) {
                    $('#txtDescripcionVideo').val('');
                    $('#pVideoError').html('Video agregado');
                } else {
                    $('#pVideoError').html('Error al agregar un video. Vuelve a intentar en unos minutos');
                }
            }
        });
    }

}

function multimedia_valid()
{
    var valid = true;
    if ($('#txtTituloVideo').val().trim() == '')
    {
        valid = false;
        $('#pVideoError').html('Debe ingresar un título');
    }
    else if ($('#txtYoutubeVideo').val().trim() == '')
    {
        valid = false;
        $('#pVideoError').html('Debe ingresar un link a Youtube');
    }
    else if ($('#txtDescripcionVideo').val().trim() == '')
    {
        valid = false;
        $('#pVideoError').html('Debe ingresar una descripción');
    }

    return valid;
}


function limpiar_panel_foto(){
    $('#txtTitulo').val("");
    $('#fileZip').val("");
    $('#txtDescripcion').val("");
    $('#pErrorFotos').html('');
}

function limpiar_panel_video(){
    $('#txtTituloVideo').val("");
    $('#txtYoutubeVideo').val("");
    $('#txtDescripcionVideo').val("");
    $('#pVideoError').html('');
}

function video_eliminar(id_video) {
    if(confirm('¿Está seguro que desea eliminar este video?')) {
        if (!document.sending) {
            document.sending = true;
            $.ajax({
                type: "POST",
                url: WEB_PATH + "app/ajax/video_del.php",
                data: {
                    id_video: id_video
                },
                success: function(response) {
                    if(isNaN(response)) {
                        alert('Ocurrió un error al intentar borrar el video.\nIntentalo denuevo mas tarde.');
                    }
                    else {
                        document.sending = false;
                        $('#block_video_' + response).remove();
                    }
                }
            });
        }
    }
}

function video_get_dialog(id_escuela, id_video) {
    if (!document.sending) {
        document.sending = true;
        $.ajax({
            type: "POST",
            url: WEB_PATH + "app/ajax/video_get_dialog.php",
            data: {
                id_video: id_video,
                id_escuela: id_escuela
            },
            success: function(response) {
                document.sending = false;
                $('#content_dialog_video').html(response);
                msgbox_show('content_dialog_video');
            },
            error: function(obj, what_happen, other_obj) {
            }
        });
    }
}
