// $Id: show-movie.js 390 2008-02-12 00:45:55Z rick $
(function($) {
    var BUTTON_PADDING = 28;
    var FLASH_LINK = '<a class="get-flash" '
        + 'href="http://www.macromedia.com/go/getflashplayer">'
        + 'Get the Adobe Flash Player</a> to see ';

    var popup_html = '<div class="light" id="movie-player">'
        + '<div class="get-flash">'
        + FLASH_LINK + 'this movie.</div></div>';

    function setup(href, inline) {
        var size=["410","315"];
        var a=href.split('?');

        if(a[1]) size=a[1].split('x');

        var x=Math.min($(window).width()-24, parseInt(size[0]));
        var y=Math.min($(window).height()-(inline ? 20 : BUTTON_PADDING),
            parseInt(size[1]));

        so = new SWFObject(
            // ie cache bug: make url unique each time
            "/files/mediaplayer.swf?t=" + new Date().getTime(),
            "mediaplayer", x, (inline||y==0 ? y+20 : y), "8", '#fff'
        );

        var img=a[0].replace('.flv','.jpg');
        so.addVariable("image",img);
        so.addVariable("file", a[0]);
        so.addVariable("usefullscreen", 'false');
        if(y > 20)
            so.addVariable("displayheight", y);

        so.addParam("scale","showall");
        //so.addParam("allowfullscreen","true");

        return so;
    }
    $(document).ready(function() {
        $('.inline-movie a').each(function() {
            var so = setup(this.href, 1);
            if(so.installedVer.versionIsValid(so.getAttribute('version')))
                so.write(this.parentNode.id);
            else
                $(this).replaceWith(
                    '<span>'+FLASH_LINK+'<i>'+ $(this).text()+'</i></span>'
                );
        });
        $('.movie a').click(function() {
            var so=setup(this.href, 0);

            $(popup_html).dialog({
                autoOpen: false,
                resizable: false,
                width: so.getAttribute('width'),
                height: so.getAttribute('height')+BUTTON_PADDING,
                modal: true,
                overlay: {opacity: 0.7},
                buttons: {'Close Movie': function() {
                    so=null;
                    $(this).dialog('close');
                    $('#movie-player').remove();
                    $('.inline-movie').css({
                        display: this.nodename == 'SPAN'
                            ? 'inline' : 'block'
                    });
                }},
                open: function() {
                    $('.inline-movie').css({display: 'none'});
                    so.write("movie-player");
                }}).dialog('open');

            return false;
        });
    });
})(jQuery);
