var flowplayer_key="#@77da14f1f33c7deaec4";

$(document).ready(
    function()
    {
        // Establecemos el click para todos los reproductores de vídeo de la página
        $("a.videoplayer").click(function() {
            $(this).flowplayer("/swf/flowplayer.swf",{
                key: flowplayer_key
            });
            return false;
        });
        $("a.videoplayer_playlist").each(function() {
            $.ajax({
                async:true,
                dataType:'script',
                url: "/videos/load_playlist/" + $(this).attr("id").split("_")[1]
            });

        });
        // Cargamos todos los flash de la página
        $("div.flash_container").each(function() {
            var flashvars = {};
            var params = {
                wmode: "transparent"
            };
            var attributes = {
                wmode: "transparent"
            };
            id = $(this).attr("id").split("_")[1];
            width = $(this).attr("width");
            height = $(this).attr("height");
            file = $(this).attr("file");
            div_id = "flash_replace_" + id;
            swfobject.embedSWF(file, div_id, width, height, "8.0.0","",flashvars,params,attributes);

        });
        // Todos los enlaces de tipo nicedit_external_link han de abrirse en una ventana nueva
        $("a[rel='external']").click(function() {
            window.open($(this).attr("href"));
            return false;
        });
        //Ocultamos los divs vacíos
        $(".botones-cms").each(function() {
            if ($(this).children().length==0) {
                $(this).hide();
            }
        });
        $(".gmap").each(function() {
            if ($(this).children().length==0) {
                $(this).hide();
            }
        });
        $(".galeria").each(function() {
            if ($(this).children().length==0) {
                $(this).hide();
            }
        });
        $("a.fancy_img").fancybox();
        $('a[rel=lightbox]').lightBox();
        $(".no-lightbox").unbind("click");
    }
    );

function load_videos_from_folder(folder,videos,width,height) {
    var pl = [];
    for(var i=0;i<videos.length;i++) {
        pl.push({
            url: videos[i]
        });
    }
    $("a.videoplayer_playlist[id=videos_" + folder + "]").click(function() {
        $(this).flowplayer("/swf/flowplayer.swf",{
            key: flowplayer_key,
            playlist: pl,
            plugins: {
                controls: {
                    playlist: true
                }
            }
        });
        return false;
    });
}

function load_videos_from_folder_and_start(folder,videos,width,height) {
    var pl = [];
    for(var i=0;i<videos.length;i++) {
        pl.push({
            url: videos[i]
        });
    }
    $("a.videoplayer_playlist[id=videos_" + folder + "]").flowplayer("/swf/flowplayer.swf",{
        key: flowplayer_key,
        playlist: pl,
        plugins: {
            controls: {
                playlist: true
            }
        }
    });
}

function addCodeToFunction(func,code){
    if(func == undefined)
        return code;
    else{
        return function(){
            func();
            code();
        }
    }
}

jQuery.fn.defaultText = function(text){
    if(this.val()==''){
        this.val(text);
    }else if(this.val()==text){
        this.val('');
    }
}
jQuery.fn.toggle_visibilidad = function(){
    if(this.hasClass('oculto')){
        this.removeClass('oculto');
    } else {
        this.addClass('oculto');
    }
}