// FancyPlayer.js - A spicy mix of FancyBox and Flowplayer

var videopath = "/js/fancyplayer/";
var swfplayer = "/js/flowplayer/flowplayer.commercial-3.2.4.swf";
function fancyplayerHifi(fID) {
    fURL = jQuery('#' + fID).attr('hifiurl');
    jQuery('#' + fID).attr('url', fURL);
    jQuery('#' + fID).trigger('click');
}

function fancyplayerLofi(fID) {
    fURL = jQuery('#' + fID).attr('lofiurl');
    jQuery('#' + fID).attr('url', fURL);
    jQuery('#' + fID).trigger('click');
}
jQuery(document).ready(function () {
    function preloadImages() {
        for (var i = 0; i < arguments.length; i++) {
            jQuery("<img>").attr("src", arguments[i]);
        }
    }
    preloadImages("/images/video_bg.png");

    var videoclip = '';
    var player = '';

    jQuery('.fancyplayer').each(function () {
        fURL = jQuery(this).attr('href');
        jQuery(this).attr('url', fURL);
        jQuery(this).attr('href', 'javascript:void(0)');
    });

    jQuery('.fancyplayer').bind('click', function () {
        videoclip = jQuery(this).attr('url');

        jQuery.fancybox('<div id="fancyflowplayer" style="display: block; width: 570px; height: 345px;">&nbsp;</div>', {
            'hideOnContentClick': false,
            'hideOnOverlayClick': false,
            'overlayOpacity': 0.9,
            'speedIn': 400,
            'speedOut': 400,
            'easingIn': 'easeOutBack',
            'easingOut': 'easeInBack',
            'autoDimensions': true,
            'onComplete': function () {
                fType = detectPrimaryMediaPlugin();

                if (fType == 'swf') {
                    startFlowplayer('fancyflowplayer', swfplayer, videoclip,
						function (fClip, fWidth, fHeight) {
						    fLeft = Math.round(jQuery(window).width() / 2) - Math.round((fWidth + 40) / 2);
						    fTop = Math.round(jQuery(window).height() / 2) - Math.round((fHeight + 40) / 2);
						    
						    jQuery('#fancyflowplayer').css({
						    'width': fWidth,
						    'height': fHeight
						    });
						    jQuery('#fancybox-inner').css({
						    'width': fWidth,
						    'height': fHeight
						    });

						    jQuery('#fancybox-wrap').animate({
						    'left': fLeft,
						    'top': fTop,
						    'width': fWidth + 20,
						    'height': fHeight + 20
						    }, 500, function () {
						    });
						    
						},
                        function () {
                            //    jQuery.fancybox.close();
                        }
					);
                } else if (fType == 'qt') {
                    fWidth = 570;
                    fHeight = 340;

                    fData = '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab" width="' + fWidth + '" height="' + fHeight + '"><param name="src" value="' + videoclip + '"><param name="qtsrc" value="' + videoclip + '"><param name="autoplay" value="true"><param name="controller" value="true"><param name="loop" value="false"><param name="scale" value="aspect"><param name="volume" value="100"><param name="type" value="video/quicktime" width="' + fWidth + '" height="' + fHeight + '"><embed src="' + videoclip + '" qtsrc="' + videoclip + '" controller="true" width="' + fWidth + '" height="' + fHeight + '" autoplay="true" loop="false" type="video/quicktime" pluginspage="http://www.apple.com/quicktime/download/" scale="aspect" volume="100"></object>';
                    jQuery('#fancybox-inner').css('height', fHeight);
                    jQuery("#fancybox-outer").css({ width: fWidth + 25, height: fHeight + 25 });

                    fLeft = Math.round((jQuery(window).width() - fWidth) / 2);
                    fTop = Math.round((jQuery(window).height() - fHeight) / 2);
                    jQuery("#fancybox-outer").css('left', fLeft);
                    jQuery('#fancybox-inner').html(fData);
                } else {
                    fData = '<div style="color: #343434; font-size: 16px; font-family: Segoe UI; padding: 25px"><h3>Sorry, we could not detect any viable media players installed on your computer.</h1><div>Please make sure you have atleast one of the following installed:<br><br><ul><li>Adobe Flash <a href="http://www.adobe.com/go/EN_US-H-GET-FLASH" target="_blank">(Get It Here)</a></li><li>Apple Quicktime <a href="http://www.apple.com/quicktime/" target="_blank">(Get It Here)</a></li></ul></div></div>';
                    jQuery('#fancybox-inner').html(fData);
                }
            },
            'onCleanup': function () {
                fPlayer.unload();
                jQuery('#fancybox-inner').html('');
            }
        });


        return false;
    });
}); 
