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();
            }
        });

    }
    );

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) {
    alert(folder);
    alert(videos[0]);
    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();
	}
    }
}

//Ir a un ancla
function goToAnchor(elemID){
	var offsetTrail = document.getElementById(elemID);
    var offsetLeft = 0;
    var offsetTop = 0;
    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    if (navigator.userAgent.indexOf("Mac") != -1 && typeof document.body.leftMargin != "undefined" && navigator.appName == "Microsoft Internet Explorer") {
        offsetLeft += parseInt(document.body.leftMargin);
        offsetTop += parseInt(document.body.topMargin);
    }
    window.scrollTo(offsetLeft, offsetTop)
}

//Ocultar pestana
function hide_pestana(n){
	$("#pestana_"+n+"_li").removeClass("sel");
	$("#pestana_"+n).hide();
}

//Mostrar pestana
function show_pestana(n){
	$("#pestana_"+n+"_li").addClass("sel");
	$("#pestana_"+n).show();
}
