
uploader = function(name,config) {
    this.name = name;
    this.id = config['id'];
    this.do_debug = config['debug'];
    this.config = config;

    this.ReadyEvent    = new YAHOO.util.CustomEvent('ReadyEvent',this);
    this.SelectedEvent = new YAHOO.util.CustomEvent('SelectedEvent',this);
    this.ProgressEvent = new YAHOO.util.CustomEvent('ProgressEvent',this);
    this.UploadedEvent = new YAHOO.util.CustomEvent('UploadedEvent',this);
    this.CompleteEvent = new YAHOO.util.CustomEvent('CompleteEvent',this);
    this.ErrorEvent    = new YAHOO.util.CustomEvent('ErrorEvent',this);
};

uploader.prototype.browse = function() {
    var f = document.getElementById(this.id);
    f.Browse();
}

uploader.prototype.clear = function() {
    var f = document.getElementById(this.id);
    f.ClearFile();
}

uploader.prototype.startUpload = function(sQuery) {
    var f = document.getElementById(this.id);
    f.StartUpload(sQuery);
}

uploader.prototype.stopUpload = function() {
    var f = document.getElementById(this.id);
    f.StopUpload();
}

// Callback wrapper
uploader.prototype.onFlashReady = function() {
    this.debug('[uploader.js] onFlashReady()');
    this.ReadyEvent.fire();
}

uploader.prototype.onFileSelected  = function(filename) {
    this.debug('[uploader.js] onFileSelected()');
    this.SelectedEvent.fire(filename);
}

uploader.prototype.onFileProgress  = function(filename,current,total) {
    this.debug('[uploader.js] onFileProgress()');
    this.ProgressEvent.fire(filename,current,total);
}

uploader.prototype.onFileCancelled = function(filename) {
    this.debug('[uploader.js] onFileCancelled()');
}

uploader.prototype.onFileComplete  = function(filename) {
    this.debug('[uploader.js] onFileComplete()');
    this.UploadedEvent.fire(filename);
}

uploader.prototype.onUploadComplete  = function(filename,response) {
    this.debug('[uploader.js] onUploadComplete()');
    this.CompleteEvent.fire(filename,response);
}
uploader.prototype.onFileError  = function(filename) {
    this.debug('[uploader.js] onFileError()');
    this.ErrorEvent.fire(filename);
}


uploader.prototype.debug = function(msg) {
    if( "console" in window ) {
        if( this.do_debug ) {
            console.log(msg);
        }
    }
}

uploader.prototype.create_flash = function() {
    var config = this.config;
    this.debug('[uploader.js] create_flash()');

    var sFURL   = config['flashURL'];
    var sFId    = config['id'];

    // BUILD FLASH VARS
    var sURL    = config['uploadTargetURL'];
    var sName   = config['filePostName'];
    var aParams = config['postParams'];

    var sFlashVars = '';
    sFlashVars += 'uploadTargetURL='+encodeURIComponent(sURL);
    sFlashVars += '&filePostName='+encodeURIComponent(sName);
    sFlashVars += '&postParams='+encodeURIComponent(aParams);

    sFlashVars += '&flashReady='+encodeURIComponent(this.name+".onFlashReady");
    sFlashVars += '&fileSelected='+encodeURIComponent(this.name+".onFileSelected");
    sFlashVars += '&fileProgress='+encodeURIComponent(this.name+".onFileProgress");
    sFlashVars += '&fileCancelled='+encodeURIComponent(this.name+".onFileCancelled");
    sFlashVars += '&fileComplete='+encodeURIComponent(this.name+".onFileComplete");
    sFlashVars += '&uploadComplete='+encodeURIComponent(this.name+".onUploadComplete");
    sFlashVars += '&fileError='+encodeURIComponent(this.name+".onFileError");

    if( config['debug'] ) {
        sFlashVars += '&debug='+encodeURIComponent(this.name+".debug");
    }

    var html = '';

    if(navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) {
        html += '<embed src="'+sFURL+'"';
        html += ' quality="high"';
        html += ' bgcolor="#f00"';
        html += ' width="1px"';
        html += ' height="1px"';
        html += ' id="'+sFId+'"';
        html += ' name="'+sFId+'"';
        html += ' play="true"';
        html += ' loop="false"';
        html += ' type="application/x-shockwave-flash"';
        html += ' flashvars="'+sFlashVars+'"';
        html += '>';
        html += '</embed>';
    } else {
        html += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"';
        html += ' id="'+sFId+'" ';
        html += ' width="1px"';
        html += ' height="1px"';
        html += ' codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">';
        html += '<param name="movie" value="'+sFURL+'" />';
        html += '<param name="quality" value="high" />';
        html += '<param name="bgcolor" value="#869ca7" />';
        html += '<param name="flashvars" value="'+sFlashVars+'"/>';
        html += '</object>';
    }

    target_element = document.getElementsByTagName("body")[0];
    if( typeof(target_element) === 'undefined' || target_element === null ) {
        this.debug('[uploader.js] could not find body to attach flash');
        return;
    }

    var container = document.createElement('DIV');
    target_element.appendChild(container);
    container.innerHTML = html;
    //document.write(html);
}


