var barcodeReaderListeners = null;
function barcodeReaderCallback(id,text){    
    var element = barcodeReaderListeners.get(id);
	if(element){
		element.callback(text);
	}
}

var BarcodeReader = Class.create({
	initialize: function(container){
		this.container = $(container);
		this.rand = '' + Math.round(Math.random()*10000000000000);
		this.object = new Element('object',{data: '/barcodereader/barcodereader.swf',type: 'application/x-shockwave-flash',width: '300',height:'300',border:'0'});
		this.object.insert(new Element('param',{name: 'allowScriptAccess', value: 'true'}));
		this.object.insert(new Element('param',{name: 'movie', value: '/barcodereader/barcodereader.swf'}));
		this.object.insert(new Element('param',{name: 'quality', value: 'high'}));
	    this.container.update(this.object);
	},
	getListeners: function(){
		if(!barcodeReaderListeners){
			barcodeReaderListeners = $H();
		}
		return barcodeReaderListeners;
	},
	start: function(){		
		this.getListeners().set(this.rand,this);		
		if(this.object.start){			
		    this.object.start(this.rand);
		}else{
			this.start.bind(this).delay(0.1);			
		}
	},
	stop: function(){
		this.getListeners().unset(this.rand);
		this.object.stop();
	},
	scan: function(){
		return this.object.scan();
	},
	reset: function(){
		this.object.reset();
	},
	callback: function(text){
		this.result = text;
		this.container.fire("barcodereader:scanned");		
	}
});

