$(function() {
    $("[download='true']").click(function() {

        var a$ = $(this);

        var href = a$.attr("href");

        var win = window.open(href, "mp3", "width=200,height=50,scrollbars=0,status=0");
        win.focus();

        return false;

    })
})

function overlay(template$, width, height, opts, callback) {

    try {

        $("body").css("overflow", "hidden");

        if (!width) {
            width = template$.width();
        }

        if (!height) {
            height = template$.height();
        }

        var marginleft = (width / 2);

        var margintop = (height / 2);

        var top = "50%";

        var left = "50%";

        var css = {
            width: width + "px", height: height + "px",
            top: top, left: left, margin: "-" + margintop + "px 0 0 -" + marginleft + "px",
            cursor: "pointer",
            zindex: "10000"
        }

        if (opts) {

            for (var k in opts) {
                css[k] = opts[k];
            }

        }

        var onUnblockFunc = function() {
            try {
                $("body").css("overflow", "auto");
                if (callback) {
                    try {
                        callback();
                    } catch (e) {
                        alert(e);
                    }
                }
            } catch (f) {
                alert(f);
            }
        }

        $("[action='close-overlay']", template$).click(function() {
            $.unblockUI({
                onUnblock: onUnblockFunc
            });
        })

        $.blockUI({fadeIn: 50, fadeOut: 50, message: template$, baseZ: 10000, css: css, onUnblock: onUnblockFunc});

        $('.blockOverlay').attr('title', 'Click to unblock').click(function() {
            $.unblockUI({
                onUnblock: onUnblockFunc
            });
        });

    } catch (e) {
        alert(e);
    }

}

function pulldownOverlay(template$, opts) {

    try {

        var css = {cursor: "pointer", "border": "none", "background": "none",zindex: "10000"};

        for (var k in opts) {
            css[k] = opts[k];
        }

        var unblockFunc = function() {
                    $("body").css("overflow", "auto");
                };

        //var opts0 = {timeout: 10000, fadeIn: 50, fadeOut: 50, message: template$, baseZ: 10000, css: css, onUnblock: unblockFunc};
        var opts0 = {fadeIn: 50, fadeOut: 50, message: template$, baseZ: 10000, css: css, onUnblock: unblockFunc};

        $.blockUI(opts0);

        $("body").css("overflow", "hidden");

        $('.blockOverlay').attr('title', 'Click to unblock').click(function() {
            $("body").css("overflow", "auto");
            $.unblockUI({
                onUnblock: unblockFunc
            });
        });

    } catch (e) {
        alert(e);
    }

}

function showVideoOverlay(clip) {

    var embed = "";

    window.scrollTo(0,0);

    if (clip.match(/vimeo.com/)) {
        var clipid = clip.replace(/http:\/\/(www\.)?vimeo.com\//, "");
        embed = '<iframe src="http://player.vimeo.com/video/'+clipid+'?title=0&amp;byline=0&amp;portrait=0&amp;color=3AAEB8" width="500" height="378" frameborder="0" webkitAllowFullScreen allowFullScreen></iframe>';
    } else if (clip.match(/youtube/)) {
        var clipid = clip.split("v=")[1];
        clipid = clipid.split("&")[0];
        embed = '<iframe width="500" height="378" src="http://www.youtube.com/embed/'+clipid+'" frameborder="0" allowfullscreen></iframe>';
    }

    jQuery("#vimeo-overlay .url").html(embed);

    jQuery("#vimeo-overlay").fadeIn();
}

$(function() {



    jQuery(".lookup-idclip-play-vimeo-video-in-overlay-action").click(function() {
        var t$ = jQuery(this);
        var videolink = t$.attr("href");
        jQuery.post("/city/bridge/br_videoIdClip.jsp", {
            "id": videolink
        }, function(o) {
            showVideoOverlay("http://vimeo.com/moogaloop.swf?clip_id=" + o.idClip);
        }, "json");
        return false;
    });

    jQuery(".play-vimeo-video-in-overlay-action").click(function() {

        var clip = jQuery(this).attr("href");

        showVideoOverlay(clip);

        return false;

    })

    jQuery(".start-wrap-video").each(function() {

        var t$ = jQuery(this);

        var clip = jQuery(this).attr("href");

        var width = "auto";

        var height = "auto";

        if (t$.attr("width")) {
            width = t$.attr("width");
        }

        if (t$.attr("height")) {
            height = t$.attr("height");
        }

        var embed = "";

        if (clip.match(/vimeo.com/)) {
            var clipid = clip.replace(/http:\/\/(www\.)?vimeo.com\//, "");
            embed = '<iframe src="http://player.vimeo.com/video/'+clipid+'?title=0&amp;byline=0&amp;portrait=0&amp;color=3AAEB8" width="'+width+'" height="'+height+'" frameborder="0" webkitAllowFullScreen allowFullScreen></iframe>';
            t$.replaceWith(embed);
        } else if (clip.match(/youtube/)) {
            var clipid = clip.split("v=")[1];
            clipid = clipid.split("&")[0];
            embed = '<iframe width="'+width+'" height="'+height+'" src="http://www.youtube.com/embed/'+clipid+'" frameborder="0" allowfullscreen></iframe>';
            t$.replaceWith(embed);
         }



    })

    $("[action='where-is-my-boombox']").click(function() {
        try {
            window.opener.window.focus();
            window.close();
        } catch (e) {

        }
        return false;
    })

    $(".video-cover").each(function() {
        var v$ = $(this);
        var playbutton$ = $('<div class="video-play-button"><div><img src="img/common/video_play.png"/></div></div>');
        v$.append(playbutton$);
    })

    $("[video]").click(function() {

        var video = $(this).attr("video") || "0";

        var artist = $(this).attr("artist") || "0";

        var overlay$ = $("#overlay");

        overlay$.html("");

        try {
            window.player.pause();
        } catch (e) {

        }
        try {
            window.opener.player.pause();
        } catch (e) {


        }

        try {
            window.opener.window.close();
        } catch (e) {

        }

        try {
            window.opener.close();
        } catch (e) {

        }

        overlay$.load("loadvideo.jsp?dim=600&video=" + video + "&artist=" + artist, {}, function(res) {
            overlay(overlay$);
        });
        return false;
    });

})
