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();
            }
        });
        $(".content-map_canvas").each(function() {
            $(this).show();
        });
        $(".map_canvas").each(function() {
            $(this).show();
        });
        $(".como-funciona").each(function() {
            $(this).show();
        });
        $(".boton_js").each(function() {
            $(this).show();
        });
        $(".direcciones_js").each(function() {
            $(this).show();
        });
    }
    );

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

function close_ruta_preview(n){
    $('#detalle_ruta_' + n).html("");
}

function close_garaje_preview(n){
    $('#detalle_garaje_' + n).html("");
}



function setpopupinfo() {
    $('.bubbleHowto').each(function () {
        // options
        var distance = 10;
        var time = 250;
        var hideDelay = 500;
        var posx = 0;
        var posy = 0;

        var hideDelayTimer = null;

        // tracker
        var beingShown = false;
        var shown = false;

        var trigger = $('.trigger', this);
        var popup = $('.popup', this).css('opacity', 0);

        // set the mouseover and mouseout on both element
        $([trigger.get(0), popup.get(0)]).mouseover(function (e) {
            // stops the hide event if we move from the trigger to the popup element
            if (hideDelayTimer) clearTimeout(hideDelayTimer);

            // don't trigger the animation again if we're being shown, or already visible
            if (beingShown || shown) {
                return;
            } else {
                beingShown = true;
                // Detect mouse position
                posx=20;
                posy=-60;

                // reset position of popup box
                popup.css({
                    top: '-35px',
                    left: posx,
                    display: 'block' // brings the popup back in to view
                })

                // (we're using chaining on the popup) now animate it's opacity and position
                .animate({
                    top: '-35px',
                    opacity: 1
                }, time, 'swing', function() {
                    // once the animation is complete, set the tracker variables
                    beingShown = false;
                    shown = true;
                });
            }
        }).mouseout(function () {
            // reset the timer if we get fired again - avoids double animations
            if (hideDelayTimer) clearTimeout(hideDelayTimer);

            // store the timer so that it can be cleared in the mouseover if required
            hideDelayTimer = setTimeout(function () {
                hideDelayTimer = null;
                popup.animate({
                    top: '-35px',
                    opacity: 0
                }, time, 'swing', function () {
                    // once the animate is complete, set the tracker variables
                    shown = false;
                    // hide the popup entirely after the effect (opacity alone doesn't do the job)
                    popup.css('display', 'none');
                });
            }, hideDelay);
        });
    });
}

function show_pestana(n){
	$("#pestania_"+n).addClass("sel");
	$("#pestania_"+n+"_text").show();
}

function hide_pestana(n){
	$("#pestania_"+n).removeClass("sel");
	$("#pestania_"+n+"_text").hide();
}
