// FancyPlayer.js - A spicy mix of FancyBox and Flowplayer

$(document).ready(function() {

    var swfplayer = "/flowplayer/flowplayer-3.2.2.swf";

    var videoclip = '';
    var player = '';

    $(".fancymedia").live("mouseover", function() {
        videoclip = $(this).attr('href');

        if (videoclip.toString().indexOf(".swf") > 0) {
            $(this).fancybox({
                overlayOpacity: 0.7,
                overlayColor: '#000',
                centerOnScroll: true,
                type: "swf",
                titleShow: false
            });
            return;
        }

        $(this).attr({ "href": "#video_box" });

        if (videoclip.indexOf("www.youtube.com") > 0) {
            $(this).attr({ "href": "#youtubePanel" });

            if (videoclip.indexOf('autoplay') < 0) {
                videoclip += '&autoplay=1';
            }

            var youtubeplayer = "<object width='640' height='390'><param name='movie' value='" + videoclip + "'></param>" +
                            "<param name='allowFullScreen' value='true'></param>" +
                            "<param name='allowscriptaccess' value='always'></param>" +
                            "<embed src='" + videoclip + "' type='application/x-shockwave-flash'" +
                            "allowscriptaccess='always' allowfullscreen='true' width='640' height='390'></embed></object>";

            $("#youtubePanel").html(youtubeplayer);

            $(this).fancybox({
                'width': '680px',
                'height': '430px',
                'overlayOpacity': .6,
                'transitionIn': 'elastic',
                'transitionOut': 'elastic'
            });

            return;
        }

        $(this).videofancybox({
            'hideOnContentClick': false,
            'overlayOpacity': .6,
            'zoomSpeedIn': 400,
            'zoomSpeedOut': 400,
            'easingIn': 'easeOutBack',
            'easingOut': 'easeInBack',
            'callbackOnShow': function() {
                player = $f("fancy_div", swfplayer, {
                    play: { opacity: 0 },
                    clip: {
                        autoPlay: true,
                        autoBuffering: true,
                        url: videoclip + '',
                        onStart: function(clip) {
                            var wrap = jQuery(this.getParent());
                            var clipwidth = clip.metaData.width;
                            var clipheight = clip.metaData.height;
                            var pos = $.fn.videofancybox.getViewport();
                            $("#fancy_outer").css({ width: clipwidth, height: clipheight });
                            $("#fancy_outer").css('left', ((clipwidth + 36) > pos[0] ? pos[2] : pos[2] + Math.round((pos[0] - clipwidth - 36) / 2)));
                            $("#fancy_outer").css('top', ((clipheight + 50) > pos[1] ? pos[3] : pos[3] + Math.round((pos[1] - clipheight - 50) / 2)));
                        },
                        onFinish: function() {
                            $('#fancy_close').trigger('click');
                        }
                    }
                });
                player.load();
                $('#fancy_close').click(function() {
                    $("#fancy_div_api").remove();
                });
            },
            'callbackOnClose': function() {
                $("#fancy_div_api").remove();
            }
        });
    }).live("mouseout", function() {
        $(this).attr({ "href": "" + videoclip + "" });
    });

}); 
