var _webcam=null;
function WebCam(id) {
    var _id = id;
    var _width;
    var _height;
    var _url;
    var _cookies;
    var _movie;
    var _instance = this;
    var _initialized = false;
    var _callback = null;
    
    this.init = function(url, width, height, cookies) {
        _width = width;
        _height = height;
        _url = url;
        _cookies = cookies;
        _webcam=this;
        
        if(!document.getElementById)
            return(false);
        //alert(_id);    
        _movie = document.getElementById(_id);
        //alert(_movie);    
        
        if(!_movie)
            return(false);
            
        activate();
        
        return(true);
    }
    
    this.shoot = function() {
        if(_initialized)
            _movie.shoot(); 
    }
    
    this.save = function() {
        if(_initialized)
            _movie.save(); 
    }

    this.handleEvent = function(e, m) {
        switch(e) {
            case "camconnected":
                _initialized = true;
                break;
        }

        if(_callback != null) {
            _callback(e, m);
        }
    }

﻿    this.setEventCallback = function(c) {
        _callback = c;
    }
    
    function activate() {
        if(_movie.setEventCallback == undefined) {
            setTimeout(activate,100);    
            //alert("undefined");
            return;
        }

        //var fn = getInstanceName(_instance) + ".handleEvent";
        var fn = '_webcam' + ".handleEvent";
        _movie.setEventCallback(fn);
        _movie.init(_url, _width, _height, _cookies);
        //alert("activate");
    }
    
    function obSub(ob) {
        var r = [];
        var i = 0;
        for (var z in ob) {
            try{
              if (ob.hasOwnProperty(z)) {
                  r[i++] = z;
              }
            }catch(e){}
        }
        return r;
    }

    function getInstanceName(variable){
        return obSub(window).map(
            function(a){ 
               if( window[a] === variable ){
                    return a
                }
        }).sort()[0];
    }
    
}