// FancyPlayer.js - A spicy mix of FancyBox and Flowplayer

var videopath = "/net/js/fancyplayer/";
var swfplayer = "/net/js/flowplayer/flowplayer.commercial-3.2.4.swf";
defaultVideoWidth = 570;
defaultVideoHeight = 345;

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 () {
	initFancyPlayers();
});

initFancyPlayerHref = function (fObj) {
	fObj = jQuery(fObj);
	fURL = fObj.attr('href');
	fObj.attr('url', fObj.attr('href'));
	fObj.attr('href', 'javascript:void(0)');

	fObj.bind('click', function () {
		fVideoClip = jQuery(this).attr('url');
		fVideoWidth = jQuery(this).attr('video-width');
		fVideoHeight = jQuery(this).attr('video-height');

		if (fVideoWidth == '' || fVideoWidth == 0 || fVideoWidth == undefined) {
			fVideoWidth = defaultVideoWidth;
		}

		if (fVideoHeight == '' || fVideoHeight == 0 || fVideoHeight == undefined) {
			fVideoHeight = defaultVideoHeight;
		}

		jQuery.fancybox('<div id="fancyflowplayer" style="display: block; width: ' + fVideoWidth + 'px; height: ' + fVideoHeight + 'px;">&nbsp;</div>', {
			'hideOnContentClick': false,
			'hideOnOverlayClick': false,
			'overlayOpacity': 0.9,
			'speedIn': 400,
			'speedOut': 400,
			'easingIn': 'easeOutBack',
			'easingOut': 'easeInBack',
			'scrolling': 'no',
			'autoDimensions': true,
			'onComplete': function () {
				fType = detectPrimaryMediaPlugin();
				switch (fType) {
					case 'swf':
						startFlowplayer('fancyflowplayer', swfplayer, fVideoClip,
						function (fClip, fWidth, fHeight) {
							jQuery.fancybox.resize();
							jQuery.fancybox.center();
						}, function () {
							jQuery.fancybox.close();
						});
						break;
					case '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="' + fVideoClip + '"><param name="qtsrc" value="' + fVideoClip + '"><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="' + fVideoClip + '" qtsrc="' + fVideoClip + '" 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);
						break;
					default:
						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);
						break;
				}
			},
			'onCleanup': function () {
				try {
					fPlayer.unload();
				} catch (e) {
				}
			}
		});



		jQuery(this).removeClass('fancyplayer');
		jQuery(this).addClass('fancyplayer-configured');
		return false;
	});

};

function initFancyPlayers() {
	jQuery('.fancyplayer').each(function () {
		initFancyPlayerHref(this);
	});
}
