/*
 * Movie - jQuery Plugin
 * Simple movie popup using fancybox (http://fancybox.net)
 *
 * Copyright (c) 2010 Distinct
 *
 * Version: 1.0 (08-09-2010)
 * Requires: jQuery v1.3+
 *
 */

;(function($) {

	if (!window.swfobject) {
		document.write('<script type="text/javascript" src="framework/js/swfobject.js"></script>');
	}
	
	if (!$.fancybox) {
		document.write('<script type="text/javascript" src="fancybox/jquery.fancybox-1.3.1.pack.js"></script>');
		document.write('<link rel="stylesheet" type="text/css" href="fancybox/jquery.fancybox-1.3.1.css" />');
	}
	
	$.fn.movie = function(options) {
		$(this).unbind('click.movie').bind('click.movie', function(e) {
			e.preventDefault();
			
			$(this).blur();
			
			$.movie(options);
			
			return false;
		});
		
		return this;
	}
	
	var playerControlsHeight = 24;

	$.movie = function(options) {
		var params = { menu: false, wMode: 'opaque', allowfullscreen: 'true', 'allowscriptaccess' : 'always' };
		var flashvars = {};
		flashvars.file = options.movie;
		flashvars.autostart = 'true';
		
		var height = options.height + playerControlsHeight;
		
		window.onStateChanged = function(obj) {
			if (obj.newstate == 'COMPLETED')
				$.fancybox.close();
		}
		
		window.playerReady = function(obj) {
			var player = swfobject.getObjectById("flashplayer");
			
			player.addModelListener('STATE', 'onStateChanged');
		}
		
		$('<div id="flashplayer"/>').width(options.width).height(height).appendTo(document.body);
		
		$.fancybox({
			'onComplete' : function() {
				swfobject.embedSWF("flash/player.swf", "flashplayer", options.width, height, '9.0.0', null, flashvars, params);
			},
			'onClosed' : function() {
				swfobject.removeSWF('flashplayer');
			},
			'scrolling'			: 'no',
			'padding'           : 10,
			'autoScale'     	: false,
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'href' : '#flashplayer',
			'title' : options.title
		});
	}
})(jQuery);
