
/**
 * Initialize JavaScript elements.
 */
jQuery(document).ready(function($) {
	cSeVideoPopup.init();
});


/**
 * An object that contains utility functions for the customSiteElement videoPopup.
 * @param $ A jQuery object.
 * @return 
 */
var cSeVideoPopup = new (function($) {
	
	/**
	 * 
	 */
	this.init = function() {
		var videoPopups = $('.customSiteElement .videoPopup');
		
		videoPopups.each( function() {
			var videoPopup = $(this);
		
			var dialog = videoPopup.find('.popupContents').dialog( {
				title: videoPopup.find('a.popupTrigger').attr('title'),
				modal: true,
				autoOpen: false,
				draggable: false,
				resizable: false,
				width: 853,
				height: 525,
				dialogClass: 'customSiteElement videoPopup'
			} );
			
			videoPopup.find('a.popupTrigger').click( function( event ) {
				dialog.dialog('open');
				return false;
			} );
		} );
	};
	
})(jQuery);


/**
 * A utility function that is necessary to control embedded YouTube videos through JavaScript.
 */
function onYouTubePlayerReady( playerid ) {
	var $ = jQuery;
	var playerObject = $('.ui-dialog.customSiteElement.videoPopup:visible .popupContents object').get(0);
	var playerEmbed = $('.ui-dialog.customSiteElement.videoPopup:visible .popupContents embed').get(0);
	
	if ( playerEmbed && $.isFunction( playerEmbed.playVideo ) ) {
		playerEmbed.playVideo();
	} 
	else if ( playerObject && $.isFunction( playerObject.playVideo ) ) {
		playerObject.playVideo();
	}
}

