// FancyPlayer.js - A spicy mix of FancyBox and Flowplayer

$(document).ready(function() {

    function preloadImages() {
        for (var i = 0; i < arguments.length; i++) {
            $("<img>").attr("src", arguments[i]);
        }
    }

    preloadImages("images/video_bg.png");

    var videoclip = '';
    var player = '';
    var currentVideo;


    $(".video").hover(function() {
        currentVideo = $(this).attr("class");
        videoclip = $(this).attr('href');
        $(this).attr({ "href": "#video_box" });
        /* $(this).removeAttr('target');*/
    },

	function() {
	    $(this).attr({ "href": "" + videoclip + "" });
	});

    $(".video").fancybox({
        'hideOnContentClick': false,
        'overlayOpacity': .6,
        'zoomSpeedIn': 400,
        'zoomSpeedOut': 400,
        'easingIn': 'easeOutBack',
        'easingOut': 'easeInBack',
        'callbackOnShow': function() {
            player = $f("fancy_div", "flowplayer-3.1.5.swf", {

                play: { opacity: 0 },
                clip: {
                    scaling: 'fit',
                    autoPlay: true,
                    autoBuffering: true,
                    url: videopath + videoclip + '',
                    provider: 'rtmp',

                    onStart: function(clip) {
                        var wrap = jQuery(this.getParent());
                        var customClip = clip.metaData.width - 60;
                        var clipwidth = customClip;
                        var clipheight = clip.metaData.height;
                        var pos = $.fn.fancybox.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');

                        if ((currentVideo == 'video ep1')) {
                            $("#fancy_div_api").remove();
                            $("#fancy_div").css('color', '#000');

                            $("#fancy_div").html($(".ep1VideoEndText").html());

                            $("#fancy_div").css('paddingTop', '40px');
                            $("#fancy_div").css('textAlign', 'center');
                            $("#fancy_div").css('fontSize', '18px');
                            $("#fancy_div a").css('textDecoration', 'Underline');
                            $("#fancy_div a").css('color', '#E29C46');
                            $(".Links .link2").click(function() {
                                $("#fancy_div").fadeOut('fast');
                                window.location = "?page=Campaign/signup";
                            });
                        }

                        else if ((currentVideo == 'video ep2')) {

                            //$("#fancy_div_api").remove();
                            $("#fancy_div").css('color', '#000');
                            $("#fancy_div").html($(".ep2VideoEndText").html());
                            $("#fancy_div").css('paddingTop', '40px');
                            $("#fancy_div").css('textAlign', 'center');
                            $("#fancy_div").css('fontSize', '18px');
                            $("#fancy_div a").css('textDecoration', 'Underline');
                            $("#fancy_div a").css('color', '#E29C46');

                            $(".Links .link2").click(function() {
                                $("#fancy_div").fadeOut('fast');
                                window.location = "?page=About/170173";
                            });
                        }

                        else {

                            $('#fancy_close').trigger('click');
                        }
                    }
                },

                plugins: {
                    // here is our rtpm plugin configuration 
                    rtmp: { url: 'flowplayer.rtmp-3.1.3.swf',

                        // netConnectionUrl defines where the streams are found
                        netConnectionUrl: 'rtmp://ircadeits.fcod.llnwd.net/a1460/o15'

                    }
                }
            });


            player.load();
            $('#fancy_close').click(function() {

                $("#fancy_div_api").remove();
            });
        },
        'callbackOnClose': function() {
            $("#fancy_div_api").remove();
        }
    });

}); 
