
var FlashExpander = new Class({
	ClassName: 'FlashExpander',
	semaphore: [],

	initialize: function() {
		Register.addInstance(this);
	},

	processMessage: function(msg) {
		if (msg.msg == 'flashExpand') {
			if (this.checkSemaphore(msg.data.id,  1) == 1) this.resizeElement(msg.data);
		}
		else if (msg.msg == 'flashCollapse') {
			if (this.checkSemaphore(msg.data.id, -1) == 0) this.resizeElement(msg.data);
		}
	},

	resizeElement: function(data) {
		var el = $(data.id);
		if ($defined(el)) {
			if (data.x) {
				el.setStyle('width', data.x);
				el.set('width', data.x);
				if (data.resizeParent) el.getParent().setStyle('width', data.x);
			}
			if (data.y) {
				el.setStyle('height', data.y);
				el.set('height', data.y);
				if (data.resizeParent) el.getParent().setStyle('height', data.y);
			}
		}
	},

	checkSemaphore: function(id, value) {
		if (this.semaphore[id])
			this.semaphore[id] += value;
		else
			this.semaphore[id]  = Math.max(value, 0);
		return this.semaphore[id];
	}
});

new FlashExpander();
