Dialog.Flash = Class.create(Dialog.Base, {
	defaultOptions: Object.extend(Object.extend({}, Dialog.Base.prototype.defaultOptions), {
		dialogClass: 'flash_dialog',
		overlayOpacity: 0.85,
		width: 168,
		onClose: Prototype.emptyFunction,
		onLoad: Prototype.emptyFunction
	}),
	
	initialize: function($super, options) {
		$super(options);
		this.loadContent(this.options.path);
	},
	
	setContents: function() {
		var loader = $img({src: '/images/progress/big_loading.gif', alt: 'Loading, please wait&#8230;', width: 128, height: 128});
		this.dialogBox.appendChild($div({'style': 'text-align: center; margin: 20px 0;'}, loader));
	},
	
	loadContent: function(path) {
		new Ajax.Request(path, {
			method: 'get',
			onSuccess: this.success.bind(this),
			onFailure: this.failure.bind(this)
		});
	},
	
	createCloser: function() {
		var closer = $a({href: '#', 'class': 'flash_dialog_closer'}, 'Close');
		closer.observe('click', function(event) {
			event.stop();
			Dialog.current.close();
			Dialog.current.options.onClose(event);
		});
		return closer;
	},
	
	success: function(transport) {
		this.clearContent();
		var content = $div().hide().update(transport.responseText);
		content.appendChild(this.createCloser());
		this.dialogBox.appendChild(content);
		var object = content.down('object');
		this.resizeDialog(object.width, object.height);
		content.appear({
			duration: 0.4, queue: {position: 'end', scope: 'dialog'},
			afterFinish: this.options.onLoad
		});
	},
	
	failure: function() {
		this.clearContent();
		var content = $p({'class': 'ajax_failure'}, 'Failed to load content, we have been notified of the issue, please try again later.').hide().setStyle('margin: 10px');
		content.appendChild(this.createCloser());
		this.dialogBox.appendChild(content);
		this.resizeDialog(400, content.getHeight());
		content.appear({
			duration: 0.4, queue: {position: 'end', scope: 'dialog'}
		});
	},
	
	clearContent: function() {
		var dims = this.dialogBox.getDimensions();
		this.dialogBox.setStyle({height: dims.height + 'px', width: dims.width + 'px'});
		this.dialogBox.update();
	},
	
	resizeDialog: function(newWidth, newHeight) {
		var currentDims = this.dialogBox.getDimensions();
		currentDims.width = currentDims.width;
		currentDims.height = currentDims.height;
		if (currentDims.width == newWidth && currentDims.height == newHeight) return;
		var currentTop = parseFloat(this.dialogBox.getStyle('top'));
		var currentLeft = parseFloat(this.dialogBox.getStyle('left'));

		var newTop = currentTop + (currentDims.height - newHeight)/2;
		var newLeft = currentLeft + (currentDims.width - newWidth)/2;
		this.dialogBox.morph('top:' + newTop + 'px;left:' + newLeft + 'px;width:' + newWidth + 'px;height:' + newHeight + 'px;', {duration: 0.6, queue: {position: 'end', scope: 'dialog'}});
	}
});

FlashHandler = {
	players: {},
	observers: {},
	
	playerReady: function(player) {
		if (FlashHandler.observers[player.id]) {
			FlashHandler.observers[player.id](FlashHandler.getPlayer(player.id));
		} else {
			FlashHandler.players[player.id] = true;
		}
	},
	
	observe: function(id, callback) {
		if (FlashHandler.players[id]) {
			callback(FlashHandler.getPlayer(id));
		} else {
			FlashHandler.observers[id] = callback;
		}
	},
	
	getPlayer: function(id) {
		return swfobject.getObjectById(id);
	}
};

Event.addBehavior({
	'a.popup_video:click': function(event) {
		event.stop();
		new Dialog.Flash({path: this.href});
	}
});

function playerReady(player) {
	FlashHandler.playerReady(player);
}