var ImageUploaders = new Array();
var ImageUploaderCaller = Class.create({
	element: null,
	iframeSrc: null,
	uploaders: [],
	
	uploadStarted: function() {
	},

	uploadFinished: function() {
	},

	uploaded: function(path) {
	},

	error: function(message) {
		alert(message);
	},

	initialize: function(element, iframeSrc, options) {
		this.element = element;
		this.iframeSrc = iframeSrc;

		if(options != null) {
			if(options['uploadStartedCallback'] != null) {
				this.uploadStarted = options['uploadStartedCallback'];
			}
	
			if(options['uploadFinishedCallback'] != null) {
				this.uploadFinished = options['uploadFinishedCallback'];
			}
	
			if(options['uploadedCallback'] != null) {
				this.uploaded = options['uploadedCallback'];
			}
	
			if(options['errorCallback'] != null) {
				this.error = options['errorCallback'];
			}
		}
		ImageUploaders.push(this);

		this.renderUploader();
	},
	
	renderUploader: function() {
		var uploader = $(this.element);

		var iframe = document.createElement('iframe');
		iframe.setAttribute('frameborder', 0);
		iframe.setAttribute('noresize', 'noresize');
		iframe.setAttribute('scrolling', 'no');
		iframe.src = this.iframeSrc;

		uploader.appendChild(iframe);
	}
});

var ImageUploaderReceiver = Class.create({
	sender: null,

	initialize: function(sender) {
		if(sender == null) {
			url = document.location.href.split("?");
			if(url.length == 2) {
				url[1].split("&").each(function(q) {
					
					param = q.split("=");
					if(param.length == 2) {
						if(param[0] == "sender") {
							sender = param[1];
						}
					}
				});
			}
		}

		if(window.parent) {
			for(i = 0; i < window.parent.ImageUploaders.length; i++) {
				e = window.parent.ImageUploaders[i];
				if(sender == e.element.id) {
					this.sender = e;
				}	
			};
		}
	},
	progress: function() {
		this.sender.progress();
	},

	uploadStarted: function() {
		this.sender.uploadStarted();
	},

	uploadFinished: function() {
		this.sender.uploadFinished();
	},

	uploaded: function(path) {
		this.sender.uploaded(path);
	},

	error: function(message) {
		this.sender.error(message);
	}
});
