var FileUploader = Class.create({

    ID_KEY         : 'APC_UPLOAD_PROGRESS',
    statusUrl      : '/modules/ditshop/front/upload_status.php',
    pollDelay      : 1.0,

    form           : null, // HTML form element
    status         : null, // element where the upload status is displayed
    statusTemplate : null, // Prototype Template object
    idElement      : null, // element that holds the APC upload ID
    iframe         : null, // iframe we create that form will submit to

    initialize : function(form, status)
    {
        
        if($(form) ){  
            // initialize the form and observe the submit element
            this.form = $(form);
            this.form.observe('submit', this._onFormSubmit.bindAsEventListener(this));

            // create a hidden iframe
            this.iframe = new Element('iframe', { name : '_upload_frame' }).hide();

            // make the form submit to the hidden iframe
            this.form.appendChild(this.iframe);
            this.form.target = this.iframe.name;

            // initialize the APC ID element so we can write a value to it later
            this.idElement = this.form.getInputs(null, this.ID_KEY)[0];

            // initialize the status container
            this.status = $(status);

            // create a template based on the HTML inside the status container
            this.statusTemplate = new Template(this.status.innerHTML);

            // clear the status template
            this.status.update();
        }
    },

    generateId : function()
    {
        var now = new Date();
        return now.getTime();
    },

    delay : function(seconds)
    {
        var ms   = seconds * 1000;
        var then = new Date().getTime();
        var now  = then;

        while ((now - then) < ms)
            now = new Date().getTime();
    },

    _onFormSubmit : function(e)
    {
        var id = this.generateId();

        $('icm_addtolist').hide();
        
        if(checkICMFile($F('icm-file'))){
        this.idElement.value = id;
        $('status').update('<div align="center" style="width:100%;z-index: 1000;margin: 0px;padding: 0px;"><img src="/modules/ditshop/images/indicator.gif" alt="Een ogenblik geduld a.u.b." />&nbsp;Upload bezig! Ogenblik geduld a.u.b.!</div>');
        this._monitorUpload(id);
        }
        else{
         return false; 
        }
        
               
    },

    _monitorUpload : function(id)
    {
        var options = {
            parameters : 'status_id=' + id,
            onSuccess  : this._onMonitorSuccess.bind(this)
        };

        new Ajax.Request(this.statusUrl, options);
    },

    _onMonitorSuccess : function(transport)
    {
        var json = transport.responseJSON;

        this.status.show();
        

        if (!json.finished) {
            //this.status.update('<div align="center" style="width:100%;z-index: 1000;margin: 0px;padding: 0px;"><img src="/modules/ditshop/images/indicator.gif" alt="Een ogenblik geduld a.u.b." /></div>');
            this.delay(this.pollDelay);
            this._monitorUpload(json.id);
        }
        else{    

            if(json.filename){
                this.status.update(this.statusTemplate.evaluate(json));   
                alert("File succesvol geladen. U kunt nu de artikelen toevoegen aan de bestellijst!");
                $('uploaded_filename').value = json.filename;
                $('icm_addtolist').show();            
            
            }
            else{
                alert("File uploaden mislukt!");
            }
            return false;    
        }
    }
});